Documente Academic
Documente Profesional
Documente Cultură
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
rha130-6.1-1
rha130-6.1-1
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.
rha130-6.1-1
The above figure diagrams a disk partitioned using DOS partitioning, including the features outlined below.
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.
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
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.
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.
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
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
Start
2048
12290048
54233088
End
12290047
54233087
68569087
Blocks
6144000
20971520
7168000
Id System
b Win95 FAT32
7 HPFS/NTFS
83 Linux
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
Column
Function
MS/DOS bootloader), fdisk does report and allow the user to set the bootable
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
System
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.
Label
Role
83
Linux
82
Linux Swap
8e
Linux LVM
fd
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.
rha130-6.1-1
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.
Function
(d)elete partition
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
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
Four different formats can be used when specifying the ending sector.
Format
Interpretation
+n
+nM
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
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
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
rha130-6.1-1
Empty
FAT12
1c
1e
Hidden Win95 FA 70
Hidden Win95 FA 75
12
DiskSecure Mult bb
PC/IX
be
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
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
The partition label, which was "Linux", has changed to "Linux swap".
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
/dev/sda4
/dev/sda5
/dev/sda6
68569088
68571136
69621760
251658239
69619711
71718911
91544576
524288
1048576
5
82
83
Extended
Linux
Linux
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
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.
rha130-6.1-1
14
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
Start
End
Blocks
Id
System
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)
rha130-6.1-1
16
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
Online Exercises
Lab Exercise
Objective: Use fdisk to add new partitions.
rha130-6.1-1
17
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
d.
e.
2.
/dev/sda7
b.
/dev/sdb5
c.
/dev/sdb4
d.
/dev/sdc8
e.
3.
/dev/sda3
b.
/dev/sda5
c.
/dev/sdb1
d.
/dev/sdb4
e.
4.
fdisk --dump
b.
dumppart
c.
fdisk -cul
d.
fdisk --list
e.
5.
Which of the following commands could be used to add the partition /dev/sda8?
a.
fdisk -a /dev/sda8
b.
c.
fdisk -l /dev/sda8
d.
e.
rha130-6.1-1
19
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.
7.
b.
420 megabytes
c.
8.4 gigabytes
d.
84 megabytes
e.
8.
The administrator wants to use the space is left unallocated on the drive /dev/sdb. What options
are avaialble?
a.
b.
c.
One or more new logical partitions can be created with the remaining space
d.
e.
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.
b.
c.
d.
e.
Either A or C
20
10.
rha130-6.1-1
b.
c.
d.
e.
21
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.
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
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.
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
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
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
jfs
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.
rha130-6.1-1
24
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.
Effect
-b blocksize
-c
-i density
-L label
-m n
-N number
-j
-t type
The command line switches introduce some filesystem concepts which are addressed below.
rha130-6.1-1
25
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.
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.
rha130-6.1-1
26
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
rha130-6.1-1
27
...
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
...
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
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.
rha130-6.1-1
29
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
...
rha130-6.1-1
30
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.
rha130-6.1-1
31
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
ext3
b.
iso9660
32
c.
reiserfs
d.
vfat
e.
2.
mkfs.ext3
b.
mkfs -t ext3
c.
mke2fs -j
d.
mkfs.ext2 -j
e.
3.
Which of the following command lines could be used to create an ext2 filesystem with a blocksize
of 2048 bytes?
a.
b.
mke2fs /dev/sda4
c.
d.
e.
A and B
rha130-6.1-1
33
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.
b.
c.
d.
e.
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.
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.
7.
rha130-6.1-1
Which of the following command lines would create a filesystem with a volume label of data?
a.
b.
c.
34
d.
e.
8.
Which of the following command lines would create an ext4 filesystem with a block size of 4096?
a.
b.
mkfs.ext4 -b 4k /dev/sda1
c.
d.
e.
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.
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.
b.
When convenient, he should unmount the partition, and perform filesystem maintenance.
c.
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
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.
rha130-6.1-1
36
Mounting Filesystems
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.
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.
rha130-6.1-1
37
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.
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.)
rha130-6.1-1
38
Mounting Filesystems
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.
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
Mounting Filesystems
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
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
Mounting Filesystems
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.
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
rha130-6.1-1
41
Mounting Filesystems
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
Mounting Filesystems
Questions
1.
b.
c.
d.
e.
2.
Which of the following command lines would mount the device /dev/sda3, formatted with the
vfat filesystem, to the mount point /dosc?
a.
b.
c.
d.
e.
3.
mtime
b.
noatime
c.
noexec
d.
ro
e.
4.
rha130-6.1-1
/dev/sda1
b.
/dev/sda7
c.
/dev/sda5
43
Mounting Filesystems
d.
/dev/sda3
e.
5.
/dev/sda3
b.
/dev/sda7
c.
/dev/sda1
d.
/dev/sda5
e.
6.
When accessing the file /var/www/html/index.html, how many partitions does the kernel
involve?
a.
b.
c.
d.
e.
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.
d.
e.
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
Mounting Filesystems
e.
9.
a.
b.
c.
d.
e.
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.
45
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.
/
/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
/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"
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.
Role
/dev/sda6
/home
ext4
defaults
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
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.
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
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.
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
/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
/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)
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
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.
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.
d.
e.
A and B only
51
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.
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.
5.
b.
c.
d.
e.
6.
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.
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
d.
fuser
e.
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.
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.
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.
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
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
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.
rha130-6.1-1
55
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
Like creating a filesystem, initializing swap space with the mkswap command is a one time action.
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.
rha130-6.1-1
56
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
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
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
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.
/proc/swaps
b.
/etc/fstab
c.
/var/state/swap
d.
/etc/swaptab
e.
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.
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.
d.
fdisk /dev/sda3
e.
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.
59
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.
b.
c.
d.
e.
8.
swapstop
b.
umount
c.
uswap
d.
swapoff
e.
9.
Which of the following command lines could be used to activate a swap partition with a priority
of 3?
a.
b.
swap /dev/sda3 3
c.
d.
swapon -p 3 /dev/sda3
e.
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
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.
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.
rha130-6.1-1
61
/sbin/fsck.vfat
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".
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
-L name
Set the volume name to name. This option is identical in function to the e2label
command.
-m 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
Miscellaneous Filesystem
Management Commands
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.
rha130-6.1-1
63
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)
/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
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.
c.
fsck.ext2 /dev/sda3
d.
e2fsck /dev/sda3
e.
2.
Which of the following command lines could be used to assign the label /data to an ext2 filesystem?
a.
b.
c.
d.
e.
rha130-6.1-1
Which of the following commands could you get away with not using?
a.
e2label
b.
mkswap
65
Miscellaneous Filesystem
Management Commands
c.
fdisk
d.
mount
e.
4.
What is the most reasonable ordering for the commands you will use?
a.
b.
c.
d.
e.
5.
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.
b.
c.
d.
e.
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.
c.
d.
e.
66
Miscellaneous Filesystem
Management Commands
a.
swapoff /dev/sdd3
b.
fsck.vfat /dev/sdd3
c.
fisk /dev/sdd3
d.
fsck /dev/sdd3
e.
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.
b.
c.
d.
e.
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
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.
rha130-6.1-1
68
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.
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
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.
rha130-6.1-1
70
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
rha130-6.1-1
71
rha130-6.1-1
72
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
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
rha130-6.1-1
73
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
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
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.
rha130-6.1-1
75
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
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.
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
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.
rha130-6.1-1
78
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
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
79
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.
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
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
rha130-6.1-1
80
[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
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.
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.
3.
mkpv
b.
mklvm -p
c.
pvmake
d.
pvcreate
e.
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
c.
vgadmin
d.
pvcreate
e.
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.
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.
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
Alloc PE / Size
Free PE / Size
VG UUID
8.
4 megabytes
b.
37.15 gigabytes
c.
0.25 megabytes
d.
8 megabytes
e.
9.
b.
c.
d.
A and B
e.
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
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.
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
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!
rha130-6.1-1
86
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
rha130-6.1-1
87
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
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.
Questions
1.
rha130-6.1-1
ext4 filesystems
89
swap partitions
c.
msdos filesystems
d.
Answers A and B
e.
2.
Physical partitions
b.
NFS mounts
c.
Logical volumes
d.
Answers A and C
e.
3.
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.
cryptsetup formatLuks
b.
cryptsetup lockLuks
c.
cryptsetup luksFormat
d.
cryptsetup isLuks
e.
cryptsetup luksClose
5.
rha130-6.1-1
cryptsetup luksFormat
b.
cryptsetup unlockLuks
c.
cryptsetup luksUnlock
d.
cryptsetup luksOpen
90
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