Read LPI Linux Certification in a Nutshell Online
Authors: Adam Haeder; Stephen Addison Schneiter; Bruno Gomes Pessanha; James Stanger
Tags: #Reference:Computers
mkswap
mkswapdevice
Prepare a partition for use as swap space. This
command can also set up swap space in a file on another
filesystem.
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
#
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.
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.
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.
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.
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.
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.)
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.
df
df [options
] [file
[file
...]]
Display overall disk utilization information for mounted
filesystems onfile
. Usually,file
is a device file for a partition,
such as
/dev/hda1
. Thefile
may also be the mount point or any
file beneath the mount point. Iffile
is
omitted, information for mounted filesystems on all devices in
/etc/fstab
are displayed.
Displays results in a human-readable format, including
suffixes such asM
(megabytes) andG
(gigabytes).
Displays information on remaining inodes rather than the
default disk space information.
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.
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.
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.
du
du [options
] [directories
]
Display disk utilization information fordirectories
. Ifdirectories
are omitted, the current
working directory is searched.
Shows all files, not just directories.
Produces a grand total for all listed items.
Displays results in a human-readable format, including
suffixes such asM
(megabytes) andG
(gigabytes).
Prints a summary for each of thedirectories
specified, instead of
totals for each subdi
rectory
found
recursively.
Excludes subdirectories from counts and totals, limiting
totals todirectories
.
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
Display utilization by files in
/etc
,
including subdirectories beneath it:
#du -s /etc
13002 /etc
Display utilization by files in
/etc
, but
not in subdirectories beneath it:
#du -Ss /etc
1732 /etc
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.
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.
tune2fs
tune2fs [options
]device
Modify tunable parameters on the
ext2
or
ext3
filesystem ondevice
.
device
List the tunable parameters ondevice
.
n
Set the maximum mount count ton
. 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 to0
tells the kernel and
e2fsck
to ignore the mount count.
n
Set the maximum time between two filesystem checks ton
. Ifn
is a number or is followed byd
, the value is in days. A value
followed byw
specifies
weeks. A value followed bym
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 of0
disables this check.
label
Sets the volume label of the filesystem tolabel
. The volume label can also be
set with the
e2label
command.
Adds an
ext3
journal file to the
filesystem and sets thehas_journal
feature flag.
n
Sets the reserved block percentage ton
. 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.
n
Sets the number of reserved blocks ton
. This is similar to the
-m
option, except it specifies a number
instead of a percentage.
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
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