0% found this document useful (0 votes)
39 views

Linux System Administration For Researchers: Chapter 9: Filesystems

The document discusses filesystems and partitions. It begins by explaining that partitions divide a hard disk into sections, traditionally used to hold filesystems. It then covers disk geometry, the CHS addressing scheme, and limitations of MBR partitioning. Logical volume managers and GPT partitioning are introduced as alternatives. The document concludes by demonstrating how to view, create, and modify partitions using fdisk.

Uploaded by

IjazKhan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Linux System Administration For Researchers: Chapter 9: Filesystems

The document discusses filesystems and partitions. It begins by explaining that partitions divide a hard disk into sections, traditionally used to hold filesystems. It then covers disk geometry, the CHS addressing scheme, and limitations of MBR partitioning. Logical volume managers and GPT partitioning are introduced as alternatives. The document concludes by demonstrating how to view, create, and modify partitions using fdisk.

Uploaded by

IjazKhan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Linux System Administration for Researchers

Chapter 9: Filesystems

This time we'll talk about filesystems. We'll start out by


looking at disk partitions, which are the traditional
places to put filesystems. Then we'll take a look at
logical volumes, which are an abstraction that
moves us away from physical disk partitions.

Part 1: Partitions

A partition is just a section of a hard disk. We'll look at


why we'd want to chop up a hard disk into partitions,
but we'll start by looking at the structure of a hard
disk.

Disk Geometry:
Disks are made of stacks of spinning
platters, each surface of which is read by
an independent read head.
Originally, the position of a piece of data
on a disk was given by the coordinates
C,H and S, for Cylinder, Head and
Sector.

Block, or
Track Sector

The intersection of a cylinder with a


platter surface is a Track.
The intersection of a sector with a track
is a Block. Confusingly, the terms
Track Sector or just Sector are also
often used to refer to blocks.
Today, the CHS coordinates don't really
refer to where the data is actually
located on the disk. They're just
abstractions. A more recent coordinate
scheme, Logical Block Addressing
(LBA) just numbers the blocks on the
disk, starting with zero.

Each block is typically 512


bytes.

The CHS coordinate system began with floppy disks, where the
(c,h,s) values really told you where to find the data. Some
reasons CHS doesn't really tell you where the data is on a
modern hard disk:


As disks became smarter, they began transparently hiding bad


blocks and substituting good blocks from a pool of spares.
Modern disks actually increase the number of blocks per track
for the outer cylinders, since there's more space in those
tracks.
These disks also try to optimize I/O performance, so they want
to choose where to really put the data.
You can have arrays of disks (e.g. RAID) that appear (to the
operating system) to be one disk.
The same addressing scheme can be applied to non-disk
devices, like solid-state disks.

If (c,h,s) is hard to grasp, realize that it's just equivalent to (r,z,).


They're coordinates in a cylindrical coordinate system.

Partitions:
Sometimes, it's useful to split up a disk into smaller pieces, called
partitions. Some motivations for this are:
The operating system may not be able to use storage devices as
large as the whole disk.

You may want to install multiple operating systems.

You may want to designate one partition as swap space.

You may want to prevent one part of your storage from filling up the
whole disk.

One potential problem with having multiple partitions on a disk is that


partitions are generally difficult to re-size after they are created.
4

Each disk will have at least one partition. Note that


you can only have up to four primary partitions. We'll
talk about how to get around the 2 Terabyte size limit
later.
The MBR also contains a few other values, like a disk
signature, but you can see by adding up the
numbers that the boot code and the parition table
make up the bulk of the MBR.
In LBA coordinates, the MBR is LBA=0.

In practice, you seldom see disks with more than half a


dozen partitions. These days, the typical desktop
Linux computer's disk has only two or three.

EFI and GUID Partition Tables:


