You are on page 1of 91

Part Workbook 3.

Linux
Filesystem Management

Table of Contents
1. Disk Recognition and Partitioning ...................................................................................... 5
Discussion ................................................................................................................ 5
Disk Partitions .................................................................................................. 5
How Linux Refers to Partitions .................................................................... 5
DOS Partitioning Oddities ........................................................................... 5
Partitioning with fdisk ........................................................................................ 7
Using fdisk to List Partitions ....................................................................... 7
Using fdisk to Edit a Partition Table ............................................................. 9
Viewing Partitions in /proc/partitions ................................................................... 14
Examples ............................................................................................................... 15
Example 1. Partitioning a New Drive ................................................................... 15
Online Exercises ...................................................................................................... 17
Specification ................................................................................................... 18
Deliverables .................................................................................................... 18
Questions ............................................................................................................... 18
2. Creating and Managing Filesystems .................................................................................. 22
Discussion .............................................................................................................. 22
The ext4 Filesystem ......................................................................................... 22
Filesystem Extents ................................................................................... 22
Creating ext4 Filesytems ........................................................................... 22
Filesystems ..................................................................................................... 22
Linux Filesystems ............................................................................................ 23
Creating Filesystems ........................................................................................ 24
Creating ext2 Filesystems: mke2fs ...................................................................... 24
Command Line Switches ........................................................................... 25
Filesystem Labels (-L) .............................................................................. 25
Block Size (-b) ........................................................................................ 25
Inodes (-N or -i) ...................................................................................... 26
Reserved Blocks ...................................................................................... 26
Examining Filesystems with dumpe2fs ................................................................ 26
Filesystem Features .......................................................................................... 28
needs_recovery ........................................................................................ 28
has_journal ............................................................................................. 29
The ext3 Journaling Filesystem .......................................................................... 29
Journaling Filesystems .............................................................................. 29
Creating ext3 Filesystems .......................................................................... 29
Examples ............................................................................................................... 30
Example 1. Creating an ext2 Filesystem ................................................................ 30
Example 2. Creating a vfat Filesystem .................................................................. 31
Example 3. Creating an ext4 Filesystem ................................................................ 31
Online Exercises ...................................................................................................... 32
Specification ................................................................................................... 32
Deliverables .................................................................................................... 32
Questions ............................................................................................................... 32
3. Mounting Filesystems .................................................................................................... 36
Discussion .............................................................................................................. 36
Mounting Filesystems ....................................................................................... 36
The root partition ..................................................................................... 36
"Partitioning Off" Parts of the Directory Tree ................................................ 37
Mounting Filesystems with the mount Command ................................................... 38
Listing All Mounted Devices ..................................................................... 39

rha130-6.1-1

Copyright 2011, Red Hat Inc.

Linux Filesystem Management

Shortcutting the Filesystem Type ................................................................ 39


Mount Options ........................................................................................ 39
Unmounting Filesystems with the umount Command .............................................. 40
Using fuser or lsof to Track Down Processes ................................................ 41
Remounting Partitions with mount -o remount ...................................................... 41
Examples ............................................................................................................... 41
Example 1. Creating Mount Points ....................................................................... 41
Online Exercises ...................................................................................................... 42
Specification ................................................................................................... 42
Deliverables .................................................................................................... 42
Questions ............................................................................................................... 43
4. The /etc/fstab File .......................................................................................................... 46
Discussion .............................................................................................................. 46
Specifying Devices by Filesystem UUID .............................................................. 46
The /etc/fstab File Syntax .................................................................................. 47
Mount Options Relevant to the /etc/fstab File ........................................................ 48
Specifying Devices by Filesystem Label .............................................................. 48
Using Filesystem Labels on the Mount Command Line ........................................... 49
Adding Lines to the /etc/fstab File ...................................................................... 49
Examples ............................................................................................................... 49
Example 1. Defining Mount Points in the /etc/fstab File ........................................... 49
Online Exercises ...................................................................................................... 50
Specification ................................................................................................... 50
Deliverables .................................................................................................... 51
Questions ............................................................................................................... 51
5. Managing Swap Space ................................................................................................... 55
Discussion .............................................................................................................. 55
Swap Space .................................................................................................... 55
Monitoring Swap Partitions: /proc/swaps and /proc/meminfo .................................... 55
Initializing Swap Space with mkswap .................................................................. 56
Activating Swap Space with swapon ................................................................... 56
Deactivating Swap Areas with swapoff ................................................................ 56
Predefining Swap Areas in the /etc/fstab File ........................................................ 56
Examples ............................................................................................................... 57
Example 1. Initializing Swap Partitions ................................................................. 57
Example 2. Using Swap Files ............................................................................. 57
Online Exercises ...................................................................................................... 58
Specification ................................................................................................... 58
Deliverables .................................................................................................... 58
Questions ............................................................................................................... 58
6. Miscellaneous Filesystem Management Commands ............................................................. 61
Discussion .............................................................................................................. 61
The e2label Command ...................................................................................... 61
File System Repair with fsck ............................................................................. 61
Using fsck .............................................................................................. 62
The /lost+found Directory .......................................................................... 62
Automatic fscking on Startup ..................................................................... 62
The tune2fs Command ...................................................................................... 62
Converting ext2/ext3 filesystems to ext4 ...................................................... 63
The ext2 Mount Count ............................................................................. 63
Converting ext2 Filesystems to ext3 ............................................................ 63
Mounting ext3 filesystems as ext2 .............................................................. 63
Review: Adding a New Disk ............................................................................. 63
Examples ............................................................................................................... 64

rha130-6.1-1

Copyright 2011, Red Hat Inc.

Linux Filesystem Management

Example 1. Converting an ext2 filesystem to ext3 ................................................... 64


Online Exercises ...................................................................................................... 64
Specification ................................................................................................... 64
Deliverables .................................................................................................... 65
Questions ............................................................................................................... 65
7. Logical Volume Management .......................................................................................... 68
Discussion .............................................................................................................. 68
Logical Volume Management ............................................................................ 68
Physical Volumes .................................................................................... 68
Volume Groups ....................................................................................... 69
Logical Volumes ..................................................................................... 69
Dynamic Resizing .................................................................................... 70
Creating Physical Volumes, Volumes Groups and Logical Volumes .......................... 71
Resizing Logical Volumes ................................................................................. 72
Resizing Volume Groups .................................................................................. 73
Examining LVM Components ............................................................................ 74
Using system-config-lvm ................................................................................... 75
Creating Logical Volumes at Install Time ............................................................ 76
Online Exercises ...................................................................................................... 78
Specification ................................................................................................... 78
Create a Volume Group and 2 Logical Volumes ............................................ 78
Expand Your Volume Group by Adding a Physical Volume ............................. 80
Reduce Your Volume Group by 1 Physical Volume ....................................... 81
Deliverables .................................................................................................... 81
Questions ............................................................................................................... 82
8. Enabling Data Privacy with Partition Encryption ................................................................. 85
Discussion .............................................................................................................. 85
Disk Encryption ................................................................................................ 85
What is block device encryption? ................................................................ 85
Encrypting block devices using dm-crypt/LUKS ............................................. 85
Creating Encrypted Block Devices on the Installed System After Installation ........ 86
Online Exercises ...................................................................................................... 89
Specification ................................................................................................... 89
Deliverables .................................................................................................... 89
Questions ............................................................................................................... 89

rha130-6.1-1

Copyright 2011, Red Hat Inc.

Chapter 1. Disk Recognition and


Partitioning
Key Concepts
The fdisk utility is used to display and edit partition tables.
Disks may have up to 4 primary partitions.
One primary partition may be used a an extended partition.
The extended partition is then used as a container for holding multiple logical partitions.
Every partition has an associated label which identifies how the partition is intended to be used.

Discussion
The Linux Operating System treat disks as a large array of bytes, which can be divided and structured
to serve a variety of needs. This Workbook teaches the skills that allow administrators to configure and
structure disks to their needs.

Disk Partitions
Like most modern operating systems, Linux allows disks to be divided into multiple partitions, where
each partition is effectively treated as a separate disk. The process of creating partitions is referred to as
partitioning a disk.

How Linux Refers to Partitions


The Linux kernel refers to individual partitions through distinct device nodes, where the name of the node
is derived by appending the partition number to the name of the disk. For example, the first partition on
the drive /dev/sda is referred to as /dev/sda1, while the seventh partition on the drive /dev/sdc
would be referred to as /dev/sdc7.

DOS Partitioning Oddities


Different operating systems have established different conventions for recording a disk's partitioning
information. Because Linux has historically inter-operated with DOS based operating systems, the most
commonly encountered partitioning scheme is DOS partitioning. The DOS partitioning format was derived
in days when a 200 megabyte hard disk was considered large and the number of necessary partitions on a
disk was small, the implications of which will soon be discovered.

rha130-6.1-1

Copyright 2011, Red Hat Inc.

Disk Recognition and Partitioning

Figure 1.1. A Sample Partitioning of the Drive /dev/sda

The above figure diagrams a disk partitioned using DOS partitioning, including the features outlined below.

The Master Boot Record (MBR)


The first 1MB of every disk is reserved for the Master Boot Record, or MBR. (The size of the MBR in the
above figure is greatly out of proportion.) The MBR contains the following items.
Bootloader: On bootable disks, a low level executable referred to as a bootloader resides in the MBR.
When booting, BIOS passes control to the bootloader, which is then responsible for loading and passing
control to the appropriate operating system. The bootloader will be discussed in more detail in a later
Workbook.
Partition Table: On every disk, 64 bytes of the Master Boot Record is reserved for the disk's partition
table. This small amount of space records information for up to 4 partitions, referred to as primary
partitions. For each partition, a starting location, an ending location, and a partition type is recorded.

Primary Partitions
Each disk can be divided into up to four primary partitions, whose properties are recorded in the partition
table located in the MBR. Linux always uses partition numbers 1 through 4 to refer to primary partitions.
In the above diagram, the first primary partition, /dev/sda1, is a FAT filesystem for Windows operating
system. The second primary partition, /dev/sda2, is a swap partition which Linux uses to implement
virtual memory. The third partition, /dev/sda3, is an ext4 filesystem which contains a Linux /boot
partition.

The Extended Partition


With the three partitions mentioned above, we are already about to use up the four allowed primary
partitions. As a way to work around this limitation, an awkward structure was created, referred to as the
extended partition. DOS partitioning allows any one primary partition to be used as the extended partition.
The extended partition is used as a container for storing more partitions, referred to as logical partitions.
Once a primary partition is used as the extended partition, it may not be used for any other purpose.
An extended partition is optional. If four partitions serve the partitioning needs for a particular disk, then
no extended partition is required. In theory, any of the four primary partitions may be used as the extended
partition. In practice, the first three primary partitions are generally created as needed, and the remainder
of the disk is devoted to a fourth primary partition used as the extended partition. This strategy was
implemented in the figure above, where /dev/sda4 serves as the extended partition.

Logical Partitions
Within the extended partition, multiple logical partitions may be created. The partitioning information for
logical partitions is maintained as a linked list, so in theory, there is no limit to the number of logical

rha130-6.1-1

Copyright 2011, Red Hat Inc.

Disk Recognition and Partitioning

partitions which may be created. In practice, device drivers impose limitations. There may be no more
than 63 IDE partitions, and no more than 15 SCSI partitions. Linux always begins counting the first logical
partition as partition number 5, even if not all 4 primary partitions are used.
In the above figure, 4 logical partitions (/dev/sda5, /dev/sda6, /dev/sda7 and /dev/sda8)
contain ext4 filesystems for the /, /home, /var, and pub partitions, respectively.

Partitioning with fdisk


The most commonly used tool in Red Hat Enterprise Linux for partitioning disks is fdisk. The command
line utility is intentionally kept simple and unpretentious, so that it can be used in minimal environments
such as a rescue shell. As standard users do not have permission to read (and certainly not modify) disks
directly through their device node, the fdisk utility can only usefully be used as the root user.

Note
Red Hat Enterprise Linux 6 automatically aligns the first partition to start at sector 2048 instead
of sector 63 (the "traditional" start of cylinder 1). This is to ensure maximum performance on
new 4KiB sector hard drives as well as legacy 512 byte sector hard drives, and is compatible with
the behavior of other recent operating systems that use the MBR partitioning scheme. Partition
misalignment can lead to significant performance loss, so be careful adjusting these settings.

Using fdisk to List Partitions


Perhaps the simplest use of fdisk is to display known partitions. The -c option disables legacy DOScompatibility mode and -u displays output in sectors (not cylinders, which are obsolete). With the -l
command line switch, fdisk will list partition tables for all known disks. In the following example, fdisk
-l generates a partition table of the two attached hard disks on the system, /dev/sda and /dev/sdb.
The output of this single command has been broken into two listings, in order to allow us to focus on
each disk individually.
[root@station root]# fdisk -cul
Disk /dev/sda: 128.8 GB, 128849018880 bytes
255 heads, 63 sectors/track, 15665 cylinders, total 251658240 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: 0x000a8f61
Device Boot
/dev/sda1
/dev/sda2
*
/dev/sda3

Start
2048
12290048
54233088

End
12290047
54233087
68569087

Blocks
Id System
6144000
b Win95 FAT32
20971520
7 HPFS/NTFS
7168000
83 Linux

We now attempt to make sense of the reported structure of the first disk.
The first disk is about 128 gigabytes in size.
This line displays the drive's geometry, or how the drive is internally structured.
Here we find the actual partition table. This disk has only three partitions, each as a primary partition.
(How do we know this? For starters, the partition numbers are less then or equal to 4). Each line
gives the device name, the starting and ending cylinder, the size of the partition, and the partition
type. We will further discuss interpreting these lines below.
The last partition ends on sector 68569087, while we know from the line listing the disk geometry
(labeled "2" above) that the disk has 251658240 sectors. We also know that each sector is 512 bytes.
We can estimate that the disk has about (251658240 - 68569087 sectors) / 2 = 91544576 KiB or
about 90 gigabytes of space left unallocated.

rha130-6.1-1

Copyright 2011, Red Hat Inc.

Disk Recognition and Partitioning

Next, we focus on the second disk, /dev/sdb.


Disk /dev/sda: 85.9 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders, total 167772160 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: 0x00023bd9
Device Boot
/dev/sda1
*
/dev/sda2
/dev/sda3
/dev/sda4
/dev/sda5
/dev/sda6
/dev/sda7

Start
2048
309248
16793600
17317888
17319936
80236544
96622592

End
309247
16793599
17317887
167772159
80234495
96620543
167772159

Blocks
153600
8242176
262144
75227136
31457280
8192000
35574784

Id
83
83
82
5
83
83
83

System
Linux
Linux
Linux swap / Solaris
Extended
Linux
Linux
Linux

We now perform a similar analysis on the second disk.


The second disk is 85 gigabytes in size.
Now that we know that fdisk does the work for us, we only focus on the number of sectors, in this
case ...
... where each cylinder is about 512 bytes in size.
This disk has allocated all four primary partitions.
The fourth primary partition is being used as the extended partition, ending on the disk's last sector
(167772159).
The remaining partitions are logical partitions. Notice that their sector boundaries fall inside
the bounds of the extended partition (i.e., between sector numbers 17317888 and 167772159
inclusively).
The end of the last logical partition (sector 167772159) coincides with the end of the extended
partition, which coincides with the end of the disk. There is no more space left on the drive.

Focusing on a Single Line of fdisk's Partition Table


In order to discuss a single line in detail, we list again the partition table for the disk /dev/sda.
Disk /dev/sda: 128.8 GB, 128849018880 bytes
255 heads, 63 sectors/track, 15665 cylinders, total 251658240 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: 0x000a8f61
Device Boot
/dev/sda1
/dev/sda2
*
/dev/sda3

Start
2048
12290048
54233088

End
12290047
54233087
68569087

Blocks
6144000
20971520
7168000

Id System
b Win95 FAT32
7 HPFS/NTFS
83 Linux

Each line of the partition contains the following seven columns.

Table 1.1. Column Definitions for fdisk


Column

Function

Device

The device node which references the partition, commonly used as the "name"
of the partition.

Boot

Identifies the "bootable" partition. On each disk, one of the primary partitions
may be labeled "bootable". Although not used by Linux (it is used by the

rha130-6.1-1

Copyright 2011, Red Hat Inc.

Disk Recognition and Partitioning

Column

Function
MS/DOS bootloader), fdisk does report and allow the user to set the bootable
partition.

Start and End

The starting and ending sector of the partition.

Blocks

The size of the partition, in "blocks" of 1024 bytes (i.e, 1k). Usually, this
is redundant (albeit convenient) information, as it can be calculated from
sector boundaries. For example, for the partition /dev/sda2, multiplying the
number of sector by the number of bytes per sector yields 20480040 exactly.
(3249-540 sector) * (512 bytes/sector) * (1 kilobyte / 1024 bytes) = 20480040
kilobytes
Sometimes, especially when examining disks partitioned by some utility other
than fdisk, sectors do not fall exactly on sector boundaries. For example, when
performing similar math on the first partition (/dev/sda1), the reported
number of bytes is exactly 32 kilobytes short. In such situations, fdisk flags
the reported number of blocks with an appended +.

Id

A 2 digit hexadecimal number representing the partition's type Id.

System

A text name for the partition type, as defined by the Id.

Partition Labels
As seen in the above table, the partition table includes a one byte identifier that is used to assign the
partition's "Id" (sometimes referred to as the partition's "type", or the partition's "label"). The Id is used to
identify the role for the partition: is it to be used as swap space, an ext3 filesystem, a FAT filesystem, part
of Linux Volume Management, or an extended partition?
Many different operating systems follow the same partition Id naming conventions. The following table
lists partition types which occur commonly within Linux, in approximate order of how commonly they
occur.

Table 1.2. Partition Id's Relevant to Linux


Id

Label

Role

83

Linux

A Linux ext2 or ext3 or ext4 filesystem

82

Linux Swap

A Linux swap (virtual memory) partition

8e

Linux LVM

A Linux logical volume management physical


volume

fd

Linux raid auto

A Linux software RAID partition

In Linux, partition Ids are often cosmetic. A partition labeled Linux may be initialized and used for swap
space, while a partition labeled Linux Swap may be used as an ext2 filesystem. Some Ids, however, are
significant to the Linux kernel (for instance, Linux raid auto), and many are significant to other operating
systems which might be sharing the same disk. Good practice suggests that each partition's Id should
reflect its intended use.

Using fdisk to Edit a Partition Table


Starting fdisk and Using the Command Menu
When using fdisk to edit a partition table, the fdisk command should be called with a single argument:
the device node of the disk to be edited.

rha130-6.1-1

Copyright 2011, Red Hat Inc.

Disk Recognition and Partitioning

[root@station root]# fdisk -cu /dev/sda


Command (m for help):

Next, fdisk waits patiently to be told what to do next.

Note
One of the most common mistakes by new users is to invoke fdisk with the name of a partition,
instead of a disk. For example, fdisk /dev/sda3 would cause fdisk to exit with an error.
Partitions do not have partition tables to edit; disks do.
In interactive (editing) mode, fdisk expects to be driven with single letter commands. The command m
displays a list of possible commands.
Command (m for help): m
Command action
a
toggle a bootable flag
b
edit bsd disklabel
c
toggle the dos compatibility flag
d
delete a partition
l
list known partition types
m
print this menu
n
add a new partition
o
create a new empty DOS partition table
p
print the partition table
q
quit without saving changes
s
create a new empty Sun disklabel
t
change a partition's system id
u
change display/entry units
v
verify the partition table
w
write table to disk and exit
x
extra functionality (experts only)

Fortunately, most users can survive using a small subset of these commands.

Table 1.3. Commonly Used fdisk Commands


Command

Function

list command (m)enu

(p)rint partition table

create (n)ew partition

(d)elete partition

change partition (t)ype (Id)

(q)uit without saving changes

(w)rite changes and exit

Often, a user's first action is orient themselves by using the p command to print the current partition table.
Command (m for help): p
Disk /dev/sda: 128.8 GB, 128849018880 bytes
255 heads, 63 sectors/track, 15665 cylinders, total 251658240 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: 0x000a8f61

rha130-6.1-1

10

Copyright 2011, Red Hat Inc.

Disk Recognition and Partitioning

Device Boot
/dev/sda1
/dev/sda2
*
/dev/sda3

Start
2048
12290048
54233088

End
12290047
54233087
68569087

Blocks
6144000
20971520
7168000

Id
b
7
83

System
Win95 FAT32
HPFS/NTFS
Linux

We see again the partition table for the disk /dev/sda, with three primary partitions and over 85
gigabytes of free space.

Adding a Partition
In our discussion, we assume that the administrator wants to create two new partitions. The first will be
used as a 512 megabyte swap space, and the second will be used as a 1024 megabyte ext4 filesystem. The
n command is used to create a new partition.
Command (m for help): n
Command action
e
extended
p
primary partition (1-4)

The next question depends on the state of the drive. If no extended partitions exist (our case), fdisk asks if
the new partition should be the extended partition, or a (normal) primary partition. If an extended partition
does exist, fdisk asks if the new partition should be logical or primary. If all primary partitions are used,
fdisk just doesn't ask.
Because three primary partitions already exist, and our administrator wants to create 2 new partitions, she
must first create an extended partition to hold them. She answers e. Next, fdisk prompts for the starting
sector number for the partition. Our administrator just presses RETURN, accepting the suggested default
68569088 (the first available sector).
Selected partition 4
First sector (68569088-251658239, default 68569088): RETURN

Next, fdisk asks for the ending sector number.


Using default value 68569088
Last sector, +sectors or +size{K,M,G} (68569088-251658239, default 251658239):

Four different formats can be used when specifying the ending sector.
Format

Interpretation

Use ending sector n.

+n

Use starting sector incremented by n sector.

+nM

Use starting sector incremented by n megabytes. (A trailing K implies kilobytes


and a trailing G implies gigabytes).

RETURN

Use the suggested default, which is chosen to make the resulting partition as
large as possible.

Because the extended partition is a primary partition, it will be the fourth (and thus last) primary partition
on the disk. Since any disk space not included in the extended partition will become inaccessible, there is
no reason not to use the suggested sector (which is the last sector on the disk).
Our administrator does so, and then prints the new partition table.
Using default value 251658239
Command (m for help): p

rha130-6.1-1

11

Copyright 2011, Red Hat Inc.

Disk Recognition and Partitioning

Disk /dev/sda: 128.8 GB, 128849018880 bytes


255 heads, 63 sectors/track, 15665 cylinders, total 251658240 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: 0x000a8f61
Device Boot
/dev/sda1
/dev/sda2
*
/dev/sda3
/dev/sda4

Start
2048
12290048
54233088
68569088

End
12290047
54233087
68569087
251658239

Blocks
6144000
20971520
7168000
91544576

Id
b
7
83
5

System
Win95 FAT32
HPFS/NTFS
Linux
Extended

Command (m for help):

The newly added extended partition, which spans the rest of the drive.
Happy with the results, she next proceeds to add a new 512 megabyte partition. Because all four primary
partitions are now used, fdisk does not prompt for the partition type. A logical partition is the only type
available.
Command (m for help): n
First sector (68571136-251658239, default 68571136): RETURN
Using default value 68571136
Last sector, +sectors or +size{K,M,G} (68571136-251658239, default 251658239): +512M
Command (m for help): p
Disk /dev/sda: 128.8 GB, 128849018880 bytes
255 heads, 63 sectors/track, 15665 cylinders, total 251658240 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: 0x000a8f61
Device Boot
/dev/sda1
/dev/sda2
*
/dev/sda3
/dev/sda4
/dev/sda5

Start
2048
12290048
54233088
68569088
68571136

End
12290047
54233087
68569087
251658239
69619711

Blocks
6144000
20971520
7168000
91544576
524288

Id
b
7
83
5
83

System
Win95 FAT32
HPFS/NTFS
Linux
Extended
Linux

Command (m for help):

By simply pressing RETURN, the default starting sector is accepted.


The syntax +512M specifies that the partition should be 512 megabytes in size (or as close as
possible).
The newly added logical partition.

Changing a Partition's Label


By default, when fdisk adds a partition, the partition Id is set to "Linux" (appropriate for a partition that
will contain an ext2 or ext3 or ext4 filesystem). Because she plans to use the partition for swap space, she
would like to set the partition Id appropriately. She uses the t command to set the partitions type, and
specifies the appropriate partition. Taking advantage of the ability to list all known labels, she locates the
appropriate "Linux swap" label, and its associated hex code, 82.
Command (m for help): t
Partition number (1-5): 5
Hex code (type L to list codes): L
0
1

rha130-6.1-1

Empty
FAT12

1c
1e

Hidden Win95 FA 70
Hidden Win95 FA 75

12

DiskSecure Mult bb
PC/IX
be

Boot Wizard hid


Solaris boot

Copyright 2011, Red Hat Inc.

Disk Recognition and Partitioning

