LPI Linux Certification in a Nutshell (26 page)

Read LPI Linux Certification in a Nutshell Online

Authors: Adam Haeder; Stephen Addison Schneiter; Bruno Gomes Pessanha; James Stanger

Tags: #Reference:Computers

BOOK: LPI Linux Certification in a Nutshell
11.8Mb size Format: txt, pdf, ePub
Name

mkswap

Syntax
mkswap
device
Description

Prepare a partition for use as swap space. This
command can also set up swap space in a file on another
filesystem.

Example

On an existing partition, which should be set to type 82
(Linux swap), ready swap space:

#
mkswap /dev/hda5
Setting up swapspace version 1, size = 139792384 bytes
#
Note

Running any of the filesystem creation programs is, like
fdisk
, potentially dangerous. All data in
any previously existing filesystems in the specified partition
will be deleted. Since
mkfs
does not warn
you prior to creating the filesystem, be certain that you are
operating on the correct partition.

On the Exam

The exam is likely to contain general questions about
using
mkfs
and
mkswap
,
although details such as inode allocation are beyond the scope
of the LPIC Level 1 exams.

Objective 2: Maintain the Integrity of Filesystems

Over the course of time, active filesystems can develop
problems, such as:

  • A filesystem fills to capacity, causing programs or perhaps the
    entire system to fail.

  • A filesystem is corrupted, perhaps due to a power failure or
    system crash.

  • A filesystem runs out of inodes, meaning that new filesystem
    objects cannot be created.

Carefully monitoring and checking Linux filesystems on a regular
basis can help prevent and correct these types of problems.

Monitoring Free Disk Space and Inodes

A read/write filesystem isn’t much good if it grows to the
point where it won’t accept any more files. This could happen if the
filesystem fills to capacity or runs out of
inodes
.

Inodes are the data structures within filesystems that describe
files on disk. Every filesystem contains a finite number of inodes, set
when the filesystem is created. This number is also the maximum number
of files that the filesystem can accommodate. Because filesystems are
created with a huge number of inodes, you’ll probably never create as
many files as it would take to run out of inodes. However, it is
possible to run out of inodes if a partition contains many small
files.

It is important to prevent space and inode shortages from
occurring on system partitions. The
df
command gives you the information
you need on the status of both disk space utilization and inode
utilization.

Monitoring Disk Usage

Have you ever found yourself wondering, “Where did all the disk
space go?” Some operating systems make answering this question
surprisingly difficult using only native tools. On Linux, the
du
command can help display disk
utilization information on a per-directory basis and perhaps answer that
question.
du
recursively examines directories and
reports detailed or summarized information on the amount of space
consumed.

Modifying a Filesystem

There are many cases where an administrator might want to
make changes to an existing filesystem. For example, if the purpose of a
particular filesystem changes, the volume label should be changed to
match. This and many other
ext2
filesystem settings
can be viewed and modified using the
tune2fs
command.

Checking and Repairing Filesystems

No matter how stable, computers do fail, even due to
something as simple as a power cable being accidentally unplugged.
Unfortunately, such an interruption can make a mess of a filesystem. If
a disk write operation is aborted before it completes, the data in
transit could be lost, and the portions of the disk that were allocated
for it are left marked as used. In addition, filesystem writes are
cached in memory, and a power loss or other crash prevents the kernel
from synchronizing the cache with the disk. Both of these scenarios lead
to inconsistencies in the filesystem and must be corrected to ensure
reliable operation.

Filesystems are checked with
fsck
. Like
mkfs, fsck
is a frontend to filesystem-specific
utilities, including
fsck.ext2
, which is a link to
the
e2fsck
program. (See its manpage for
detailed information.)

Note

e2fsck
can also check
ext3
filesystems. When it finds an
ext3
filesystem that was not cleanly unmounted,
it first commits the journal, then checks the filesystem as it
normally would with
ext2
.

Part of the information written on disk to describe a filesystem
is known as the
superblock
, written in block 1 of the
partition. If this area of the disk is corrupted, the filesystem is
inaccessible. Because the superblock is so important, copies of it are
made in the filesystem at regular intervals, by default every 8192
blocks. The first superblock copy is located at block 8193, the second
copy is at block 16385, and so on. As you’ll see,
fsck
can use the information in the superblock
copies to restore the main superblock.

Name

df

Syntax
df [
options
] [
file
[
file
...]]
Description

Display overall disk utilization information for mounted
filesystems on
file
. Usually,
file
is a device file for a partition,
such as
/dev/hda1
. The
file
may also be the mount point or any
file beneath the mount point. If
file
is
omitted, information for mounted filesystems on all devices in
/etc/fstab
are displayed.

Frequently used options
-h

Displays results in a human-readable format, including
suffixes such as
M
(megabytes) and
G
(gigabytes).

-i

Displays information on remaining inodes rather than the
default disk space information.

Example 1

Check disk space utilization on all filesystems:

#
df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 387M 56M 311M 15% /
/dev/sda5 296M 5.2M 276M 2% /boot
/dev/sda9 1.9G 406M 1.4G 22% /home
/dev/sda6 53M 12M 39M 23% /root
/dev/sda10 99M 104k 93M 0% /tmp
/dev/sda8 972M 507M 414M 55% /usr
/dev/sda7 296M 9.3M 272M 3% /var

This example shows that of the seven filesystems mounted by
default, none exceeds 55 percent capacity.

Example 2

Check the same filesystems for inode utilization:

#
df -i
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/sda1 102800 7062 95738 7% /
/dev/sda5 78312 29 78283 0% /boot
/dev/sda9 514000 934 513066 0% /home
/dev/sda6 14056 641 13415 5% /root
/dev/sda10 26104 60 26044 0% /tmp
/dev/sda8 257040 36700 220340 14% /usr
/dev/sda7 78312 269 78043 0% /var

Among these partitions, the largest consumption of inodes is a
mere 14 percent. It is clear that none of the filesystems is
anywhere near consuming the maximum number of inodes available. Note
that the
/usr
partition (with 14 percent of
inodes used) has used 55 percent of the disk space. With utilization
like this, the
/usr
volume will most likely
fill to capacity long before the inodes are exhausted.

Example 3

Quickly determine which partition the current working
directory (represented simply by a single dot) is located:

#
df .
Filesystem 1k-blocks Used Available Use% Mounted on
/dev/sda1 102800 7062 95738 7% /

When a filesystem is nearing capacity, files may simply be
deleted to make additional space available. However, in the rare
case in which an inode shortage occurs, the filesystem must be
recreated with a larger number of inodes unless a significant number
of files can be deleted.

Name

du

Syntax
du [
options
] [
directories
]
Description

Display disk utilization information for
directories
. If
directories
are omitted, the current
working directory is searched.

Frequently used options
-a

Shows all files, not just directories.

-c

Produces a grand total for all listed items.

-h

Displays results in a human-readable format, including
suffixes such as
M
(megabytes) and
G
(gigabytes).

-s

Prints a summary for each of the
directories
specified, instead of
totals for each subdi
rectory
found
recursively.

-S

Excludes subdirectories from counts and totals, limiting
totals to
directories
.

Example 1

Examine disk utilization in
/etc/rc.d
:

#
du /etc/rc.d
882 /etc/rc.d/init.d
1 /etc/rc.d/rc0.d
1 /etc/rc.d/rc1.d
1 /etc/rc.d/rc2.d
1 /etc/rc.d/rc3.d
1 /etc/rc.d/rc4.d
1 /etc/rc.d/rc5.d
1 /etc/rc.d/rc6.d
904 /etc/rc.d
Example 2

Display utilization by files in
/etc
,
including subdirectories beneath it:

#
du -s /etc
13002 /etc
Example 3

Display utilization by files in
/etc
, but
not in subdirectories beneath it:

#
du -Ss /etc
1732 /etc
Example 4

Show a summary of all subdirectories under
/home
, with human-readable output:

#
du -csh /home/*
42k /home/bsmith
1.5M /home/httpd
9.5M /home/jdean
42k /home/jdoe
12k /home/lost+found
1.0k /home/samba
11M total

This result shows that 11 MB of total disk space is
used.

Example 5

Show the same summary, but sort the results to display in
order of largest to smallest disk utilization:

#
du -cs /home/* | sort -nr
11386 total
9772 jdean
1517 httpd
42 jdoe
42 bsmith
12 lost+found
1 samba

This result shows that user
jdean
is
consuming the largest amount of space. Note that the human-readable
format does not sort in this way, since
sort
is unaware of the
human-readable size specifications.

Name

tune2fs

Syntax
tune2fs [
options
]
device
Description

Modify tunable parameters on the
ext2
or
ext3
filesystem on
device
.

Frequently used options
-l
device

List the tunable parameters on
device
.

-c
n

Set the maximum mount count to
n
. When the filesystem has been
mounted this many times, the kernel will warn that the
filesystem has exceeded the maximum mount count when the
filesystem is mounted, and
e2fsck
will
automatically check the filesystem. See the discussion of
e2fsck
in the next section,
Checking and Repairing Filesystems
.

Setting this value to
0
tells the kernel and
e2fsck
to ignore the mount count.

-i
n

Set the maximum time between two filesystem checks to
n
. If
n
is a number or is followed by
d
, the value is in days. A value
followed by
w
specifies
weeks. A value followed by
m
specifies months.

The time since the last filesystem check is compared to
this value by the kernel and
e2fsck -p
,
much like the maximum mount count. A value of
0
disables this check.

-L
label

Sets the volume label of the filesystem to
label
. The volume label can also be
set with the
e2label
command.

-j

Adds an
ext3
journal file to the
filesystem and sets the
has_journal
feature flag.

-m
n

Sets the reserved block percentage to
n
. By default,
ext2
filesystems reserve 5 percent of the
total number of available blocks for the
root
user. This means that if a
filesystem is more than 95 percent full, only
root
can write to it. (It also means that
df
will report the filesystem as 100
percent full when it is really only 95 percent full.)

On very large filesystems, or filesystems where only
user data will be written, the reserved block percentage can
be safely reduced to make more of the filesystem available for
writing by regular users.

-r
n

Sets the number of reserved blocks to
n
. This is similar to the
-m
option, except it specifies a number
instead of a percentage.

Example 1

List the contents of the superblock on
/dev/sda1
:

#
tune2fs -l /dev/sda1
tune2fs 1.41.4 (27-Jan-2009)
Filesystem volume name: /boot
Last mounted on:
Filesystem UUID: 35f8a3e0-9257-4b71-913d-407bef4eeb90
Filesystem magic number: 0xEF53
Filesystem revision #: 1 (dynamic)
Filesystem features: has_journal ext_attr resize_inode \
dir_index filetype needs_recovery sparse_super
Filesystem flags: signed_directory_hash
Default mount options: user_xattr acl
Filesystem state: clean
Errors behavior: Continue
Filesystem OS type: Linux
Inode count: 50200
Block count: 200780
Reserved block count: 10039
Free blocks: 158854
Free inodes: 50152
First block: 1
Block size: 1024
Fragment size: 1024
Reserved GDT blocks: 256
Blocks per group: 8192
Fragments per group: 8192
Inodes per group: 2008
Inode blocks per group: 251
Filesystem created: Mon Dec 15 14:43:58 2008
Last mount time: Fri Jul 24 10:25:08 2009
Last write time: Fri Jul 24 10:25:08 2009
Mount count: 23
Maximum mount count: 20
Last checked: Mon Dec 15 14:43:58 2008
Check interval: 31536000 (12 months, 5 days)
Reserved blocks uid: 0 (user root)
Reserved blocks gid: 0 (group root)
First inode: 11
Inode size: 128
Journal inode: 8
Default directory hash: half_md4
Directory Hash Seed: 92b218b8-9e1f-4aab-b481-08bec3ea2946
Journal backup: inode blocks
Example 2

Turn off the maximum mount count and check interval tests on
/dev/sda1
:

#
tune2fs -i 0 -c 0 /dev/sda1
tune2fs 1.41.4 (27-Jan-2009)
Setting maximal mount count to -1
Setting interval between checks to 0 seconds

Other books

Defenseless by Corinne Michaels
Part of the Pride by Kevin Richardson
Salamis by Christian Cameron
The Power by Rhonda Byrne
The Summer House by Moore, Lee
The Mind-Murders by Janwillem Van De Wetering
Dragonvein Book Four by Brian D. Anderson