The successor to the PC BIOS is called Extensible
Firmware Interface (EFI). Currently, Intel-based Macintosh
computers are the only common computers that use EFI
instead of a BIOS, but it may become more common as
time goes by.
Instead of an MBR-based partition table, EFI uses a
different scheme, called a GUID Partition Table (GPT).
GPT uses 8 bytes to store addresses, so the maximum
theoretical size of partitions is about 9.4 Zettabytes (9.4
billion TB). That should hold us for the near future.

You can create GPT partition tables using GNU parted.


See the following page for an example:
http://www.cyberciti.biz/tips/fdisk-unable-to-create-partition-greater-2tb.html

Compare this with the way we'll use fdisk, later, to


create partitions.
Note that Windows (up through Windows 7, at least)
only supports GPT partition tables under 64-bit
versions of the operating system, and then only
when running on a computer with EFI instead of a
BIOS. Linux can use GPT on any computer, under
either 32- or 64-bit versions.
Note that there are some special considerations when
booting a computer from a GPT-partitioned disk.
See the following for details:
http://www.rodsbooks.com/gdisk/booting.html

Disk and Partition Files in /dev:


In Linux, each whole disk drive or partition is represented by a special file in
the /dev directory. Programs manipulate the disks and partitions by using
these special files. The files have different names, depending on the type of
disk.

SATA, SCSI, USB or Firewire Disks:


These disks are represented by files named /dev/sd[a-z]. They're
named in the order they're detected at boot time. Partitions have
names like sda1, sda2, etc.

IDE/PATA Disks:
These disks are represented by files named /dev/hd[a-z]. The disk
names will be:


hda -- Master disk on the 1st IDE channel.


hdb -- Slave disk on the 1st IDE channel.
 hdc -- Master disk on the 2nd IDE channel.
 hdd -- Slave disk on the 2nd IDE channel.
...etc.

Non-logical partitions on each disk are numbered sequentially, 1


through 4. Logical partitions are numbered beginning with 5.

Note that you're unlikely to encounter an IDE/PATA


disk these days.

Part 2: Manipulating Partitions

Now we'll look at how to create and otherwise


manipulate partitions on a disk. This can be
dangerous work. You always need to be careful
about which disk you're working on. I've tried to
indicate clearly which commands require special
caution.

Viewing Partitions with fdisk:


You can use the fdisk -l command to view the partition layout on a
disk:
[root@demo ~]# fdisk -l /dev/sda
Disk /dev/sda: 160.0 GB, 160000000000 bytes
255 heads, 63 sectors/track, 19452 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot
/dev/sda1
*
/dev/sda2

Start
1
14

End
13
19452

Blocks
104391
156143767+

Partition Type
Id
83
8e

System
Linux
Linux LVM

Near the top, you can see the number of heads, sectors and cylinders. These
may not represent physical reality, but they're the way the disk presents itself
to the operating system.
Fdisk reports the size of each partition in 1024-byte blocks. The two
partitions above are about 100 MB and about 156 GB. The + sign on the
size of the second partition means that its size isn't an integer number of
1024-byte blocks.
The start and end values are in units of cylinders, by default. You can use
the -u switch to cause fdisk to display start and end in terms of 512-byte 10
track sectors.

To add to the confusion about terms like block and


sector, fdisk uses a size of 1024 bytes (not 512)
when it reports the number of blocks in a partition.
The Linux kernel uses blocks of this size, and many
Linux programs will assume that a block is 1024
bytes. Filesystems typically use blocks of 1024,
2048 or 4096 bytes.
The disk-drive industry is currently pushing new
standards that would make the on-disk block size
4096 bytes.

Partition Types:
Here's the list of partition types that fdisk knows about. The most
common ones are highlighted.
0
1
2
3
4
5
6
7
8
9
a
b
c
e
f
10
11
12
14
16
17
18
1b
1c

