Thursday, December 4, 2008

Mounting an external drive using volume label

The device nodes keep changing if you are pluggin in and out several different usb disks and in different orders. For various reasons for instance for file synchronization, it is useful to mount these drives according to their names.

First of all, find out the correct device partitions for usb drives.

$ sudo fdisk -l
Disk /dev/sda: 250.0 GB, 250059350016 bytes
255 heads, 63 sectors/track, 30401 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x4be002e1

Device Boot Start End Blocks Id System
/dev/sda1 1 1402 11261533+ 12 Compaq diagnostics
/dev/sda2 * 1403 15918 116592640 6 FAT16
/dev/sda3 15918 29975 112918572 f W95 Ext'd (LBA)
/dev/sda4 29976 30402 3420160 12 Compaq diagnostics
/dev/sda5 15918 22292 51200000 7 HPFS/NTFS
/dev/sda6 22293 22304 96358+ 83 Linux
/dev/sda7 22305 22912 4883728+ 82 Linux swap / Solaris
/dev/sda8 22913 26559 29294496 83 Linux
/dev/sda9 26560 29975 27438988+ 83 Linux

Disk /dev/sdb: 320.0 GB, 320072933376 bytes
255 heads, 63 sectors/track, 38913 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x5b6ac646

Device Boot Start End Blocks Id System
/dev/sdb1 1 24315 195310206 c W95 FAT32 (LBA)
/dev/sdb2 24316 38913 117258435 82 Linux swap / Solaris

The Usb disk is mounted at /dev/sdb.

Before changing the label, unmount the device partitions. This is very important.

$ sudo umount /dev/sdb1
$ sudo umount /dev/sdb2

Install "mtools" and "e2fsprogs"

$ sudo apt-get install mtools e2fsprogs

in order to label vfat partitions, we need 'mtools' and 'e2label' is needed for labelling ext2/3 partitions.

$ sudo mlabel -i /dev/sdb1 -s :: SWG_WIN

now check the label with following command

$ sudo mlabel -i /dev/sdb1
Volume label is SWG_WIN

$ sudo e2label /dev/sdb2 swg_lin

check the label using following command:
$ sudo e2label /dev/sdb2
swg_lin

Now edit /etc/fstab and enter following lines:

LABEL=SWG_WIN /mnt/usb_win vfat user,rw,auto,umask=0 0 0
LABEL=swg_lin /mnt/usb_linux ext3 user,rw,auto 0 0

save the file and remount them

$ sudo mount -a

More information about changing partition labels are given here.

No comments: