Linux administration

Control Filesystem Mounting and Unmounting

Nguyen Hai Chau
Vietnam National University

Mounting Filesystems

  • As far as many parts of a Linux system are concerned, a partition contains entirely arbitrary data
  • When installing, you set things up so that a partition contains a filesystem — a way of organising data into files and directories
  • One filesystem is made the root filesystem: the root directory on that filesystem becomes the directory named /
  • Other filesystems can be mounted: the root directory of that filesystem is grafted onto a directory of the root filesystem
    • This arranges for every file in every mounted filesystem to be accessible from a single unified name space
  • The directory grafted onto is called the mount point

Mounting a Filesystem: mount

  • 'Important' filesystems are mounted at boot-up; other filesystems can be mounted or unmounted at any time
  • The mount command mounts a filesystem
    • You usually need to have root permission to mount a filesystem
  • mount makes it easy to mount filesystems configured by the system administrator
  • For example, many systems are configured so that
$ mount /mnt/cdrom

will mount the contents of the machine's CD-ROM drive under the directory /mnt/cdrom

Mounting Other Filesystems

  • mount /dev/sdb3 /mnt/extra mounts the filesystem stored in the /dev/sdb3 device on the mount point /mnt/extra
  • You may occasionally need to specify the filesystem type explicitly:
# mount -t vfat /dev/hdd1 /mnt/windows
  • Allowable filesystem types are listed in the mount(8) manpage
    • To see a list of the filesystems currently mounted, run mount without any options

Unmounting a Filesystem: umount

  • A filesystem can be unmounted with umount
    • Note the spelling!
  • umount /mnt/extra unmounts whatever is on the /mnt/extra mount point
  • umount /dev/sdb3 unmounts the filesystem in the /dev/sdb3 device, wherever it is mounted
  • You normally need to have root permission to unmount a filesystem
  • It's also impossible to unmount a 'busy' filesystem
    • A filesystem is busy if a process has a file on it open
    • Or if a process has a directory within it as its current directory

Configuring mount: /etc/fstab

  • The /etc/fstab file contains information about filesystems that are known to the system administrator

    • Specifying a filesystem in /etc/fstab makes it possible to use its mount point as the only argument to mount
  • /etc/fstab also configures which filesystems should be mounted at boot-up

  • Each line in /etc/fstab describes one filesystem

  • Six columns on each line

Sample /etc/fstab

  • A sample /etc/fstab file:
#
# /etc/fstab
# Created by anaconda on Mon Apr 21 13:10:29 2014
#
# 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
#
/dev/mapper/fedora-root /                       ext4    defaults        1 1
UUID=05305fa3-3b85-4107-bf73-41ce39a6403f /boot                   ext4    defaults        1 2
/dev/mapper/fedora-home /home                   ext4    defaults        1 2
/dev/mapper/fedora-swap swap                    swap    defaults        0 0
/dev/sda2   /media/C    ntfs    ro,auto 0 0
/dev/sda3   /media/D    ntfs    ro,auto 0 0

Filesystem Types

  • The most common filesystem types are:
Type Usage
ext4 The standard Linux filesystem
iso9660 The filesystem used on CD-ROMs
proc Not a real filesystem, so uses none as the device. Used as a way for the kernel to report system information to user processes
vfat The filesystem used by Windows 95
auto Not a real filesystem type. Used as a way of asking the mount command to probe for various filesystem types, particularly for removable media
  • Networked filesystems include nfs (Unix-specific) and smbfs (Windows or Samba)
  • Other, less common types exist; see mount(8)

Mount Options

  • Comma-separated options in /etc/fstab
  • Alternatively, use comma-separated options with -o on the mount command line
  • Common mount options:
Option Description
noauto In /etc/fstab, prevents the filesystem being mounted at bootup. Useful for removable media
ro Mount the filesystem read-only
users Let non-root users mount and unmount this filesystem
user Like users, but non-root users can only unmount filesystems that they themselves mounted
  • Other less common mount options exist, as well as many options for individual filesystem types - see mount(8)

Columns in /etc/fstab

  • 1st column: Block device of remote file system to be mount
  • 2nd column: Mount point (a directory with absolute path)
  • 3rd column: File system type (ext4, ntfs etc.)
  • 4th column: Mount option, separated by commas (,), for example: ro is read-only, rw is read-write
  • 5th column: This is used by dump(8) to determine which filesystems need to be dumped, default to 0
  • 6th column: This field is used by fsck(8) to determine the order in which filesystem checks are done at boot time. The root file system should have this column set to 1, other file system should be 2.

Mounting a File

  • Using loop device, Linux can mount a filesystem stored in a normal file, instead of a disk
  • Useful for testing images of CD or DVD before burning them to disk
  • For example, to create a filesystem of 3700 input blocks:
# dd if=/dev/zero of=disk.img bs=1024 count=3700
# mke2fs -F disk.img
  • To mount the file so that its contents is accessible through /mnt:
# mount -o loop disk.img /mnt

Exercises

  • a. Use mount to find out which filesystems are mounted. everything on it is properly written, and it is safe to remove.
  • b. Try the commands on the last slide to mount a file, and try copying some files into it. Try using the df command to see how much space is available in the file. Unmount /mnt as you would any other filesystem.