Empty
FAT12
XENIX root
XENIX usr
FAT16 <32M
Extended
FAT16
HPFS/NTFS
AIX
AIX bootable
OS/2 Boot Manag
W95 FAT32
W95 FAT32 (LBA)
W95 FAT16 (LBA)
W95 Ext'd (LBA)
OPUS
Hidden FAT12
Compaq diagnost
Hidden FAT16 <3
Hidden FAT16
Hidden HPFS/NTF
AST SmartSleep
Hidden W95 FAT3
Hidden W95 FAT3

1e
24
39
3c
40
41
42
4d
4e
4f
50
51
52
53
54
55
56
5c
61
63
64
65
70
75

Hidden W95 FAT1


NEC DOS
Plan 9
PartitionMagic
Venix 80286
PPC PReP Boot
SFS
QNX4.x
QNX4.x 2nd part
QNX4.x 3rd part
OnTrack DM
OnTrack DM6 Aux
CP/M
OnTrack DM6 Aux
OnTrackDM6
EZ-Drive
Golden Bow
Priam Edisk
SpeedStor
GNU HURD or Sys
Novell Netware
Novell Netware
DiskSecure Mult
PC/IX

80
81
82
83
84
85
86
87
88
8e
93
94
9f
a0
a5
a6
a7
a8
a9
ab
b7
b8
bb

Old Minix
Minix / old Lin
Linux swap / So
Linux
OS/2 hidden C:
Linux extended
NTFS volume set
NTFS volume set
Linux plaintext
Linux LVM
Amoeba
Amoeba BBT
BSD/OS
IBM Thinkpad hi
FreeBSD
OpenBSD
NeXTSTEP
Darwin UFS
NetBSD
Darwin boot
BSDI fs
BSDI swap
Boot Wizard hid

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

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

Note that the partition type is just a label. You can put
anything you want into a partition of any type. The
partition type designation just provides the operating
system with clues about what to expect.

Creating Partitions with fdisk:


You can use fdisk to create or delete partitions on a disk. If you type fdisk
/dev/sda, for example, you'll be dropped into fdisk's command-line
environment, where several simple one-character commands allow you to
manipulate partitions on the disk.
Some fdisk commands:

[root@demo ~]# fdisk /dev/sdb

p Print the partition table

Command (m for help): n

n Create a new partition

Command action
e
extended
p
primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-9726, default 1): +
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-9726,
default 9726): +40G

d Delete a partition
t Change a partition's
type
q Quit without saving
changes
w Write the new partition
table and exit

Command (m for help): p


Disk /dev/sdb: 80.0 GB, 80000000000 bytes
255 heads, 63 sectors/track, 9726 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot
Note: In fdisk, the term primary
System
partition means one that's not an /dev/sdb1
extended partition.

Start

End

1 4864

Blocks
39070048+

Id
83

Linux
12

Notice that nothing you do in fdisk is actually written to


the disk until you type w. If you decide you've
made a mistake, you can always quit without saving
anything by typing q.

Changing a Partition's Type:


Here's how to change a partition's type, using fdisk. In this example,
we change the partition from the default type (Linux) to mark it as a
swap partition.
Command (m for help): p
Disk /dev/sdb: 80.0 GB, 80000000000 bytes
255 heads, 63 sectors/track, 9726 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot
/dev/sdb1
/dev/sdb2

Start
1
4865

End
4864
9726

Blocks
39070048+
39054015

Id
83
83

System
Linux
Linux

Command (m for help): t


Partition number (1-4): 2
Hex code (type L to list codes): 82
Changed system type of partition 2 to 82 (Linux swap / Solaris)
Command (m for help): p
Disk /dev/sdb: 80.0 GB, 80000000000 bytes
255 heads, 63 sectors/track, 9726 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot
/dev/sdb1
/dev/sdb2

Start
1
4865

End
4864
9726

Blocks
39070048+
39054015

Id
83
82

System
Linux
13
Linux swap / Solaris

Formatting a Swap Partition:


Before a swap partition can be used, it needs to be formatted. You
can do this with the mkswap command:
WARNING!

[root@demo ~]# mkswap /dev/sdb2


Setting up swapspace version 1, size=39054015 kB
WARNING!

Note that this will re-format the designated partition immediately,


without asking for confirmation, so be careful!
To start using the new swap space immediately, use the swapon
command:
[root@demo ~]# swapon /dev/sdb2
As we'll see later, you can also cause this swap partition to be used
automatically, at boot time.
14

Here's one of those very dangerous commands.


Please make sure you point mkswap at the right disk
partition.

Saving Partition Layout with sfdisk:


You can save a partition layout into a file, so that it can later be restored.
One way to do this is the sfdisk command. For example, this
command will save the disk partitioning information into the file hda.out:
[root@demo ~]# sfdisk -d /dev/hda > hda.out
If the disk is replaced later, or if you have another identical disk that
you want to partition in the same way, you can use this command:
WARNING!

[root@demo ~]#

sfdisk /dev/hda < hda.out


WARNING!

Note that this command should be used very carefully, since it will
(without asking for confirmation) wipe out any existing partition table
on the disk. The content of hda.out looks like this:
# partition table of /dev/hda
unit: sectors
/dev/hda1
/dev/hda2
/dev/hda3
/dev/hda4

:
:
:
:

start=
start=
start=
start=

63,
208845,
0,
0,

size=
208782,
size=312287535,
size=
0,
size=
0,

Another dangerous command.

Id=83, bootable
Id=8e
Id= 0
Id= 0

15

Part 3: Filesystem
Structure

16

In order to understand filesystems, it's important to


have a little knowledge about how they're laid out on
disk. Terms like superblock and block group
show up in error messages sometimes, and knowing
what they mean can save you a lot of grief. Here's a
primer on filesystem structure.

What is a Filesystem?
A filesystem is a way of organizing data on a block device. The filesystem
organizes data into files, each of which has a name and other metadata
attributes. These files are grouped into hierarchical directories, making it
possible to locate a particular file by specifying its name and directory path.
Some of the metadata typically associated with each file are:
Timestamps, recording file creation or modification times.
Ownership, specifying a user or group to whom the file belongs.

Permissions, specifying who has access to the file.




Linux originally used the minix filesystem, from the operating system of the
same name, but quickly switched to what was called the Extended
Filesystem (in 1992) followed by an improved Second Extended
Filesystem (in 1993). The two latter filesystems were developed by French
software developer Remy Card.
The Second Extended Filesystem (ext2) remained the standard Linux
filesystem until the early years of the next century, when it was supplanted
by the Third Extended Filesystem (ext3), written by Scottish software
developer Stephen Tweedie. Recently, this has been superseded by ext4,17
developed by Ted Ts'o.

Linux also supports many other filesystems, including


Microsoft's VFAT and NTFS, and the ISO9660
filesystem used on CDs and DVDs.
A block device is a device like a disk where you can
directly address individual blocks of data. Linux
separates devices into character devices, which
just read and write streams of bytes, and block
devices, in which parts of the device's storage can
be directly addressed.

How ext2, ext3 and ext4 Work:


The ext2, ext3 and ext4 filesystems are very similar. Each divides a disk
partition into block groups of a fixed size. At the beginning of each block
group is metadata about the filesystem in general, and that block group in
particular. There is much redundancy in this metadata, making it possible to
detect and correct damage to the filesystem.
Disk Partition Data

Block Group 0

Superblock

All Group
Descriptors

Block Group 1

Data
Inode Inode
Bitmap Bitmap Table

Block Group N

Data Blocks
18

This Group's Descriptor

Superblocks:
The ext2/ext3/ext4 filesystem as a whole is described in a chunk of data
called the Superblock. The superblock contains:
a name for the filesystem (a label),
the size of the filesystem's block groups,
 timestamps showing when the filesystem was last mounted,

a flag saying whether it was unmounted cleanly,

a number showing the amount of unused space in the filesystem,



and much other information. The superblock is duplicated at the beginning


of many block groups. Normally, the operating system only uses the copy
at the beginning of block group 0, but if this is lost or damaged, the data can
be recovered from one of the other copies. During normal operation, the
operating system keeps all copies of the superblock synchronized.
Block Group

Superblock

All Group
Descriptors

Data
Inode Inode
Bitmap Bitmap Table

Data Blocks
19

This Group's Descriptor

The superblock is actually duplicated at the beginning


of each block group for ext2 filesytems. For ext3 and
ext4, there's the option of only duplicating it in some
block groups. If this option is turned on (as it is by
default), the superblock is only duplicated in block
groups 0, 1 and powers of 3, 5 and 7.

Inodes and Group Descriptors:


Each file's data is stored in the data blocks section of a block group. Files are
described by records stored in chunks called index nodes (inodes). The inodes
are stored in the inode table in a part of the block group called the group
descriptor. Data in each inode includes:
the file's name,
the file's owner,
 the group to which the file belongs,
 several timestamps,

permission settings for the file,
 pointers to the data blocks that contain the file's data,



and other information. The group descriptors are so important that copies of the
block descriptors for every block group are stored in each block group. Normally,
the operating system only uses the descriptors stored in block group 0 for all block
groups, but if a filesystem is damaged or has been uncleanly unmounted it's
possible to verify the filesystem's integrity and repair damage by using other
copies.
Superblock

All Group
Descriptors

Data
Inode Inode
Bitmap Bitmap Table

Data Blocks
20

This Group's Descriptor

As with the superblock, the operating system normally keeps all of


the copies of a given group's group descriptors in sync.
Directories are also described by inodes. Each inode has a type
that identifies it a a file, a directory, or some other special type of
thing.
The inodes are numbered sequentially, and files can be identified
by their inode number as well as their name.
The data bitmap is a set of ones and zeroes, each corresponding
to one of the blocks in the block group's data section. If a one is
set in the bitmap, that means that this block is used. A zero
means that it's free. The data bitmap lets us know which blocks
we can use.
Similarly, the inode bitmap tells us which entries in the inode
table are free.

The Journal:
Although ext2, ext3 and ext4 are very similar, ext3/4 have one
important feature that ext2 lacks: journaling. We say that ext3/4 are
journaled filesystems because, instead of writing data directly into
data blocks, the filesystem drivers first write a list of tasks into a
journal. These tasks describe any changes that need to be made to
the data blocks.
The operating system then periodically looks at the journal to see if
there are any tasks that need doing. These tasks are then done, in
order, and each completed task is marked as done in the journal.
If the computer crashes, the journal is examined at the next reboot to
see if there were any outstanding tasks that needed to be done. If so,
they're done. Any garbled information left at the end of the journal is
ignored and cleared.
Journaling makes it much quicker to check the integrity of a filesytem
after a crash, since only a few items in the journal need to be looked
21
at. In contrast, when an ext2 filesystem crashes, the operating
system needs to scan the entire filesystem looking for problems.

Other than journaling, ext2 and ext3 are largely the


forward- and backward-compatible. An ext2
filesystem can easily be converted to ext3 by adding
a journal. Going the other way may be possible, too,
if an ext3 filesystem doesn't use any features that
aren't present in an ext2 filesystem. Similar
considerations apply when going between ext3 and
ext4 filesystems.
The journal is described by a special inode, usually
inode number 8.

Inode Structure, and Filesystem Limits:

Some size limits for filesystems:


Size Limits
ext2
2 TB
Max. File Size:
Max. Filesystem Size: 16 TB

ext3
2 TB
16 TB

ext4
16 TB
22
1 EB

Part 4: Filesystem Tools:

23

Now lets look at some tools for creating and


manipulating filesystems.

Making an ext3 or ext4 Filesystem:


To make an ext3 or ext4 filesystem, use one of the mkfs commands:
WARNING!

[root@demo ~]# mkfs.ext4 -Lmydata /dev/sdb1


Use mkfs.ext3 instead, if
you want to make it ext3.

Give it this
label.

Create it on this
partition.

Note that the command above will format (or re-format) the
designated partition without asking for any confirmation. Please
make sure you point it at the partition you really want to format.
The filesystem label can be any text you choose, but usually the label
is chosen to be the same as the name of the location at which you
expect to mount the filesystem. For example, a filesystem intended
to be mounted at /boot, would probably probably be created with
-L/boot. For the / and /boot filesystems, this should always be
done, but it's good practice for other filesystems, too.
24

Another dangerous command.

Example mkfs.ext4 Output:


[root@demo ~]# mkfs.ext4 -Lmydata /dev/sdb1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=mydata
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
121896960 inodes, 487585272 blocks
24379263 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
14880 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200,
884736, 1605632, 2654208,
4096000, 7962624, 11239424, 20480000, 23887872,
71663616, 78675968,
102400000, 214990848
25
Writing inode tables: done.
creating root dir

Note that mke2fs divides the disk up into 14,880 block


groups, but only (!) 18 copies of the superblock are
created. If this were an ext2 filesystem, there would
be 14,880 copies. The total number of inodes
available (including all inodes in all block groups) is
121,896,960. This is the maximum number of files
that this filesystem will hold.
You might notice the reference to fragments per
group in the output above. In this context, a
fragment is a chunk of storage space that's smaller
than a block. This is seldom used, and in this case
you can see that the fragment size is just set to the
block size.

Changing the Attributes of a Filesystem:


The tune2fs command can be used to change the attributes of an
ext2/ext3/ext4 filesystem after it has been created. For example, to
change the filesystem's label:
[root@demo ~]# tune2fs -L/data /dev/sdb1
Some other useful things that tune2fs can do:
-l List superblock information.
-c Set the maximum mount count for the filesystem, after which a
filesystem check will occur (0 = never check).
-i Set the interval between filesystem checks (0 = never check).

26

Changing a filesystem's label is perfectly safe. It won't


cause you to lose any data. (But it might cause
confusion if you're already referring to the old label
somewhere.) The same is true for the other flags
listed above.

Looking at Filesystem Metadata:


tune2fs -l will show you a filesystem's superblock information:
[root@demo ~]# tune2fs -l /dev/sda1
tune2fs 1.39 (29-May-2006)
Filesystem volume name:
/boot
Filesystem state:
clean
Inode count:
26104
Block count:
104388
Reserved block count:
5219
Free blocks:
55562
Free inodes:
26037
First block:
1
Block size:
1024
Blocks per group:
8192
Inodes per group:
2008
Inode blocks per group:
251
Filesystem created:
Mon Sep 10 10:58:16
Last mount time:
Fri Dec 26 10:23:03
Last write time:
Fri Dec 26 10:23:03
Mount count:
60
Maximum mount count:
-1
Last checked:
Mon Sep 10 10:58:16
Check interval:
0 (<none>)
Reserved blocks uid:
0 (user root)
Reserved blocks gid:
0 (group root)
First inode:
11
Inode size:
128
Journal inode:
8
etc...

2007
2008
2008

2007

27

You can see this plus block group information by using the dumpe2fs command.

Note the mount count, maximum mount count, last


checked and check interval entries. We'll see later
that the fsck command uses these.

