Thursday 30 April 2020

Exam 101: Control mounting and unmounting of filesystems

LPI Study Material, LPI Guides, LPI Learning, LPI Certification, LPI Exam Prep


Linux filesystems


The Linux filesystem is one big tree rooted at /, and yet we have filesystems on different devices and partitions. How do we resolve this apparent incongruity? The root (/) filesystem is mounted as part of the initialization process. Each of the other filesystems that you create is not usable by your Linux system until it is mounted at a mount point.

Prerequisites


To get the most from the post in this series, you should have a basic knowledge of Linux and a working Linux system on which you can practice the commands covered in this post. Unless otherwise noted, the examples in this post use CentOS 6, with a 2.6.32-504 kernel. Your results on other systems may differ. Sometimes different versions of a program will format output differently, so your results may not always look exactly like the listings and figures shown here.

Mounting filesystems


In the current set of mounted filesystems, a mount point is simply a directory where the filesystem on a device is grafted into the tree. Mounting is the process of making the filesystem on the device accessible. For example, you might mount filesystems on hard drive partitions as /boot, /tmp, or /home, and you might mount the filesystem on a floppy drive as /mnt/floppy and the filesystem on a CD-ROM as /media/cdrom1. As you can see, mount points may be in the root directory, or in a subdirectory farther down the tree.

Besides filesystems on partitions, floppy disks, and CDs, there are other types of filesystems. The tmpfs filesystem is a virtual memory filesystem. You can also mount filesystems from one system on another system using a networked filesystem such as NFS or AFS. You can even create a file in an existing filesystem and format that as a, possibly different, kind of filesystem and mount that too. This is often done with images of optical media, where you perhaps download an ISO CD or DVD image, then mount that file rather than burning it to real media. Swap space in a file rather than a dedicated swap partition is another example.

While the mount process actually mounts the filesystem on some device (or other resource), it is common to simply say that you “mount the device,” which is understood to mean “mount the filesystem on the device.”

Mounting and unmounting filesystems usually requires root authority. If you are logged in as an ordinary user, you will either use su - to switch to root or sudo. In our examples, when the command prompt ends with #, as in the listing below, you will need root authority.

The basic form of the mount command takes two parameters: the device (or other resource) containing the filesystem to be mounted, and the mount point. We’ll mount our small FAT32 partition /dev/sda3 at the mount point /dos. The mount point must exist before you mount anything over it. If it does not, you will get an error and need to create the mount point or use a different mount point. We illustrate these aspects of basic mounting in Listing 2.

Listing 2. Mount error

[root@attic4‑cent ~]#mount /dev/sda3 /dos
mount: mount point /dos does not exist
[root@attic4‑cent ~]#mkdir /dos
[root@attic4‑cent ~]#mount /dev/sda3 /dos

When you mount a filesystem over an existing directory, the files on the filesystem you are mounting become the files and subdirectories of the mount point. If the mount point directory already contained files or subdirectories, they are not lost, but are no longer visible until the mounted filesystem is unmounted, at which point they become visible again. It is a good idea to avoid this problem by using only empty directories as mount points.

After mounting a filesystem, any files or directories created or copied to the mount point or any directory below it will be created on the mounted filesystem. So a file such as /dos/sampdir/file.txt will be created on the FAT32 filesystem that we mounted at /dos in our example.

Usually, the mount command will automatically detect the type of filesystem being mounted. Occasionally you may need to specify the filesystem type explicitly using the -t option as shown in Listing 3.

Listing 3. Mounting with explicit filesystem type

[root@attic4‑cent ~]#mount ‑t vfat /dev/sda3 /dos

To see what filesystems are mounted, use the mount command with no parameters. Listing 4 shows our example system. Note that you do not need root authority to simply list mounted filesystems.

Listing 4. Displaying mounted filesystems

