Udev manages devices via rules that determine what to do when a device is inserted (or removed). Udev itself doesn't handle mounting, but you can make it invoke an external program to do the mounting.
There are rules, stored in the various files under /etc/udev/rules.d/
, that create entries in /dev/disk/by-label/
. We can use the same matching conditions to match a USB device which has a filesystem label and run a custom script.
ENV{ID_FS_LABEL_ENC}=="?*", ENV{ID_FS_USAGE}=="filesystem|other", \
SUBSYSTEMS=="usb", \
RUN += "/usr/local/sbin/udev-mount-by-label '%E{ID_FS_LABEL_ENC}'"
The custom script should create the mount point and perform the mounting. It should take a bit of care in case the directory already exists. The script I've written will do nothing if the mount point is already in use as a mount point, but will happily shadow a non-empty directory. Customize to your taste.
#!/bin/sh
export mount_point="/media/$1"
current_device=$(awk '$2 == ENVIRON["mount_point"] {print $1; exit}' </proc/mounts)
if [ -n "$current_device" ]; then
echo 1>&2 "$current_device already mounted on $mount_point"
exit 1
fi
mount "/dev/disk/by-label/$1" "$mount_point"
Don't forget to unmount the device before unplugging it, otherwise you may lose data.
Ubuntu - Automatically mount external drives to /media/LABEL on boot without a user logged in? shows the same technique with a different script.
man blockdev
--setbsz bytes
Set blocksize. Note that the block size is specific to the cur‐
rent file descriptor opening the block device, so the change of
block size only persists for as long as blockdev has the device
open, and is lost once blockdev exits.
In block/ioctl.c:
case BLKBSZGET: /* get block device soft block size (cf. BLKSSZGET) */
return put_int(arg, block_size(bdev));
case BLKSSZGET: /* get block device logical block size */
return put_int(arg, bdev_logical_block_size(bdev));
case BLKPBSZGET: /* get block device physical block size */
return put_uint(arg, bdev_physical_block_size(bdev));
So BSZ reported by blockdev
is neither logical nor physical block size. It is the "soft block size".
Looking at this code, the part about soft block size being specific to the file descriptor does not appear to make sense. Nor does wanting to set that with blockdev
, given that no other option is documented in terms of blocks (only fixed-size 512 byte sectors).
In my own tests, what actually happens is that BSZ is preserved for as long as any process holds the block device open. It looks like it gets reset on the last close().
Parted got confused by this too some years ago
belay that. BLKBSZGET is the kernel's chosen block size it will use
to access the device (for normal disks turns out this is 1k, for
ata_ram this is 4k), which is not the underlying disk's logical block
size. :-( So we will likely need another ioctl() to get the right
value from the kernel, and BLKSSZGET may wind up being the disks's
logical block size, while a new ioctl() exports the disk's physical
sector size. ugh.
Another quirk:
On Wed, Apr 09, 2003 at 06:53:17PM +0200, Rob van Nieuwkerk wrote:
I get 4096 with BLKBSZGET on several unmounted partitions on my system
(RH 2.4.18-27.7.x kernel). Some give 1024 .. Maybe it is because I
had them mounted first and unmounted them for the test ?
That would be the most likely answer. When you unmount, I don't believe the
filesystem bothers to set_blocksize(get_hardsect_size(dev)).
Best Answer
I used this command and it fixed the problem:
Usually,
<id_of_your_device>
issdb
, but make sure you check that, because if you usesda
and it turns out to be your hard disk, all the data on your hard disk will get erased.