Checking a Filesystem:
[root@demo ~]# fsck /dev/sdb1
If a computer loses power unexpectedly, the filesystems on its disks may be left in
an untidy state. The filesystem check (fsck) command looks at ext2/ext3/ext4
filesystems and tries to find and repair damage. Fsck can only be run on
unmounted filesystems.
Each filesystem's superblock contains a flag saying whether the filesystem was
cleanly unmounted. If it was, fsck just exits without doing anything further.
If the filesystem wasn't cleanly unmounted, fsck checks it. Under ext3/ext4, fsck
first just looks at the journal and completes any outstanding operations, if possible.
If this works, then fsck exits.
If the ext3/ext4 journal is damaged, or if this is an ext2 filesystem, fsck scans the
filesystem for damage. It does this primarily by looking for inconsistencies between
the various copies of the superblock and block group descriptors. If
inconsistencies are found, fsck tries to resolve them, using various strategies.
The filesystem's superblock also contains a mount count, maximum mount
count, last check date and check interval. If the mount count exceeds the
maximum, a scan of the filesystem is forced even if it was cleanly unmounted. If
28
the time since the last check date exceeds the check interval, a scan is also forced.
Both of these forced checks can be disabled, by using tune2fs.

Modifying fsck's Behavior:


Some useful fsck options:
-f Force a scan, even if the filesystem appears to have been cleanly
unmounted.
-b Specify an alternative superblock, in case the primary superblock
has been damaged.
-y Answer yes to any questions fsck asks.
-A Check all filesystems.
-C Show a progress bar as fsck works. (It can sometimes take a
very long time.)

29

Fsck is actually a wrapper that calls a different typespecific filesystem checker for each different type of
filesystem that it knows about.

The directory tree of each physical device is grafted


onto the same tree, with the root directory (/) at the
top. There are no C: or D: drives under Linux.
Every file you have access to lives in the same tree,
and you don't need to care what device the file lives
on.

Mounting Filesystems Automatically at Boot Time:


The file /etc/fstab (filesystem table) contains a list of filesystems to
be mounted automatically at boot time. It looks like this:
/etc/fstab
Disk partition
Specified by label
Special filesystems
created by the kernel
Disk partition
(Note that this file also
lists swap partitions.)

/dev/sda1
LABEL=/boot
devpts
tmpfs
proc
sysfs
/dev/sda2

Filesystem

/
/boot
/dev/pts
/dev/shm
/proc
/sys
swap

Mount Point

The dump flag is used by a backup utility called


dump. Filesystems marked with a 1 here will be
backed up by dump.

The fsck order field determines what order


filesystems are checked when fsck is run
automatically at boot time. A value of zero means that
this filesystem won't be checked. Others are checked
in ascending order of these values.

ext3
ext3
devpts
tmpfs
proc
sysfs
swap

defaults
defaults
mode=620
defaults
defaults
defaults
defaults

Type

Options

1
1
0
0
0
0
0

1
2
0
0
0
0
0

dump Flag
fsck Order

31

Among the options settings you can use noauto to


cause the given filesystem not to be automatically
mounted at boot time. In that case, you'd need to
manually mount it later, using the mount command.
If you have a filesystem listed in /etc/fstab, you can
mount it either like this, with two arguments:
mount /dev/sda1 /
or like this, with one argument:
mount /dev/sda1
or
mount /
since /etc/fstab lets mount know what you mean by
these.

Part 5: Logical Volume Management

32

Most current Linux distributions use Logical Volume


Management by default. You'll need to know a little
about LVM to understand how any current Linux
computer's filesystems are laid out.

The LVM System:


The ext2, ext3 and ext4 filesystems are limited by the size of the
partitions they occupy. Partitions are difficult to resize, and they can't
grow beyond the whole size of the disk. What can we do if we need
more space than that for our filesystem?
One solution is the Logical Volume Management (LVM) system. LVM
lets you define logical volumes that can be used like disk partitions.
Unlike partitions, logical volumes can span multiple disks, and they
can easily grow or shrink.
These days, when you install a Linux distribution on a computer,
some of the filesystems that are created will (by default) be on logical
volumes, not physical disk partitions. This makes it important to
understand how LVM works.

33

As we'll see, LVM also provides us with another,