[ian@attic4‑cent ~]$ mount
/dev/sda11 on / type ext3 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
/dev/sda1 on /grubfile type ext3 (rw)
/dev/sdb2 on /home/ian/data type ext4 (rw)
/dev/sdb3 on /home/ian/research type ext3 (rw)
/dev/sdc1 on /home/ian/pictures type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
/dev/sda3 on /dos type vfat (ro)
/dev/sr0 on /media/KNOPPIX type iso9660 (ro,nosuid,nodev,uhelper=udisks,uid=1000,
gid=1000,iocharset=utf8,mode=0400,dmode=0500)

You can also view similar information by displaying /proc/mounts or /etc/mtab, both of which contain information about mounted filesystems.

Mount options


The mount command has several options that override the default behavior. For example, you can mount a filesystem read-only by specifying -o ro. If the filesystem is already mounted, add remount as shown in Listing 5.

Listing 5. Remounting read-only

[root@attic4‑cent ~]#mount ‑o remount,ro /dos

Notes:

◉ Use commas to separate multiple options, such as remount and ro.

◉ When remounting an already mounted filesystem, it suffices to specify either the mount point or the device name. It is not necessary to specify both.

◉ You cannot mount a read-only filesystem as read-write. Media that cannot be modified, such as CD-ROM discs, will automatically be mounted read-only.

◉ To remount a writable device read-write, specify -o remount,rw-oremount,rw

Remount commands will not complete successfully if any process has open files or directories in the filesystem you are remounting.

Labels, UUIDs, and links


In UNIX and early Linux systems, the /dev directory usually contained entries for all the devices that might ever be attached to a system. Any device that was used was always located in the same place in the /dev tree, so using names such as /dev/sda6 was natural. With the advent of hot-plugging of devices such as USB or Firewire (IEEE 1394) attached devices, a given device might appear in one USB port today, and that same device might be plugged into a different USB port tomorrow. In this environment, you might want to always mount your USB stick at /media/myusbstick, regardless of which USB port you plug it in to. If the filesystem on the partition supports either, you can use these with the mount command too. Use the blkid command to find out the UUID and label (if present) associated with a device.

Listing 6 shows how to use blkid to find the label and UUID for our root partition and then how to create two additional mount points and mount the root partition at these two additional points. This example is for illustration. You would not normally do this in a production environment.

Listing 6. Mount using labels or UUIDs

[root@attic4‑cent ~]#blkid /dev/sda11
/dev/sda11: UUID="2f60a3b4‑ef6c‑4d4c‑9ef4‑50d7f75124a2" TYPE="ext3" LABEL="CentOS 6"
[root@attic4‑cent ~]#mkdir /mnt/sda11label
[root@attic4‑cent ~]#mkdir /mnt/sda11uuid
[root@attic4‑cent ~]#mount LABEL="CentOS 6" /mnt/sda11label
[root@attic4‑cent ~]#mount UUID="2f60a3b4‑ef6c‑4d4c‑9ef4‑50d7f75124a2" /mnt/sda11uuid

With the advent of udev, you will find additional symbolic links in the /dev directory for devices such as hard drives. Listing 7 shows the links to /dev/sda6 on my CentOS 6 system.

Listing 7. Symbolic links to /dev/sda6

