Removable media

From Gentoo Wiki
Jump to:navigation Jump to:search
Other languages:

Removable media are simply 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

  • Kernel support for the storage device.
  • Kernel support for the filesystem used by the device.

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 /          ext4                     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 /          ext4                     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

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 /           ext4                     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 mount(8) man page.

Another alternative is to edit /usr/share/polkit-1/actions/org.freedesktop.UDisks2.policy, under action id "org.freedesktop.udisks2.filesystem-mount", change auth_admin to yes for allow_any. This will allow a disk to be mounted by remote and local users, so consider this change as a potential security risk.

FILE /usr/share/polkit-1/actions/org.freedesktop.UDisks2.policy
    <action id="org.freedesktop.udisks2.filesystem-mount">
      <description>Mount a filesystem</description>
      <message>Authentication is required to mount the filesystem</message>
      <defaults>
        <allow_any>yes</allow_any>
        <allow_inactive>auth_admin</allow_inactive>
        <allow_active>yes</allow_active>
      </defaults>
    </action>

MTP

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

See also

  • Mount — the attaching of an additional filesystem to the currently accessible filesystem of a computer.
  • /etc/fstab — a configuration file that defines how and where the main filesystems are to be mounted, especially at boot time.
  • CurlFtpFS — allows for mounting an FTP folder as a regular directory to the local directory tree.
  • AutoFS — a program that uses the Linux kernel automounter to automatically mount filesystems on demand.
  • Udisks — a D-Bus daemon offering storage related services.
  • Silk Guardian — a Linux kernel module kill switch that upon detecting changes to USB ports, wipes the RAM, securely deletes user specified files, and then shuts down the system.

External resources