software-based, way to avoid the 2 TB partition limit
imposed by MBR-style partition tables.

Logical Volume Structure:


LVM divides each disk (or physical volume) into chunks called physical extents
(PEs). Disks are added to volume groups (VGs). Each VG is a pool of physical
extents from which logical volumes (LVs) can be formed. An LV can be expanded
by adding more PEs from the pool. If an LV needs to grow even larger, more PEs
can be added to the pool by adding disks to the volume group.
Volume Group

VolGroup00

Physical Volume (PV)

sda
PE

PE

PE

PE

PE

PE

PE

PE

PE

PE

PE

PE

PE

PE

sdb

Physical Extent (PE)

LogVol00
Logical Volume

34

Note that LVM can use either a whole disk or a disk


partition as a physical volume.

Creating Logical Volumes:


First, let's make a new disk available to the LVM system by initializing
it as an LVM physical volume using pvcreate:
[root@demo ~]# pvcreate /dev/sdb
Then, let's create a new volume group and add the newly-initialized
disk to it:
[root@demo ~]# vgcreate VolGroup01 /dev/sdb
Now, let's create a 500 GB logical volume from the pool of space in
our new volume group:
[root@demo ~]# lvcreate -L500G -nLogVol00 VolGroup01
Now we can create a filesystem on the logical volume, just as we'd
use a partition:
[root@demo ~]# mkfs.ext4 -L/data /dev/VolGroup01/LogVol00

Finally, we can mount the logical volume just as we'd mount a partition:
[root@demo ~]# mount /dev/VolGroup01/LogVol00 /data35

Note that you can point pvcreate at either a whole disk,


as above, or a disk partition (like /dev/sdb1). If you
use a whole disk, the disk's partition table is wiped
out, since LVM doesn't need it. Thus, LVM can be
used to completely avoid the 2 TB limit imposed by
MBR-style partition tables.
This may be one of the reasons current distributions
are using LVM by default. Disks are rapidly
approaching 2 TB in size, and it looks like most nonMac computers are going to be using the BIOS/MBR
architecture for a while, rather than moving to
EFI/GPT. LVM provides a way to support large disks
without any hardware changes.

Examining Volume Groups:


You can find out about a volume group by using the vgdisplay command:
[root@demo ~]# vgdisplay VolGroup00
--- Volume group --VG Name
VolGroup00
System ID
Format
lvm2
Metadata Areas
1
Metadata Sequence No 3
Notice these. They tell
VG Access
read/write
you how many physical
VG Status
resizable
extents are in the volume
MAX LV
0
group, and how many are
Cur LV
2
Open LV
2
still available for making
Max PV
0
new logical volumes.
Cur PV
1
Act PV
1
VG Size
148.91 GB
PE Size
32.00 MB
Total PE
4765
Alloc PE / Size
4765 / 148.91 GB
Free PE / Size
0 / 0
VG UUID
blHfoy-z03Z-DzTQ-PH4p-uYfJ-jkHS-29Hxob

If you move a disk to a different computer that already has a volume group
with the same name, you may need to use the UUID of the volume groups to
36
rename one of them. Use vgrename for this.

Growing a Logical Volume:


If we don't have any free PEs in our volume group, we can add
another disk:
[root@demo ~]# vgextend VolGroup01 /dev/sdc
Now that we have more PEs, we can assign some of them to one of
our existing logical volumes, to make it bigger:
[root@demo ~]# lvextend -L+100G /dev/VolGroup01/LogVol00

Extending the logical volume doesn't extend the filesystem on top of


it. We have to do that by hand. For ext2/ext3/ext4 filesystems, you
can use the resize2fs command to do this. The command below will
just resize the filesystem so that it occupies all of the available space
in the logical volume:
[root@demo ~]# resize2fs /dev/VolGroup01/LogVol00

For many more stupid LVM tricks see: http://www.howtoforge.com/linux_lvm


37

The End

38

Thank You!

You might also like