[root@attic4‑cent ~]#find /dev ‑lname "*sda11"/dev/root
/dev/disk/by‑label/CentOS\x206
/dev/disk/by‑uuid/2f60a3b4‑ef6c‑4d4c‑9ef4‑50d7f75124a2
/dev/disk/by‑id/wwn‑0x50014ee056628af6‑part11
/dev/disk/by‑id/scsi‑SATA_WDC_WD6401AALS‑_WD‑WMASY6347052‑part11
/dev/disk/by‑id/ata‑WDC_WD6401AALS‑00L3B2_WD‑WMASY6347052‑part11
/dev/disk/by‑path/pci‑0000:00:11.0‑scsi‑0:0:0:0‑part11
/dev/block/8:11
/dev/.udev/watch/113
/dev/.udev/links/disk\x2fby‑label\x2fCentOS\x5cx206/b8:11
/dev/.udev/links/root/b8:11
/dev/.udev/links/disk\x2fby‑id\x2fwwn‑0x50014ee056628af6‑part11/b8:11
/dev/.udev/links/disk\x2fby‑uuid\x2f2f60a3b4‑ef6c‑4d4c‑9ef4‑50d7f75124a2/b8:11
/dev/.udev/links/disk\x2fby‑path\x2fpci‑0000:00:11.0‑scsi‑0:0:0:0‑part11/b8:11
/dev/.udev/links/disk\x2fby‑id\x2fscsi‑SATA_WDC_WD6401AALS‑_WD‑WMASY6347052‑part11/b8:11
/dev/.udev/links/disk\x2fby‑id\x2fata‑WDC_WD6401AALS‑00L3B2_WD‑WMASY6347052‑part11/b8:11

You can also use a symbolic link as another way of specifying the device name when mounting a device.

Boot time and fstab


In the post for topic 102, “Learn Linux, 101: Boot managers,” you learned how to use the root= parameter in both GRUB and LILO to tell the boot loader what filesystem should be mounted as root. For GRUB2, this is the set rootsetroot statement. Once the root filesystem is mounted, the initialization process runs mount with the -a option to automatically mount a set of filesystems. The set is specified in the file /etc/fstab.

Listing 8 shows /etc/fstab for a sample CentOS 6 system. In this example, most hard drive partitions are identified by UUID. I have added examples of how to mount /dev/sda3 at /dos as we did earlier, and also how to use a label to mount a labeled partition at /mnt/fedora22.

Listing 8. Example CentOS 6 fstab

[root@attic4‑cent ~]#cat /etc/fstab

#
#/etc/fstab
#Created by anaconda on Wed Jul  8 09:34:46 2015
#
#Accessible filesystems, by reference, are maintained under '/dev/disk'
#See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=2f60a3b4‑ef6c‑4d4c‑9ef4‑50d7f75124a2 /                   ext3    defaults        1 1
UUID=3c3de27e‑779a‑44d5‑ad7a‑61c5fd03d9e7 /grubfile           ext3    defaults        1 2
UUID=158d605e‑2591‑4749‑bf59‑5e92e1b1c01d swap                swap    defaults        0 0
tmpfs                                     /dev/shm            tmpfs   defaults        0 0
devpts                                    /dev/pts            devpts  gid=5,mode=620  0 0
sysfs                                     /sys                sysfs   defaults        0 0
proc                                      /proc               proc    defaults        0 0
UUID=4c962b67‑c646‑467f‑96fb‑cbbd6de40140 /home/ian/data      ext4    defaults        1 2
UUID=0998d33c‑3398‑463d‑b0e3‑7c13ca0c675f /home/ian/research  ext3    defaults        1 2
UUID=e3be4658‑b79b‑470d‑82fe‑bb434bcdcc2f /home/ian/pictures  ext4    defaults        1 2
LABEL="FEDORA22"                          /mnt/fedora22       ext4    defaults        1 2
/dev/sda3                                 /dos                vfat    defaults        0 0

Lines starting with a # character are comments. Remaining lines contain six fields. Because the fields are positional, they must all be specified.

file system
This may be a device name such as /dev/sda1, or a label (LABEL=) or UUID (UUID=). For the root filesystem of our CentOS 6 example, it could be /dev/sda11, LABEL=”CentOS 6″, or UUID=2f60a3b4-ef6c-4d4c-9ef4-50d7f75124a2. Using a label or UUID makes your system more robust when devices are added or removed.

mount point
This is the mount point we discussed in Mounting filesystems above. For swap space, this should be the value ‘none’ or ‘swap’. On older systems you will usually find the value ‘none’.