2 XENIX root
3 XENIX usr
4 FAT16 <32M
5 Extended
6 FAT16
7 HPFS/NTFS
8 AIX
9 AIX bootable
a OS/2 Boot Manag
b Win95 FAT32
c Win95 FAT32 (LB
e Win95 FAT16 (LB
f Win95 Ext'd (LB
10 OPUS
11 Hidden FAT12
12 Compaq diagnost
14 Hidden FAT16 <3
16 Hidden FAT16
17 Hidden HPFS/NTF
18 AST SmartSleep
1b Hidden Win95 FA
Hex code (type L to
Changed system type

24 NEC DOS
80 Old Minix
39 Plan 9
81 Minix / old Lin
3c PartitionMagic 82 Linux swap
40 Venix 80286
83 Linux
41 PPC PReP Boot
84 OS/2 hidden C:
42 SFS
85 Linux extended
4d QNX4.x
86 NTFS volume set
4e QNX4.x 2nd part 87 NTFS volume set
4f QNX4.x 3rd part 8e Linux LVM
50 OnTrack DM
93 Amoeba
51 OnTrack DM6 Aux 94 Amoeba BBT
52 CP/M
9f BSD/OS
53 OnTrack DM6 Aux a0 IBM Thinkpad hi
54 OnTrackDM6
a5 FreeBSD
55 EZ-Drive
a6 OpenBSD
56 Golden Bow
a7 NeXTSTEP
5c Priam Edisk
a8 Darwin UFS
61 SpeedStor
a9 NetBSD
63 GNU HURD or Sys ab Darwin boot
64 Novell Netware b7 BSDI fs
65 Novell Netware b8 BSDI swap
list codes): 82
of partition 5 to 82 (Linux swap)

c1
c4
c6
c7
da
db
de
df
e1
e3
e4
eb
ee
ef
f0
f1
f4
f2
fd
fe
ff

DRDOS/sec (FATDRDOS/sec (FATDRDOS/sec (FATSyrinx


Non-FS data
CP/M / CTOS / .
Dell Utility
BootIt
DOS access
DOS R/O
SpeedStor
BeOS fs
EFI GPT
EFI (FAT-12/16/
Linux/PA-RISC b
SpeedStor
SpeedStor
DOS secondary
Linux raid auto
LANstep
BBT

Command (m for help): p


Disk /dev/sda: 128.8 GB, 128849018880 bytes
255 heads, 63 sectors/track, 15665 cylinders, total 251658240 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: 0x000a8f61
Device Boot
/dev/sda1
/dev/sda2
*
/dev/sda3
/dev/sda4
/dev/sda5

Start
2048
12290048
54233088
68569088
68571136

End
12290047
54233087
68569087
251658239
69619711

Blocks
6144000
20971520
7168000
91544576
524288

Id
b
7
83
5
82

System
Win95 FAT32
HPFS/NTFS
Linux
Extended
Linux swap

Command (m for help):

The partition label, which was "Linux", has changed to "Linux swap".

Adding a Second Partition


The administrator now proceeds to add the second partition.
Command (m for help): n
First sector (69621760-251658239, default 69621760): RETURN
Using default value 69621760
Last sector, +sectors or +size{K,M,G} (69621760-251658239, default 251658239):
+1024M
Command (m for help): p
Disk /dev/sda: 128.8 GB, 128849018880 bytes
255 heads, 63 sectors/track, 15665 cylinders, total 251658240 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: 0x000a8f61
Device Boot
/dev/sda1
/dev/sda2
*
/dev/sda3

rha130-6.1-1

Start
2048
12290048
54233088

End
12290047
54233087
68569087

Blocks
6144000
20971520
7168000

13

Id
b
7
83

System
Win95 FAT32
HPFS/NTFS
Linux

Copyright 2011, Red Hat Inc.

Disk Recognition and Partitioning

/dev/sda4
/dev/sda5
/dev/sda6

68569088
68571136
69621760

251658239
69619711
71718911

91544576
524288
1048576

5
82
83

Extended
Linux
Linux

Command (m for help):

The newly added partition.

Deleting a Partition
Were the administrator to make a mistake, or needed to remove a partition to create more room, deleting
a partition is straightforward, and would look like the following.
Command (m for help): d
Partition number (1-6): 4

There are only a couple of details to note.


1. If an extended partition is deleted, all logical partitions that it contains are deleted as well.
2. Because the partitioning information for logical partitions is maintained as a linked list, they must be
numbered consecutively. If partition number 5 of 7 is deleted, partitions 6 and 7 will become partitions
5 and 6. (This is not the case for primary partitions.)

Exiting fdisk
When finished, there are two ways to exit fdisk. First, the q command can be used to "quit" fdisk,
abandoning any changes. The fdisk utility does not write any changes until specifically instructed to, so
users may safely quit at any time.
With the w command, fdisk will "write" changes and exit. This is the normal way to exit fdisk,
implementing any specified edits.
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

In some cases, specifically when you are modifying the disk which holds the root filesystem, fdisk will
warn on exit that the changes will not take effect until the next reboot. Rebooting will cause the change to
take affect, and in some cases, so will running the suggested kpartx.

Viewing Partitions in /proc/partitions


As an alternative to fdisk -l (which only root has permissions to perform), the proc filesystem file /proc/
partitions lists all known partitions, as well as sizes (in blocks of 1024 bytes) and activity statistics.
[root@station root]# cat /proc/partitions
major minor #blocks name
8
0 125829120 sda
8
1
6144000 sda1
8
2
20971520 sda2
8
3
7168000 sda3
8
16
83886080 sdb
8
17
153600 sdb1
8
18
8242176 sdb2

rha130-6.1-1

14

Copyright 2011, Red Hat Inc.

Disk Recognition and Partitioning

8
8
8
8
8

19
20
21
22
23

262144
1
31457280
8192000
35574784

sdb3
sdb4
sdb5
sdb6
sdb7

Notice the prominence of the major and minor number of each partition (and disk) in the output. The Linux
kernel internally identifies a partition by the major and minor number associated with its device driver,
not by the name of the device node, a fact that is often observed in kernel level error messages.

Examples
Partitioning a New Drive
In this example, we look over the shoulders of the user prince, who has just attached a new disk to his
machine. He would like to divide his new disk into the following partitions.
Number

Size

Purpose

128 megs

swap space

512 megs

ext2 filesystem

512 megs

vfat filesystem

1024 megs

ext4 filesystem

Upon first booting the machine, after the new drive has been connected, prince skims the kernel boot
messages for evidence of the newly detected drive. Unfortunately, the kernel is too fast for his monitor,
which does not power up to full strength until after the messages have past.
Once the system has booted, prince skims the /var/log/dmesg file, where he finds the following lines.
...
Uniform CD-ROM driver Revision: 3.20
sr 1:0:0:0: Attached scsi CD-ROM sr0
sd 0:0:0:0: [sda] 251658240 512-byte logical blocks: (128 GB/120 GiB)
sd 0:0:0:0: [sda] Write Protect is off
sd 0:0:0:0: [sda] Mode Sense: 00 3a 00 00
sd 0:0:0:0: [sda] Write cache: disabled, read cache: enabled, doesn't support DPO or FUA
sda:
sd 0:0:1:0: [sdb] 20971520 512-byte logical blocks: (10.7 GB/10.0 GiB)
sd 0:0:1:0: [sdb] Write Protect is off
sd 0:0:1:0: [sdb] Mode Sense: 00 3a 00 00
sd 0:0:1:0: [sdb] Write cache: disabled, read cache: enabled, doesn't support DPO or FUA
sdb: sda1 sda2 sda3
unknown partition table
sd 0:0:1:0: [sdb] Attached SCSI disk
sd 0:0:0:0: [sda] Attached SCSI disk
...

Convinced that his new drive is being detected correctly, he begins partitioning the drive with the fdisk
command. He first prints the partition table with the p command.
[root@station root]# fdisk -cu /dev/sdb
Command (m for help): p
Disk /dev/sdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders, total 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes

rha130-6.1-1

15

Copyright 2011, Red Hat Inc.

Disk Recognition and Partitioning

I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk identifier: 0x2f2333e8
Device Boot

Start

End

Blocks

Id

System

Command (m for help):

As expected, the partition table is empty. As he proceeds to add his first partition, the only really interesting
interaction is when he specifies the partition's size, using +128M.
Command (m for help): n
Command action
e
extended
p
primary partition (1-4)
p
Partition number (1-4): 1
First sector (2048-20971519, default 2048): RETURN
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +128M
Command (m for help): p
Disk /dev/sdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders, total 20971520 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: 0x2f2333e8
Device Boot
/dev/sdb1

Start
2048

End
264191

Blocks
131072

Id
83

System
Linux

Because the partition is going to be used as swap space, prince changes the partition's label to the
appropriate code for Linux Swap.
Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 82
Changed system type of partition 1 to 82 (Linux swap)

The next two partitions are then added in a straightforward manner.


Command (m for help): n
Command action
e
extended
p
primary partition (1-4)
p
Partition number (1-4): 2
First sector (264192-20971519, default 264192): RETURN
Using default value 264192
Last sector, +sectors or +size{K,M,G} (264192-20971519, default 20971519): +512M
Command (m for help): n
Command action
e
extended
p
primary partition (1-4)
p
Partition number (1-4): 3
First sector (1312768-20971519, default 1312768): RETURN
Using default value 1312768
Last sector, +sectors or +size{K,M,G} (1312768-20971519, default 20971519): +512M
Command (m for help): p
Disk /dev/sdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders, total 20971520 sectors

rha130-6.1-1

16

Copyright 2011, Red Hat Inc.

Disk Recognition and Partitioning

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: 0x2f2333e8
Device Boot
/dev/sdb1
/dev/sdb2
/dev/sdb3

Start
2048
264192
1312768

End
264191
1312767
2361343

Blocks
131072
524288
524288

Id
82
83
83

System
Linux swap / Solaris
Linux
Linux

Although prince only wants to add one more partition now, he will leave unallocated space on the drive. If
he were to use his last primary partition for the filesystem, he would no longer be able to make an extended
partition, and the remaining space would be wasted. Instead, prince creates an extended partition to span
the rest of the drive, and then adds his new partition as a logical partition within it.
Command (m for help): n
Command action
e
extended
p
primary partition (1-4)
e
Selected partition 4
First sector (2361344-20971519, default 2361344): RETURN
Using default value 2361344
Last sector, +sectors or +size{K,M,G} (2361344-20971519, default 20971519): RETURN
Using default value 20971519
Command (m for help): n
First sector (2363392-20971519, default 2363392): RETURN
Using default value 2363392
Last sector, +sectors or +size{K,M,G} (2363392-20971519, default 20971519): +1024M
Command (m for help): p
Disk /dev/sdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders, total 20971520 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: 0x2f2333e8
Device Boot
/dev/sdb1
/dev/sdb2
/dev/sdb3
/dev/sdb4
/dev/sdb5

Start
2048
264192
1312768
2361344
2363392

End
264191
1312767
2361343
20971519
4460543

Blocks
131072
524288
524288
9305088
1048576

Id
82
83
83
5
83

System
Linux swap / Solaris
Linux
Linux
Extended
Linux

Command (m for help):

Happy with his work, he writes and exits.


Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.

Online Exercises
Lab Exercise
Objective: Use fdisk to add new partitions.

rha130-6.1-1

17

Copyright 2011, Red Hat Inc.

Disk Recognition and Partitioning

Estimated Time: 10 mins.

Specification
In this lab, you will add two small partitions to a disk on your computer.
1. Use the fdisk -l command to confirm that your machine has at least 256 megabytes of unallocated
space. If your machine has multiple hard drives, the space must exist all on one drive. Create the file ~/
lab3.1/disk, which contains the name of the disk you will use for this lab, as an absolute reference.
For example, if the disk you will use is /dev/sda, the file should contain the single word /dev/sda.
2. Use the fdisk command to edit the drive's partition table. Note that you will have to use your root
account. Add two new partitions, each 128 megabytes in size (or nearly so). Set the partition Id of the
first newly added partition to Linux Swap. The partition Id of the second newly added partition should
remain Linux. The two new partitions should be the last two partitions on the drive. Exit fdisk, saving
changes.
3. If necessary, reboot your machine in order for the new partitions to be recognized by the kernel before
grading your lab.

Deliverables
1.
1. The file ~/lab3.1/disk, which contains the name of the drive you used for this lab, as an
absolute reference. The drive name should be the only word in the file.
2. The next to the last partition on the specified drive should be (nearly) 128 megabytes in size,
and have a partition Id of Linux Swap.
3. The last partition on the specified drive should be (nearly) 128 megabytes in size, and have a
partition Id of Linux.

Questions
Use the output from the following command to answer the next question.
[root@station root]$ cat /proc/partitions
major minor #blocks name
ng use aveq
8
8
8
8
8
8

0
1
2
3
4
5

1.

rha130-6.1-1

8891650
136521
3502170
2096482
1
2096451

sda
sda1
sda2
sda3
sda4
sda5

Which of the following best explains why the partition sda4 is listed in /proc/partitions
with only a size of 1?
a.

The partition has been initialized with a filesystem, but the filesystem is not mounted.

b.

The partition is being used as the extended partition, and therefore is not accessed directly.

c.

The partition has been initialized as swap space, but there has not yet been a high demand
placed on physical memory.

18

Copyright 2011, Red Hat Inc.

Disk Recognition and Partitioning

d.

The disk has not yet had a partition sda4 allocated.

e.

None of the above

2.

Which of the following partitions could be an extended partition?


a.

/dev/sda7

b.

/dev/sdb5

c.

/dev/sdb4

d.

/dev/sdc8

e.

None of the above

3.

Which of the following would be a logical partition?


a.

/dev/sda3

b.

/dev/sda5

c.

/dev/sdb1

d.

/dev/sdb4

e.

None of the above

4.

Which of the following commands would list all known partitions?


a.

fdisk --dump

b.

dumppart

c.

fdisk -cul

d.

fdisk --list

e.

None of the above

5.

Which of the following commands could be used to add the partition /dev/sda8?
a.

fdisk -a /dev/sda8

b.

fdisk -cu /dev/sda8

c.

fdisk -l /dev/sda8

d.

fdisk --add /dev/sda8

e.

None of the above

Use the following transcript to answer the next 3 questions.


[root@station root]$ fdisk -cul /dev/sdb
Disk /dev/sdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders, total 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes

rha130-6.1-1

19

Copyright 2011, Red Hat Inc.

Disk Recognition and Partitioning

I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk identifier: 0x2f2333e8
Device Boot
/dev/sdb1
/dev/sdb2
/dev/sdb3
/dev/sdb4

6.

End
206847
16984063
18032639
20129791

Blocks
102400
8388608
524288
1048576

Id
83
83
82
83

System
Linux
Linux
Linux swap / Solaris
Linux

Assuming that the system Id's indicate the true use of the partitions, approximately how much
partition space is allocated to swap space?
a.

128 megabytes

b.

520 megabytes

c.

8.4 gigabytes

d.

5.5 megabytes

e.

Not enough information is provided

7.

Approximately how much space is left unallocated on the drive /dev/sdb?


a.

b.

420 megabytes

c.

8.4 gigabytes

d.

84 megabytes

e.

Note enough information is provided

8.

The administrator wants to use the space is left unallocated on the drive /dev/sdb. What options
are avaialble?
a.

A new ext4 filesystem can be created with the remaining space

b.

A new extended partition can be created with the remaining space

c.

One or more new logical partitions can be created with the remaining space

d.

The remaining space is not available for use

e.

Note enough information is provided

9.

rha130-6.1-1

Start
2048
206848
16984064
18032640

After editing the partition table with fdisk, what action must (usually) be taken for the kernel to
recognize the new partitions?
a.

The machine must be rebooted.

b.

The new partition should be added to /proc/partitions with a text editor.

c.

The partprobe command should be run.

d.

None of the above

e.

Either A or C

20

Copyright 2011, Red Hat Inc.

Disk Recognition and Partitioning

10.

rha130-6.1-1

Which of the following fdisk commands is used to abandon all changes?


a.

b.

c.

d.

e.

21

Copyright 2011, Red Hat Inc.

Chapter 2. Creating and Managing


Filesystems
Key Concepts
The mkfs command, and its filesystem specific friends, are used to create filesystems.
The most common filesystem in Linux is the ext2 filesystem, and its close cousins, the ext3 and ext4
filesystems.
The ext3 filesystem is exactly like the ext2 filesystem, with the added feature of journaling.
The ext4 filesystem uses extents and other new features to expand the capabilities and enhance
performance.
The dumpe2fs command can be used to examine ext2, ext3, and ext4 filesystems.

Discussion
The ext4 Filesystem
Filesystem Extents
The next generation ext filesystem, ext4, is the default filesystem for Red Hat Enterprise Linux 6. The
ext4 combines the stability of ext3 with significant scalability and performance enhancements. The ext4
filesystem supports files and filesystems up to 8 terabytes in size. It also supports an unlimited number
of sub-directories.
Ext4 uses extents (as opposed to the traditional block mapping scheme used by ext2 and ext3), which
improves performance when using large files and reduces metadata overhead for large files. In addition,
ext4 also labels unallocated block groups and inode table sections accordingly, which allows them to
be skipped during a file system check. This makes for quicker file system checks, which becomes more
beneficial as the file system grows in size.

Creating ext4 Filesytems


To create an ext4 filesystem, the type option can be used with either the mke2fs or the mkfs or simply
use the mkfs.ext4.
When looking at the dumpe2fs output for an ext4 filesystem, look for the additional filesystem features,
specifically extent, huge_file, and uninit_bg

Filesystems
At the lowest level, block device nodes allow disks and disk partitions to be accessed as if they were simply
an array of bytes (recall the Unix mantra, "everything is a file"). But when Linux users store information
on disks, they prefer to use the familiar constructs of files and directories. Somehow, this array of bytes
that is a disk partition must be made to act like the /home/elvis directory, and the files it contains.
The Linux kernel implements a virtual filesystem layer, which dictates that everything that exists in a
directory tree, such as regular files, directories, device nodes, and symbolic links, must have a uniform
structure which consists of the following elements.

rha130-6.1-1

22

Copyright 2011, Red Hat Inc.

Creating and Managing Filesystems

inodes

Inodes store all of the metadata associated with a file. A file's metadata is all of the
information associated with a file, except for its name and its content. For example, a
file's ownerships, permissions, and modify time are all stored in it's inode. More than
anything else, the inode provides the identity of the file.

dentries

Dentries, whose name is a shortening of the term "Directory Entries", contain a file's
name and location within the directory structure, and associate this identity with the
file's inode.

data

Lastly, every file has an array of bytes which constitute its content and are referenced
by the file's inode.

The intermediate structure which occurs on a disk or disk partition that defines a particular chunk of bytes
as containing dentries, and another chunk of bytes as containing the inodes which the dentries reference,
and another chunk of bytes as containing the data which the inodes reference, is referred to as a filesystem.
In other operating systems, the act of initializing the filesystem on a particular partition is referred to as
formatting the partition. In Linux (and Unix), the act is usually simply referred to as creating a filesystem.

Linux Filesystems
Filesystem design is a general problem in computer engineering, and much time and effort has
been devoted to designing filesystems which balance efficiency, robustness, capability, and simplicity
appropriately for a given situation. Because a filesystem must be closely coordinated with the operating
system that supports it, most operating systems only support one or two filesystems which are considered
"native" to that operating system.
In contrast, Linux supports a large number of filesystems, some of which are native to Linux, many of
which are shared with other operating systems. The following table contains a partial list of filesystems
supported by Linux.

Table 2.1. Filesystems Supported in Linux


Filesystem

Comments

ext2

The ext2 ("extended 2") filesystem has been the native filesystem of Linux
during its rise in popularity from the mid 1990's. The ext2 filesystem is the
model around which the virtual filesystem layer is designed, and natively
implements almost all of the features expected from a filesystem under Linux.

ext3

The ext3 ("extended 3") filesystem, introduced around the year 2000, is an
extension of the ext2 filesystem that supports journaling. In every other way,
the ext3 filesystem is identical to the ext2 filesystem, and what is said about
one generally applies to the other.

ext4

The ext4 ("extended 4") filesystem, introduced around The ext4 filesystem
uses extents and other features to combine the stability of ext3 with significant
scalability and performance enhancements. It is the default filesystem in Red
Hat Enterprise Linux 6.

msdos

The msdos filesystem is the FAT filesystem used by DOS and Windows.
It does not support many advanced features, such as file ownerships and
permissions, and file names consist of at most 8 characters with a 3 character
extension.

vfat

The vfat filesystem is an extension of the msdos filesystem that allows long
filenames.

nfs

The nfs "network filesystem" is used to share directories between Linux (and
Unix) machines. One machine exports part of its directory structure over the

rha130-6.1-1

23

Copyright 2011, Red Hat Inc.

Creating and Managing Filesystems

Filesystem

Comments
network, which another machine may import into its local directory structure
at the filesystem level.

smbfs

The smbfs filesystem is used to share directories between Linux (and Unix)
and Windows machines. Using smbfs, a Linux machine can incorporate an
exported Windows share into its local directory structure.

iso9660

The iso9660 filesystem is a read-only filesystem commonly used on compact


disks.

proc

The proc filesystem is a virtual filesystem that resides entirely within the kernel
(i.e., there is no hard disk associated with the filesystem).

reiserfs

The reiserfs is an alternate journaling filesystem native to Linux and supported


by Red Hat Enterprise Linux.

jfs

The jfs "journaling filesystem" is a mature journaling filesystem with many


enterprise features native to IBM machines. The jfs filesystem has been
recently open sourced and ported to Linux, and is supported by Red Hat
Enterprise Linux.

More could be added to the table, but hopefully the point has been made that Linux features support for
a wide variety of filesystems. The curious should start with the fs(5) man page for more information. We
will discuss the ext2/ext3/ext4 filesystem in some detail, and acquaint ourselves with a few others from
the table as well.

Creating Filesystems
Before a block device can be used to store files, it must be initialized with a filesystem. In Red Hat
Enterprise Linux, some variant of the mkfs command is used to create filesystems. These commands tend
to reside in the /sbin directory.
[root@station root]# ls /sbin/mkfs.*
/sbin/mkfs.cramfs /sbin/mkfs.ext3 /sbin/mkfs.ext4dev
/sbin/mkfs.ext2
/sbin/mkfs.ext4 /sbin/mkfs.msdos

/sbin/mkfs.vfat

The mkfs command is a front-end command that expects to be called with the -t command line switch,
whose argument is the filesystem type to create. The other listed commands, which start mkfs., are
filesystem specific variants for particular filesystems. For example, mkfs -t ext2 and mkfs.ext2
are equivalent commands.
Initializing a partition can be as easy as the following.
[root@station root]# mkfs.ext2 /dev/sda6

All versions of the mkfs commands expect some optional, filesystem specific command line switches,
and a required first argument which is the file (partition) to structure. An optional, seldom used second
argument may also be used, which specifies the size of the filesystem in blocks. The second argument is
seldom used because, by default, the filesystem will fill the entire specified partition or disk. While anyone
has permissions to execute the mkfs commands, in practice, only root has permissions to write to the block
device nodes that are being initialized.

Creating ext2 Filesystems: mke2fs


The most common filesystem encountered in Linux is the ext2 filesystem, or its close cousin the ext3
filesystem. Our discussion will focus on the ext2 filesystem. Near its conclusion, we will find that all that
is said about the ext2 filesystem applies directly to the ext3 and ext4 filesystems as well.

rha130-6.1-1

24

Copyright 2011, Red Hat Inc.

Creating and Managing Filesystems

Because the ext2 filesystem is so commonly used, yet another command synonymous with mkfs.ext2
exists, namely mke2fs. The commands mke2fs, mkfs.ext, and mkfs -t ext2 yield identical results.

Command Line Switches


The following table specifies some of the mke2fs command's more commonly used command line
switches.

Table 2.2. Command Line Switches for the mke2fs Command


Switch

Effect

-b blocksize

Specify the filesystem blocksize in bytes, where blocksize may be one of


1024, 2048, or 4096.

-c

Check for bad block while creating the filesystem.

-i density

Specify an inode density of density bytes per inode.

-L label

Set the volume label of the filesystem to label.

-m n

Set the reserved block percentage to n.

-N number

Directly specify the number of inodes.

-j

Create a filesystem journal (ext3)

-t type

Create a specific type of filesystem (ext4)

The command line switches introduce some filesystem concepts which are addressed below.

Filesystem Labels (-L)


Like most filesystems, an ext2 filesystem can be labeled with a text string identifier, which is at most 16
characters. Other operating systems often refer to this label as a volume name (as they refer to a partition
formatted with a filesystem as a volume). We will discover that Red Hat Enterprise Linux makes functional
use of filesystem labels.

Block Size (-b)


Above, we ambiguously mentioned organizing a filesystem around "chunks of bytes", as in "this chunk
of bytes is storing inodes, while this chunk of bytes is storing dentries." The chunks we refer to are more
appropriately called blocks, and when creating a filesystem, a block size has to be decided. The ext2
filesystem supports block sizes of 1024, 2048, and 4096 bytes ("1k", "2k", and "4k", respectively).
The block size establishes the granularity of the filesystem. When space on the filesystem is devoted to
storing the contents of a particular file, the space is allocated in fixed size blocks of the specified block
size. For example, if the block size were 1024 bytes, then storing a file with 5000 bytes of data would
require 5 blocks. The same file stored in a filesystem with a block size of 4096 would require 2 blocks.
Once a block has been assigned to a particular purpose, it cannot be used for anything else.
At first glance, a smaller block size seems to result in less wasted space. In the above example, the file
system with a block size of 1k wastes about 1020 bytes (the remainder of the fifth block), while the
filesystem with a blocksize of 4k wastes 4092 bytes (the remainder of the second block). However, a
smaller blocksize requires more overhead on the part of the filesystem. For example, the filesystem needs
to maintain an internal table of which blocks have been used, and for what purpose. Again referring to the
above example, the filesystem with a 1k blocksize has to keep track of 5 blocks, while the filesystem with
a 4k blocksize only has to keep track of 2.

rha130-6.1-1

25

Copyright 2011, Red Hat Inc.

Creating and Managing Filesystems

As a general rule, the larger the filesystem, the larger the appropriate block size. By default, the mke2fs
chooses a block size of either 1k or 4k, depending on the size of the filesystem being created.

Inodes (-N or -i)


Every file is directly associated with a structure called an inode. An inode must exist for each file in the
filesystem. The number of inodes that a filesystem contains needs to be specified as the filesystem is
created. Because the number of inodes cannot be changed later, care must be taken to estimate the number
of required inodes conservatively.
Usually, the mke2fs command chooses appropriate default settings. In a couple of situations, however,
the number of inodes may need to be specified directly.
When a filesystem is expected to contain a large number of small files, the number of inodes may need
to be increased, or the filesystem may run out.
When the filesystem is expected to contain a small number of large files, the excess amount of space
devoted to unused inodes takes away from space that could potentially be used for storing data. As a
result, the number of inodes may want to be decreased.
There are two ways to specify the number of inodes. The -N command line switch can be used to specify
an absolute number of inodes. For situations where a statement such as "This filesystem will never contain
more than 18,112 files." can be made, this switch is appropriate. In contrast, the -i command line switch
is used to specify an inode density, in bytes per inode. When constructing the filesystem, the mke2fs
command will divide the total size of the filesystem by the density (or, more exactly, the inverse-density)
to determine the number of inodes. For situations where statements such as "I expect my average file size
to be 180 kilobytes" can be made, this is the appropriate switch.

Reserved Blocks
By default, the ext2 filesystem reserves 5% of its space for the root user. This feature attempts to provide
the administrator a little bit of wiggle room if a user on the system accidentally (or intentionally) fills the
entire filesystem. The -m command line switch allows the percentage of blocks to reserve to be specified.

Examining Filesystems with dumpe2fs


As the name implies, the dumpe2fs command dumps to standard out low level information about an ext2
filesystem. In the following example, the mke2fs command is used to create a filesystem on the partition
sda1 with default options.
[root@station root]# mke2fs /dev/sdb2
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
32768 inodes, 131072 blocks
6553 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=134217728
4 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304

rha130-6.1-1

26

Copyright 2011, Red Hat Inc.

Creating and Managing Filesystems

Writing inode tables: done


Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 21 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.

As the filesystem is being created, the mke2fs command displays details about the new filesystem,
including the following.
The specified filesystem label (in this case, none).
The blocksize (chosen by mke2fs, if not specified on the command line). In this case, 4096 bytes.
The number of inodes and blocks in the filesystem.
After the filesystem has been created, the dumpe2fs command is used to examine its details. The dumpe2fs
command expects to be called with a single argument, the name of a file (disk or partition) that contains
an ext2 filesystem.
[root@station root]# dumpe2fs /dev/sda1
dumpe2fs 1.39 (29-May-2006)
Filesystem volume name:
/boot
Last mounted on:
<not available>
Filesystem UUID:
dcf63b6b-157f-4493-a257-5221a44c37b9
Filesystem magic number: 0xEF53
Filesystem revision #:
1 (dynamic)
Filesystem features:
filetype sparse_super
Default mount options:
user_xattr acl
Filesystem state:
clean
Errors behavior:
Continue
Filesystem OS type:
Linux
Inode count:
31744
Block count:
126960
Reserved block count:
6348
Free blocks:
122934
Free inodes:
31733
First block:
1
Block size:
1024
Fragment size:
1024
Blocks per group:
8192
Fragments per group:
8192
Inodes per group:
1984
Inode blocks per group:
248
Filesystem created:
Fri Sep 26 17:17:53 2003
Last mount time:
n/a
Last write time:
Fri Sep 26 17:17:54 2003
Mount count:
0
Maximum mount count:
39
Last checked:
Fri Sep 26 17:17:53 2003
Check interval:
15552000 (6 months)
Next check after:
Wed Mar 28 18:17:53 2004
Reserved blocks uid:
0 (user root)
Reserved blocks gid:
0 (group root)
First inode:
11
Inode size:
128

Group 0: (Blocks 1-8192)


Primary superblock at 1, Group descriptors at 2-2
Block bitmap at 3 (+2), Inode bitmap at 4 (+3)
Inode table at 5-252 (+4)
7927 free blocks, 1973 free inodes, 2 directories
Free blocks: 266-8192
Free inodes: 12-1984
Group 1: (Blocks 8193-16384)
Backup superblock at 8193, Group descriptors at 8194-8194

rha130-6.1-1

27

Copyright 2011, Red Hat Inc.

Creating and Managing Filesystems

...

The filesystem label. Unlike many versions of Linux, Red Hat Enterprise Linux makes functional use
of the volume label. For reasons we will see, Anaconda initializes volume labels with the intended
mount point of the filesystem. The label can later be changed with the e2label command.
The ext2 filesystem can have several features enabled, which would be listed here.
The Anaconda installer embeds the acl mount option into the filesystem superblock, so that Red
Hat Enterprise Linux filesystems (which were created at install time) support access control lists by
default. Access control lists will be covered in more detail in a later lesson.
The number of inodes in the filesystem.
The number of blocks in the filesystem. Dividing this value by the number of inodes (found on the
previous line), we find that by default, the mke2fs command included one inode for every four blocks.
The blocksize, in this case 1024 bytes ("1k"). Because the partition was relatively small (126960
blocks at "1k" each = about 127 megabytes), the mke2fs command chose the smaller block size.
Lastly, the mke2fs command generates an accounting of free blocks and other information for each
of several block groups. The role of block groups in the ext2 filesystem structure is beyond the scope
of this discussion.
More detailed information about the design of the ext2 filesystem can be found at http://
e2fsprogs.sourceforge.net.

Filesystem Features
We now use the dumpe2fs command to examine a currently mounted /dev/sda3 partition.
[root@station root]# dumpe2fs /dev/sda3
dumpe2fs 1.39 (29-May-2006)
Filesystem volume name:
/
Last mounted on:
<not available>
Filesystem UUID:
c6c6c0ec-6430-470d-b371-754503e49ff6
Filesystem magic number: 0xEF53
Filesystem revision #:
1 (dynamic)
Filesystem features:
has_journal filetype needs_recovery sparse_super
Default mount options:
(none)
Filesystem state:
clean
Errors behavior:
Continue
Filesystem OS type:
Linux
Inode count:
652800
Block count:
1303273
Reserved block count:
65163
Free blocks:
148316
Free inodes:
418518
First block:
0
Block size:
4096
Fragment size:
4096
...

In this case, the filesystem has been given a label of /.


The filesystem features. More on these below.
The blocksize of the filesystem is 4096 bytes.
In the above output, a couple of filesystem features merit attention.

needs_recovery
Recall from the previous Workbook that the kernel buffers all I/O activity associated with block devices.
While this provides a wonderful boost to performance, if the kernel were to shutdown for some reason
before committing any pending writes from its in memory cache, the internal information of the filesystem
(i.e., which blocks are being used for what purpose) could be left in an inconsistent state.

rha130-6.1-1

28

Copyright 2011, Red Hat Inc.

Creating and Managing Filesystems

When a filesystem is mounted (i.e., used. More on this in the next Lesson), its needs_recovery flag is
set. If the filesystem is unmounted through normal techniques, as when the system is shut down from the
command line, the kernel has an opportunity to flush its pending writes to disk, and the needs_recovery
flag is removed. If the filesystem is not cleanly unmounted, however, as when the system loses power or is
switched off without being shut down, the needs_recovery flag never gets cleared. When next used (upon
next boot, for example), the needs_recovery flag will be discovered, and maintenance will be performed
on the filesystem.

has_journal
The mke2fs command subtly mentions that this ext2 filesystem has an associated journal. Earlier, we
stated that the ext3 filesystem is just an ext2 filesystem with a journal. What are we left to conclude? We
are using the dumpe2fs command to examine what is really an ext3 filesystem! Again, the ext3 filesystem
is just an ext2 filesystem with the added "feature" of a journal.

The ext3 Journaling Filesystem


Journaling Filesystems
What is the importance of a journaling filesystem? A journaling filesystem maintains a log of all of its
pending transactions, through a process commonly referred to as a "two phase commit". When writing
information to disk, a journaling filesystem takes an approach similar to the following.
1. The filesystem writes to the journal the details about the transaction it is about to perform.
2. The filesystem performs the transaction.
3. Upon successful completion, the filesystem removes the details about the transaction from the journal.
What is gained by this extra work? Just as the captain's log is used tell rescuers what was happening on
the ship when disaster struck, the filesystem's journal tells filesystem repair utilities what the filesystem
was up to when a filesystem crash occurs.
When repairing an ext2 filesystem, which does not maintain a journal, the filesystem repair utility must
examine the entire filesystem, searching for any internal structuring information which is in an inconsistent
state. For even a moderately large filesystem (40 gigabytes, for example), this process can take several
minutes.
In contrast, when repairing an ext3 filesystem, the repair utility needs only to focus on the journal of
pending transactions. The repair utility can then reset the internal filesystem structures associated with
these transactions to a consistent state, and assume the rest of the filesystem is healthy. The presence of
a journal to guide the repair utility dramatically reduces the amount of time needed to begin reusing a
crashed filesystem.

Creating ext3 Filesystems


How is an ext3 filesystem created? Notice in the above table of commonly used command line switches for
the mke2fs command, the -j command line switch was included without discussion. The switch specifies
that the mke2fs utility should include a journal with the newly created ext2 filesystem. In other words,
make an ext3 filesystem.
As a convenience, the mkfs.ext3 command is also provided. The command is a synonym for mke2fs -j
(or mkfs.ext2 -j, or mkfs -t ext2 -j). It shares all command line switches in common with mke2fs.

rha130-6.1-1

29

Copyright 2011, Red Hat Inc.

Creating and Managing Filesystems

Examples
Creating an ext2 Filesystem
Continuing to configure his new disk, the user prince next proceeds to create his first filesystem, which he
wanted to be the ext2 filesystem. He makes the following decisions about the specifications.
He knows that he is going to be using the filesystem for storing images, so he decides to give the
filesystem the label pics.
As he is the only user that will routinely be using the partition, he decides not to waste space reserving
blocks for the root user, and sets the reserved block percentage to 0.
Because he expecting to store relatively few large files, he decides to force the blocksize to 4096 bytes.
To be safe, he has the mke2fs command check for bad blocks before creating the filesystem.
Composing the appropriate command line switches, he runs the the mke2fs command on the partition.
[root@station root]# mke2fs -L pics -m 0 -b 4096 -c /dev/sdb2
mke2fs 1.39 (29-May-2006)
Filesystem label=pics
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
126720 inodes, 126622 blocks
0 blocks (0.00%) reserved for the super user
First data block=0
4 block groups
32768 blocks per group, 32768 fragments per group
31680 inodes per group
Superblock backups stored on blocks:
32768, 98304
Checking for bad blocks (read-only test): done
Writing inode tables: done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 20 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.

In order to confirm that the filesystem was created "to spec", he turn right around and runs the dumpe2fs
command, whose output is excerpted below.
[root@station root]# dumpe2fs /dev/sdb2
dumpe2fs 1.39 (29-May-2006)
Filesystem volume name:
pics
Last mounted on:
<not available>
Filesystem UUID:
dbc2276d-3393-4f75-9e1b-e6d114e87e53
Filesystem magic number: 0xEF53
Filesystem revision #:
1 (dynamic)
Filesystem features:
filetype sparse_super
...
Inode count:
126720
Block count:
126622
Reserved block count:
0
...
Block size:
4096
...

All appears to be well.

rha130-6.1-1

30

Copyright 2011, Red Hat Inc.

Creating and Managing Filesystems

Creating a vfat Filesystem


The user prince's machine is a dual boot machine, with Red Hat Enterprise Linux and Windows XP. His
Windows partition is using the NTFS filesystem, which is not supported by Red Hat Enterprise Linux.
Because he would like to easily share files between the two operating systems, prince wants to make a
FAT partition that can be seen from both worlds.
Although he knows that the mkfs.vfat command has several options which can be used to specify FAT
filesystem details, he assumes that the default settings are appropriate, and quickly creates the partition.
[root@station root]# mkfs.vfat /dev/sdb3
mkfs.vfat 3.0.9 (31 Jan 2010)

Seeing no information returned from the command, an knowing no analogue to the dumpe2fs command
for the vfat filesystem, prince assumes all went well and moves along.

Creating an ext4 Filesystem


Lastly, prince is going to create an ext4 filesystem to hold all of his music which he has ripped from CD's,
into the ogg vorbis format. He decides to label the filesystem ogg, appropriately. He knows he could use
the mke2fs -t ext4 command, but because his fingers are in the habit of making typos, he decides that
command completion might be his friend and chooses the shorter command of mkfs.ext4.
[root@station root]# mkfs.ext4 -c -L ogg /dev/sdb5
mke2fs 1.41.12 (17-May-2010)
Filesystem label=ogg
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
65536 inodes, 262144 blocks
13107 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=268435456
8 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376
Checking for bad blocks (read-only test): done
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 22 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.

Again, he confirms the results with the dumpe2fs command.

[root@station root]# dumpe2fs /dev/sdb5


dumpe2fs 1.41.12 (17-May-2010)
Filesystem volume name:
ogg
Last mounted on:
<not available>
Filesystem UUID:
02438bad-425f-4666-be90-bb309ffb0455
Filesystem magic number: 0xEF53
Filesystem revision #:
1 (dynamic)
Filesystem features:
has_journal ext_attr resize_inode dir_index filetype extent flex_bg sparse_su
Filesystem flags:
signed_directory_hash
Default mount options:
(none)
...
Inode count:
65536

rha130-6.1-1

31

Copyright 2011, Red Hat Inc.

Creating and Managing Filesystems

Block count:
Reserved block count:
...
Block size:
Fragment size:
...
Journal inode:
Default directory hash:
Directory Hash Seed:
Journal backup:
Journal features:
Journal size:
Journal length:
Journal sequence:
Journal start:
...

262144
13107
4096
4096
8
half_md4
8f9f5f47-48c6-4806-951d-deefdc55fc5b
inode blocks
(none)
32M
8192
0x00000001
0

Seeing the filesystem features huge_file uninit_bg and extent as well as some signs that the journal was
created, prince is satisfied that the filesystem is an ext4 filesystem.

Online Exercises
Lab Exercise
Objective: Create a new ext2 partition.
Estimated Time: 10 mins.

Specification
This lab exercise will continue to use the partition created in the previous lab exercise. Ensure that the file
~/lab3.1/disk still exists, and that it still references the correct drive.
Create an ext2 filesystem on the last partition created in the previous exercise. The filesystem should meet
the following criteria.
1. The filesystem should have the label lab3.2.
2. The filesystem should have a blocksize of 2048 bytes.
3. The filesystem should have exactly 24,000 inodes (or as near to 24,000 as possible).

Deliverables
1.
1. The last partition on the drive specified in the file ~/lab3.1/disk should be formatted with
an ext2 filesystem, whose label is lab3.2, has a blocksize of 2048 bytes, and has (very close
to) 24,000 inodes.

Questions
1.

rha130-6.1-1

Which of the following filesystems is usually associated with compact disks?


a.

ext3

b.

iso9660

32

Copyright 2011, Red Hat Inc.

Creating and Managing Filesystems

c.

reiserfs

d.

vfat

e.

None of the above

2.

Which of the following commands could be used to create an ext3 filesystem?


a.

mkfs.ext3

b.

mkfs -t ext3

c.

mke2fs -j

d.

mkfs.ext2 -j

e.

All of the above

3.

Which of the following command lines could be used to create an ext2 filesystem with a blocksize
of 2048 bytes?
a.

mke2fs -b 2048 /dev/sda4

b.

mke2fs /dev/sda4

c.

mke2fs -b2k /dev/sda4

d.

mke2fs -b2 /dev/sda4

e.

A and B

Use the following transcript to answer the next 2 questions.


[root@station root]# dumpe2fs /dev/sdb5
dumpe2fs 1.39 (29-May-2006)
Filesystem volume name:
/home
Last mounted on:
<not available>
Filesystem UUID:
4a26ac4a-fcd9-4231-bf3a-e5e5fb2456b5
Filesystem magic number: 0xEF53
Filesystem revision #:
1 (dynamic)
Filesystem features:
has_journal filetype needs_recovery sparse_super
Default mount options:
(none)
Filesystem state:
clean
Errors behavior:
Continue
Filesystem OS type:
Linux
Inode count:
3842720
Block count:
7681070
Reserved block count:
384053
Free blocks:
1150544
Free inodes:
3597708
First block:
0
Block size:
4096
Fragment size:
4096
Blocks per group:
32768
Fragments per group:
32768
Inodes per group:
16352
Inode blocks per group:
511
Last mount time:
Sun Oct 5 13:59:31 2003
Last write time:
Sun Oct 5 13:59:31 2003
Mount count:
74
Maximum mount count:
-1
Last checked:
Fri Sep 19 09:16:20 2003
Check interval:
15552000 (6 months)

rha130-6.1-1

33

Copyright 2011, Red Hat Inc.

Creating and Managing Filesystems

Next check after:


Reserved blocks uid:
Reserved blocks gid:
First inode:
Inode size:
Journal UUID:
Journal inode:
Journal device:
First orphan inode:

4.

Which of the following command lines could have created this filesystem? (Assume that the
filesystem has not been modified with tune2fs since it was created.)
a.

mke2fs -L /home -b 2048 /dev/sdb5

b.

mkfs.ext3 -b 2048 /dev/sdb5

c.

mkfs -t ext2 -b 4096 /dev/sdb5

d.

mke2fs -j -L /home -b 4096 -N 3842720 /dev/sdb5

e.

None of the above

5.

When creating this filesystem, which of the following would be the appropriate argument to the -i
command line switch to the mke2fs command to generate the appropriate number of inodes?
a.

-i 2

b.

-i 4096

c.

-i 8192

d.

-i 4

e.

None of the above

6.

When formatting a floppy with the mke2fs command, why might one be tempted to include the
-m 0 command line switch?
a.

Because journaling would be too slow on a floppy, this command line switch would disable
journaling.

b.

The command line switch would prevent any blocks being reserved for only the root user.

c.

The command line switch would set the number of inodes to 0, which is appropriate for
temporary media such as floppies.

d.

The command line switch would cause the floppy to be overwritten with binary 0's before the
filesystem is constructed, wiping clean any previous data.

e.

None of the above

7.

rha130-6.1-1

Wed Mar 17 08:16:20 2004


0 (user root)
0 (group root)
11
128
<none>
8
0x0000
0

Which of the following command lines would create a filesystem with a volume label of data?
a.

mkfse2 -l data /dev/sda1

b.

mkfs.ext2 --label=data /dev/sda1

c.

mkfs.ext2 -m data /dev/sda1

34

Copyright 2011, Red Hat Inc.

Creating and Managing Filesystems

d.

mkfs -t ext2 -L data /dev/sda1

e.

None of the above

8.

Which of the following command lines would create an ext4 filesystem with a block size of 4096?
a.

mke2fs --journal --block=4096 /dev/sda1

b.

mkfs.ext4 -b 4k /dev/sda1

c.

mkfs -t ext2 -j -b4 /dev/sda1

d.

mkfs -t ext4 --block=4k /dev/sda1

e.

None of the above

9.

Which of the following command line switches causes the mke2fs command to check the disk for
bad blocks before creating a filesystem?
a.

-L

b.

-c

c.

-b

d.

-s

e.

None of the above

10.

rha130-6.1-1

Your friend has just run the dumpe2fs command on his /home filesystem, and noticed that the
flag needs_recovery was set. What advice would you give him?
a.

He should reboot his machine immediately.

b.

When convenient, he should unmount the partition, and perform filesystem maintenance.

c.

He should not be concerned, as the flag is always set on mounted filesystems.

d.

He should not be concerned, as the Linux kernel automatically runs a filesystem recovery
algorithm every 10 minutes.

e.

You do not believe him, and suspect he ran the dumpe2fs command on a partition that does
not contain an ext2 filesystem.

35

Copyright 2011, Red Hat Inc.

Chapter 3. Mounting Filesystems


Key Concepts
The mount command is used to attach the root directories of filesystems to already existing directories
in the directory tree.
The pre-existing directory is referred to as the mount point of the mount.
The umount command is used to detach filesystems (which are not busy) from the directory tree.
Various mount options can be used to qualify the capabilities of mounted filesystems.
The remount mount option can be used to associate new options with already existing mounts.

Discussion
Mounting Filesystems
In Linux (and Unix), filesystems are gracefully combined into a single directory tree through a concept
called mounting. Every filesystem provides a root directory which servers as the base of the filesystem.
When a filesystem is mounted, its root directory is bound to an already existing directory in the system's
directory tree, referred to as the mount point for the filesystem. Whenever the mount point is referenced,
the root directory of the mounted filesystem is presented instead.
As we elaborate on the concept of mounting, we will continue to use the SCSI drive introduced in our
partitioning discussion.

Figure 3.1. Partitioning for the Drive /dev/sda

The root partition


Because the mount point must be an already existing directory, the first mount is special, and performed by
the kernel directly at startup. This first filesystem is referred to as the root partition, and the root partition's
root directory becomes the root directory of the directory tree, "/". In the above example, the partition /
dev/sda5 is being used as the root partition.

rha130-6.1-1

36

Copyright 2011, Red Hat Inc.

Mounting Filesystems

Figure 3.2. The /dev/sda5 Root Partition

This filesystem contains a root directory and commonly recognized subdirectories such as /etc, /home,
and /boot. If desired, the root partition could serve as a machine's only filesystem. The directories
above would contain subdirectories, and those subdirectories would in turn contain subdirectories, so that
every file on the system is stored in this single filesystem. For simple workstations, this may well be the
appropriate design.

"Partitioning Off" Parts of the Directory Tree


In practice, Linux (and Unix) administrators often choose to compose their system's directory tree out of
multiple partitions. In the above example, the partition /dev/sda6 is referred to as the /home partition,
because the filesystem in intended to be mounted to the /home directory in the directory tree. In the
following diagram, we examine the contents of this filesystem.

Figure 3.3. The /dev/sda6 Filesystem

The filesystem has its own root directory, and subdirectories such as /blondie, /elvis, etc. When
mounted to the /home directory, the mount point /home becomes bound to the root directory of the /
dev/sda6 filesystem.

Figure 3.4. The /dev/sda6 Filesystem Mounted to /home

rha130-6.1-1

37

Copyright 2011, Red Hat Inc.

Mounting Filesystems

Once mounted, all subsequent references to the /home directory no longer see the contents of the /home
directory on the partition /dev/sda5, but instead the root directory of the partition /dev/sda6. To
the end user, the effect is seamless. When the user asks for a ls of /home he sees the directories elvis,
madonna, etc.
The process can be continued as often as desired. The following diagram reflects the fully assembled
directory tree for the IDE drive above, with the /dev/sda6 filesystem mounted to /home, the /dev/
sda7 filesystem mounted to /var, and the /dev/sda8 filesystem mounted to /var/ftp/pub. All
filenames are listed relative to the root directory of the filesystem which contains them.

Figure 3.5. The Fully Assembled Directory Tree

Often, the end user, who might ask for an md5sum of the file /var/ftp/pub/images/
bootdisk.img, does not realize that her absolute reference traverses three different partitions.

Note
In the diagram of the IDE drive, and in our discussion, the partition /dev/sda6 has been
referred to as the "/home" partition. The fact that the directory /home does not exist in the /
dev/sda6 filesystem should be emphasized. The filesystem is only referred to as the /home
partition because it is intended to be mounted on the /home directory. It could just as easily
be mounted to the /etc directory, resulting in a directory tree which contains the directories /
etc/blondie, /etc/elvis, etc. (This is possible, not desirable.)

Mounting Filesystems with the mount Command


Filesystems are mounted with the mount command. Generally, only root can mount filesystems. The
following example of the mount command illustrates the general syntax.

rha130-6.1-1

38

Copyright 2011, Red Hat Inc.

Mounting Filesystems

[root@station root]# mount -t ext2 -o noatime,noexec /dev/sdb1 /home

Each execution of the mount command must somehow resolve the following four questions.
1. What device is being mounted? The first argument specifies the name of a block device to be mounted.
2. What filesystem is the device formatted with? The filesystem can be specified with the -t command
line switch.
3. Where should the device be mounted? The second argument specifies the already existing directory
which should serve as the mount point for the filesystem.
4. Are there any options which should be associated with the mounted filesystem? Mount options are
generally specified with the -o command line switch. In the above example, the mounted filesystem
will not record file access times, and will not interpret any files as commands to be executed.

Listing All Mounted Devices


The mount command, when called without arguments, will list all mounted filesystems along with the
answers to the four questions above.
[root@station root]# mount
/dev/sda2 on / type ext4 (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 /boot type ext4 (rw)
/dev/sda6 on /home type ext4 (rw)
/dev/sda7 on /usr type ext4 (rw)
/dev/sda5 on /var type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
/dev/sdb1 on /home type ext2 (rw,noexec,noatime)

Shortcutting the Filesystem Type


If the filesystem type is not specified explicitly, the mount command will consult the file /etc/
filesystems for a list of candidate filesystems.
[root@station root]# cat /etc/filesystems
ext3
ext2
nodev proc
nodev devpts
iso9660
vfat
hfs
hfsplus

When mounting one of these filesystem types, the filesystem does not need to be specified manually.

Mount Options
The Linux kernel supports the following mount options, which by design are generic to any filesystem.
In practice, some filesystems silently ignore some of the options. Most mount options are implemented as
complementary choices, such as exec and noexec. Such binary options are coupled in the following table.

rha130-6.1-1

39

Copyright 2011, Red Hat Inc.

Mounting Filesystems

Table 3.1. Linux Mount Options


Option

Effect

ro, rw

The ro option specifies that the filesystem should be mounted read only. Internal
filesystem parameters (such as the mount count, or last mount time) are not modified
as well. The default is rw.

(a)sync

The sync option specifies that all transactions should occur synchronously, i.e.,
writes should bypass the kernel cache. While this option provides better integrity
(transactions are written to disk immediately), it can significantly degrade overall
system performance. The default is async. This option is currently only supported
by the ext2/ext3/ext4 and ufs filesystems.

(no)atime

The noatime option specifies that file access times should not be maintained.
When access times are recorded, every read effectively becomes a write (because
the inode's atime requires updating). If access times are not considered important,
system performance can be improved by ignoring them. Also useful on laptops, this
option can prevent the drive from "spinning up" unnecessarily. The default is atime.

(no)dev

The nodev option specifies that no files within the filesystem should be interpreted
as device nodes. The default is dev.

(no)exec

The noexec option specifies that no files within the filesystem should be interpreted
as executable commands. The default is exec.

(no)suid

The nosuid option specifies that the suid permission bit should not be honored on
any files within the filesystem. The suid bit allows scripts to elevate privileges upon
execution, and is commonly used to implement trap doors on compromised systems.
The default is suid.

In addition, most filesystems support mount options specific to that filesystem. Consult the mount(8) man
page for details.
In practice, the default options are generally appropriate, and the mount command is often called with
only two arguments, as in the following.
[root@station root]# mount /dev/sdb1 /home

Unmounting Filesystems with the umount Command


Filesystems are unmounted with the umount command. The umount command expects to be called with
a single argument, which may be either the device name or the mount point associated with the mount.
Either of the following two commands would attempt to unmount the /dev/sdb1 partition from the /
home directory.
[root@station root]# umount /home
[root@station root]# umount /dev/sdb1

While simple in concept, one complication often serves to frustrate attempts to unmount devices:
[root@station home]# umount /home
umount: /home: device is busy

The umount command will only unmount filesystems which are not "busy". A filesystem is considered
busy if any process has an open file in the filesystem, or any process has a directory within the filesystem as
its current working directory. Before the filesystem can be unmounted, the process which is busying it must
be tracked down and killed (or convinced to release the files and/or directories to which it is laying claim).

rha130-6.1-1

40

Copyright 2011, Red Hat Inc.

Mounting Filesystems

Using fuser or lsof to Track Down Processes


Fortunately, a command is designed with just this purpose in mind. The fuser command lists the process
ids of processes which are somehow using a file. More relevant for our purposes, if the fuser command
is called with the the -m command line switch, it will list all processes which are using files anywhere on
the filesystem which contains the specified file.
[root@station home]# fuser -m /home
/home:
1912c

In this case, process id 1912 has a current working directory within the specified filesystem. Observation
of the bash prompt implies that the id probably belongs to the shell itself. Changing directories "out of"
the /home partition, the device can be unmounted.
[root@station home]# cd
[root@station root]# umount /home/

The lsof command (for "list open files") provides an alternative to fuser. When invoked on a directory
serving as amount point, lsof lists every currently open file within the filesystem.
[root@station6 foo]# lsof /home
COMMAND
PID USER
FD
TYPE DEVICE SIZE NODE NAME
bash
1912 root cwd
DIR
3,5 1024 12049 /home
lsof
15612 root cwd
DIR
3,5 1024 12049 /home
lsof
15613 root cwd
DIR
3,5 1024 12049 /home

The lsof command is very versatile and is a good tool to have at your disposal, but at this point we merely
mention this one use.

Remounting Partitions with mount -o remount


The mount command can also be used to associate new mount options with already mounted partitions.
When remounting partitions, the mount command should be called with the remount option, combined
with whatever new options are to be associated with the filesystem. Consider the following example.
[root@station root]# date > /home/foo
[root@station root]# mount -o remount,ro /home
[root@station root]# date > /home/foo
-bash: /home/foo: Read-only file system
[root@station root]# mount -o remount,rw /home
[root@station root]# date > /home/foo

Examples
Creating Mount Points
Continuing his work from the previous example, prince decides to create the following mount points for
his 3 filesystems.
Partition

Size

Filesystem

Mountpoint

/dev/sdb2

512 megabytes

ext2

/picts

/dev/sdb3

512 megabytes

vfat

/dosd

/dev/sdb5

1024 megabytes

ext4

/ogg

His first step is to create the needed directories.

rha130-6.1-1

41

Copyright 2011, Red Hat Inc.

Mounting Filesystems

[root@station root]# mkdir /picts /dosd /ogg

Next, he mount his new partitions, one by one. Because he is using standard filesystem types, he does not
need to specify filesystem types to the mount command.
[root@station root]# mount /dev/sda6 /picts/
[root@station root]# mount /dev/sda7 /dosd
[root@station root]# mount /dev/sda8 /ogg
[root@station root]# mount
/dev/sda2 on / type ext4 (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 /boot type ext4 (rw)
/dev/sda6 on /home type ext4 (rw)
/dev/sda7 on /usr type ext4 (rw)
/dev/sda5 on /var type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
/dev/sdb2 on /picts type ext2 (rw)
/dev/sdb3 on /dosd type vfat (rw)
/dev/sdb5 on /ogg type ext4 (rw)

After performing the mount, price decides he would like to protect the vfat filesystem until he can confirm
that it looks good after rebooting into Windows XP. He decides to add the ro mount option.
[root@station root]# mount -o remount,ro /dosd
[root@station root]# mount
...
/dev/sdb2 on /picts type ext2 (rw)
/dev/sdb3 on /dosd type vfat (ro)
/dev/sdb5 on /ogg type ext4 (rw)

Online Exercises
Lab Exercise
Objective: Mount an ext2 filesystem to the specified directory, with appropriate mount options.
Estimated Time: 10 mins.

Specification
This lab exercise will continue to use the filesystem created in the previous lab exercise. Ensure that the
file ~/lab3.1/disk still exists, and that it still references the correct drive. The last partition of the
drive should have a properly formatted ext2 filesystem.
1. Create the directory /lab3, which will serve as a mount point for your partition.
2. Mount the last partition on the specified drive to the /lab3 directory, with the noexec and noatime
mount options.

Deliverables
1.
1. The last partition on the drive specified in the file ~/lab3.1/disk should be mounted to the
directory /lab3, with the noexec and noatime options.

rha130-6.1-1

42

Copyright 2011, Red Hat Inc.

Mounting Filesystems

Questions
1.

Which of the following could be a legitimate invocation of the mount command?


a.

mount -t /dev/sda2 /home

b.

mount /home /dev/sda2

c.

mount /dev/sda2 /home

d.

mount -o /home /dev/sda2

e.

None of the above

2.

Which of the following command lines would mount the device /dev/sda3, formatted with the
vfat filesystem, to the mount point /dosc?
a.

mount -o vfat -d /dev/sda3 /dosc

b.

mount -t vfat /dev/sda3 /dosc

c.

mount -t vfat -o /dosc /dev/sda3

d.

mount -o vfat /dosc /dev/sda3

e.

None of the above

3.

Which of the following is not a common Linux mount option?


a.

mtime

b.

noatime

c.

noexec

d.

ro

e.

All are common mount options

Use the following transcript to answer the next 3 questions.


[root@station root]# mount
/dev/sda3 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/sda1 on /boot type ext4 (rw)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
[root@station root]# mount /dev/sda7 /var
[root@station root]# mount /dev/sda5 /var/www/html

4.

rha130-6.1-1

Which partition contains the file /proc/meminfo?


a.

/dev/sda1

b.

/dev/sda7

c.

/dev/sda5

43

Copyright 2011, Red Hat Inc.

Mounting Filesystems

d.

/dev/sda3

e.

None of the above

5.

Which partition contains the mount point /var/www/html?


a.

/dev/sda3

b.

/dev/sda7

c.

/dev/sda1

d.

/dev/sda5

e.

None of the above

6.

When accessing the file /var/www/html/index.html, how many partitions does the kernel
involve?
a.

b.

c.

d.

e.

Not enough information is provided

Use the following transcript to answer the next 2 questions.


[root@station root]# mount /dev/fd0 /mnt/floppy
[root@station root]# cd /mnt/floppy
[root@station floppy]# cal > september.txt
[root@station floppy]# umount /mnt/floppy
umount: /mnt/floppy: device is busy

7.

Which is the most likely reason the /mnt/floppy device was considered busy?
a.

The kernel had not yet finished writing the file september.txt.

b.

Although the kernel had finished writing the file september.txt, the writes had not yet
been committed to disk.

c.

The filesystem on the /mnt/floppy device has become corrupted.

d.

The bash shell's current working directory is /mnt/floppy.

e.

None of the above

8.

rha130-6.1-1

Which of the following commands would resolve the problem, so that if it were immediately
followed by the same umount command listed above, the unmount would succeed?
a.

fuser -m /mnt/floppy

b.

fsck -m /mnt/floppy

c.

sync

d.

cd

44

Copyright 2011, Red Hat Inc.

Mounting Filesystems

e.
9.

None of the above


Which of the following command lines would add the noatime option to an already mounted /
home partition?

a.

mount -o noatime /dev/sda4 /home

b.

mount -a noatime /dev/sda4 /home

c.

mount -remount -o noatime /dev/sda4 /home

d.

mount -o remount,noatime /dev/sda4 /home

e.

None of the above

Use the following transcript to answer the next question.


[root@station root]# mount
/dev/sdb6 on / type ext4 (rw)
none on /proc type proc (rw)
usbdevfs on /proc/bus/usb type usbdevfs (rw)
/dev/sdb7 on /var/ftp/pub type ext4 (rw)
/dev/sdb5 on /home type ext4 (rw)
none on /dev/pts type devpts (rw,gid=5,mode=620)
none on /dev/shm type tmpfs (rw)

10.

rha130-6.1-1

Which of the following command lines could be used to unmount the /home partition?
a.

unmount /home

b.

umount -t ext3

c.

umount /dev/sdb5

d.

unmount -a

e.

None of the above

45

Copyright 2011, Red Hat Inc.

Chapter 4. The /etc/fstab File


Key Concepts
The /etc/fstab file can be used to predefine mount points.
When the mount command is called with only half of the information it needs, it consults the /etc/
fstab for the rest.
The mount -a command attempts to mount (almost) every predefined mount point listed in the /etc/
fstab file.
In the /etc/fstab file, devices may either be referenced by name, or by filesystem label.

Discussion
In the previous Lesson, the mount command was introduced requiring two arguments: the device to mount,
and the mount point to which to mount it. Often, however, shortcuts are taken with the mount command,
as in the following 3 examples.
[root@station root]# mount /mnt/floppy
[root@station root]# mount /dev/fd0
[root@station root]# mount -a

Whenever the mount command is called without enough information, it looks to a "filesystem table" to
provide more: the /etc/fstab file.

Specifying Devices by Filesystem UUID


Labels are applied by humans and are prone to human errors. If there are two devices with the same label,
then the system will use the first device it finds with that label. If drives are moved around, it is possible
to end up with two filesystems with the label of /boot and the first one discovered by the system will be
used by the mount command.
Devices can be also identified on the mount command line (and the /etc/fstab file) by their
"UUID" (Universal Unique Identifier). Beginning in Red Hat Enterprise Linux 6, this is the default method
used by Anaconda. Again, the only difference occurs in the first column of the /etc/fstab file.
[root@station root]# cat /etc/fstab
UUID=9b85eb65-e07d-46d7-a1ea-10cd0710a56e
UUID=33c69e98-638a-445f-9ae8-4bea832bdf78
UUID=057e192c-9c27-4688-afdd-df8223a4b72f
UUID=e695c59b-d224-4a33-b7d3-8b045f9b6e45
UUID=8ff614de-b864-4f5e-a755-26020582b571
UUID=e289ec71-7776-4eee-8f65-205f0b398ba6
tmpfs
/dev/shm
devpts
/dev/pts
sysfs
/sys
proc
/proc

/
/boot
/home
/usr
/var
swap

ext4
ext4
ext4
ext4
ext4
swap
tmpfs
devpts
sysfs
proc

defaults
defaults
defaults
defaults
defaults
defaults
defaults
gid=5,mode=620
defaults
defaults

1
1
1
1
1
0
0
0
0
0

1
2
2
2
2
0

0
0
0
0

The UUID is listed in the output of the dumpe2fs command. Another useful tool for discovering the UUID
of a filesystem is the blkid.
[root@station root]# blkid

rha130-6.1-1

46

Copyright 2011, Red Hat Inc.

The /etc/fstab File

/dev/sda1:
/dev/sda2:
/dev/sda3:
/dev/sda5:
/dev/sda6:
/dev/sda7:

UUID="33c69e98-638a-445f-9ae8-4bea832bdf78"
UUID="9b85eb65-e07d-46d7-a1ea-10cd0710a56e"
UUID="e289ec71-7776-4eee-8f65-205f0b398ba6"
UUID="8ff614de-b864-4f5e-a755-26020582b571"
UUID="057e192c-9c27-4688-afdd-df8223a4b72f"
UUID="e695c59b-d224-4a33-b7d3-8b045f9b6e45"

TYPE="ext4"
TYPE="ext4"
TYPE="swap"
TYPE="ext4"
TYPE="ext4"
TYPE="ext4"

The /etc/fstab File Syntax


The following /etc/fstab file might be found on the machine using the SCSI drive from our previous
lesson.
[root@station root]# cat /etc/fstab
/dev/sda5
/
/dev/sda6
/home
/dev/sda7
/var
/dev/sda8
/var/ftp/pub
/dev/sda3
/boot
tmpfs
/dev/shm
devpts
/dev/pts
sysfs
/sys
proc
/proc
/dev/sda2
swap

ext4
ext4
ext4
ext4
ext4
tmpfs
devpts
sysfs
proc
swap

defaults
defaults
defaults
defaults
defaults
defaults
gid=5,mode=620
defaults
defaults
defaults

1
1
1
1
1
0
0
0
0
0

1
2
2
2
2
0
0
0
0
0

Each column of the file is separated by some combination of white space (generally, spaces and/or tabs).
Each row defines a mount, with the first four column directly answering the four questions associated with
each mount posed in the previous lesson: what, where, with which filesystem, and how. The latter two
columns are more subtle, and are described in the table below.

Table 4.1. Format of the /etc/fstab File


Column Example

Role

/dev/sda6

Which device (disk, partition, or otherwise) to mount.

/home

The mount point associated with the mount.

ext4

The filesystem to expect to find on the device, or the keyword "auto",


which means the filesystem should be automatically detected.

defaults

A comma separated list of options to associate with the mount, as would


be specified with the -o command line switch to the mount command.

Used by the dump command to determine if this partition should be


archived when performing backups. The dump utility is seldom used.

The "fsck"ing order. On system startup, the fsck "file system check"
command is run on each identified filesystem. There are three relevant
values for this field: 1 (reserved for the root partition), 2 (for all partitions
to be checked except for the root partition), and 0 (for partitions which
should not be fscked on startup).

Whenever the mount command is called with just a mount point, or just a device, as its lone argument,
it looks for a matching line within the /etc/fstab file to supply the remaining information. Examples
of successful commands have been given above. Notice the reference to the /etc/fstab file when the
following unsuccessful mount command complains.
[root@station root]# mount /foo
mount: can't find /foo in /etc/fstab or /etc/mtab

The mount -a command specifies to mount "all". The command reads the /etc/fstab line by line, and
performs all appropriate mounts. The mount -a command is executed as part of the system startup.

rha130-6.1-1

47

Copyright 2011, Red Hat Inc.

The /etc/fstab File

Mount Options Relevant to the /etc/fstab File


The following options, while documented in the mount(8) man page, only make sense when used in the
/etc/fstab file.

Table 4.2. Mount Options Relevant to the /etc/fstab File


Option

Effect

defaults

Use default options: rw, suid, dev, exec, auto, nouser, and async. (The word defaults
has the same effect as specifying no options, but something must hold the place of
the fourth column in the /etc/fstab file.)

(no)auto

The noauto option specifies that the partition should not be mounted when executing
mount -a. This option is generally used on lines defining mount points for temporary
media, such as floppies, CD-ROM's, and USB drives.

user

By default, only root may perform mounts. The user option specifies that anyone
should be able to perform the predefined mount.

Specifying Devices by Filesystem Label


The above example of an /etc/fstab file could be found in most any version of Unix, and would also
work in Linux. The default /etc/fstab file in Red Hat Enterprise Linux, however, takes advantage of
an additional feature of the Linux mount command. In Red Hat Enterprise Linux, the same file would
probably look more like the following, where the only differences occur in the first column.
[root@station root]# cat /etc/fstab
LABEL=/
/
LABEL=/home
/home
LABEL=/var
/var
LABEL=pub
/var/ftp/pub
LABEL=/boot
/boot
tmpfs
/dev/shm
devpts
/dev/pts
sysfs
/sys
proc
/proc
LABEL=SWAP-sda2
swap

ext4
ext4
ext4
ext4
ext4
tmpfs
devpts
sysfs
proc
swap

defaults
defaults
defaults
defaults
defaults
defaults
gid=5,mode=620
defaults
defaults
defaults

1
1
1
1
1
0
0
0
0
0

1
2
2
2
2
0
0
0
0
0

When discussing the mke2fs command, we mentioned that Red Hat Enterprise Linux makes functional
use of a filesystem labels. This is it. Rather than specifying a device directly, the device can be specified
by filesystem label. Think of the second line as saying, "Examine all known filesystems, and mount the
one labeled "/home" to the /home directory.
By convention, Anaconda (the Red Hat Enterprise Linux Installer) labels filesystems by their intended
mount point. This way, the filesystem internally "knows" where it is intended to be mounted. As the fourth
line above implies, however, this is merely convention. Any label will suffice.
Why does Red Hat Enterprise Linux add this extra level of indirection, with its associated complexity?
Because device names can be fickle. There are two situations that can lead to devices shifting identities.
1. Deleting Logical Partitions: Recall that logical partitions are maintained as a linked list, and if logical
partition /dev/sda6 were to be deleted, logical partitions /dev/sda7 and /dev/sda8 would shift
to /dev/sda6 and /dev/sda7.
2. Adding (or Removing) SCSI Drives: When a SCSI drive is added or removed from the system, SCSI
drives can shift identities. What was /dev/sdb on a previous boot might become /dev/sdc.

rha130-6.1-1

48

Copyright 2011, Red Hat Inc.

The /etc/fstab File

In the latter case, a move as innocently intentioned as adding a SCSI disk to the system could throw
a traditional /etc/fstab file out of whack, potentially leaving the system unbootable. Identifying
filesystems by filesystem label rather than device names protects against such unfortunate events.

Using Filesystem Labels on the Mount Command Line


Just as filesystem labels can be used in the /etc/fstab file to specify a device, they can be used on the
mount command line directly, with the -L command line switch, as in the following example.
[root@station root]# dumpe2fs /dev/sda1 | head -1
dumpe2fs 1.39 (29-May-2006)
Filesystem volume name:
/boot
[root@station root]# mount -L /boot /boot

Adding Lines to the /etc/fstab File


When creating a new filesystem, there are several reasons why a line defining a mount point for the
filesystem might be added to the /etc/fstab file.
A /etc/fstab line defines a "place" for the filesystem. While the filesystem could be mounted at
any time to any directory, defining a default mount point has the effect of giving the files a home in
the directory tree.
The mount becomes part of system bootup process. Unless the noauto option is added, the mount will
occur automatically as the system boots.
Users other than root can perform the mount. By default, only root may perform a mount. If the user
or owner option is specified in the /etc/fstab file, however, this privilege may be bestowed to
other users.
In principle, adding a line to the /etc/fstab file is easy, and may be performed with any text editor.
Care must be taken, however, not to mangle the syntax of the file. Because of the mount -a command, the
/etc/fstab file plays a fundamental role in the system's bootup process. A mangled or mis-specified
/etc/fstab file can leave the system unbootable. When editing the /etc/fstab file, text editors (or
word processors) that implement "word wrapping" should be avoided like the plague.

Examples
Defining Mount Points in the /etc/fstab File
Because prince does not want to perform the three mounts for his three new filesystems manually every
time he reboots the machine, he decides to define the mounts in his /etc/fstab file. He first examine
his current file, looking for lines similar to what he desires.
[root@station root]# cat /etc/fstab
LABEL=/
/
LABEL=/var/ftp/pub
/var/ftp/pub
LABEL=/home
/home
LABEL=/boot
/boot
devpts
/dev/pts
proc
/proc
sysfs
/sys
tmpfs
/dev/shm
LABEL=SWAP-sda2
swap

ext4
ext4
ext4
ext4
devpts
proc
sysfs
tmpfs
swap

defaults
defaults
defaults
defaults
gid=5,mode=620
defaults
defaults
defaults
defaults

1
1
1
1
0
0
0
0
0

1
2
2
2
0
0
0
0
0

He decides that the line specifying the /boot partition looks as good as any. Before proceeding, he makes
a backup of the /etc/fstab file, just in case.

rha130-6.1-1

49

Copyright 2011, Red Hat Inc.

The /etc/fstab File

[root@station root]# cp /etc/fstab /etc/fstab.bak

Next, using a text editor, he duplicates his chosen line 3 times.


LABEL=/boot
LABEL=/boot
LABEL=/boot
LABEL=/boot

/boot
/boot
/boot
/boot

ext4
ext4
ext4
ext4

defaults
defaults
defaults
defaults

1
1
1
1

2
2
2
2

1
1
1
1

2
2
2
2

Next, he begins implementing his customizations, changing only what is necessary.


LABEL=/boot
LABEL=pics
/dev/sdb3
LABEL=/ogg

/boot
/picts
/dosd
/ogg

ext4
ext2
vfat
ext4

defaults
defaults
defaults
defaults

Because he did not give his vfat filesystem a volume label, he must refer to it by device name. He saves
the file and quits the editor.
Lastly, he tests out his configuration by first unmounting his partitions, and then simulating a system
startup by running the mount -a command.
[root@station root]# umount /picts
[root@station root]# umount /dosd
[root@station root]# umount /ogg
[root@station root]# mount -a
mount: special device LABEL=/ogg does not exist

Realizing his mistake, he corrects the /etc/fstab file (replacing LABEL=/ogg with LABEL=ogg),
and tries again.
[root@station root]# umount /picts
[root@station root]# umount /dosd
[root@station root]# umount /ogg
umount: /ogg: not mounted
[root@station root]# mount -a
[root@station root]# mount
...
/dev/sdb2 on /picts type ext2 (rw)
/dev/sdb3 on /dosd type vfat (rw)
/dev/sdb5 on /ogg type ext4 (rw)

He is now satisfied with the results.

Online Exercises
Lab Exercise
Objective: Create a /etc/fstab entry which correctly defines a mount point for your drive.
Estimated Time: 10 mins.

Specification
This exercise will continue to use the filesystem created and mounted in the previous 2 exercises. Ensure
that the file ~/lab3.1/disk still exists, and that it still references the correct drive. The last partition
of the drive should have a properly formatted ext2 filesystem.
1. Add a line to your /etc/fstab file which defines the mount performed in the last lab. To recap,
the last partition of the specified drive should be mounted to the directory /lab3, with the noatime
and noexec options.

rha130-6.1-1

50

Copyright 2011, Red Hat Inc.

The /etc/fstab File

2. The line should refer to your partition not by device name, but by filesystem label.
3. Add the appropriate customization so that your filesystem is not mounted by default on system boots.
4. Configure the line appropriately so that the filesystem would not be archived by the dump command.
5. Unmount the partition before grading your exercise.

Deliverables
1.
1. A properly configured /etc/fstab file which mounts the last partition on the specified drive
to the directory /lab3 with the noexec and noatime options. The partition should not be
mounted automatically on system boots, and should not be included in archives created by the
dump command. The partition should be referenced by filesystem label.
2. The partition should not be currently mounted.

Questions
Use the following transcript to answer the next 5 questions.
[root@station root]# cat /etc/fstab
LABEL=/
/
LABEL=/var/ftp/pub
/var/ftp/pub
LABEL=/home
/home
LABEL=/boot
/mnt/boot
/dev/sda5
/dos
devpts
/dev/pts
proc
/proc
sysfs
/sys
tmpfs
/dev/shm
LABEL=SWAP-sdb2
swap
/dev/fd0
/media/floppy

1.

defaults
1 1
defaults
1 2
defaults
1 2
defaults,noauto 1 2
defaults,noauto,user
gid=5,mode=620 0 0
defaults
0 0
defaults
0 0
defaults
0 0
defaults
0 0
user
0 0

0 2

Which of the following mount points would not be used by a mount -a command?
a.

/var/ftp/pub

b.

/dev/pts

c.

/home

d.

/dos

e.

All of the above mount points would be used.

2.

rha130-6.1-1

ext4
ext4
ext4
ext4
vfat
devpts
proc
sysfs
tmpfs
swap
auto

Which of the following command lines would attempt to mount the /dev/sda5 partition?
a.

mount /dev/sda5

b.

mount /dos

c.

mount /dev/sda5 /mnt/dos

d.

All of the above

e.

A and B only

51

Copyright 2011, Red Hat Inc.

The /etc/fstab File

3.

Which of the following command lines would attempt to mount a filesystem to the /mnt/boot
directory?
a.

mount -a

b.

mount /boot

c.

mount -L /boot

d.

A and C

e.

None of the above

4.

What is the most likely reason that the third column of the last line contains the word auto?
a.

Because a FAT or ext2 or otherwise formatted floppy could be inserted into the drive, the
filesystem type should be probed for each mount.

b.

When a floppy is inserted into the drive, the filesystem should be automatically mounted.

c.

Upon bootup, the system should attempt to mount the floppy automatically.

d.

The auto filesystem is the most common filesystem used on floppy drives.

e.

None of the above adequately explain the configuration.

5.

What users would be able to mount the device /dev/fd0?


a.

Only root may perform mounts.

b.

Any user logged in physically at the console (and root).

c.

Any user on the system.

d.

The current owner of the directory /mnt/floppy (and root).

e.

None of the above.

6.

What does the noauto mount option imply?


a.

The filesystem should not be managed by an automounting daemon.

b.

The filesystem should not be automatically mounted whenever a file within the filsystem is
references.

c.

The filesystem should not be mounted as part of the system startup process.

d.

The line should not be automatically removed from the configuration file if the underlying
partition is deleted.

e.

None of the above

7.

rha130-6.1-1

What command could be used to create the label referred to by the LABEL= syntax in the /etc/
fstab file?
a.

mke2fs

b.

fdisk

c.

mount

52

Copyright 2011, Red Hat Inc.

The /etc/fstab File

d.

fuser

e.

None of the above

Use the following transcript to answer the next 3 questions.


[root@station root]$ cat /etc/fstab
/dev/sda6
/
/dev/sda2
/boot
devpts
/dev/pts
proc
/proc
sysfs
/sys
tmpfs
/dev/shm
/dev/sda1
tmp
/dev/sda3
/var
/dev/sda4
/var/data

8.

defaults
defaults
gid=5,mode=620
defaults
defaults
defaults
defaults
defaults
defaults 1 1

1
1
0
0
0
0
1
1

1
2
0
0
0
0
2
2

What is suspicious about the first column of this file, which leads you to believe that there must
be some mistake?
a.

A logical partition is being used as the root partition, which is not allowed.

b.

A device of type "none" is being mounted to the /proc partition, which makes no sense.

c.

All of the partitions are being mounted from a single drive.

d.

There is a jump from 4 to 6 in the listed partitions, with no mention of partition number 5.

e.

The file implies that all 4 primary partitions contain a filesystem, but a logical partition exists
as well.

9.

What is suspicious about the second column of this file, which leads you to believe that there must
be some mistake?
a.

Partitions are configured to be mounted to both /var/data and /var, but partitions may
only be mounted to directories contained by the root partition.

b.

The mount point tmp is specified as a relative reference.

c.

Filesystems are configured to be mounted to directories in the /dev directory, but the /dev
directory may only contain device nodes.

d.

A partition is configured to be mounted to /, but only the kernel may mount the root partition.

e.

A filesystem is configured to be mounted to /proc, but the kernel automatically mounts /


proc as part of its bootup process.

10.

rha130-6.1-1

ext4
ext4
devpts
proc
sysfs
tmpfs
ext4
ext4
ext4

What is suspicious about the last column of this file, which leads you to believe that there must
be some mistake?
a.

The column contains the value 0, when only the values greater than 0 are allowed.

b.

The column contains multiple entries for the value "2", which will result in an error when the
fsck command tries to decide which to check first.

c.

The column contains the value "2", when only the values 1 and 0 are meaningful.

d.

The last column only exists for legacy reasons, so the fact that it contains non-zero values is
suspect.

53

Copyright 2011, Red Hat Inc.

The /etc/fstab File

e.

rha130-6.1-1

The column contains multiple entries for the value "1", when the value 1 is only supposed to
be used for the root partition.

54

Copyright 2011, Red Hat Inc.

Chapter 5. Managing Swap Space


Key Concepts
Partitions and Files may be used as Swap Space to extend a system's apparent memory.
Swap areas must be initialized with the mkswap command.
Swap areas are activated with the swapon command, and deactivated with the complementary swapoff
command.
Swap areas may be listed in the /etc/fstab file in order to be activated automatically at system
startup.

Discussion
Swap Space
Linux allows block devices to be used to extend the system's physical memory. In other operating systems,
disk space used for such a purpose is often referred to as "virtual memory". In Linux (and Unix), it is
referred to as swap space, and the act of using the disk based memory is referred to as swapping out
memory to disk.
In Linux, swap space may be allocated either as an entire partition or as a file in an already existing (and
mounted) filesystem. The former is the more common, but can be more difficult to set up once the system
is installed. The latter is less efficient, but can be quickly put into place if some upcoming operation is
going to require large amounts of memory.
Disk based memory is dramatically slower to access than RAM. As a result, swap space is generally used
to overcome temporary peak memory demands, and not as a general replacement for physical memory. If
a system is consistently using large amounts of swap space, more physical memory would greatly improve
its performance.
Users new to Linux are sometimes surprised to discover that, even when not running processes demanding
large amounts of memory, the Linux kernel uses swap space. Recall that the Linux kernel primarily
uses memory for two purposes: process memory, and caching I/O operations. Sometimes, the kernel will
sacrifice seldom used process memory for the sake of increasing the size of the I/O cache, "swapping out"
portions of seldom used processes in an attempt to improve overall system performance. The fact that the
Linux kernel is using a little bit of swap space does not necessarily imply that more physical memory is
required.
In a similar vein, even systems with large amounts of physical memory should provide some amount
of swap space. The Linux kernel prefers to have the option of swapping memory to disk available as
it develops its memory optimization strategies. Systems which do not provide swap space are officially
considered an unsupported configuration by Red Hat.

Monitoring Swap Partitions: /proc/swaps and /proc/


meminfo
The proc filesystem file /proc/swaps lists currently active swap spaces, their type (either partition or
file), their sizes, and their priority.

rha130-6.1-1

55

Copyright 2011, Red Hat Inc.

Managing Swap Space

[root@station root]# cat /proc/swaps


Filename
Type
/dev/sda2
partition

Size
522104

Used
42284

Priority
-1

The already familiar proc filesystem file /proc/meminfo lists the total amount of currently allocated
swap space, along with physical memory statistics.
[root@station root]# cat /proc/meminfo
MemTotal:
255232 kB
MemFree:
19200 kB
MemShared:
0 kB
...
SwapTotal:
522104 kB
SwapFree:
479820 kB

Swap utilization in conventional kilobytes (where 1 kB = 1024 bytes).

Initializing Swap Space with mkswap


In order to use a disk partition or file as swap space, it must first be initialized with the mkswap command.
Like the mkfs family of commands, the mkswap command expects to be called with a mandatory
argument, the file to be initialized. In the following example, the mkswap command is used to initialize
the device /dev/sda1 as swap space.
[root@station root]# mkswap /dev/sdb1
Setting up swapspace version 1, size = 130002 kB

Like creating a filesystem, initializing swap space with the mkswap command is a one time action.

Activating Swap Space with swapon


Once initialized, swap space must be activated with the swapon command. In the following example, the
previously initialized device /dev/sdb1 is activated, examining the file /proc/swaps both before
and after to reflect the change.
[root@station root]# cat /proc/swaps
Filename
Type
/dev/sda2
partition
[root@station root]# swapon /dev/sda1
[root@station root]# cat /proc/swaps
Filename
Type
/dev/sda2
partition
/dev/sdb1
partition

Size
522104

Used
42284

Priority
-1

Size
522104
126952

Used
42284
0

Priority
-1
-2

The swapon command also allows priorities to be set with the -p command line switch. Swap areas with
higher priorities will be used before swap areas with lower priorities. See the swapon(2) man page for
details.

Deactivating Swap Areas with swapoff


The complement to the swapon command, swapoff deactivates the specified swap file. If the swap file
was in use, the command may take a while to run, as the kernel relocates all of the swapped pages to other
swap areas, or back into RAM.

Predefining Swap Areas in the /etc/fstab File


When invoked with the -a command line switch, the swapon command will examine the /etc/fstab,
and activate all listed swap partitions or files. When specifying a swap area in /etc/fstab, the first

rha130-6.1-1

56

Copyright 2011, Red Hat Inc.

Managing Swap Space

column should be the name of the partition or file to activate, the second and third columns should both
be set to the word swap, and the fourth column should either be set to defaults, or the pri=n
option can be used to assign the swap area a priority of n. The last two fields should be set to 0.

Examples
Initializing Swap Partitions
Having configured his filesystems to his liking, elvis now returns his attention to the swap partition he
created. He initializes the partition with the mkswap command.
[root@station root]# mkswap /dev/sda9
Setting up swapspace version 1, size = 139309 kB

Next, he activates the swap space with the swapon command, and confirms that it activated properly by
examining /proc/swaps.
[root@station root]# swapon /dev/sda9
[root@station root]# cat /proc/swaps
Filename
Type
/dev/sda2
partition
/dev/sdb1
partition

Size
Used
2096472 0
136040 0

Priority
-1
-2

In order to activate the swap partition automatically at system bootup, he adds a line to the /etc/fstab
file. Following the same technique used with his filesystems, he finds a similar line and duplicates it.
[root@rosemont-es root]# cp /etc/fstab /etc/fstab.bak
cp: overwrite `/etc/fstab.bak'? y
/dev/sda2
/dev/sda2

swap
swap

swap
swap

defaults
defaults

0 0
0 0

Now, by editing a single word, his new swap partition is incorporated into the bootup process.
/dev/sda2
/dev/sdb1

swap
swap

swap
swap

defaults
defaults

0 0
0 0

Using Swap Files


The user blondie is about to perform a compilation of a large program she downloaded, and is concerned
that her machine does not have enough memory. In order to help herself get past this one task, she decides
to create a swap file.
First, she used the dd command to create a 256 megabyte file full of zeros. On the dd command line, she
specifies her input file as the virtual device /dev/zero, the output file as her swap file, /tmp/swap,
the blocksize as 1 megabyte, and the transfer size of 256 blocks.
[root@station root]# dd if=/dev/zero of=/tmp/swap bs=1024k count=256
256+0 records in
256+0 records out
[root@station root]# ls -s /tmp/swap
262404 /tmp/swap

After creating the file, she treats it as if it were a partition, first initializing it for use as swap space, and
then activating the file.
[root@station root]#
Setting up swapspace
[root@station root]#
[root@station root]#

rha130-6.1-1

mkswap /tmp/swap
version 1, size = 268431 kB
swapon /tmp/swap
cat /proc/swaps

57

Copyright 2011, Red Hat Inc.

Managing Swap Space

Filename
/dev/sdb2
/dev/sda9
/tmp/swap

Type
partition
partition
file

Size
2096472
136040
262136

Used
0
0
0

Priority
-1
-2
-3

She then performs her compilation. When finished, she cleans up by first deactivating the swap space,
and the removing the file.
[root@station root]# swapoff /tmp/swap
[root@station root]# rm /tmp/swap
rm: remove regular file `/tmp/swap'? y

Online Exercises
Lab Exercise
Objective: Initialize a swap partition, and configure your /etc/fstab file so that the partition
is activated automatically at system startup.
Estimated Time: 15 mins.

Specification
This exercise will use the first partition created in the first Exercise of this Workbook. Ensure that the file
~/lab3.1/disk still exists, and that it still references the correct drive. The partition of the specified
drive should be about 128 megabytes in size, and have a partition id of Linux Swap.
1. Initialize the partition to be used as swap space.
2. Manually activate the swap partition.
3. Add a line to your /etc/fstab file, such that the swap partition is activated automatically at system
startup.

Note
In the process of grading your lab, the swap space will be deactivated and reactivated. If the
reactivation fails, you should manually restart the swap space (step 2) before regrading.

Deliverables
1.
1. A properly initialized and activated swap partition, which is the next to the last partition on the
specified drive.
2. A properly configured /etc/fstab file, such that the swap partition is automatically activated
at bootup.

Questions
1.

What is the threshold amount of physical memory, above which allocating swap space is no longer
recommended?
a.

rha130-6.1-1

128 megabytes

58

Copyright 2011, Red Hat Inc.

Managing Swap Space

b.

256 megabytes

c.

512 megabytes

d.

1 gigabyte

e.

A swap partition should always be created, no matter how much physical memory is available.

2.

Which of the following files lists currently active swap partitions?


a.

/proc/swaps

b.

/etc/fstab

c.

/var/state/swap

d.

/etc/swaptab

e.

None of the above

3.

Which file is used to define swap partitions which should be activated automatically as the system
boots?
a.

/etc/swaptab

b.

/etc/fstab

c.

/proc/swaps

d.

/var/state/swap

e.

none of the above

4.

What command line would be used to initialize a disk partition to be used as swap space?
a.

swapinit /dev/sda3

b.

mkswap /dev/sda3

c.

mkfs -t swap /dev/sda3

d.

fdisk /dev/sda3

e.

None of the above

5.

6.

rha130-6.1-1

What command line would be used to initialize a file to be used as swap space?
a.

mkswap /tmp/swapfile

b.

mkswap -f /tmp/swapfile

c.

swapinit -f /tmp/swapfile

d.

mkswapfile /tmp/swapfile

e.

None of the above


ext3 is to mount as swap is to ....

59

Copyright 2011, Red Hat Inc.

Managing Swap Space

a.

swapon

b.

mkswap

c.

swaps

d.

swapoff

e.

startswap

7.

Which of the following lines could be used to define a swap area that should be activated at system
startup in the /etc/fstab file?
a.

/dev/sda4 swap swap defaults 0 0

b.

swap /dev/sda4 swap defaults 0 0

c.

/tmp/swapfile swap swap noauto 0 0

d.

/tmp/swapfile none none defaults 0 0

e.

None of the above

8.

Which of the following commands is used to deactivate a swap area?


a.

swapstop

b.

umount

c.

uswap

d.

swapoff

e.

None of the above

9.

Which of the following command lines could be used to activate a swap partition with a priority
of 3?
a.

mkswap -p 3 /dev/sda3; swapon /dev/sda3

b.

swap /dev/sda3 3

c.

mount -t swap -o pri=3 /dev/sda3

d.

swapon -p 3 /dev/sda3

e.

none of the above

10.

rha130-6.1-1

Which of the following commands could confirm that a swap partition is active with a priority of 3?
a.

cat /proc/swaps

b.

cat /proc/meminfo

c.

top

d.

cat /proc/slabinfo

e.

A and B

60

Copyright 2011, Red Hat Inc.

Chapter 6. Miscellaneous Filesystem


Management Commands
Key Concepts
The e2label command can be used to examine and set ext2, ext3, and ext4 filesystem labels.
The fsck command is used to check and repair filesystems.
The tune2fs command can be used to modify already existing ext2, ext3, and ext4 filesystems.
With the -j command line switch, the tune2fs command can convert ext2 filesystems into ext3
filesystems.

Discussion
In this last Lesson of the Workbook, we cover a few miscellaneous commands and concepts that have not
yet been addressed, and end with a summary which ties together the skills developed in this Workbook
and places them into context with one another.

The e2label Command


The e2label command is used to display or set the volume name of an ext2/ext3/ext4 filesystem. The
first argument should be the name of the partition which contains the filesystem. If only one argument is
provided, the volume name is displayed. If an optional second argument is provided, the volume name is
set to it. Both are exemplified in the following sequence.
[root@station root]# e2label /dev/sda1
/boot
[root@station root]# e2label /dev/sda1 foo
[root@station root]# e2label /dev/sda1
foo

Recall that in Red Hat Enterprise Linux, you should not change volume names without also considering
the configuration of the /etc/fstab file. After performing this example, unless the LABEL= reference
for the /boot partition is also changed in the /etc/fstab file, the /dev/sda1 partition will probably
not be mounted when the machine reboots.

File System Repair with fsck


Filesystems generally maintain large amounts of internal data about their directory structure. Over time,
disk imperfections or kernel bugs can lead to minor corruptions of this filesystem data. If a system is
shutdown before a filesystem is unmounted, the filesystem is almost certain to contain corruptions.
Traditionally, Unix provides a fsck command, named for "(f)ile (s)ystem (c)hec(k)", which is used to
diagnose and fix filesystem corruptions. The act of executing the fsck command on a filesystem is often
referred to as "fisking" the partition.
Much like the mkfs command, Linux uses a front-end fsck command, backed by several file-system
specific versions which start fsck..
[root@station root]# ls /sbin/fsck*
/sbin/fsck.cramfs /sbin/fsck.ext3 /sbin/fsck.ext4dev
/sbin/fsck.ext2
/sbin/fsck.ext4 /sbin/fsck.msdos

rha130-6.1-1

61

/sbin/fsck.vfat

Copyright 2011, Red Hat Inc.

Miscellaneous Filesystem
Management Commands
The fsck command can be called with the -t command line switch to specify a filesystem type, or the
filesystem specific version of the command can be used. If fsck is used without the -t switch, it will try to
automatically determine the filesystem type, though this could be risky for heavily damaged filesystems.
Because the ext2 filesystem is so common, yet another name for the fsck.ext2 command exists, e2fsck.
The commands fsck -t ext2, fsck.ext2, and e2fsck are all synonyms.

Using fsck
The fsck command is generally called with the name of the partition to check as its lone argument. If the
fsck command finds a problem which it can fix without a risk of losing data, it will implement the fix. If
there is a possibility of losing data, the fsck command will pause and prompt to ask if it should implement
the fix. For administrators not blessed with a detailed knowledge of internal ext2 filesystem design, there
is really little choice but to say "yes". In fact, the fsck command is often invoked with the -y command
line switch, which in effect says "don't ask, just do it".

The /lost+found Directory


Often, the fsck command may come across damaged data which it recognizes as a file, but which has no
associated a name or directory. (Recall that a file's name is generally stored in a dentry, not in the inode.)
Whenever an ext2 filesystem is created, a directory called lost+found is created in its root directory.
This directory exists to claim any lost files discovered by fsck. The fsck command will make up a name
for the file, and place it in this directory.

Automatic fscking on Startup


By default, most Linux (and Unix) systems apply the fsck command to all filesystems at startup. If a
filesystem was not previously unmounted cleanly, a thorough fscking occurs. For ext2 filesystems, the
entire filesystem is examined. For ext3 (and other) journaling filesystems, only the journal of pending
transactions is examined, resulting in a much quicker check. Even for journaling filesystems, however, an
occasional, thorough check of the entire filesystem is suggested.

The tune2fs Command


The tune2fs command is used to tweak filesystem parameters for an already existing ext2/ext3/ext4
filesystems, which is specified as its lone argument. Before a filesystem can be operated on with tune2fs,
it must be unmounted. The following table lists some of the parameters which may be modified by the
tune2fs command.

Table 6.1. Command Line Switches for the tune2fs Command


Switch

Effect

-c n

Set the maximum mount count to n. After the filesystem has been mounted n times,
a mandatory fsck check of the filesystem occurs.

-j

Add an ext3 or ext4 filesystem journal.

-L name

Set the volume name to name. This option is identical in function to the e2label
command.

-m n

Set the reserved blocks percentage to n.

-o
mount Change the default mount options for the filesystem. For example, -o acl
options...
-O filesystem Set or clear filesystem features. For example, -O extent.
features...

rha130-6.1-1

62

Copyright 2011, Red Hat Inc.

Miscellaneous Filesystem
Management Commands

Converting ext2/ext3 filesystems to ext4


Converting to an ext4 filesystem is not supported by Red Hat. In theory it can be done by using tune2fs to
add the extents, uninit_bg, and huge_file features. Next check the filesystem with e2fsck -f and edit the /
etc/fstab to reflect the ext4 filesystem type. If you also need to add a journel use either tune2fs -j or
tune2fs -O has_journel. Not all ext4 features are available when converting from ext2 or ext3 filesystems.
An ext4 filesystem cannot be reverted to an old format.

The ext2 Mount Count


Most of these options refer to features of the ext2 filesystem which should already be familiar. The
exception is the -c command lines switch, and the concept of a maximum mount count.
The ext2 filesystem maintains a mount count, which is incremented every time the filesystem is mounted,
and a maximum mount count parameter. If a mount would exceed the maximum mount count, a mandatory
fsck of the filesystem is applied, and the mount count is reset to 0.

Converting ext2 Filesystems to ext3


As implied by the -j command line switch mentioned above, converting an ext2 filesystem into an ext3
filesystem is trivial. Run tune2fs -j on it. Afterward, specify the filesystem type as ext3 when mounting
it (or in the /etc/fstab file).
The only complexity occurs if this is the first time an ext3 filesystem has been used on the system. In that
case, the system's initial ramdisk (initrd) needs to be reconstructed so as to contain the ext3.o kernel
module. While this rather complicated topic will be addressed in a later Workbook, for now, consider the
following command line, run as root, the appropriate incantation for reconstructing an initial ramdisk.
[root@station root]# mkinitrd /boot/initrd-$(uname -r).img $(uname -r)

Again, this command is only necessary if the system does not already contain an ext3 filesystem. Because
the ext3 filesystem is the default, this is rarely the case.

Mounting ext3 filesystems as ext2


There is no need to "convert" an ext3 filesystem to ext2. Instead, just mount the filesystem, specifying the
filesystem type as ext2. The journal will be ignored.

Review: Adding a New Disk


In order to make use of a new disk added to an existing system, the following steps are required. These steps
serve as a review of many of the topics covered in this workbook, placed into context with one another.
1. Physical Connection: Physically connect the disk to the machine. Upon rebooting, the new disk should
be identified in the stream of kernel messages seen on the console at bootup, or logged in the file /
var/log/dmesg. If evidence of the new drive is not found, there is a BIOS or physical connectivity
problem which must be addressed before continuing.
2. Partitioning: The disk is subdivided into multiple partitions using the fdisk command. As we are
considering a newly connected drive, the system will probably not have to be rebooted, as would be
the case for repartitioning a drive already in use.
3. Formatting: Any partitions which are going to be used as filesystems must be initialized with the
appropriate variant of the mkfs command, such as mkfs.ext4. Likewise, any partitions which are going
to be used as swap space must be initialized with the mkswap command.

rha130-6.1-1

63

Copyright 2011, Red Hat Inc.

Miscellaneous Filesystem
Management Commands
4. Mounting: Filesystems must be mounted to a mount point before they can be used. Using the mkdir
command to create the mount point is an often overlooked step. Analogously, any swap partitions must
be activated with the swapon command.
5. Updating /etc/fstab: If the filesystem is to be mounted (or the swap area activated) automatically
at bootup, the appropriate lines should be added to the /etc/fstab file.

Examples
Converting an ext2 filesystem to ext3
After a few weeks of using his new disk, and one too many waits for his ext2 filesystem to repair itself,
prince decides he really should have made the filesystem ext3 to begin with. He decides to convert it now.
First, he unmounts the filesystem.
[root@station root]# umount /picts

Next, he operates on the ext2 filesystem with the tune2fs command, using the -j command line switch
to specify that a journal should be added. Because his memory is hazy, he first confirms that he has the
right partition by examining its label.
[root@station root]# e2label /dev/sda6
pics
[root@station root]# tune2fs -j /dev/sda6
tune2fs 1.32 (09-Nov-2002)
Creating journal inode: done
This filesystem will be automatically checked every 20 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.

Having finished the conversion, he tries performing the mount as an ext3 filesystem manually.
[root@station root]# mount -t ext3 -L pics /picts
[root@station root]# mount
...
/dev/sda7 on /dosd type vfat (rw)
/dev/sda8 on /ogg type ext4 (rw)
/dev/sda6 on /picts type ext3 (rw)

As that seemed to work without incident, he updates his /etc/fstab file.


LABEL=pics

/picts

ext3

defaults

1 2

Online Exercises
Lab Exercise
Objective: Use filesystem maintenance commands to modify existing filesystems.
Estimated Time: 20 mins.

Specification
This exercise will continue to use the filesystem created and mounted in the previous Exercises. Ensure
that the file ~/lab3.1/disk still exists, and that it still references the correct drive. The last partition
of the drive should have a properly formatted ext2 filesystem.

rha130-6.1-1

64

Copyright 2011, Red Hat Inc.

Miscellaneous Filesystem
Management Commands
1. Use the appropriate commands to convert your ext2 filesystem into an ext3 filesystem. Do not create
a new filesystem. Any information contained in the filesystem should be preserved.
2. Change the filesystem label to lab3.6.
3. Update your /etc/fstab file, so that the appropriate label is used, and the filesystem is mounted
as an ext3 filesystem.
4. Unmount the partition before grading your exercise.

Deliverables
1.
1. The last partition of the specified drive should be an ext3 filesystem, with a label of lab3.6, and
no blocks reserved for root. The contents of the filesystem should be preserved in the process.
2. An appropriately configured /etc/fstab file, updated to refer to the new label and the ext3
filesystem.

Questions
1.

Which of the following command lines could be used to check and repair an ext2 filesystem?
a.

fsck /dev/sda3

b.

fsck -t ext2 /dev/sda3

c.

fsck.ext2 /dev/sda3

d.

e2fsck /dev/sda3

e.

All of the above

2.

Which of the following command lines could be used to assign the label /data to an ext2 filesystem?
a.

e2label /data /dev/sda3

b.

e2label -L /data /dev/sda3

c.

e2label /dev/sda3 /data

d.

label -t ext2 /dev/sda3 /data

e.

None of the above

The remaining questions refer to the following scenario.


You have just attached a newly purchased drive to an already existing Red Hat Enterprise Linux
installation. Using only the tools discussed in this Workbook, you would like to create 6 ext4 filesystems,
each mounted to the directories /data1, /data2, ..., /data6, and a 256 megabyte swap partition. You
would like all 6 partitions to be mounted and the swap area to be activated automatically at bootup.
3.

rha130-6.1-1

Which of the following commands could you get away with not using?
a.

e2label

b.

mkswap

65

Copyright 2011, Red Hat Inc.

Miscellaneous Filesystem
Management Commands
c.

fdisk

d.

mount

e.

All of the above commands are required.

4.

What is the most reasonable ordering for the commands you will use?
a.

mke2fs, mkswap, fdisk, mount, swapon

b.

fdisk, swapon, mkswap, mke2fs, mount

c.

mount, fdisk, mke2fs, mkswap, swapon

d.

fdisk, mke2fs, mkswap, mount, swapon

e.

mkswap, swapon, mke2fs, mount, fdisk

5.

At least how many logical partitions will need to be used?


a.

b.

c.

d.

e.

6.

Which of the following command lines could be used to create an ext4 filesystem on the first logical
partition, and assign it the label /data3?
a.

mkfs.ext4 -L /data3 /dev/sdd5

b.

mkfs.ext4 /dev/sdd5; e2label /dev/sda5 /data3

c.

mkfs.ext4 /dev/sdd5; tune2fs -L /dev/sda5 /data3

d.

All of the above

e.

None of the above

7.

8.

rha130-6.1-1

Once the ext4 filesystems have been created, and are all mounted, you decide that you would like
the filesystem on partition /dev/sdd2 to be an ext3 filesystem instead. Which command line could
be used to convert the filesystem to ext3 (without losing any data already in the filesystem)?
a.

tune2fs -j /dev/sdd2

b.

umount /dev/sdd2 && mkfs.ext3 /dev/sdd2

c.

umount /dev/sdd2 && tune2fs -j /dev/sdd2

d.

umount /dev/sdd2 && mke2fs -j /dev/sdd2

e.

An ext4 filesystem cannot be reverted to an ext2 or ext3 filesystem


You decide that you would like to check and repair the filesystem on partition /dev/sdd3. You
unmount the partition. What command could you run next to perform the check?

66

Copyright 2011, Red Hat Inc.

Miscellaneous Filesystem
Management Commands
a.

swapoff /dev/sdd3

b.

fsck.vfat /dev/sdd3

c.

fisk /dev/sdd3

d.

fsck /dev/sdd3

e.

None of the above

9.

The check and repair utility used above mentions that it has restored some file, but could not find
names for. Where should you look for these files?
a.

The /lost+found directory in your root partition.

b.

The lost+found directory in the repaired filesystem's root directory.

c.

The /proc/lost+found directory.

d.

The /tmp/lost+found directory.

e.

None of the above

10.

rha130-6.1-1

You realize that your previously installed Red Hat Enterprise Linux system had not used the
ext3 filesystem. After successfully converting your new ext2 filesystem to ext3, what additional
command should be run?
a.

ext3init

b.

kudzu

c.

mkinitrd

d.

setup

e.

mkext3

67

Copyright 2011, Red Hat Inc.

Chapter 7. Logical Volume


Management
Key Concepts
Logical volumes are easily-resizable pseudo-partitions maintained by the Logical Volume Management
(lvm) component of the Linux kernel.
Any block device can be designated a physical volume, added to a volume group and used to create
logical volumes.
Logical volume management uses physical volumes, volume groups, and logical volumes, which are
created with the pvcreate, vgcreate, and lvcreate commands, respectively.
Ext2, ext3, or ext4 filesytems may be resized with resize2fs.

Discussion
Logical Volume Management
Logical Volume Management (or LVM) allows administrators to allocate disk space in a more flexibly
than traditional partitioning. LVM treats disks at three separate layers: physical volumes, volume groups,
and logical volumes.
At the bottom layer are physical volumes, which are simply disk partitions. At the top layer are logical
volumes, on which filesytems are created and which are mounted into the filesystem. In between these
two layers sits the abstract concept of a volume group.

Physical Volumes
Volume groups consist of physical volumes. In the following diagram, we start with three physical
volumes, which are essentially just disk partitions from three separate disks which have been reserved for
lvm use: /dev/sda3, /dev/sdb2, and /dev/sdc1.

Figure 7.1. Logical Volume Management: Physical Volumes

rha130-6.1-1

68

Copyright 2011, Red Hat Inc.

Logical Volume Management

Volume Groups
Physical volumes are combined into an abstract concept called a volume group, which is represented by a
directory called /dev/vg0/. The name of the volume group, and thus the name of the directory which
represents it, is specified when the volume group is created.
When a physical volume joins a volume group, its space is divided into small evenly sized chunks called
physical extents, or often just PEs. The size of a physical extent is specified when the volume group is
created, and all physical volumes which join the group will use the same physical extent size. A typical
physical extent size is 4 Megabytes.

Figure 7.2. Logical Volume Management: Volume Groups

Logical Volumes
At the upper layer, the physical extents of a volume group can be devoted to any of several logical volumes.
The important feature of logical volumes is that they are not restricted by the boundaries of the underlying
physical volume. To a logical volume, a physical extent is just another physical extent, no matter on which
physical volume the physical extent resides.
Logical volumes are referenced using device nodes which are rooted by the device node for the volume
group, but have the logical volume name appended. For example, the three logical volumes in the figure
below, named lv1, lv2, and lv3, are referenced as /dev/vg0/lv1, /dev/vg0/lv2, and /dev/
vg0/lv3.

rha130-6.1-1

69

Copyright 2011, Red Hat Inc.

Logical Volume Management

Figure 7.3. Logical Volume Management: Logical Volumes

Dynamic Resizing
Logical Volumes may be grown or shrunk by simply allocate more physical extents to the logical volume,
or returning physical extents to the volume group. Volume groups may also be grown or shrunk by adding
or removing physical volumes.
For example, in the following diagram, a new physical volume (/dev/hdc2) has been added to the
volume group vg0. The physical extents provided by this physical volume have then been distributed to
the logical volumes lv1 and lv2.

Figure 7.4. Logical Volume Management: Dynamic Resizing

rha130-6.1-1

70

Copyright 2011, Red Hat Inc.

Logical Volume Management

Creating Physical Volumes, Volumes Groups and


Logical Volumes
In order to initialize LVM on a Red Hat Enterprise Linux system, the command vgscan must be performed
once.
[root@station root]# vgscan
vgscan -- reading all physical volumes (this may take a while...)
vgscan -- found active volume group "vg3"
vgscan -- "/etc/lvmtab" and "/etc/lvmtab.d" successfully created
vgscan -- WARNING: This program does not do a VGDA backup of your volume group

Before a disk or partition can be added to a volume group, it must be prepared by designating it as a
physical volume. This is done with the pvcreate command. Note that all data on the partition is lost when
pvcreate is run. The following command would designate three partitions as physical volumes.
[root@station] pvcreate /dev/sdb1 /dev/sdc1 /dev/sdd1

Once the physical volumes are created they can be added to a volume group. The volume group can be
called anything, but we will be unimaginative and call it vg0.
[root@station] vgcreate vg0 /dev/sdb1 /dev/sdc1 /dev/sdd1

This will create a new directory (not device node) called /dev/vg0/. Each logical volume we create
inside this volume group will be represented by a device node in this directory.
We are now ready to create logical volumes. LVs are created using the lvcreate command. You must tell
lvcreate the name of the LV you wish to create (-n), how big you want it to be in megabytes ( -L ) and
which volume group you wish to create it from.
[root@station] lvcreate -n lv0 -L 150 vg0

This will create a logical volume called /dev/vg0/lv0 with a size of 152 megabytes. But wait, didn't
we say to create an LV with a size of 150 megabytes? This warrants explanation.
Behind the scenes, LVM works by dividing the PVs in a volume group into 4MB chunks called physical
extents (PEs). When a logical volume is created the PEs in the volume group are mapped to logical extents
(LEs) of the same size in the logical volume. In other words, logical volumes are made up of 4MB chunks
of virtual disk space (LEs) that map to 4MB chunks of physical disk space (PEs). Thus the size of a logical
volume will always be a multiple of 4MB. If this doesn't make sense, don't worry about it. Simply put, when
you create a logical volume LVM will sometimes tweak the size you tell it, but only by a few megabytes.
Once the logical volume has been created it can be formatted, mounted and added to the /etc/fstab file.
[root@station] mke2fs /dev/vg0/lv0
[root@station] mount /dev/vg0/lv0 /data
[root@station] df -h /data
Filesystem
Size Used Avail Use% Mounted on
/dev/mapper/vg0-lv0
148M 5.6M 135M
4% /data

The following figure illustrates the creation of a set of logical volumes.

rha130-6.1-1

71

Copyright 2011, Red Hat Inc.

Logical Volume Management

Figure 7.5. Logical Volume Creation

Resizing Logical Volumes


Logical Volumes can be grown with lvextend and reduced with lvreduce. In both cases, the most important
argument to the command is -L, which allows you to specify the the size by which you want to increase or
decrease the specified logical volume. You can specify the desired end size or you can add or subtract size
in your logical volume. In the following example the logical volume /dev/vg0/lv0 will be increased
(note the plus sign) by 12 megabytes (or three 4-megabyte physical extents):

rha130-6.1-1

72

Copyright 2011, Red Hat Inc.

Logical Volume Management

[root@station] lvextent -L +12M /dev/vg0/lv0


Extending logical volume lv0 to 164.00 MiB
Logical volume lv0 successfully resized

This only changed the size of the underlying logical volume, not the filesystem! Recall our filesystem size
when we first created the filesystem.
[root@station] df -h /data
Filesystem
Size
/dev/mapper/vg0-lv0
148M

Used Avail Use% Mounted on


5.6M

135M

4% /data

Logical volumes that have ext2, ext3, or ext4 filesystems can be resized with the resize2fs command. If
you only provide the device to resize, then the resize2fs will increase the size of the filesystem to use all
remaining space. The resize2fs also accepts a size as the final argument which allows you to specify the
the size by which you want to increase or decrease the specified logical volume. Remember that in order
to reduces the size a logical volume it must be unmounted first. In the following example the (mounted,
ext4-formatted) logical volume /dev/vg0/lv0 will be increased to use all remaining space.
[root@station] resize2fs /dev/vg0/lv0
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg0/lv0 is mounted on /data; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/vg0/lv0 to 167936 (1k) blocks.
The filesystem on /dev/vg0/lv0 is now 167936 blocks long.
[root@station ~]# df -h /data
Filesystem
Size Used Avail Use% Mounted on
/dev/mapper/vg0-lv0
159M 5.6M 145M
4% /data

To reduce the filesystem and then the logical volume, we must first unmount and check the filesystem.
[root@station] umount /dev/vg0/lv0
root@station] fsck -f /dev/vg0/lv0
1.41.12 (17-May-2010)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/vg0/lv0: 11/43008 files (0.0% non-contiguous), 11077/167936 blocks
[root@station] resize2fs /dev/vg0/lv0 148M
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/vg0/lv0 to 151552 (1k) blocks.
The filesystem on /dev/vg0/lv0 is now 151552 blocks long.
[root@station] lvreduce -L -12M /dev/vg0/lv0
WARNING: Reducing active logical volume to 152.00 MiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv0? [y/n]: y
Reducing logical volume lv0 to 152.00 MiB
Logical volume lv0 successfully resized
[root@station ~]# mount /dev/vg0/lv0 /data
[root@station ~]# df -h /data
Filesystem
Size Used Avail Use% Mounted on
/dev/mapper/vg0-lv0
144M 5.6M 131M
5% /data

Resizing Volume Groups


LVM allows you to resize volume groups by adding and removing drives without damaging data. Suppose
you have a logical volume that contains, among others, an drive called /dev/sdb1, which is reaching

rha130-6.1-1

73

Copyright 2011, Red Hat Inc.

Logical Volume Management

the end of its life. You have decided to swap it for a bigger, faster and newer SCSI drive, /dev/sdc1.
The process of switching these drives is made up of three simple steps.
First, the new drive should be added to the array. This is done with the vgextend command once /dev/
sdc1 has been designated as a physical volume.
[root@station] pvcreate /dev/sdc1
[root@station] vgextend vg0 /dev/sdc1

The physical volume /dev/sdc1 is now part of the vg0 volume group. However, we can't just remove
/dev/sdb1 yet because it still has data on it. We must first get the portions of the logical volume that
reside on there to other parts of the volume group. Fortunately this complicated task is made extremely
simple by a command called pvmove.
[root@station] pvmove /dev/sdb1

Examining LVM Components


Red Hat Enterprise Linux includes three useful tools for examining physical volumes, volume groups and
logical volumes: pvdisplay, vgdisplay and lvdisplay.
pvdisplay displays information about physical volumes.
[root@station] pvdisplay /dev/sda2
--- Physical volume --PV Name
/dev/sda2
VG Name
vg0
PV Size
37.16 GB [7792848 secs] / NOT usable 4.1 MB [LVM: 165 KB]
PV#
1
PV Status
available
Allocatable
yes
Cur LV
6
PE Size (KByte)
4096
Total PE
9511
Free PE
2
Allocated PE
9509
PV UUID
ptzILn-vnWA-SmcN-fu2m-Bsjt-MUO6-67tjDT

Don't panic! You do not need to understand all of pvdisplay's output. The most important things to note
are the "PV Size" and "VG Name" fields, which show how big /dev/sda2 is and which volume group
it is a part of, respectively.
vgdisplay displays information about volume groups.
[root@station] vgdisplay vg0
--- Volume group --VG Name
vg0
VG Access
read/write
VG Status
available/resizable
VG #
0
MAX LV
256
Cur LV
6
Open LV
5
MAX LV Size
255.99 GB
Max PV
256
Cur PV
1
Act PV
1
VG Size
37.15 GB
PE Size
4 MB
Total PE
9511
Alloc PE / Size
9509 / 37.14 GB

rha130-6.1-1

74

Copyright 2011, Red Hat Inc.

Logical Volume Management

Free PE / Size
VG UUID

2 / 8 MB
GonJxd-GLBD-XFWC-2zuU-JM8Q-isye-3B345B

Here the most important fields are "VG Size", "Alloc PE / Size" and "Free PE / Size", near the bottom.
They tell you the total size of the volume group, how much space is used and how much is left free. Note
that these last two fields display values in both megabytes/gigabytes and physical extents. Knowing how
much space is free on a volume group will help you calculate how much more space can be allocated
to creating new logical volumes and/or creating new logical volumes. A list of all logical volumes and
physical volumes associated with a volume group can be obtained by passing vgdisplay the -v (as in
verbose) command line switch.
lvdisplay displays (you guessed it) information about logical volumes.
[root@station] lvdisplay /dev/vg0/lv0
--- Logical volume --LV Name
/dev/vg0/lv0
VG Name
vg0
LV Write Access
read/write
LV Status
available
LV #
1
# open
1
LV Size
1.95 GB
Current LE
500
Allocated LE
500
Allocation
next free
Read ahead sectors
1024
Block device
58:0

The main field of interest in lvdisplay's output is "LV Size", which shows the total size of the logical
volume. However, this information, as well as information on how much space is used and free, can also
be obtained by mounting the logical volume and running the df command.

Using system-config-lvm
There is also a graphical front end for managing LVM. You can access this application by selecting the
System Administration Logical Volume Management menu item. If the utility is not listed it may
need to be installed with the command yum install system-config-lvm.

Figure 7.6. system-config-lvm

rha130-6.1-1

75

Copyright 2011, Red Hat Inc.

Logical Volume Management

Creating Logical Volumes at Install Time


The Red Hat Enterprise Linux installer allows users to take advantage of logical volumes when installing
the system. Using Disk Druid in the Anaconda installer, logical volumes are created using the following
steps.
First, one or more disk partitions are created and initialized as physical volumes using the "Create" dialog
and selecting "LVM Physical Volume".

Figure 7.7. Creating Physical Volumes in Anaconda

Secondly, one or more physical volumes are committed to a newly created volume group using the "Create"
dialog and choosing "LVM Volume Group". Optionally, adjust the group name and extent size then click
"Add" to define logical volumes.

rha130-6.1-1

76

Copyright 2011, Red Hat Inc.

Logical Volume Management

Figure 7.8. Creating Volume Groups in Anaconda

Thirdly, one or more logical volumes can be created within the volume group by selecting "Add" within
the volume group creation dialog. When creating a logical volume, the size, mount point and filesystem
type for the volume are also specified.

Figure 7.9. Creating Logical Volumes in Anaconda

Once created, logical volumes are shown in Disk Druid's partition browser in a region distinct from normal
disk partitions.

rha130-6.1-1

77

Copyright 2011, Red Hat Inc.

Logical Volume Management

Figure 7.10. Displaying Volume Groups in Anaconda

Online Exercises
Lab Exercise
Objective: Create and manage a Volume Group.
Estimated Time: 45 mins.

Specification
This lab exercise will continue to use the volume group created in the previous lab exercise. Ensure that
the file ~/lab3.5/disk still exists, and that it still references the correct drive.
1. Create a logical volume data of about 52M in the volume group vg3. You may delete the filesystems
and logical volumes of the previous exercises if space is needed.
2. Encrypt the block device with a passphrase of redhat55
3. Place an ext4 filesystem on the device and mount the filesystem as /encrypted.
4. Configure the /etc/crypttab and /etc/fstab so that the filesystem can be mounted with the
command mount /encrypted after a reboot. Do not mount the filesystem as the system boots.
Reboot the system before grading.

Create a Volume Group and 2 Logical Volumes


1. The file ~/lab3.5/disk, which contains the name of the drive you used for this lab, as an absolute
reference. The drive name should be the only word in the file.

rha130-6.1-1

78

Copyright 2011, Red Hat Inc.

Logical Volume Management

2. Use the fdisk command to add 3 new partitions, each about 32 megabytes in size. Set the filesystem
labels of each newly created partition to "8e" (Linux LVM). Write the partition table, and exit. You
may have to reboot your machine for the changes to take effect.
[root@station root]# fdisk -l /dev/sda
Disk /dev/sda: 20.0 GB, 20020396032 bytes
255 heads, 63 sectors/track, 2434 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot
/dev/sda1
*
/dev/sda2
/dev/sda3
/dev/sda4
/dev/sda5
/dev/sda6
/dev/sda7

Start
1
17
1061
1191
1191
1194
1197

End
16
1060
1190
2434
1193
1196
1199

Blocks
128488+
8385930
1044225
9992430
24066
24066
24066

Id
83
83
82
5
8e
8e
8e

System
Linux
Linux
Linux swap
Extended
Linux LVM
Linux LVM
Linux LVM

3. Initialize LVM on your system by running the command vgscan.


[root@station root]# vgscan
vgscan -- reading all physical volumes (this may take a while...)
vgscan -- "/etc/lvmtab" and "/etc/lvmtab.d" successfully created
vgscan -- WARNING: This program does not do a VGDA backup of your volume group

4. Use the pvcreate command to initialize each of the newly created partitions as physical volumes.
[root@station root]#
pvcreate -- physical
pvcreate -- physical
pvcreate -- physical

pvcreate /dev/sda[5-7]
volume "/dev/sda5" successfully created
volume "/dev/sda6" successfully created
volume "/dev/sda7" successfully created

5. Use the vgcreate command to create a volume group named vg3 with the first two physical volumes.
Leave the third physical volume unused.
[root@station root]# vgcreate vg3 /dev/sda5 /dev/sda6
vgcreate -- INFO: using default physical extent size 4 MB
vgcreate -- INFO: maximum logical volume size is 255.99 Gigabyte
vgcreate -- doing automatic backup of volume group "vg3"
vgcreate -- volume group "vg3" successfully created and activated

6. Use the lvcreate command to create a logical volume named lv0, which is 8 megabytes in size.
[root@station root]# lvcreate -n lv0 -L 8 vg3
lvcreate -- doing automatic backup of "vg3"
lvcreate -- logical volume "/dev/vg3/lv0" successfully created

Do the same for a logical volume named lv1, also 8 megabytes in size.
7. Format the logical volumes lv1 and lv2 with ext2 filesystems.
[root@station root]# mke2fs /dev/vg3/lv0
mke2fs 1.32 (09-Nov-2002)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
...

8. Create the mount points (directories) /lvm0 and /lvm1. Mount the logical volumes lv0 and lv1 to
the respective mount points.
[root@station root]# df -h
Filesystem
Size
/dev/sda2
7.9G
/dev/sda1
122M

rha130-6.1-1

Used Avail Use% Mounted on


2.9G 4.7G 38% /
9.0M 107M
8% /boot

79

Copyright 2011, Red Hat Inc.

Logical Volume Management

none
/dev/vg3/lv0
/dev/vg3/lv1

250M
7.8M
7.8M

0
13K
13K

250M
7.4M
7.4M

0% /dev/shm
1% /lvm0
1% /lvm1

9. Create entries within your /etc/fstab file so that the logical volumes are mounted automatically at
bootup. Reboot your machine, and confirm that the mounts are automatically performed.

Expand Your Volume Group by Adding a Physical Volume


1. Use the vgextend command to add your remaining physical volume to the volume group.
[root@station root]# vgextend vg3 /dev/sda7
vgextend -- INFO: maximum logical volume size is 255.99 Gigabyte
vgextend -- doing automatic backup of volume group "vg3"
vgextend -- volume group "vg3" successfully extended

2. Use the lvextend and resize2fs commands to dynamically grow the mounted filesystem on logical
volume lv0 by 8 megabytes
[root@station root]# lvextend -L +8M /dev/vg3/lv0
Extending logical volume lv0 to 160.00 MiB
Logical volume lv0 successfully resized

Note that the kernel will not support online resizing of ext2 filesystems. See man resize2fs
[root@station1 root]# umount /dev/vg3/lv0

[root@station1 root]# e2fsck -f /dev/vg3/lv0


e2fsck 1.41.12 (17-May-2010)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/vg3/lv0: 11/2048 files (0.0% non-contiguous), 306/8192 blocks

[root@station root]# resize2fs /dev/vg3/lv0


resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg3/lv0 is mounted on /data; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/vg0/lv0 to 163840 (1k) blocks.
The filesystem on /dev/vg3/lv0 is now 163840 blocks long.

3. Unmount the lv1 filesytem on the logical volume. Use the lvreduce to to shrink the logical volume lv1
by 4 megabytes. Do not forget to check and resize the filesystem first. Remount the filesystem
[root@station root]# umount /dev/vg3/lv1
[root@station root]# e2fsck -f /dev/vg3/lv1
e2fsck 1.41.12 (17-May-2010)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/vg.data/lv1: 11/2048 files (0.0% non-contiguous), 306/8192 blocks

[root@station root]# resize2fs /dev/vg3/lv1 4M


resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/vg3/lv1 to 4096 (1k) blocks.
The filesystem on /dev/vg3/lv1 is now 4096 blocks long.

rha130-6.1-1

80

Copyright 2011, Red Hat Inc.

Logical Volume Management

[root@station root]# lvreduce -L -4M /dev/vg3/lv1


WARNING: Reducing active logical volume to 4.00 MiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv1? [y/n]: y
Reducing logical volume lv1 to 4.00 MiB
Logical volume lv1 successfully resized
[root@station root]# mount /lvm1
[root@station root]# df -h
Filesystem
Size
/dev/sda2
7.9G
/dev/sda1
122M
none
250M
/dev/vg3/lv0
16M
/dev/vg3/lv1
3.8M

Used Avail Use% Mounted on


2.9G 4.7G 38% /
9.0M 107M
8% /boot
0 250M
0% /dev/shm
13K
15M
1% /lvm0
13K 3.6M
1% /lvm1

Reduce Your Volume Group by 1 Physical Volume


1. Use vgdisplay to confirm that your volume group has at least one physical partition's worth of free
space (about 20 megabytes).
[root@station root]# vgdisplay -s
vgdisplay -- "vg3" (active/used) 48 MB

[20 MB

used / 28 MB free]

2. Use the pvmove command to free all physical extents associated with your first physical volume.
[root@station root]# pvmove /dev/sda5
pvmove -- moving physical extents in active volume group "vg3"
pvmove -- WARNING: if you lose power during the move you may need
to restore your LVM metadata from backup!
pvmove -- do you want to continue? [y/n] y
/dev/vg3/group::/dev/vg3/lv0: 0305 8576, 0306 24960
/dev/vg3/group::/dev/vg3/lv0: 0305 16768, 0306 33152
/dev/vg3/group::/dev/vg3/lv1: 0305 24960, 0307 8576
pvmove -- doing automatic backup of volume group "vg3"
pvmove -- 3 extents of physical volume "/dev/sda5" successfully moved

3. Finally, remove your first physical volume from the volume group with vgreduce.
[root@station root]# vgreduce vg3 /dev/sda5
vgreduce -- doing automatic backup of volume group "vg3"
vgreduce -- volume group "vg3" successfully reduced by physical volume:
vgreduce -- /dev/sda5
[root@station root]# vgdisplay -s vg3
vgdisplay -- "vg3" (active/used) 32 MB

[20 MB

used / 12 MB free]

Deliverables
1.
1. An active volume group name vg3, which consists of two logical volumes (named lv0 and lv1)
and two physical volumes (which are the last two in a series of three consecutive partitions).
2. The logical volume lv0 should contain a mounted ext2 filesystem which as approximately 16
megabytes in size.
3. The logical volume lv1 should contain a mounted ext2 filesystem which as approximately 4
megabytes in size.
4. The first of the three consecutive physical volumes should not be associated with any volume
group.

rha130-6.1-1

81

Copyright 2011, Red Hat Inc.

Logical Volume Management

Questions
1.

Which of the following could be an appropriate reference to one of the volume group vg0's physical
volumes?
a.

/dev/vg0/lv1

b.

/dev/vg0/hda1

c.

/dev/lv1/sda3

d.

/dev/sdb3

e.

None of the above

2.

Which of the following would be an appropriate reference to the logical volume lv1?
a.

/dev/lvm/lv1

b.

/dev/vg0/lv1

c.

/dev/lv1

d.

/dev/hda1

e.

None of the above

3.

Which of the following commands is used to initialize an lvm physical volume?


a.

mkpv

b.

mklvm -p

c.

pvmake

d.

pvcreate

e.

None of the above

4.

What is the name of the granular unit which is used to structure the assignments of disk space to
logical volumes within a volume group?
a.

sector

b.

physical extent

c.

block

d.

chunk

e.

stripe

5.

rha130-6.1-1

Which command is used to add a physical volume to an already existing volume group?
a.

vgextend

b.

vggrow

82

Copyright 2011, Red Hat Inc.

Logical Volume Management

c.

vgadmin

d.

pvcreate

e.

None of the above

Use the output of the following command to answer the next two questions.
[root@station] pvdisplay /dev/sdb1
--- Physical volume --PV Name
/dev/sdb1
VG Name
vg0
PV Size
37.16 GB [7792848 secs] / NOT usable 4.1 MB [LVM: 165 KB]
PV#
1
PV Status
available
Allocatable
yes
Cur LV
6
PE Size (KByte)
4096
Total PE
9511
Free PE
2
Allocated PE
9509
PV UUID
ptzILn-vnWA-SmcN-fu2m-Bsjt-MUO6-67tjDT

6.

What is the name of the volume group to which this physical volume has been contributed?
a.

/dev/sda2

b.

ptzILn-vnWA-SmcN-fu2m-Bsjt-MUO6-67tjDT

c.

vg0

d.

Not enough information is provided.

7.

What is the size of the physical extents for the physical volume?
a.

1 kilobyte

b.

4 kilobytes

c.

9511 kilobytes

d.

4 megabytes

e.

Not enough information is provided.

Use the output of the following command to answer the next two questions.
[root@station] vgdisplay vg0
--- Volume group --VG Name
vg0
VG Access
read/write
VG Status
available/resizable
VG #
0
MAX LV
256
Cur LV
6
Open LV
5
MAX LV Size
255.99 GB
Max PV
256
Cur PV
1
Act PV
1
VG Size
37.15 GB
PE Size
4 MB
Total PE
9511

rha130-6.1-1

83

Copyright 2011, Red Hat Inc.

Logical Volume Management

Alloc PE / Size
Free PE / Size
VG UUID

8.

How much space is available for assignment to a logical volume?


a.

4 megabytes

b.

37.15 gigabytes

c.

0.25 megabytes

d.

8 megabytes

e.

None of the above

9.

Which of the following must be specified when creating a logical volume?


a.

The name of the logical volume

b.

The size of the logical volume

c.

The size of a physical extent within the volume

d.

A and B

e.

All of the above

10.

rha130-6.1-1

9509 / 37.14 GB
2 / 8 MB
GonJxd-GLBD-XFWC-2zuU-JM8Q-isye-3B345B

Which of the following can be created at install time using the Red Hat Enterprise Linux installer?
a.

Physical volumes

b.

Volume Groups

c.

Logical Volumes

d.

A and C

e.

A, B and C

84

Copyright 2011, Red Hat Inc.

Chapter 8. Enabling Data Privacy with


Partition Encryption
Key Concepts
LUKS is a standard format for device encryption.
LUKS encrypts the partition or volume which must be decrypted before the file system in it can be
mounted.
Encrypted devices can be configured during installation with anaconda or post installation with
cryptsetup commands.
The /etc/crypttab is used to store mappings between the underlying block device and unencrypted
system reference.

Discussion
A word of thanks...
Many thanks to the Fedora Project for using Creative Commons license for all documentation.
Most of this discussion is taken from Appendix C of the Installation Guide for Fedora 15 which
can be found at http://docs.fedoraproject.org/ The source for the Installation Guide is available
at http://git.fedorahosted.org/git/?p=docs/install-guide.git

Disk Encryption
What is block device encryption?
Block device encryption protects the data on a block device by encrypting it. To access the device's
decrypted contents, a user must provide a passphrase or key as authentication. This provides additional
security beyond existing OS security mechanisms in that it protects the device's contents even if it has
been physically removed from the system.

Encrypting block devices using dm-crypt/LUKS


Linux Unified Key Setup (LUKS) is a specification for block device encryption. It establishes an on-disk
format for the data, as well as a passphrase/key management policy.
LUKS uses the kernel device mapper subsystem via the dm-crypt module. This arrangement provides a
low-level mapping that handles encryption and decryption of the device's data. User-level operations, such
as creating and accessing encrypted devices, are accomplished through the use of the cryptsetup utility.

Overview of LUKS
What LUKS does:
LUKS encrypts entire block devices. LUKS is thereby well-suited for protecting the contents of mobile
devices such as removable storage media and lLaptop disk drives

rha130-6.1-1

85

Copyright 2011, Red Hat Inc.

Enabling Data Privacy


with Partition Encryption
The underlying contents of the encrypted block device are arbitrary. This makes it useful for encrypting
swap devices. This can also be useful with certain databases that use specially formatted block devices
for data storage.
LUKS uses the existing device mapper kernel subsystem. This is the same subsystem used by LVM,
so it is well tested.
LUKS provides passphrase strengthening. This protects against dictionary attacks.
LUKS devices contain multiple key slots. This allows users to add backup keys/passphrases.
What LUKS does not do:
LUKS is not well-suited for applications requiring many (more than eight) users to have distinct access
keys to the same device.
LUKS is not well-suited for applications requiring file-level encryption.
More detailed information about LUKS is available from the project website at http://code.google.com/
p/cryptsetup/.

How will I access the encrypted devices after installation? (System Startup)
During system startup you will be presented with a passphrase prompt. After the correct passphrase has
been provided the system will continue to boot normally. If you used different passphrases for multiple
encypted devices you may need to enter more than one passphrase during the startup.

Tip
Consider using the same passphrase for all encrypted block devices in a given system. This will
simplify system startup and you will have fewer passphrases to remember. Just make sure you
choose a good passphrase!

Choosing a Good Passphrase


While dm-crypt/LUKS supports both keys and passphrases, the anaconda installer only supports the use
of passphrases for creating and accessing encrypted block devices during installation.
LUKS does provide passphrase strengthening but it is still a good idea to choose a good (meaning "difficult
to guess") passphrase. Note the use of the term "passphrase", as opposed to the term "password". This is
intentional. Providing a phrase containing multiple words to increase the security of your data is important.

What Kinds of Block Devices Can Be Encrypted?


Most types of block devices can be encrypted using LUKS. From anaconda you can encrypt partitions,
LVM physical volumes, LVM logical volumes, and software RAID arrays.

Creating Encrypted Block Devices on the Installed System After


Installation
Encrypted block devices can be created and configured after installation. We will follow our administrator
as they step through the process.

Create and encrypt a block device


The first step is to create the block devices to be encrypted by using commands such as fdisk or lvcreate.
[root@station ~]# lvcreate -L 128M -name data vg.station

rha130-6.1-1

86

Copyright 2011, Red Hat Inc.

Enabling Data Privacy


with Partition Encryption
Logical volume "data" created

Filling the device with random data before encrypting it greatly increases the strength of the encryption.
The downside is that it can take a very long time. Knowing this filesystem will used for very important
data, our administrator decides to place high quality random data on the drive. This will destroy all existing
data on the device and could take several minutes but he continues anyway.
[root@station ~]# dd if=/dev/urandom of=/dev/vg.station/data
dd: writing to `/dev/vg.station/data': No space left on device
262145+0 records in
262144+0 records out

Our administrator is now ready to format the device as a dm-crypt/LUKS encrypted device. The command
below will destroy any existing data on the device.
[root@station ~]# cryptsetup luksFormat /dev/vg.station/data
WARNING!
========
This will overwrite data on /dev/vg.station/data irrevocably.
Are you sure? (Type uppercase yes): YES
Enter LUKS passphrase:
Verify passphrase:

After supplying the passphrase twice the device will be formatted for use. For more information, read the
cryptsetup(8) man page. To verify, our adminstrator uses the following command:
[root@station ~]# cryptsetup isLuks /dev/vg.station/data && echo Success
Success

To see a summary of the encryption information for the device, use the following command:
[root@station ~]# cryptsetup luksDump /dev/vg.station/data
LUKS header information for /dev/vg.station/data
Version:
Cipher name:
Cipher mode:
Hash spec:
Payload offset:
MK bits:
MK digest:
MK salt:
MK iterations:
UUID:

1
aes
cbc-essiv:sha256
sha1
4096
256
bf 6d 13 12 a4 ba 1d 58 5a b1 b5 03 3d d7 ec 6f 2e b2 cd 83
b5 8c d7 94 3c e1 8d ba 09 57 55 5c 2b 44 da ac
64 0d 13 31 5e d7 28 7f 15 85 67 da a2 d1 6c 0b
13250
a5a926a0-57bd-4d35-89c3-f13d58d02e4e

lot 0: ENABLED
Iterations:
Salt:

53490
9f 42 ba 35 08 ed 49 e0 81 c4 e0 1a e0 c1 5f 9d
8b eb da 5c 9d f1 4d 14 29 bc 59 d5 32 24 4a 14
Key material offset: 8
AF stripes:
4000
Key Slot 1: DISABLED
Key Slot 2: DISABLED
Key Slot 3: DISABLED
Key Slot 4: DISABLED
Key Slot 5: DISABLED
Key Slot 6: DISABLED
Key Slot 7: DISABLED

Create a mapping to allow access to the device's decrypted contents


We now have a storage space allocated and encrypted. Next, in order to access the device's decrypted
contents, a mapping must be established using the kernel device-mapper. It is useful to choose a

rha130-6.1-1

87

Copyright 2011, Red Hat Inc.

Enabling Data Privacy


with Partition Encryption
meaningful name for this mapping. Similar to the filesystems we saw earlier in this workbook, LUKS
provides a UUID (Universally Unique Identifier) for each device. This, unlike the device name (eg: /dev/
sda3), is guaranteed to remain constant as long as the LUKS header remains intact. Use the cyptsetup
luksUUID to find the device's UUID.
[root@station ~]# cryptsetup luksUUID /dev/vg.station/data
5a926a0-57bd-4d35-89c3-f13d58d02e4e

An example of a reliable, informative and unique mapping name would be luks-<uuid>, where <uuid>
is replaced with the device's LUKS UUID (eg: luks-50ec957a-5b5a-47ee-85e6-f8085bbc97a8). This
naming convention might seem unwieldy but is it not necessary to type it often. Our administrator,
however, chooses the simple name of luks-data
[root@station ~]# cryptsetup luksOpen /dev/vg.station/data luks-data
Enter passphrase for /dev/vg.station/data:
[root@station ~]# ls -l /dev/mapper/luks-data
lrwxrwxrwx. 1 root root 7 Aug 28 23:02 /dev/mapper/luks-data -> ../dm-1

There should now be a device node, /dev/mapper/luks-data, which represents the decrypted
device. This block device can be read from and written to like any other unencrypted block device. Our
administrator uses a dmsetup to see information about the new device.
[root@station ~]# dmsetup info /dev/mapper/luks-data
Name:
luks-data
State:
ACTIVE
Read Ahead:
256
Tables present:
LIVE
Open count:
0
Event number:
0
Major, minor:
253, 1
Number of targets: 1
UUID: CRYPT-LUKS1-a5a926a057bd4d3589c3f13d58d02e4e-luks-data

Finally, our adminstrator creates an ext4 filesystem and mounts the filesystem to /data
[root@station ~]# mkfs.ext4 /dev/mapper/luks-data
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
32256 inodes, 129024 blocks
6451 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67371008
16 block groups
8192 blocks per group, 8192 fragments per group
2016 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 28 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@station ~]# mkdir /data
[root@station ~]# mount /dev/mapper/luks-data /data
[root@station ~]# df -h /data
Filesystem
Size Used Avail Use% Mounted on
/dev/mapper/luks-data
123M 5.6M 111M
5% /data

rha130-6.1-1

88

Copyright 2011, Red Hat Inc.

Enabling Data Privacy


with Partition Encryption

Add the mapping information to /etc/crypttab


As with the unencrypted filesystem, our administrator can add an entry to the /etc/fstab to use for
mounting the filesystem. However, he also needs to configure the /etc/crypttab file to set up a
mapping for the device. If the file doesn't exist, create it and change the owner and group to root (root:root)
and change the mode to 0744. Add a line to the file with the name of the resulting device, a path to the
underlying block device, and the path the file holding the passphrase or the word none if the device is to
be mounted manually.
[root@station ~]# grep data /etc/fstab
/dev/mapper/luks-data /data ext4 noauto,rw 0 0
[root@station ~]# cat /etc/crypttab
# name device
passphrase
luks-data /dev/vg.station/data none

The <device> field can be given in the form "UUID=<luks_uuid>", where <luks_uuid> is the LUKS
uuid as given by the command cryptsetup luksUUID <device>. This ensures the correct device will be
identified and used even if the device node (eg: /dev/sda5) changes. For details on the format of the
/etc/crypttab file, read the crypttab(5) man page.

Online Exercises
Lab Exercise
Objective: Create and manage an encrypted filesystem.
Estimated Time: 30 mins.

Specification
This lab exercise will continue to use the volume group created in the previous lab exercise. Ensure that
the file ~/lab3.5/disk still exists, and that it still references the correct drive.
1. Create a logical volume data of about 52M in the volume group vg3. You may delete the filesystems
and logical volumes of the previous exercises if space is needed.
2. Encrypt the block device with a passphrase of redhat55
3. Place an ext4 filesystem on the device and mount the filesystem as /encrypted.
4. Configure the /etc/crypttab and /etc/fstab so that the filesystem can be mounted with the
command mount /encrypted after a reboot. Do not mount the filesystem as the system boots.
Reboot the system before grading.

Deliverables
1.

(This exercise has no automated grading element.)

Questions
1.

Which of the following can be encrypted with LUKS?


a.

rha130-6.1-1

ext4 filesystems

89

Copyright 2011, Red Hat Inc.

Enabling Data Privacy


with Partition Encryption
b.

swap partitions

c.

msdos filesystems

d.

Answers A and B

e.

All of the above

2.

Which of the following devices can be encrypted with LUKS?


a.

Physical partitions

b.

NFS mounts

c.

Logical volumes

d.

Answers A and C

e.

All of the above

3.

Which is the correct order for preparing an encrypted device?


a.

Create the partition, format for encryption, unlock the partition, create a filesystem, mount
the filesystem

b.

Create the partition, create a filesystem, encrypt the filesystem, unlock the partition, mount
the filesystem

c.

Create the partition, unlock the partion, create the filesystem, mount the filesystem, encrypt
the filesystem

d.

Create the partition, create the filesystem, mount the filesystem, unlock the partition, encrypt
the filesystem

e.

Create the partition, format for encryption, mount the device, create the filesystem, unlock
the filesystem

4.

Which command encrypts the device?


a.

cryptsetup formatLuks

b.

cryptsetup lockLuks

c.

cryptsetup luksFormat

d.

cryptsetup isLuks

e.

cryptsetup luksClose

5.

rha130-6.1-1

Which command is used to manually unlock an encrypted device?


a.

cryptsetup luksFormat

b.

cryptsetup unlockLuks

c.

cryptsetup luksUnlock

d.

cryptsetup luksOpen

90

Copyright 2011, Red Hat Inc.

Enabling Data Privacy


with Partition Encryption
e.
6.

rha130-6.1-1

cryptsetup isLuks
What file holds the mappings for encrypted devices?

a.

/etc/crypttab

b.

/etc/lukstab

c.

/etc/fstab

d.

/etc/sysconfig/crypt

e.

/etc/sysconfig/luks

91

Copyright 2011, Red Hat Inc.

You might also like