Removable media

From Gentoo Wiki
Jump to: navigation, search
This page contains changes which are not marked for translation.

Removable media consists of any media that is easily removed from a system; typically this includes CDs, DVDs, USB drives, or memory cards of all form factors. These types of media require special handling if an unprivileged user desires to mount an unknown device.

Prerequisites

UUIDs and labels

In many cases storage devices are identified by their device file, e.g. /dev/sd*. Alternatively their UUID (Universally Unique Identifier) (/dev/disk/by-uuid/*) or label (/dev/disk/by-label/*) can be used. In comparison to device files UUIDs and labels are persistent and will never change because of asynchronous detection. The UUID is generated automatically during filesystem creation. The label can be specified at filesystem creation or changed afterwards.

Use tree /dev/disk/ or lsblk (part of sys-apps/util-linux) to show all storage devices and their UUIDs and labels:

user $tree /dev/disk/
user $lsblk -o +fstype,label,uuid,partuuid
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT FSTYPE   LABEL           UUID                                 PARTUUID
sda      8:0    0 111.8G  0 disk                                                                          
├─sda1   8:1    0    96M  0 part            ext2                     xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx xxxxxxxx-01
├─sda2   8:2    0     1K  0 part                                                                          xxxxxxxx-02
├─sda3   8:3    0   100M  0 part            ntfs     System Reserved XXXXXXXXXXXXXXXX                     xxxxxxxx-03
├─sda4   8:4    0  29.6G  0 part            ntfs                     XXXXXXXXXXXXXXXX                     xxxxxxxx-04
├─sda5   8:5    0   2.8G  0 part [SWAP]     swap                     xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx xxxxxxxx-05
└─sda6   8:6    0  79.2G  0 part /          reiserfs                 xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx xxxxxxxx-06
sr0     11:0    1  1024M  0 rom    

Mounting removable media

For mounting as a normal user without root privileges the device needs an entry with the user option in /etc/fstab.

Plug removable media to the computer then run tree /dev/disk/ or lsblk.

user $lsblk -o +fstype,label,uuid,partuuid
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT FSTYPE   LABEL           UUID                                 PARTUUID
sda      8:0    0 111.8G  0 disk                                                                          
├─sda1   8:1    0    96M  0 part            ext2                     xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx xxxxxxxx-01
├─sda2   8:2    0     1K  0 part                                                                          xxxxxxxx-02
├─sda3   8:3    0   100M  0 part            ntfs     System Reserved XXXXXXXXXXXXXXXX                     xxxxxxxx-03
├─sda4   8:4    0  29.6G  0 part            ntfs                     XXXXXXXXXXXXXXXX                     xxxxxxxx-04
├─sda5   8:5    0   2.8G  0 part [SWAP]     swap                     xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx xxxxxxxx-05
└─sda6   8:6    0  79.2G  0 part /          reiserfs                 xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx xxxxxxxx-06
sdb      8:16   1   3.8G  0 disk                                                                          
└─sdb1   8:17   1   3.8G  0 part            vfat                     QWER-1234                            
sdc      8:32   1   1.9G  0 disk                                                                          
└─sdc1   8:33   1   1.9G  0 part            vfat     QWERTZ12345     4321-REWQ                            qwer1234-01
sr0     11:0    1  1024M  0 rom    

The sdb1 and sdc1 lines display the UUIDs QWER-1234 and 4321-REWQ to be added to /etc/fstab for two media just plugged-in. sdc1 also has the label QWERTZ12345 which could be used alternatively. Let's create their mountpoints larry1 and larry2:

root #mkdir /mnt/{larry1,larry2}
root #chmod 777 /mnt/{larry1,larry2}

And add them in /etc/fstab.

FILE /etc/fstab
# <fs>            <mountpoint> <type> <opts>         <dump/pass>
...
UUID=QWER-1234    /mnt/larry1  vfat   noauto,rw,user     0 0
LABEL=QWERTZ12345 /mnt/larry2  vfat   noauto,rw,user     0 0

With the user option in these entries, they can be mounted / unmounted by normal users.

user $mount /mnt/larry1
user $mount /mnt/larry2

Programs using the gtk file chooser show them on the left under "Places" where thy can be clicked for mounting.

In the gtk file chooser, entries from fstab having the user option are listed on the left under "Places". In this case larry1 is not connected to the computer. If connected, it could be mounted by clicking on it.

To see what is mounted, run mount without arguments or lsblk again and find the MOUNTPOINT column populated for sdb1 and sdc1:

user $lsblk -o +fstype,label,uuid,partuuid
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT  FSTYPE   LABEL           UUID                                 PARTUUID
sda      8:0    0 111.8G  0 disk                                                                          
├─sda1   8:1    0    96M  0 part             ext2                     xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx xxxxxxxx-01
├─sda2   8:2    0     1K  0 part                                                                           xxxxxxxx-02
├─sda3   8:3    0   100M  0 part             ntfs     System Reserved XXXXXXXXXXXXXXXX                     xxxxxxxx-03
├─sda4   8:4    0  29.6G  0 part             ntfs                     XXXXXXXXXXXXXXXX                     xxxxxxxx-04
├─sda5   8:5    0   2.8G  0 part [SWAP]      swap                     xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx xxxxxxxx-05
└─sda6   8:6    0  79.2G  0 part /           reiserfs                 xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx xxxxxxxx-06
sdb      8:16   1   3.8G  0 disk                                                                          
└─sdb1   8:17   1   3.8G  0 part /mnt/larry1 vfat                     QWER-1234                            
sdc      8:32   1   1.9G  0 disk                                                                          
└─sdc1   8:33   1   1.9G  0 part /mnt/larry2 vfat     QWERTZ12345     4321-REWQ                            qwer1234-01
sr0     11:0    1  1024M  0 rom    

Once a device is mounted it can be accessed like a normal hard disk. Usual operations like cp, mv, rm, etc. work fine.

For unmounting the usage of mountpoint, label or UUID is equivalent as is for mounting. Any of them will do it.

user $umount /mnt/larry2
user $umount LABEL=QWERTZ12345
user $umount UUID=4321-REWQ

Mounting without the fstab entry can be done by root only.

root #mount UUID=QWER-1234 /mnt/larry1
user $mount
/dev/sdb1 on /mnt/larry1 type vfat (rw,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=iso8859-1,shortname=mixed,errors=remount-ro)

For further details see man 8 mount.

MTP

For handling media using the MTP (Media Transfer Protocol) protocol see the MTP article.

See also

External resources