type
Specifies the type of filesystem. CD/DVD drives will often support either ISO9660 or UDF filesystems, so you may specify multiple possibilities in a comma-separated list if you specify such a drive in /etc/fstab. If you want mount to automatically determine the type, specify auto. For example, you might see lines like the following on some older systems for a CD or DVD and a floppy disk.
/dev/scd0 /media/cdrom0 udf,iso9660 user,noauto,exec,utf8 0 0
/dev/fd0 /media/floppy0 auto rw,user,noauto,exec,utf8 0 0

option
◉ Specifies the mount options. Specify defaults if you want default mount options. Some options you will want to know about are:

◉ rw and ro specify whether the filesystem should be mounted read-write or read-only.

◉ noauto specifies that this filesystem should not be automatically mounted at boot time or whenever mount -a is issued. In our example, this is done for the removable drives.

◉ user specifies that a non-root user is permitted to mount and unmount the filesystem. This is especially useful for removable media. In older systems, this option is specified in /etc/fstab rather than on the mount command. With newer systems, it may be specified in udev rules that are located in rules files within /lib/udev/rules.d or /etc/udev/rules.d. The options for the DVD drive on my CentOS 6 system come from udev rules, and that is why there is no entry in /etc/fstab for an optical drive.

◉ exec and noexec specify whether or not to allow execution of files from the mounted filesystem. User-mounted filesystems default to noexec unless exec is specified afteruser.

◉ noatime will disable recording of access times. Not using access times may improve performance.

dump
Specifies whether the dump command should consider this ext2 or ext3 filesystem for backups. A value of 0 tells dump to ignore this filesystem.

pass
Non-zero values of pass specify the order of checking filesystems at boot time, as discussed in our post “Learn Linux, 101: Maintain the integrity of filesystems.”

When you mount a filesystems that is listed in /etc/fstab, you can give either the device name or the mount point when mounting the filesystem. You do not need to give both.

On some systems, for example SUSE 11.2, you may find that the fstab generated at install time uses symbolic links to the device. So, you may see /dev/disk/by-id/ata-WDC_WD1001FALS-00J7B1_WD-WMATV3772868-part6, rather than /dev/sda6 for the file system value. Refer back to Listing 7 for additional possibilities

Consult the man pages for fstab, mount, and udev for additional information, including options not covered here.

Unmounting filesystems


All mounted filesystems are usually unmounted automatically by the system when it is rebooted or shut down. When a filesystem is unmounted, any cached filesystem data in memory is flushed to the device.

You may also unmount filesystems manually. Indeed, you should do this when removing writable media such as diskettes or USB drives or memory keys.

Use the unmount command to unmount the filesystem, specifying either the device name or mount point as an argument. This listing shows how to unmount /dos, then remount it and unmount again using the device name.

Listing 10. Unmounting filesystems

[root@attic4‑cent ~]#umount /dos
[root@attic4‑cent ~]#mount /dev/sda3 /dos
[root@attic4‑cent ~]#umount /dev/sda3

After a filesystem is unmounted, any files in the directory used for the mount point are visible again.

If you attempt to unmount a filesystem while a process has open files on that filesystem, you will see an error message. Before unmounting a filesystem, you should check that there are no processes running that have open files on the filesystem. Use the lsof or fusercommand to determine what files are open or what process has open files. You may need the -w option on lsof to avoid warning messages related to the Gnome Virtual File system (gvfs). Check the man pages to learn about additional mount options and lsof. If you are checking a whole device, you can specify the device name or the mount point. You may also check whether an individual file is in use or not.

To illustrate these commands, I created a copy of /etc/fstab on /dos and a small script to read lines from stdin and print them to stdout with a 10 second pause between each line. Listing 11 shows the error message from umount when files are in use and the use of lsof and fuser to check for open files on /dos, or the underlying device /dev/sda9.

Listing 11. Checking for open files

[root@attic4‑cent ~]#umount /dos
umount: /dos: device is busy.
        (In some cases useful info about processes that use
         the device is found by lsof(8) or fuser(1))
