I am very often dealing with formatting USB drives, that are registered as /dev/sdX. This includes executing mkfs
and fdisk
and mount
and other commands usually executed as root. However, I fear that accidentally I may mistype one single letter, and format my hard drive.
Ideally, I would want to set /dev/sdX*
devices in "read-only" mode, so that any fdisk
mkfs
wipefs
will fail on them, unless the device is switched to "read-write" mode by a manual command.
I thought chmod ugo-w /dev/sdX
would work exactly like that. However, to my surprise, chmod 0000 /dev/sdc1
followed by mkfs /dev/sdc1
works completely fine.
How can I prevent all users, also root, from modifying a hard drive and its partition table and partitioning the hard-drive and writing to partitions in a way other than via a mounted file system? How can I enable writing to the drive with this method if I would want to?
I know I can make the device being owned by user. This, however, requires me to switch between user and root to make commands like chroot
or mount
or umount
and this is a security hole. I do not want all USB storage devices being owned by user. I am searching for a better solution. Ideally, I would want to stay as the root user, just without the possibility of formatting the wrong disc.
Remember, a mounted partition can't be formatted
Sure it can, trybusybox mkfs.vfat
. This is a quality of implementation issue. This is just a verbose check inside specificmkfs
implementations, like github.com/util-linux/util-linux/blob/master/disk-utils/… . If specific mkfs is missing that check, you can. In busybox: github.com/brgl/busybox/blob/master/util-linux/mkfs_vfat.c#L298 . I tested, busybox formats mounted filesystem without any issues.rm
torm -i
, but make it a habit to userm -i
. Before formatting a device, usecfdisk
to have a look at its partition table and size and usefile
to check the filesystem (or encryption header) on the device before formatting. Check what's on the device, before executing dangerous operations on the device