Chapter 12
Managing Disks and Filesystems
Your operating system, applications, and data all need to be kept on some kind of permanent storage so that when you turn off your computer, it is all still there when the computer is turned on again. Traditionally, that storage has been provided by a hard disk in your computer. To organize the information on that disk, the disk is usually divided into partitions, with most partitions given a structure referred to as a filesystem.
This chapter describes how to work with hard disks. Hard disk tasks include partitioning, adding filesystems, and managing those filesystems in various ways.
After covering basic partitions, I describe how logical volume management (LVM) can be used to make it easier to grow, shrink, and otherwise manage filesystems more efficiently.
The basics of how data storage works are the same in most modern operating systems. When you install the operating system, the disk is divided into one or more partitions. Each partition is formatted with a filesystem. In the case of Linux, some of the partitions may be specially formatted for elements such as swap area or LVM physical volumes.
While disks are used for permanent storage, random access memory (RAM) and swap are used for temporary storage. For example, when you run a command, that command is copied from the hard disk into RAM so that your computer processor (CPU) can access it more quickly.
Your CPU can access data much faster from RAM than it can from hard disk. However, a disk is usually much larger than RAM, RAM is much more expensive, and RAM is erased when the computer reboots. Think of your office as a metaphor for RAM and disk. A disk is like a file cabinet where you store folders of information you need. RAM is like the top of your desk, where you put the folder of papers while you are using it, but put back in the file cabinet when you are not.
If RAM fills up, by running too many processes or a process with a memory leak, new processes will fail if your system doesn't have a way to extend system memory. That's where a swap area comes in. A swap space is a hard disk partition where your computer can “swap out” data from RAM that isn't being used at the moment and then “swap in” the data back to RAM when it is again needed. Although it is better to never exceed your RAM (performance takes a hit when you swap), swapping out is better than having processes just fail.
Another special partition is a logical volume manager (LVM) physical volume. LVM physical volumes enable you to create pools of storage space called volume groups. From those volume groups, you have a lot more flexibility for growing and shrinking logical volumes than you have resizing disk partitions directly.
For Linux, at least one disk partition is required, assigned to the root (/) of the entire Linux filesystem. However, it is more common to have separate partitions that are assigned to particular directories, such as /home, /var, and/or /tmp. Each of the partitions is connected to the larger Linux filesystem by mounting it to a point in the filesystem where you want that partition to be used. Any file added to the mount point directory of a partition, or a subdirectory, is stored on that partition.
The business of connecting disk partitions to the Linux filesystem is done automatically and invisibly to the end user. How does this happen? Each regular disk partition created when you install Linux is associated with a device name. An entry in the /etc/fstab file tells Linux each partition's device name and where to mount it (as well as other bits of information). The mounting is done when the system boots.
Most of this chapter focuses on understanding how your computer's disk is partitioned and connected to form your Linux filesystem, as well as how to partition disks, format filesystems and swap space, and have those items used when the system boots. The chapter then covers how to do partitioning and filesystem creation manually.
Linux provides several tools for managing your hard disk partitions. You need to know how to partition your disk if you want to add a disk to your system or change your existing disk configuration.
This section demonstrates disk partitioning using an 8GB removable USB flash drive and a fixed hard disk. To be safe, I use a USB flash drive that doesn't contain any data I want to keep to practice partitioning.
To view disk partitions, use the fdisk command with the -l option. For RHEL 6, use -c (turn off DOS compatibility mode) and -u (show size in sectors, not cylinders). The following is an example of partitioning on a removable 8GB USB flash drive:
# fdisk -c -u -l /dev/sdc Disk /dev/sdc: 8021 MB, 8021606400 bytes 16 heads, 48 sectors/track, 20400 cylinders, total 15667200 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0xc3072e18 Device Boot Start End Blocks Id System /dev/sdc1 2048 4196351 2097152 83 Linux /dev/sdc2 4196352 5220351 512000 82 Linux swap / Solaris /dev/sdc3 5220352 7317503 1048576 c W95 FAT32 (LBA) /dev/sdc4 7317504 15667199 4174848 5 Extended /dev/sdc5 7319552 7729151 204800 83 Linux /dev/sdc6 7731200 15667199 3968000 8e Linux LVM
When this USB flash drive is inserted, it is assigned to the next available sd device. In this example, /dev/sdc is assigned as the device name (the third disk on the system). Your drive might be assigned to a different device name. You can see the USB flash drive is 8021MB in size and has six partitions. Here are some things to look for:
Your first primary hard disk usually appears as /dev/sda. With RHEL and Fedora installations, there is usually at least one LVM partition, out of which other partitions can be assigned. So, the output of fdisk might be as simple as the following:
# fdisk -cul /dev/sda Disk /dev/sda: 500.1 GB, 500107862016 bytes 255 heads, 63 sectors/track, 60801 cylinders, total 976773168 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x000ebb20 Device Boot Start End Blocks Id System /dev/sda1 * 2048 411647 204800 83 Linux /dev/sda2 411648 976773119 488180736 8e Linux LVM
The first partition is roughly 200MB and is mounted on the /boot directory. The asterisk (*) under the Boot column indicates that the partition is bootable (that's where the kernel and other components needed to boot the system are stored). The rest of the disk is consumed by the LVM partition, which is ultimately used to create logical volumes. You can find out more on LVM in the “Using Logical Volume Management Partitions” section later in this chapter.
For the moment, I recommend you leave the hard disk alone and find a USB flash drive that you do not mind erasing. You can try the commands I demonstrate on that drive.
To add a new storage medium (hard disk, USB flash drive, or similar device) to your computer so that it can be used by Linux, you need to first connect the disk device to your computer, then partition the disk. Here's the general procedure:
The easiest way to add a disk or flash drive to Linux is to have the entire disk devoted to a single Linux partition. You can have multiple partitions, however, and assign them each to different types of filesystems and different mount points, if you like.
The following process takes you through partitioning a USB flash drive to be used for Linux that has only one partition. If you have a USB flash drive (any size) that you don't mind erasing, you can work through this procedure as you read. The section following this describes how to partition a disk with multiple partitions.
# tail -f /var/log/messages scsi 6:0:0:0: Direct-Access S31B1102 USB DISK 1100 PQ: 0 ANSI: 0 CCS sd 6:0:0:0: Attached scsi generic sg2 type 0 sd 6:0:0:0: [sdc] 8342528 512-byte logical blocks: (4.27 GB/3.97 GiB) sd 6:0:0:0: [sdc] Write Protect is off sd 6:0:0:0: [sdc] Mode Sense: 43 00 00 00 sd 6:0:0:0: [sdc] Assuming drive cache: write through sd 6:0:0:0: [sdc] Assuming drive cache: write through sdc: sdc1 sd 6:0:0:0: [sdc] Assuming drive cache: write through sd 6:0:0:0: [sdc] Attached SCSI removable disk
# fdisk -c -u /dev/sdc Command (m for help):
Command (m for help): p ... Device Boot Start End Blocks Id System /dev/sdc1 2048 15667199 7832576 c W95 FAT32 (LBA) Command (m for help): d Selected partition 1
Command (m for help): p ... Device Boot Start End Blocks Id System /dev/sdb1 2048 15667199 7832576 83 Linux
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
# partprobe /dev/sdc
# mkfs -t ext4 /dev/sdc1
# mkdir /mnt/test # mount /dev/sdc1 /mnt/test # df -h /mnt/test Filesystem Size Used Avail Use% Mounted on /dev/sdc1 7.4G 17M 7.0G 1% /mnt/test # mount | grep sdc1 /dev/sdc1 on /mnt/test type ext4 (rw)
# umount /dev/sdc1
/dev/sdc1 /mnt/test ext4 defaults 0 1
At this point, you have a working, permanently mounted disk partition. The next section describes how to partition a disk that has multiple partitions.
Now that you understand the basic process of partitioning a disk, adding a filesystem, and making that filesystem available (temporarily and permanently), it is time to try a more complex example. Taking that same 8GB USB flash drive, I ran the procedure described later in this section to create multiple partitions on one disk.
In this procedure, I create a partition of 500MB (sdc1 and sdc2), 300MB (sdc3), 350MB (sdc5), and 400MB (sdc6). The sdc4 device is an extended partition, which consumes all remaining disk space. Space from the sdc5 and sdc6 partitions is taken from the extended partition.
As before, insert the USB flash drive and determine the device name (in my case, /dev/sdc). Also, be sure to unmount any partitions that mount automatically when you insert the USB flash drive.
# fdisk -cu /dev/sdc Command (m for help): d Selected partition 1 Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First sector (2048-15667199, default 2048): <Enter> Using default value 2048 Last sector,+sectors or +size{K,M,G}(2048-15667199, default 15667199):+500M Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 2 First sector (1026048-15667199, default 1026048): <Enter> Using default value 1026048 Last sector, +sectorsor +size {K,M,G}(default 15667199):+500M Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 3 First sector (2050048-15667199, default 2050048): <Enter> Using default value 2050048 Last sector, +sectorsor +size {K,M,G} (...default 15667199):+300M Command (m for help): n Command action e extended p primary partition (1-4) e Selected partition 4 First sector (2664448-15667199, default 2664448): <Enter> Using default value 2664448 Last sector,+sectors or + size{K,M,G}(... default 15667199): <Enter> Using default value 15667199 Command (m for help): n First sector (2666496-15667199, default 2666496): <Enter> Using default value 2666496 Last sector, +sectors or +size{K,M,G} (...default 15667199): +350M Command (m for help): n First sector (...default 3385344): <Enter> Using default value 3385344 Last sector, +sectors or +size {K,M,G} (...default 15667199): +400M
Command (m for help): p ... Device Boot Start End Blocks Id System /dev/sdc1 2048 1026047 512000 83 Linux /dev/sdc2 1026048 2050047 512000 83 Linux /dev/sdc3 2050048 2664447 307200 83 Linux /dev/sdc4 2664448 15667199 6501376 5 Extended /dev/sdc5 2666496 3383295 358400 83 Linux /dev/sdc6 3385344 4204543 409600 83 Linux
Command (m for help): t Partition number (1-6): 2 Hex code (type L to list codes): 82 Changed system type of partition 2 to 82 (Linux swap / Solaris) Command (m for help): t Partition number (1-6): 5 Hex code (type L to list codes): c Changed system type of partition 5 to c (W95 FAT32 (LBA)) Command (m for help): t Partition number (1-6): 6 Hex code (type L to list codes): 8e Changed system type of partition 6 to 8e (Linux LVM)
Command (m for help): p ... Device Boot Start End Blocks Id System /dev/sdc1 2048 1026047 512000 83 Linux /dev/sdc2 1026048 2050047 512000 82 Linux swap / Solaris /dev/sdc3 2050048 2664447 307200 83 Linux /dev/sdc4 2664448 15667199 6501376 5 Extended /dev/sdc5 2666496 3383295 358400 c W95 FAT32 (LBA) /dev/sdc6 3385344 4204543 409600 8e Linux LVM Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. ...
# grep sdc /proc/partitions 8 32 7833600 sdc 8 33 512000 sdc1 8 34 512000 sdc2 8 35 307200 sdc3 8 36 1 sdc4 8 37 358400 sdc5 8 38 409600 sdc6
# mkfs -t ext4 /dev/sdc1
# mkswap /dev/sdc2
# mkfs /dev/sdc3
# mkfs -t vfat /dev/sdc5
# pvcreate /dev/sdc6
These partitions are now ready to be mounted, used as swap area, or added to an LVM volume group. See the next section, “Using Logical Volume Management Partitions,” to see how LVM physical volumes are used to ultimately create LVM logical volumes from volume groups. See the “Mounting Filesystems” section for descriptions of how to mount filesystems and enable swap areas.
Basic disk partitioning in Linux has its shortcomings. What happens if you run out of disk space? A common solution is to copy data to a bigger disk, restart the system with the new disk, and hope that you don't run out of space again any time soon. This process means downtime and inefficiency.
Logical Volume Manager (LVM) offers lots of flexibility and efficiency in dealing with constantly changing storage needs. With LVM, physical disk partitions are added to pools of space called volume groups. Logical volumes are assigned space from volume groups as needed. This gives you the ability to:
With LVM it is also easier to shrink filesystems to reclaim disk space, although shrinking does require that you unmount the logical volume (but no reboot is needed). LVM also supports advanced features, such as mirroring and working in clusters.
Let's start by looking at an existing LVM example on a Red Hat Enterprise Linux system. The following command displays the partitions on my first hard disk:
# fdisk -cul /dev/sda | grep /dev/sda Disk /dev/sda: 160.0 GB, 160000000000 bytes /dev/sda1 * 2048 1026047 512000 83 Linux /dev/sda2 * 1026048 312498175 155736064 8e Linux LVM
On this RHEL system, the 160GB hard drive is divided into one 500MB Linux partition (sda1) and a second (Linux LVM) partition that consumes the rest of the disk (sda2). Next, I use the pvdisplay command to see if that partition is being used in an LVM group:
# pvdisplay /dev/sda2 --- Physical volume --- PV Name /dev/sda2 VG Name vg_abc PV Size 148.52 GiB / not usable 2.00 MiB Allocatable yes (but full) PE Size 4.00 MiB Total PE 38021 Free PE 0 Allocated PE 38021 PV UUID wlvuIv-UiI2-pNND-f39j-oH0X-9too-AOII7R
You can see that the LVM physical volume represented by /dev/sda2 has 148.52GiB of space, all of which has been totally allocated to a volume group named vg_abc. The smallest unit of storage that can be used from this physical volume is 4.0MiB, which is referred to as a Physical Extent (PE).
# vgdisplay vg_abc --- Volume group --- VG Name vg_abc System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 4 VG Access read/write VG Status resizable MAX LV 0 Cur LV 3 Open LV 3 Max PV 0 Cur PV 1 Act PV 1 VG Size 148.52 GiB PE Size 4.00 MiB Total PE 38021 Alloc PE / Size 38021 / 148.52 GiB Free PE / Size 0 / 0 VG UUID c2SGHM-KU9H-wbXM-sgca-EtBr-UXAq-UnnSTh
You can see that all of the 38,021 PEs have been allocated. Using lvdisplay as follows, you can see where they have been allocated (I have snipped some of the output):
# lvdisplay vg_abc --- Logical volume --- LV Name /dev/vg_abc/lv_root VG Name vg_abc LV UUID 33VeDc-jd0l-hlCc-RMuB-tkcw-QvFi-cKCZqa LV Write Access read/write LV Status available # open 1 LV Size 50.00 GiB Current LE 12800 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0 --- Logical volume --- LV Name /dev/vg_abc/lv_home VG Name vg_abc ... LV Size 92.64 GiB --- Logical volume --- LV Name /dev/vg_abc/lv_swap VG Name vg_abc ... LV Size 5.88 GiB
There are three logical volumes drawing space from vg_abc. Each logical volume is associated with a device name that includes the volume group name and the logical volume name: /dev/vg_abc/lv_root (50GB), /dev/vg_abc/lv_home (92.64GB), and /dev/vg_abc/lv_swap (5.88GB). Other devices linked to these names are located in the /dev/mapper directory: vg_abc-lv_home, vg_abc-lv_root, and vg_abc-lv_swap. Either set of names can be used to refer to these logical volumes.
The root and home logical volumes are formatted as ext4 filesystems, whereas the swap logical volume is formatted as swap space. Let's look in the /etc/fstab file to see how these logical volumes are used:
# grep vg_ /etc/fstab /dev/mapper/vg_abc-lv_root / ext4 defaults 1 1 /dev/mapper/vg_abc-lv_home /home ext4 defaults 1 2 /dev/mapper/vg_abc-lv_swap swap swap defaults 0 0
Figure 12.1 illustrates how the different partitions, volume groups, and logical volumes relate to the complete Linux filesystem. The sda1 device is formatted as a filesystem and mounted on the /boot directory. The sda2 device provides space for the vg_abc volume group. Then logical volumes lv-home and lv-root are mounted on the /home and / directories, respectively.
If you run out of space on any of the logical volumes, you can assign more space from the volume group. If the volume group is out of space, you can add another hard drive or network storage drive and add space from that drive to the volume group, so more is available.
Now that you know how LVM works, the next section shows you how to create LVM logical volumes from scratch.
LVM logical volumes are used from the top down, but they are created from the bottom up. As illustrated in Figure 12.1, first you create one or more physical volumes (pv), use the physical volumes to create volume groups (vg), and then create logical volumes from the volume groups (lv).
Commands for working with each LVM component begin with the letters pv, vg, and lv. For example, pvdisplay shows physical volumes, vgdisplay shows volume groups, and lvdisplay shows logical volumes.
The following procedure takes you through the steps of creating LVM volumes from scratch. To do this procedure, you could use the USB flash drive and partitions I described earlier in this chapter.
# vgcreate myvg0 /dev/sdc6 Volume group "myvg0" successfully created
# vgdisplay myvg0 --- Volume group --- VG Name myvg0 ... VG Size 396.00 MiB PE Size 4.00 MiB Total PE 99 Alloc PE / Size 0 / 0 Free PE / Size 99 / 396.00 MiB
# lvcreate -n music -L 100M myvg0 Logical volume "music" created # ls /dev/mapper/myvg0* /dev/mapper/myvg0-music
# mkfs -t ext4 /dev/mapper/myvg0-music # mkdir /mnt/mymusic # mount /dev/mapper/myvg0-music /mnt/mymusic # df -h /mnt/mymusic Filesystem Size Used Avail Use% Mounted on /dev/mapper/myvg0-music 97M 5.6M 87M 7% /mnt/mymusic
/dev/mapper/myvg0-music /mnt/mymusic ext4 defaults 1 2
The next time you reboot, the logical volume is automatically mounted on /mnt/mymusic. (Be sure to unmount the logical volume and remove this line if you want to remove the USB flash drive from your computer.)
If you run out of space on a logical volume, you can add space to it without even unmounting it. To do that, you must have space available in the volume group, grow the logical volume, and grow the filesystem to fill it. Building on the procedure in the previous section, here's how to grow a logical volume:
# vgdisplay myvg0 ... VG Size 396.00 MiB PE Size 4.00 MiB Total PE 99 Alloc PE / Size 25 / 100.00 MiB Free PE / Size 74 / 296.00 MiB # df -h /mnt/mymusic/ Filesystem Size Used Avail Use% Mounted on /dev/mapper/myvg0-music 97M 5.6M 87M 7% /mnt/mymusic
# lvextend -L +100M /dev/mapper/myvg0-music Extending logical volume music to 200.00 MiB Logical volume music successfully resized
# resize2fs -p /dev/mapper/myvg0-music
# df -h /mnt/mymusic/ Filesystem Size Used Avail Use% Mounted on /dev/mapper/myvg0-music 194M 5.6M 179M 3% /mnt/mymusic
You can see that the filesystem is now about 100MB larger.
Now that you have had a chance to play with disk partitioning and filesystems, I'm going to step back and talk about how filesystems are set up to connect permanently to your Linux system.
Most of the hard disk partitions created when you install Linux are mounted automatically for you when the system boots. When you install Fedora, Ubuntu, Red Hat Enterprise Linux, and other Linux systems, you have the option to let the installer automatically configure your hard disk or create partitions yourself and indicate the mount points for those partitions.
When you boot Linux, usually all the Linux partitions on your hard disk are listed in your /etc/fstab file and are mounted. For that reason, this section describes what you might expect to find in that file. It also describes how you can mount other partitions so that they become part of your Linux filesystem.
The mount command is used not only to mount devices, but also to mount other kinds of filesystems on your Linux system. For example, mount can be used to mount directories (folders) over the network from NFS or Samba servers. It can also be used to mount filesystems from a new hard drive or USB flash drive that is not configured to automount.
To see filesystem types that are currently loaded in your kernel, type cat /proc/filesystems. The list that follows shows a sample of filesystem types that are supported in Linux, although they may not be in use at the moment or even available on the Linux distribution you are using.
To see the list of filesystems that come with the kernel you are using, type ls /lib/modules/kernelversion/kernel/fs/. The actual modules are stored in subdirectories of that directory. Mounting a filesystem of a supported type causes the filesystem module to be loaded, if it is not already loaded.
Type man fs to see descriptions of Linux filesystems.
A swap area is an area of the disk that is made available to Linux if the system runs out of memory (RAM). If your RAM is full and you try to start another application without a swap area, that application will fail. With a swap area, Linux can temporarily swap out data from RAM to the swap area and then get it back when needed. You take a performance hit, but it is better than having processes fail. To actually use a swap partition that you have created, you need to turn it on. To do that temporarily, you can use the swapon command. For example:
# free -m total used free shared buffers cached Mem: 3629 1834 1795 0 226 701 -/+ buffers/cache: 905 2723 Swap: 4095 3995 100 # swapon /dev/sdc2 # free -m total used free shared buffers cached Mem: 3629 1834 1795 0 226 701 -/+ buffers/cache: 905 2723 Swap: 4595 4445 600
The free command shows the amount of swap before and after enabling the swap area with the swapon command. That amount of swap is available immediately and temporarily to your system. To make that swap area permanent, you need to add it to your /etc/fstab file. Here is an example:
/dev/sdc2 swap swap defaults 0 0
This entry indicates that the swap area on /dev/sdc2 should be enabled at boot time. Because there is no mount point for swap area, the second field is just set to swap, as is the partition type.
If at any point you want to disable a swap area, you can do so using the swapoff command. You might do this, in particular, if the swap area is no longer needed and you want to reclaim the space being consumed by a swap file or remove a USB drive that is providing a swap partition.
First, make sure that no space is being used on the swap device (using the free command), then use swapoff to turn off the swap area so you can reuse the space. Here is an example:
# free -m total used free shared buffers cached Mem: 3629 2433 1195 0 99 580 -/+ buffers/cache: 1754 1874 Swap: 4095 0 4095 # swapoff /dev/sdc2
The hard disk partitions on your local computer and the remote filesystems you use every day are probably set up to automatically mount when you boot Linux. The /etc/fstab file contains definitions for each partition, along with options describing how the partition is mounted. Here's an example of an /etc/fstab file:
# /etc/fstab /dev/mapper/vg_abc-lv_root / ext4 defaults 1 1 UUID=78bdae46-9389-438d-bfee-06dd934fae28 /boot ext4 defaults 1 2 /dev/mapper/vg_abc-lv_home /home ext4 defaults 1 2 /dev/mapper/vg_abc-lv_swap swap swap defaults 0 0 # Mount entries added later. /dev/sdb1 /win vfat ro 1 2 192.168.0.27:/nfsstuff /remote nfs users,_netdev 0 0 //192.168.0.28/myshare /share cifs guest,_netdev 0 0 # special Linux filesystems 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
The /etc/fstab file just shown is from a default Red Hat Enterprise Linux 6 server install, with a few lines added.
For now, you can ignore the tmpfs, devpts, sysfs, and proc entries. Those are special devices associated with shared memory, terminal windows, device information, and kernel parameters, respectively.
In general, the first column of /etc/fstab shows the device or share (what is mounted), while the second column shows the mount point (where it is mounted). That is followed by the type of filesystem, any mount options (or defaults), and two numbers (used to tell commands such as dump and fsck what to do with the filesystem).
The first three entries represent the disk partitions assigned to the root of the filesystem (/), the /boot directory, and the /home directory. All three are ext4 filesystems. The fourth line is a swap device (used to store data when RAM overflows). Notice that the device names for /, /home, and swap all start with /dev/mapper. That's because they are LVM logical volumes that are assigned space from a pool of space called an LVM group (more on LVM in the “Using Logical Volume Management Partitions” section later in this chapter).
The /boot partition is on its own physical partition, /dev/sda1. Instead of using /dev/sda1, however, a unique identifier (UUID) identifies the device. Why use a UUID instead of /dev/sda1 to identify the device? Say that that you plugged another disk into your computer and booted up. It probably won't happen, but it is possible that the new disk might be identified as /dev/sda, causing the system to look for the contents of /boot on the first partition of that disk.
To see all the UUIDs assigned to storage devices on your system, type the blkid command, as follows:
# blkid /dev/sda1: UUID="78bdae46-9389-438d-bfee-06dd934fae28" TYPE="ext4" /dev/sda2: UUID="wlvuIv-UiI2-pNND-f39j-oH0X-9too-AOII7R" TYPE="LVM2_member" /dev/mapper/vg_abc-lv_root: UUID="3e6f49a6-8fec-45e1-90a9-38431284b689" TYPE="ext4" /dev/mapper/vg_abc-lv_swap: UUID="77662950-2cc2-4bd9-a860-34669535619d" TYPE="swap" /dev/mapper/vg_abc-lv_home: UUID="7ffbcff3-36b9-4cbb-871d-091efb179790" TYPE="ext4" /dev/sdb1: SEC_TYPE="msdos" UUID="75E0-96AA" TYPE="vfat"
Any of the device names can be replaced by the UUID designation in the left column of an /etc/fstab entry.
I added the next three entries in /etc/fstab to illustrate some different kinds of entries. I connected a hard drive from an old Microsoft Windows system, and I had it mounted on the /win directory. I added the ro option so it would mount read-only.
The next two entries represent remote filesystems. On the /remote directory, the /nfsstuff directory is mounted read/write (rw) from the host at address 192.168.0.27 as an NFS share. On the /share directory, the Windows share named myshare is mounted from the host at 192.168.0.28. In both cases, I added the _netdev option, which tells Linux to wait for the network to come up before trying to mount the shares. (For more information on mounting CIFS and NFS shares, refer to Chapters 19, “Configure a Windows File Sharing (Samba) Server,” and 20, “Configuring an NFS File Server,” respectively.)
To help you understand the contents of the /etc/fstab file, here is what is in each field of that file:
If you want to find out more about mount options as well as other features of the /etc/fstab file, there are several man pages you can refer to, including man 5 nfs and man 8 mount.
Linux systems automatically run mount -a (mount all filesystems) each time you boot. For that reason, you generally use the mount command only for special situations. In particular, the average user or administrator uses mount in two ways:
Any user can type mount (with no options) to see what filesystems are currently mounted on the local Linux system. The following is an example of the mount command. It shows a single hard disk partition (/dev/sda1) containing the root (/) filesystem, and proc and devpts filesystem types mounted on /proc and /dev, respectively.
$ mount /dev/sda3 on / type ext4 (rw) /dev/sda2 on /boot type ext4 (rw) /dev/sda1 on /mnt/win type vfat (rw) /dev/proc on /proc type proc (rw) /dev/sys on /sys type sysfs (rw) /dev/devpts on /dev/pts type devpts (rw,gid=5,mode=620) /dev/shm on /dev/shm type tmpfs (rw) none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) /dev/cdrom on /media/MyOwnDVD type iso9660 (ro,nosuid,nodev)
Traditionally, the most common devices to mount by hand are removable media, such as DVDs or CDs. However, depending on the type of desktop you are using, CDs and DVDs may be mounted for you automatically when you insert them. (In some cases, applications are launched as well when media is inserted. For example, a CD music player or photo editor may be launched when your inserted medium has music or digital images on it.)
Occasionally, however, you may find it useful to mount a filesystem manually. For example, you want to look at the contents of an old hard disk, so you install it as a second disk on your computer. If the partitions on the disk did not automount, you could mount partitions from that disk manually. For example, to mount read-only a disk partition sdb1 that has an older ext3 filesystem, you could type this:
# mkdir /mnt/temp # mount -t ext3 -o ro /dev/sdb1 /mnt/tmp
Another reason to use the mount command is to remount a partition to change its mount options. Say that you want to remount /dev/sdb1 as read/write, but you do not want to unmount it (maybe someone is using it). You could use the remount option as follows:
# mount -t ext3 -o remount,rw /dev/sdb1
Another valuable way to use the mount command has to do with disk images. If you download a CD or floppy disk image from the Internet and you want to see what it contains, you can do so without burning it to CD or floppy. With the image on your hard disk, create a mount point and use the -o loop option to mount it locally. Here's an example:
# mkdir /mnt/mycdimage # mount -o loop whatever-i686-disc1.iso /mnt/mycdimage
In this example, the /mnt/mycdimage directory is created, and then the disk image file (whatever-i686-disc1.iso) residing in the current directory is mounted on it. You can now cd to that directory, view the contents of it, and copy or use any of its contents. This is useful for downloaded CD images from which you want to install software without having to burn the image to CD. You could also share that mount point over NFS, so you could install the software from another computer. When you are done, just type umount /mnt/mycdimage to unmount it.
Other options to mount are available only for specific filesystem types. See the mount manual page for those and other useful options.
When you are done using a temporary filesystem, or you want to unmount a permanent filesystem temporarily, use the umount command. This command detaches the filesystem from its mount point in your Linux filesystem. To use umount, you can give it either a directory name or a device name. For example:
# umount /mnt/test
This unmounts the device from the mount point /mnt/test. You can also unmount using the form
# umount /dev/sdb1
In general, it's better to use the directory name (/mnt/test) because the umount command will fail if the device is mounted in more than one location. (Device names all begin with /dev.)
If you get the message device is busy, the umount request has failed because either an application has a file open on the device or you have a shell open with a directory on the device as a current directory. Stop the processes or change to a directory outside the device you are trying to unmount for the umount request to succeed.
An alternative for unmounting a busy device is the -l option. With umount -l (a lazy unmount), the unmount happens as soon as the device is no longer busy. To unmount a remote NFS filesystem that's no longer available (for example, the server went down), you can use the umount -f option to forcibly unmount the NFS filesystem.
You can create a filesystem for any supported filesystem type on a disk or partition that you choose. You do so with the mkfs command. Although this is most useful for creating filesystems on hard-disk partitions, you can create filesystems on USB flash drives, floppy disks, or rewritable CDs as well.
Before you create a new filesystem, make sure of the following:
The following is an example of using mkfs to create a filesystem on the first (and only) partition on a 2GB USB flash drive located as the third SCSI disk (/dev/sdc1):
# mkfs -t ext3 /dev/sdc1 mke2fs 1.40.8 (13-Mar-2008) Warning: 256-byte inodes not usable on older systems Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) 122160 inodes, 487699 blocks 24384 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=503316480 15 block groups 32768 blocks per group, 32768 fragments per group 8144 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912 Writing inode tables: done Creating journal (8192 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 39 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
You can see the statistics that are output with the formatting done by the mkfs command. The number of inodes and blocks created are output, as are the number of blocks per group and fragments per group. An inode, which hold metadata such as ownership and timestamps for each file, will be consumed for every file and directory in the filesystem. So the number of inodes shown here limits the total number of files you can create in that filesystem.
You can now mount this filesystem (mkdir /mnt/myusb ; mount /dev/sdc1 /mnt/myusb), change to /mnt/usb as your current directory (cd /mnt/myusb), and create files on it as you please.
Managing filesystems is a critical part of administering a Linux system. Using commands such as fdisk, you can view and change disk partitions. Filesystems can be added to partitions using the mkfs command. Once created, filesystems can be mounted and unmounted using the mount and umount commands, respectively.
Logical Volume Management (LVM) offers a more powerful and flexible way of managing disk partitions. With LVM, you create pools of storage, called volumes, that can allow you to grow and shrink logical volumes, as well as extend the size of your volume groups by adding more physical volumes.
With most of the basics needed to become a system administrator covered at this point in the book, Chapter 13 introduces concepts for extending those skills to manage network servers. Topics in that chapter include information on how to install, manage, and secure servers.
Use these exercises to test your knowledge of creating disk partitions, logical volume management, and working with filesystems. You will need a USB flash drive that is at least 1GB that you can erase for these exercises.
These tasks assume you are running a Fedora or Red Hat Enterprise Linux system (although some tasks will work on other Linux systems as well). If you are stuck, solutions to the tasks are shown in Appendix B (although in Linux, there are often multiple ways to complete a task).