Read

Module 7 · The system, disks and the environment

Mounting and unmounting filesystems

Linux has one tree and every disk is grafted onto it: mount the boot CD, create a RAM filesystem, read the mount table, and unmount cleanly.

What you will learn

  • Explain what a mount point is and read the mount table.
  • Mount a device with an explicit type and options, and unmount it.
  • Use mountpoint and df to confirm a mount worked.

Windows gives every disk a letter. Unix gives the whole system a single tree starting at /, and attaches each filesystem to a directory inside it. That act is mounting, the directory is the mount point, and from then on anything under that directory lives on the other device. Whatever the directory contained before is hidden, not deleted, until you unmount.

Reading the mount table

mount with no arguments lists what is mounted: *device* on *directory* type *filesystem* (*options*). The same information lives in /proc/mounts, which is where mount reads it from, and /etc/mtab is just a symlink to it here. In this VM you will see rootfs, devtmpfs, proc, sysfs and two tmpfs entries: pseudo-filesystems the kernel generates, plus RAM disks. No real disk at all.

~% mount
rootfs on / type rootfs (rw)
devtmpfs on /dev type devtmpfs (rw,relatime,size=122720k,nr_inodes=30680,mode=755)
proc on /proc type proc (rw,relatime)
tmpfs on /tmp type tmpfs (rw,relatime)
tmpfs on /run type tmpfs (rw,nosuid,nodev,relatime,mode=755)
sysfs on /sys type sysfs (rw,relatime)

Mounting the CD-ROM

The one real block device in this machine is the boot ISO, /dev/sr0. The general form is mount -t TYPE DEVICE DIRECTORY; the directory must already exist and /mnt exists for exactly this purpose. -t iso9660 names the CD filesystem; mount can usually guess the type from the data, so the option is often optional, but naming it makes errors clearer. Options go after -o: ro for read-only, loop to mount a file as if it were a disk (lesson 66), and dozens more depending on the filesystem.

~% mount -t iso9660 /dev/sr0 /mnt
~% ls /mnt
boot          boot.catalog  isolinux
~% mount | grep sr0
/dev/sr0 on /mnt type iso9660 (ro,relatime,nojoliet,check=s,map=n,blocksize=2048)
~% df -h /mnt
Filesystem                Size      Used Available Use% Mounted on
/dev/sr0                  7.4M      7.4M         0 100% /mnt
~% mountpoint /mnt
/mnt is a mountpoint

Notice the kernel mounted the CD ro without being asked: a CD cannot be written, so touch /mnt/x answers *Read-only file system*. mountpoint DIR is the yes/no test for scripts, and with -q it is silent and only sets the exit status, which is exactly how this course checks your mount tasks.

A filesystem out of thin air: tmpfs

Not every filesystem needs a device. tmpfs lives in RAM: mount -t tmpfs -o size=2m tmpfs /some/dir creates a 2 MB scratch area that vanishes on unmount. The word tmpfs in the device position is just a label; you could write anything. This is how /tmp and /run were made at boot, and it is a handy way to practise mounting when you have no spare disk.

Unmounting

umount DIRECTORY (one *n*, a historical typo that stuck) detaches the filesystem, flushing any pending writes first. It refuses with *Device or resource busy* if any process has a file open there or has it as its current directory, including your own shell: cd out of the mount point before unmounting. fuser -m DIR or lsof tell you who is holding it.

CommandDoes
mountList current mounts
mount -t iso9660 /dev/sr0 /mntAttach the CD at /mnt
mount -o ro DEV DIRMount read-only
mount -t tmpfs -o size=2m tmpfs DIRA 2 MB RAM filesystem
umount DIRDetach whatever is mounted there
mountpoint -q DIRExit 0 if DIR is a mount point

Commands in this lesson

CommandWhat it does
mountShow the mount table.
mount -t iso9660 /dev/sr0 /mntMount the boot CD at /mnt.
mount -t tmpfs -o size=2m tmpfs DIRCreate a small RAM filesystem at DIR.
umount /mntUnmount whatever is at /mnt.
mountpoint -q DIRSilently test whether DIR is a mount point.
cat /proc/mountsThe kernel's own mount list.

Quiz

  1. What happens to files that were in /mnt before you mount a device on it?

    • They are deleted
    • They are hidden until you unmount
    • They are copied onto the device
    • mount refuses if the directory is not empty
  2. umount says 'Device or resource busy'. The most common reason is:

    • The device is broken
    • A process, maybe your own shell, has a file or directory open inside the mount
    • You need the -t option
    • It was mounted read-only
  3. In `mount -t tmpfs -o size=2m tmpfs /root/scratch`, what is the second 'tmpfs'?

    • A device file in /dev
    • A label standing in for the device, since tmpfs needs none
    • A typo; it should appear once
    • The mount point
  4. Which command tells a script, via exit status only, whether /mnt has something mounted?

    • mount /mnt
    • mountpoint -q /mnt
    • df /mnt
    • ls /mnt

Practice

  1. Mount the boot CD-ROM (`/dev/sr0`, an iso9660 filesystem) on `/mnt`.

  2. The CD is currently mounted on `/mnt`. Unmount it.

  3. Create a RAM-backed tmpfs filesystem of 2 MB mounted on the existing directory `/root/lab/l65/scratch`.

Open this lesson in the app to do the tasks in a real Linux machine and have them checked.