[root@attic4‑cent ~]#lsof ‑w /dos
COMMAND     PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
slowread. 28960  ian    0r   REG    8,3     1207    2 /dos/fstab
sleep     28972  ian    0r   REG    8,3     1207    2 /dos/fstab
[root@attic4‑cent ~]#lsof ‑w /dev/sda3
COMMAND     PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
slowread. 28960  ian    0r   REG    8,3     1207    2 /dos/fstab
sleep     28978  ian    0r   REG    8,3     1207    2 /dos/fstab
[root@attic4‑cent ~]#lsof ‑w /dos/fstab
COMMAND     PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
slowread. 28960  ian    0r   REG    8,3     1207    2 /dos/fstab
sleep     28989  ian    0r   REG    8,3     1207    2 /dos/fstab
[root@attic4‑cent ~]#fuser ‑m /dos
/dos:                28960 29001
[root@attic4‑cent ~]#fuser ‑m /dev/sda3
/dev/sda3:           28960 29001

At this point you can either wait until the filesystem is no longer busy, or you can do a lazy unmount, by specifying the -l option. A lazy unmount detaches the filesystem from the filesystem tree immediately, and cleans the references to the filesystem when it is no longer busy.

Removable filesystems


We mentioned some issues with removable devices such as USB or Firewire (IEEE 1394) attached devices. It is inconvenient to switch to root access every time you need to mount or unmount such a device. The same goes for CD, DVD, and floppy drives, where you need to unmount the device to change media. In the discussion of fstab above, we mentioned the user option, which allows ordinary users to mount and unmount devices.

Note that the filesystem types for the optical drive are specified as udf,iso9660, while the filesystem type for the floppy is specified as auto. For the optical drive, the mount process will check first for a udf filesystem (common on DVD) and then for an iso9660 filesystem (common on CD). For the floppy drive, the mount process will probe for a filesystem type. You can create or edit /etc/filesystems to change the order in which the filesystems will be probed.

Note: You should always unmount removable drives or media before disconnecting the drive or attempting to remove the media. Failure to do so may result in loss of data that has not yet been written to the device.

If you run a graphical desktop such as Nautilus, you will usually find options that allow removable devices and media to be automatically mounted. For example, if I insert a Knoppix DVD into the DVD drive of my system, I might see a mount entry such as shown in Listing 12. The presence of ‘uid=1000’ shows that the user with id 1000 can unmount this disc. The id command shows the uid for user ian is 1000, so ian can unmount this disc.

Listing 12. Desktop mounting of DVD

[ian@attic4‑cent ~]$ mount | grep sr0
/dev/sr0 on /media/KNOPPIX type iso9660 (ro,nosuid,nodev,uhelper=udisks,uid=1000,
gid=1000,iocharset=utf8,mode=0400,dmode=0500)
[ian@attic4‑cent ~]$ id ian
uid=1000(ian) gid=1000(ian) groups=1000(ian)

You may also use the eject command to eject removable media when the drive supports the operation as most CD and DVD drives do. If you have not unmounted the device first, then eject will both unmount and eject the disc.

Swap space


You may have noticed in the discussion of fstab above that swap space does not have a mount point. The boot process usually enables swap space defined in /etc/fstab unless the noauto option is specified. To manually control swap space on a running system — for example, if you added a new swap partition—use the swapon and swapoff commands. See the man pages for details.

You can view the currently enabled swap devices with cat /proc/swaps or with swapon -sswapon-s as shown in Listing 13.

Listing 13. Displaying swap space

[ian@attic4‑cent ~]$ swapon ‑s
Filename  Type  Size  Used  Priority
/dev/sda2                               partition  10241432  8 ‑1
[ian@attic4‑cent ~]$ cat /proc/swaps
Filename  Type  Size  Used  Priority
/dev/sda2  partition  10241432  8  ‑1

This completes your introduction to device mounting and unmounting on Linux.

Related Posts

0 comments:

Post a Comment