Skip to main content
Share Your Experience: Take the 2024 Developer Survey
72 votes

Determine what device a directory is located on

On Linux we have findmnt from util-linux exactly made for this findmnt -n -o SOURCE --target /path/to/FILE The advantage about other solutions is that it still works if paths are obscured by ...
rudimeier's user avatar
  • 10.4k
38 votes

How can I protect /dev/sdX against accidental formatting?

Consider using /dev/disk/by-{path,id}/ /dev/disk/by-path/ has symlinks to block devices, where the names of the symlinks describe the "hardware path" of the device (subsystem, bus, ...
marcelm's user avatar
  • 2,485
35 votes

How to list disks, partitions and filesystems in Linux?

The other answers don't show the UUID which is useful to use as reference in boot scripts and configs like /etc/hdparm. so here: $ sudo lsblk --output NAME,FSTYPE,LABEL,UUID,MODE NAME FSTYPE ...
Ondra Žižka's user avatar
33 votes
Accepted

Create virtual block device which writes to /dev/null

https://wiki.gentoo.org/wiki/Device-mapper#Zero See Documentation/device-mapper/zero.txt for usage. This target has no target-specific parameters. The "zero" target create that functions ...
sourcejedi's user avatar
  • 50.7k
32 votes
Accepted

How do I find the first non-zero byte on a block device, with an optional offset?

You can do this using cmp, comparing to /dev/zero: cmp /path/to/block-device /dev/zero cmp will give you the offset of the first non-zero byte. If you want to skip bytes, you can use GNU cmp’s -i ...
Stephen Kitt's user avatar
30 votes
Accepted

What happens if you delete a device file?

Those are simply (special) files. They only serve as "pointers" to the actual device. (i.e. the driver module inside the kernel.) If some command/service already opened that file, it already has a ...
michas's user avatar
  • 21.6k
27 votes
Accepted

How to get block device name from partition device name?

If you're on linux you could use lsblk (which is part of util-linux): lsblk -no pkname /dev/sda1
don_crissti's user avatar
  • 83.2k
27 votes
Accepted

Why does one need a loop device at all?

Mounts, typically, must be done on block devices. The loop driver puts a block device front-end onto your data file. If you do a loop mount without losetup then the OS does one in the background. ...
Stephen Harris's user avatar
23 votes

Why does one need a loop device at all?

File systems expect to read from and write to block devices, but image files aren’t block devices. Loop devices provide a block device on top of a file (or another block device, optionally with ...
Stephen Kitt's user avatar
22 votes

How to get block device name from partition device name?

If a device is a partition of another device then /sys/class/block/$dev will contain a file called partition (whose content is the partition number). If that's the case, you can get the name of the ...
Stéphane Chazelas's user avatar
21 votes
Accepted

Differences Between `/dev/null` and Devices Under `null_blk` Driver

/dev/null is handled by a device driver, but it’s a very simple one. The major difference between /dev/null and null_blk is that the former is a character device, the latter a block device. The former ...
Stephen Kitt's user avatar
19 votes
Accepted

What is `/dev/sda0`? Is it a standard thing?

I’m not aware of /dev/sda0 ever being a standard device name, even on other Unix systems. And as far as I can tell, references to sda0 are likely mistakes rather than indications of a custom setup. ...
Stephen Kitt's user avatar
18 votes
Accepted

How can I tell if a Linux block device is trimmable or not?

You can check the device’s maximum discard sizes, e.g. $ cat /sys/block/X/queue/discard_max_hw_bytes (replacing X as appropriate). If this shows a value greater than 0, the device supports discards: ...
Stephen Kitt's user avatar
18 votes

How can I protect /dev/sdX against accidental formatting?

A simple ruse is to rm /dev/sda /dev/sda1 and so on after booting, if you want to prevent accidental reference to these devices. You can always re-create the inodes with mknod /dev/sda1 b 8 1 and so ...
meuh's user avatar
  • 51.7k
16 votes

Differences Between `/dev/null` and Devices Under `null_blk` Driver

/dev/null is a "character device", on which all reads return empty and all writes complete fully with no additional side-effects. These are the semantics since V5 UNIX in 1974, and are ...
telcoM's user avatar
  • 98.1k
14 votes

Is there anything similar to rsync to syncing block devices?

Some versions of rsync have this capability (it depends on your distro). There are 2 patches that distros commonly apply to rsync. One for reading from block devices (provides the --copy-devices flag),...
phemmer's user avatar
  • 72k
14 votes
Accepted

Passing a block device to Qemu

You should use host_device instead, which is admittedly difficult since it’s not documented. In your case, the non-deprecated options are -blockdev node-name=q1,driver=raw,file.driver=host_device,...
Stephen Kitt's user avatar
13 votes

Prevent a USB external hard drive from sleeping

Just my 2 cents... It is more than true that spinning down the disks decreases their service life. Years of experience have shown that starting and stopping the disk motor causes far more fatigue ...
Erebus's user avatar
  • 131
13 votes

Is there anything similar to rsync to syncing block devices?

Nowadays, rsync supports copying devices, with: rsync --copy-devices --write-devices <FROM> <TO> Beware that it only writes to a device if it finds an actual device node. So don't use /...
Stefan's user avatar
  • 303
12 votes

Why does one need a loop device at all?

You seem to be on Linux and Linux uses a wrong name for that feature. I invented that feature in 1988 on SunOS-4.0 and I call that feature fbk - File emulates BlocK device. The background is that ...
schily's user avatar
  • 19.2k
12 votes
Accepted

Given a block device, how to detect if names of partitions must contain "p"?

If device name ends with digit then kernel adds 'p' symbol to separate partition number from device name. /dev/sda -> /dev/sda1 /dev/mmcblk0 -> /dev/mmcblk0p1 For details see disk_name ...
John Doe's user avatar
  • 423
12 votes

How can I protect /dev/sdX against accidental formatting?

Well, there is blockdev --setro to make a block device read-only (--setrw for read-write). # blockdev --setro /dev/sdx3 # mkfs.ext4 /dev/sdx3 mke2fs 1.47.0 (5-Feb-2023) /dev/sdx3 contains a vfat file ...
frostschutz's user avatar
  • 49.3k
11 votes

Is there anything similar to rsync to syncing block devices?

According to the description: Bscp copies a single file or block device over an SSH connection, transferring only the parts that have changed.
Gea Planet's user avatar
11 votes
Accepted

Cygwin show Windows drive name mappings to POSIX device files /dev/disk/sd*

Use /proc/partitions $ cat /proc/partitions major minor #blocks name win-mounts 8 0 175825944 sda 8 1 175824896 sda1 C:\ 8 16 1953514582 sdb 8 17 1953512448 sdb1 ...
matzeri's user avatar
  • 961
11 votes
Accepted

persistent device naming for NVMe storage devices

Short: No. You can not rely on the name of the descriptor. And you most likely never will. The NVMe naming standard describes: nvme0: first registered device's device controller nvme0n1: first ...
rohr's user avatar
  • 156
11 votes

How can I protect /dev/sdX against accidental formatting?

Not a real solution, but you could hide the dangerous commandos behind wrapping shell scripts that check the command line arguments for dangerous devices. There might be also ways to use security ...
jofel's user avatar
  • 26.8k
11 votes
Accepted

How to programattically determine the device name/basename of the root partition?

Mount point is controlled by systemd. You can list the systemd mount unit files through: systemctl list-units --type=mount --all sample output: -.mount loaded active ...
GAD3R's user avatar
  • 67.1k
10 votes

Prevent a USB external hard drive from sleeping

I have found that the following cronjob works for me. */5 * * * * /bin/touch /dev/sdb &>/dev/null Obviously update it with the device name of your disk. You can also vary the time based on how ...
Steven's user avatar
  • 101
10 votes
Accepted

Does Linux update the size of a block device?

No. A device with internal wear leveling like eMMC and some flash drives will not advertise their full capacity in the first place. So, a device may have 1536 blocks but shows only 1024 to the system. ...
Ned64's user avatar
  • 8,822
10 votes
Accepted

How does EXT4 handle sudden lack of space in the underlying storage?

When the block device overcommits its available data capacity like when using thin provisioning or has other reasons to not be able to accept more writes, like having a snapshot full, it has to send a ...
A.B's user avatar
  • 37k

Only top scored, non community-wiki answers of a minimum length are eligible