Saturday, August 27, 2016

Managing Linux File Systems

Be prepared to be tested very heavily on this chapter in your LPIC-1/ Linux+ exam. Depending on which exam form you receive when you take your test, you may see 15–20 percent of your exam questions coming from this chapter alone. 

Managing Disk Partitions

These utilities work great; however, for your LPIC-1/Linux+ certification exam, you have to know how to manage your partitions from the command line. In this part of the chapter, you will learn the following:
Managing master boot record (MBR) disk partitions
Managing globally unique identifier (GUID) partitions Building a file system
Mounting a partition
Understanding LVM
Maintaining Linux file systems 

Managing Master Boot Record (MBR) Disk Partitions

The fdisk utility is used from the command line to view, create, or delete MBR partitions at the shell prompt. To use fdisk to create a new partition, first open a terminal session. Then, at the shell prompt, change to your root account by entering su – followed by your root user’s password.
At the shell prompt, enter fdisk device. For example, if you want to manage partitions on the first hard disk in your system, you would enter fdisk /dev/sda. At this point, you need to enter a command to tell fdisk what you want to do with the hard disk. 

You can enter m at the command prompt to display a list of all available commands.

You can also display the partition table for a specified hard disk by entering fdisk device –l at the shell prompt. The output is similar to that shown here. 

In the next example, a new hard disk (/dev/sdb) has been installed in the system and fdisk is being used to create a partition:
openSUSE:~ # fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x8239827b.
Command (m for help):

Notice that, because this is a new disk in the system, it has to be initialized with a disk label.
The fdisk utility takes care of this for you when you load the program. However, the change won’t
be applied until you write your changes to the disk.
With fdisk running, you have a Command: prompt that you can use to enter fdisk commands.
At this point, one of the best things you can do is to enter m to view the fdisk help. When you
do, a list of commands is displayed that you can use to perform actions with fdisk, as shown in
this example:
Command (m for help): m
Command action
 a    toggle a bootable flag
 b    edit bad disklabel
 c    toggle the dos compatibility flag
 d    delete a partition
 g   create a new empty GPT partition table
 G   create an IRIX (SGI) partition table
 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 disk label 
 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)

Command (m for help):


CAUTION You can enter d to delete an existing partition. Be very careful about using this action. Any data on that partition will be lost! Once the changes are committed to disk, they are not reversible. You can back off from changes made with fdisk without committing them to disk by entering q. With disk, q is your friend!

Before creating a partition, you should enter p to view any existing partitions on the disk. This
will help you determine whether there is sufficient space and, if there is, what number must be
assigned to a new partition created on the disk.
To create a new partition, you enter n. You can then specify whether you want to create a
primary disk partition or an extended disk partition. Any hard disk in your system can have up to
four partitions defined in its partition table. These can be either primary or extended partitions. If
you intend to create four partitions or fewer, you can simply use primary partitions.
However, if you want to create more than four partitions on the disk, you must create at least
one extended partition. Extended partitions are great. Within one extended partition, you can
create many logical partitions. This allows you to get around the four-partition limitation. The
general rule of thumb is to create your primary partitions first; then create your extended partition
using the remaining space on the drive and create your logical partitions within it.
To create a primary partition, enter p when prompted. To create an extended partition, enter e.
You are then prompted to specify a partition number, as shown in this example:
Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1

You need to specify the next available partition number for the partition being created. In
other words, if your disk already has two partitions on it, you would enter 3. In the preceding
example, no partitions currently exist on the drive, so I’ve entered 1 to create the first primary
partition. If you try to enter a partition number that has already being used by an existing parti-
tion, the fdisk utility will complain!
At this point, you must specify the size of the partition. This is done by specifying the beginning and ending cylinders. You first specify the cylinder on which you want to begin the partition.
By default, fdisk displays the next available cylinder. If you don’t have any partitions on the disk,
this will be cylinder 1. If you already have partitions on the disk, the next unused cylinder will be
listed. After specifying your start cylinder, you have several options for specifying the overall size
of the partition, as shown here:
Partition number (1-4, default 1): 1
First sector (2048-2097151, default 2048): 2048
Last sector, +sectors or +size{K,M,G} (2048-2097151, default 2097151):
2097151
Partition 1 of type Linux and of size 1023 MiB is set

You can

Enter the last cylinder to be used in the partition.
Specify the size of the partition in gigabytes by entering sizeG. For example, you could
create an 800GB partition by entering 800G.
After specifying the size, you should verify your new partition by entering p. This will display
all partitions for the disk, as shown in the next example:
Command (m for help): p
Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 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 label type: dos
Disk identifier: 0xd489eb71
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     2097151     1047552   83  Linux
Command (m for help):


It’s important to note that, at this point, the partition hasn’t been written to disk. All changes
are saved in memory before being committed to disk. This allows you to tweak your partitions
before actually committing the changes.
Before committing the partition to disk, however, you may need to change the partition type.
Notice in the preceding example that the partition being created is a standard Linux partition by default. Usually, this is sufficient. However, suppose you were creating a swap partition. You would need to use a different type of partition. This is done by entering t and then entering the
ID of the partition type you want to change to. If you don’t know the ID number of the partition
type you want to use, you can enter l (lowercase L) to list all the valid partition types and their
associated ID numbers, as shown here:


Command (m for help): l
0  Empty
1  FAT12
2  XENIX root
3  XENIX usr
4  FAT16 <32M
5  Extended
6  FAT16
7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux plaintext de  Dell Utility
24  NEC DOS         81  Minix / old Lin bf  Solaris
27  Hidden NTFS Win 
82  Linux swap / So 
c1  DRDOS/sec
39  Plan 9        
83  Linux           
c4  DRDOS/sec
3c  PartitionMagic  84  OS/2 hidden C:  c6  DRDOS/sec
40  Venix 80286     85  Linux extended  c7  Syrinx
41  PPC PReP Boot   86  NTFS volume set da  Non-FS data
42  SFS             87  NTFS volume set db  CP/M / CTOS
8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM
9  AIX bootable    4f  QNX4.x 3rd part 93  Amoeba
a  OS/2 Boot Manag 50  OnTrack DM      94  Amoeba BBT
b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS
df  BootIt
e1  DOS access
e3  DOS R/O
e4  SpeedStor
c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad hi eb  BeOS fs
e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD
ee GPT
ef EFI (FAT-12/16)
f0 Linux/PA-RISC
f1 SpeedStor
f W95 Ext'd (LBA)
10 OPUS
11 Hidden FAT12
54 OnTrackDM6
55 EZ-Drive
56 Golden Bow
a6 OpenBSD
a7 NeXTSTEP
a8 Darwin UFS
12 Compaq diagnost 5c Priam Edisk a9 NetBSD
14 Hidden FAT16 <3 61 SpeedStor ab Darwin boot
16 Hidden FAT16 63 GNU HURD or Sys af HFS / HFS+
17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs
18 AST SmartSleep 65 Novell Netware b8 BSDI swap
1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fe LANstep
1c Hidden W95 FAT3 75 PC/IX be Solaris boot ff BBT
1e Hidden W95 FAT1 80 Old Minix
f4 SpeedStor
f2 DOS secondary
fb VMware VMFS
fc VMware VMKCORE
fd Linux raid auto
For example, the ID for a Linux swap partition is 82. To change the type of the partition, you could
enter t and specify a partition ID of 82 if you wanted to change the partition to a swap partition.
You can also delete partitions using fdisk. To do this, enter d at the command prompt and then
specify the partition number you want to delete. Remember, any data that resides on this partition
will be lost once you commit the change to disk!
At this point, you’re ready to commit your partition to disk. If you’re unhappy with the parti-
tioning proposal, you can always enter q to quit without applying the changes. If you are happy,
however, you can apply your changes by entering w. This will commit the partition to disk and
exit fdisk, as shown in this example:
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
openSUSE:~ #
It’s important to note here that you must reboot the system before the Linux kernel will
recognize the partition changes you made with fdisk. However, you can also use the partprobe
command at the shell prompt to force the kernel to recognize the new partition table without
rebooting. The syntax is partprobe –s. The –s option causes the partprobe command to show a
summary of devices and their partitions, as shown in this example:
openSUSE:~ # partprobe –s
/dev/sda: msdos partitions 1 2 3
/dev/sdb: msdos partitions 1
openSUSE:~ #
Now that you know how to manage partitions with fdisk, let’s look at creating GUID partitions.

Managing Globally Unique Identifier (GUID) Partitions
As I mentioned previously, the MBR partitioning scheme has been in use for many years—since
the early 1980s! As such, it has many limitations and shortcomings. Here are some examples:
The MBR must be installed in the first 512 bytes of the hard disk.

Only four primary partitions can be created on a storage device.

The default block size of 512 bytes limits partitions to a maximum size of 2TB.
Many patches have been implemented over the years to get around these shortcomings:
Using logical block addressing (LBA) to enable the use of larger hard disks
•Using 4,096-byte sectors to increase the maximum partition size on disk
•Defining one of your four available partitions as an extended partition, which can hold many logical partitions
These patches have served us well for a long time, but it’s time for a new way to manage adisk partitions. Recently, the globally unique identifier (GUID) partition table scheme was introduced for Linux as a replacement for MBR.


We typically refer to this scheme as simply GPT (which stands for GUID partition table). We techies are famous for nesting acronyms within acronyms. 
GPT is a part of the overall Unified Extensible Firmware Interface (UEFI) specification, which is designed to replace the BIOS on computer systems and provide a much most robust software- to-hardware interface. Think of UEFI as the BIOS on steroids. UEFI is only implemented on late-model computers. However, you can use GPT on older Linux systems that still use the BIOS. Using GPT has several key advantages over using MBR for your hard disk partitions: 
There are no such things as primary, extended, or logical partitions in GPT. All partitions are just plain old partitions.
GPT supports gigantic disks and partitions. We’re talking about sizes measured in zebibytes instead of terabytes.
Most Linux distributions support up to 128 GPT partitions per disk. That’s a lot more than the four we were stuck with using MBR.
GPT provides partition fault tolerance by storing a copy of the partition table in the first and last sectors on the disk. This way, if one of the copies gets corrupted (it has happened to me), the redundant copy immediately takes over and the files on the disk remain accessible.
GPT performs a cyclic redundancy check(CRC) to verify the integrity of the partition table.
GPT assigns unique IDs to each disk and partition.
. To manage GPT partitions, we can’t use fdisk anymore. Instead, we use a utility named gdisk, which can be used to do a lot of things. For example, gdisk can be used to do the following:


Convert an MBR partition table to a GPT partition table.
Verify a hard disk.
Create and delete GPT partitions.
Display information about a partition.
Change the name and type of a partition. 
Back up and restore a disk’s partition table. 
For example, suppose I added a second disk to my system and I wanted to create a GPT partition on it so I can use it to store information. To do this, I would first switch to my root user and then enter gdisk /dev/sdb at the shell prompt. If you’re comfortable using fdisk, then the process of using gdisk will be a snap for you because many of the commands are the same. At the gdisk prompt, you can enter ? to view a list of commands available. This is shown in the following example: 
openSUSE:~ # gdisk /dev/sdb
GPT fdisk (gdisk) version 0.8.7
Partition table scan:
  MBR: not present
  BSD: not present
  APM: not present
  GPT: not present
Creating new GPT entries.
Command (? for help): ?
. b        back up GPT data to a file
.
. c        change a partition's name
.
. d        delete a partition
.
i       show detailed information on a partition
l       list known partition types
. n        add a new partition
.
. o        create a new empty GUID partition table (GPT)
.
. p        print the partition table
.
. q        quit without saving changes
.
. r        recovery and transformation options (experts only)
.
. s        sort partitions
.
. t        change a partition's type code
.
. v  verify disk
. w        write table to disk and exit
.
. x        extra functionality (experts only)
.
?       print this menu
Command (? for help):
In this situation, I want to add a new partition to the disk, so I would enter n at the gdisk prompt. When I do, I’m prompted to specify the following: 
The partition number
The size of the partition This can be done by specifying the beginning and ending sectors of the partition. You can also specify where on the disk you want the partition to start and end (such as at the 10GB and 20GB points on the disk).
The type of partition The partition type numbers with gdisk are different from those used with MBR partitions. For example, to create a Linux partition, you use a partition type of 8300. You can press L at the gdisk prompt to view a list of all possible partition types and their codes.
This process is shown in the following example:
Command (? for help): p
Disk /dev/sdb: 33554432 sectors, 16.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 1D3E9F48-D822-4DDF-AB94-C59B7A4E12C8
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 33554398
Partitions will be aligned on 2048-sector boundaries
Total free space is 12582845 sectors (6.0 GiB)
Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048        20973567   10.0 GiB    8300  Linux filesystem
Command (? for help): w
Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!
Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/sdb.
The operation has completed successfully.
Once this is done, you can enter p at the gdisk prompt to view a list of the partitions on the disk. As with fdisk, the changes you make with gdisk are not actually committed to disk until you write them. If you like the changes you made to the disk partitioning, press w at the gdisk prompt. If you want to delete a partition, press d. If you want to change a partition’s type, press t and then enter the partition type code you want to use. If you want to quit and start over without saving any changes, press q instead. 
In addition to gdisk, you can also use the parted command at the shell prompt to manage GPT partitions. You can use it to add, delete, and edit the partitions on your disk. 
Unlike gdisk and fdisk, the parted command writes the partition changes you specify immediately to the disk. Be absolutely certain of the changes you want to make before using parted! 
To use parted, enter parted at the shell prompt and then use the select command to specify which disk you want to manage. Be very careful, because if you don’t manually specify a disk to manage, parted will automatically select your first hard disk for you—you know, the one with your system partitions and your home partition on it? Accidentally deleting a partition on this disk could be bad! If you intend to work on a disk other than /dev/sda, be sure you use the select command. 
After selecting the appropriate hard disk, you can create a new partition using the mkpart command at the parted prompt. You need to specify the following: 
The type of partition to be created For example, to create a standard Linux partition, you would specify a value of linux
The starting point on the disk for the partition (in megabytes) For example, to create a partition that starts at the 1GB point on the disk, you would specify a value of 1024
The ending point on the disk for the partition (in megabytes) For example, to create a partition that ends at the 11GB point on the disk, you would specify a value of 11264
To view the partitions that have been created on the disk, you can use the print command at the parted prompt. In the following example, a 10GB partition is created on the second hard disk in the system (/dev/sdb): 
openSUSE:~ # parted
GNU Parted 2.4
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) select /dev/sdb
Using /dev/sdb
(parted) mkpart linux 1024 11264
(parted) print
Model: VMware Virtual disk (scsi)
Disk /dev/sdb: 17.2GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Number  Start   End     Size    File system  Name   Flags
 1      1024MB  11.3GB  10.2GB               linux
(parted) 
You can also use the following commands at the parted prompt to manage disk partitions: 
To rename a partition, enter name partition name.
To move a partition to a different location on the disk (which is a very handy thing to be able to do), enter move partition start_point end_point.
To resize a partition on the disk (another very handy thing to be able to do), enter resize partition start_point end_point.
To delete a partition from the disk, enter rm partition.
Now that you understand how to manage partitions, you need to learn how to make a file
system on those partitions using mkfs.
Building a File System
Even though we’ve created a partition, we can’t use it yet. That’s because it hasn’t been formatted with a file system yet. This is accomplished using one of the several commands. In this part of the chapter, we will look at the following topics:
• Using mkfs
• Using mkreiserfs
• Creating xfs and vfat file systems • Using mkswap
Using mkfs 
The mkfs utility is used to make an ext2, ext3, or ext4 file system on a partition. You can even use it to create an NTFS file system on the partition (which is used by Windows).This utility is actually just a front end for the real commands that make each specific type of file system: 
/sbin/mkfs.minix
/sbin/mkfs.ntfs
/sbin/mkfs.bfs
/sbin/mkfs.ext4
/sbin/mkfs.msdos
/sbin/mkfs.cramfs
/sbin/mkfs.ext3
/sbin/mkfs.ext2
/sbin/mkfs.vfat
  /sbin/mkfs.xfs 
If desired, you can run the mkfs.x utility in /sbin to create a specific file system. However, most Linux admins just use the mkfs front end. You can also use the mke2fs command at the shell prompt to create extx file systems. If you need to get help creating a specific file system, enter man mkfs.x at the shell prompt. 
Before you can create xfs file systems, xfs support must be added to the Linux kernel. Support for xfs is added by default during the installation of some distributions, such as Fedora. However, it is not included with other distributions, such as OpenSUSE and Ubuntu. 
You specify which file system you want to use by entering the –t option and the type of file system. For example, if you want to create an ext4 file system on the first partition on the second hard disk drive in your system, you would enter mkfs –t ext4 /dev/sdb1. Here is an example: 
openSUSE:~ # mkfs -t ext4 /dev/sdb1
mke2fs 1.42.8 (20-Jun-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
524288 inodes, 2096474 blocks
104823 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2147483648
64 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


Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
There are several options you can use with mkfs (in addition to –t) to customize the file system you create on the specified partition: 
–b blocksize Specifies the size of the data blocks in the file system. Values of 1024, 2048, and 4096 are allowed.
–N inodes Specifies how many inodes are created on the file system.
. Remember, an inode stores basic information about a file in the Linux file system, including its size, device, owner, and permissions. Each file has one inode (with the exception of hard links). Therefore, specifying an exact number of inodes effectively limits the total number of files that can be created on the file system.
. –i bytes_per_inode Sets the size of the inode. For bytes_per_inode, you can use the same values available for the block size.
. –j Creates a journal on the file system. This can be used to add a journal to an existing ext2 file system, effectively converting it to an ext3 file system.
If you do not include any options when running the mkfs command, an optimal data block size and the optional number of inodes will be calculated for you based on the size of the partition. In the preceding example, the following were calculated for us:
Block size=4096 This specifies that the block size is 4KB. This value was determined to be optimal for the small 8GB partition that the file system was created on in this example. Smaller partitions will have small block sizes.
524288 inodes, 2096474 blocks The file system has a maximum of 524,288 inodes and 2,096,474 blocks. This means it can hold a maximum of 524,288 files on the partition, even if the sum of their sizes is less than the total space available. If you multiply the total number of blocks (2,086,474) by the block size (4,096), you can calculate the total size of the partition (in this case, about 8GB).
Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632 The superblock is the block at the beginning of the partition (0) that contains information about the structure of the file system. In this file system, redundant copies of the superblock are stored in the specified blocks.
Using mkreiserfs
The mkreiserfs utility is used to make a Reiser file system on a partition. For example, if you want to create a Reiser file system on the first partition on the second hard drive in your system, you would enter mkreiserfs /dev/sdb1. When you do, a screen is displayed and a proposal is presented.
You can also run mkfs –t reiserfs device at the shell prompt to create a Reiser file system. 
Many of these parameters can be customized using mkreiserfs options. See the utility’s man page for more information. Usually, however, the default parameters work just great. When prompted to continue, enter y to format the partition. 
Creating xfs and vfat File Systems 
In addition to creating ext2/3/4 and reiser partitions, you can also use mkfs to create xfs and vfat partitions. The Extents File System (xfs) file system was created by Silicon Graphics (SGI) for its IRIX operating system. It’s a very fast, very flexible file system. It has been ported over to run on Linux as well, although in my opinion it doesn’t function under Linux as well as it does under IRIX. An easy way to determine whether your Linux kernel has xfs support enabled is to open a shell prompt and (as root) enter find / –name mkfs.*. If xfs support has been added to your kernel, you should see the mkfs.xfs utility installed on the system, as shown in Figure 10-1. 
Creating an xfs file system is very easy. It is done in exactly the same manner as creating extx file systems using the mkfs command. First, create a standard Linux partition (type 83) using fdisk. Then, at the shell prompt, enter mkfs –t xfs device. In Figure 10-2, the /dev/sdb1 partition is formatted with the xfs file system. 
In addition, you can also create non-Linux file systems, such as FAT, on Linux partitions using the mkfs command. First, create a standard Linux partition (type 83) using fdisk. Then simply enter mkfs –t vfat device at the shell prompt. Here is an example: 
openSUSE:~ # mkfs -t vfat /dev/sdb2
mkfs.vfat 3.0.22 (2013-07-19)
openSUSE:~ #


Using mkswap 
So far so good, but what if you need to format the partition you just created as a swap partition instead of a data partition? Never fear, you can do this too using the mkswap command at the shell prompt. Just like a data partition, a partition used for swapping has to be prepared with the mkswap command. 

A Linux system can have more than one swap partition. In fact, on a heavily used system, having multiple swap partitions spread across several hard disks can really increase performance. 
The syntax for this command is mkswap device—for example, mkswap /dev/sdb2
The type of the partition must be set to 82 using the fdisk utility 
first! 
An example follows: 
openSUSE:~ # mkswap /dev/sdb2
Setting up swapspace version 1, size = 1959924 KiB
no label, UUID=1f51a8d7-ac55-4572-b68a-7a3f179aac61
After configuring the partition as a swap partition, you use the swapon command at the shell prompt to enable it. The syntax is swapon device—for example, swapon /dev/sdb2
You can disable an existing swap partition by entering swapoff device at the shell prompt—for example, swapoff /dev/sdb2

If you want to activate all swap partitions at once, you can enter swapon –a. The swapon utility will search through your /etc/fstab file to identify all defined swap partitions and will mount them. To deactivate all currently enabled swap partitions, enter swapoff –a at the shell prompt. 
Once your partition has been formatted with a file system, you must mount it before you can use it. Let’s review how you do this next. 
Mounting a Partition 
One of the key differences between Linux and other operating systems (such as Windows) is the fact that you need to mount a file system before you can use it. This is another one of those “got- chas” that trip up new Linux administrators. You even have to mount hard disks, DVDs, and USB drives before you can use them! We’ll talk about mounting removable media later in this chapter. 
The good news is that the process of mounting a partition is relatively easy. This is done using the mount command. The mount utility mounts the partition into an existing directory in your file system. When you switch to that directory, you are actually switching to the partition. 
New Linux admins sometimes struggle with this concept. Other operating systems, such as Windows, reference partitions on the disk using a drive letter such as C: or D: by default. 

For example, if you have a hard disk in a Windows system that has two partitions on it, the first one would be accessed using drive letter C:, and the second partition would be accessed using drive letter D:. Each drive letter has its own separate hierarchy of directories and files. 
Not so with Linux! Instead, Linux uses a virtual file system (VFS) that creates a single hierarchy that encompasses all partitions on all storage devices in the system. Switching to the directory where the partition is mounted switches you to that partition. 
To use the mount command to mount a partition, first switch to your root account using su. Then enter mount –t file_system_type device mount_point at the shell prompt. I know this looks complicated, but it really isn’t. For example, suppose you created the ext4 file system on the first partition on the second hard disk in your system, and now you want to mount it in a directory named /mnt/extraspace. To do this, you would enter mount –t ext4 /dev/sdb1 /mnt/ extraspace at the shell prompt, as shown here: 
openSUSE:~ # mount -t ext4 /dev/sdb1 /mnt/extraspace/
Now, whenever you switch to the /mnt/extraspace directory in the file system, you are switch- ing to the new partition (/dev/sdb1). If you don’t know what type of file system is used by the partition, you can use the –a option with the mount command instead of –t. This will cause mount to try to mount the partition using all supported file system types until one is successful. 
On most distributions, the /mnt directory is the default directory for mounting local and re- mote file systems. You don’t have to use it, of course. You can mount a partition into any directory you want. However, by convention, /mnt is used for this purpose. On some distributions, it’s also the default location for mounting removable devices as well. However, some distributions, such as OpenSUSE and newer versions of Fedora, use /media as the default location for mounting removable media. 
You can also use the –o option with the mount command to include a variety of mounting op- tions with the command. For example, you could use –o ro to mount the partition as read-only. See the man page for mount for a complete listing of all the available options. 

One of the things I love about the mount command is the fact that it can be used to mount an ISO image in the file system, allowing you to access the files within it without burning it to an optical disc first. The syntax is mount –o loop filename.iso mount_point

After mounting the partition, you can use the mount command with no switches to view all mounted file systems. You can look through the output to verify that the device is mounted. 
For example, the following line from the output of the mount command indicates that /dev/ sdb1 is mounted on /mnt/extraspace and uses the ext4 file system: 
/dev/sdb1 on /mnt/extraspace type ext4 (rw,relatime,data=ordered)


In addition, whenever you mount or unmount a partition, the /etc/mtab file is updated with a list of mounted file systems. You can view this file using the cat command to see a list of mounted file systems. For example, the following line from the mtab file also indicates that the /dev/sdb1 partition is mounted in /mnt/extraspace: 
/dev/sdb1 /mnt/extraspace ext4 rw,relatime,data=ordered 0 0
You can also use the cat command to view the /proc/mounts file to see a list of mounted file systems. Another useful utility for viewing mounted partitions is df. This utility displays your partitions, where they’re mounted, how much space has been used, and how much free space is still available. 
If, for some reason, you need to unmount a partition, you can use the umount command. To unmount a partition, simply enter umount followed by the device or the mount directory for the partition. For example, to unmount the /dev/sdb1 partition shown in the last example, you would enter umount /dev/sdb1 or umount /mnt/extraspace, like this: 
openSUSE:~ # umount /dev/sdb1


Notice that the command is “umount,” not “unmount.” This is yet another Linux gotcha! 
This brings up yet another issue when working with Linux. Just because you mount a file system with mount does not mean it will stay mounted if you reboot the system. As long as the system stays running, the partition will stay mounted. However, if you reboot, the partition won’t be remounted when the system starts back up. 
You can remedy this using the /etc/fstab file. This file contains a list of file systems that the operating system mounts whenever it boots. A typical /etc/fstab file follows: 

openSUSE:/ # cat /etc/fstab 
/dev/sda1
/dev/sda2
/dev/sda3
swap swap / ext4 /home ext4 
defaults
acl,user_xattr
acl,user_xattr
0 0 1 1 1 2 
I know this file looks a little confusing at first, but it really isn’t too bad. Each line in the /etc/ fstab file specifies a separate file system to be mounted on boot. Each line is composed of six fields, as listed in Table 10-1. 
Notice that the /dev/sdb1 partition we created earlier isn’t listed in the sample fstab file. That means it won’t be mounted in /mnt/extraspace after a system reboot. We can fix this by adding the partition to the fstab file. Open /etc/fstab in an editor such as vi and then add the following line: 
/dev/sdb1    /mnt/extraspace    ext3    acl,user_xattr    1    2
This specifies that the /dev/sdb1 partition be mounted in /mnt/extraspace using the ext4 file system. It also specifies that access control lists be enabled for the file system (a very good thing to  do!) and that extended user attributes be enabled for the file system as well. Now, whenever you reboot the system, the partition will be automatically mounted. 



Understanding LVM 
Logical volume management (LVM) is an option you can use when partitioning Linux hard disk drives. It provides an alternative to the traditional process of creating disk partitions. Instead, you create volume groups from storage devices in your system. From the volume group, you allocate space to specific logical volumes, which are managed by the logical volume manager. Instead of mounting partitions, you mount logical volumes at mount points in your file system. This pro- vides you with a great deal of flexibility when allocating space on the system. For example, if you mount a volume at /usr and a volume on /home and then the /home directory begins to run out of space, you can reallocate space from the volume mounted at /usr to the volume mounted at /home. That’s very difficult to do with traditional disk partitions! 

In addition, LVM allows you to dynamically add space to the system. For example, suppose you are out of space on the /home volume. To add capacity, all you have to do is install a new hard drive in the system and then allocate its space to the /home volume. Voilà! The size of the volume is increased. You didn’t have to back up the data and restore it as you would with tradi- tional partitions. 
In this part of this chapter, we’re going to look at the following LVM topics: LVM components 
LVM configuration
Let’s begin by looking at the components that must be in place for LVM to work. 
LVM Components 
LVM creates a virtual pool of memory space (called a volume group) from which logical volumes can be created. Linux can use LVM logical volumes just like standard disk partitions you create with fdisk. However, the way logical volumes are defined is quite a bit more complex. The basic structure of LVM consists of the following components: 
Physical volumes A physical volume can be either a partition or an entire hard disk.
Volume groups A volume group consists of one or more physical volumes grouped together. The key thing to remember is the fact that the physical volumes can come from different hard disks. This means you can add hard disks or partitions to the volume group whenever you need more storage space.
Logical volumes These are defined from a volume group. Logical volumes can be formatted with a Linux file system and mounted just like physical partitions.
LVM Configuration 
To create and mount logical volumes, you need to create physical volumes, volume groups, and then logical volumes (in that order). 
Creating LVM Physical Volumes You can use disk partitions or even entire disks as physical LVM volumes. If you decide to use an existing partition, you should set the partition type to Linux LVM (8e). However, my experience has been that standard Linux partitions (type 83) work just as well. An example of setting the partition type with fdisk is shown here: 
Command (m for help): t
Partition number (1,2, default 2): 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Be aware that if you want to use an entire hard disk as an LVM physical volume, it cannot contain a partition table. If the disk you want to use already has an existing partition table, you can use the dd command to obliterate it. In the example shown next, I overwrite the first 512KB block on the /dev/sdc hard disk (where the MBR partition resides) with junk null characters from the /dev/zero device file: 
openSUSE:/media # dd if=/dev/zero of=/dev/sdc bs=512 count=1
1+0 records in
1+0 records out
512 bytes (512 B) copied, 0.2242 s, 2.3 kB/s
Once you have decided which disks/partitions you want to use, you run the pvcreate com- mand at the shell prompt to define them as LVM physical volumes. The syntax is pvcreate device. In the following example, the first two partitions on /dev/sdb are defined as physical volumes as well as the entire /dev/sdc hard disk: 
openSUSE:~ # pvcreate /dev/sdb1
openSUSE:~ # pvcreate /dev/sdb2
openSUSE:~ # pvcreate /dev/sdc
Once this is done, you can use the pvscan -v command to view all physical volumes on the system along with their size. This is shown in the following example: 
openSUSE:~ # pvscan –v

If, at a later time, you need to remove a physical volume for some reason (such as a failing hard disk drive), you can use the pvmove utility to move the data from the physical volume to be removed to another physical volume you’ve defined in the system. 
Once you have your physical volumes defined, you’re ready to create your volume group. Let’s review how to do that next. 
Creating LVM Volume Groups The vgcreate utility is used to create volume groups on your system. The syntax is vgcreate volume_group_name physical_volume1 physical_volume2 .... In the following example, a volume group named DATA is created using the sdb1, sdb2, and sdc physical volumes: 
openSUSE:~ # vgcreate DATA /dev/sdb1 /dev/sdb2 /dev/sdc
openSUSE:~ # pvscan –v
    Volume Groups with the clustered attribute will be inaccessible.
    Wiping cache of LVM-capable devices
    Wiping internal VG cache
    Walking through all physical volumes
Notice in the output of the pvscan command that the three physical volumes are now members of the DATA volume group. After you initially create a volume group, you can use the following commands to manage it: 
Use vgexpand to add additional physical volumes to an existing volume group.
Use vgreduce to remove a physical volume from an existing volume group. Before running vgreduce, you must use pvmove to shift the data in the physical volume to be removed to another physical volume on the system.
Use vgremove to delete a volume group. Before you can use vgremove, you must first remove all logical volumes that have been defined in the volume group.
Once your volume group has been defined, you are ready to create logical volumes. Let’s discuss how this is done next.
. Creating LVM Logical Volumes You use the lvcreate command to create logical vol- umes within a volume group. The syntax is lvcreate –L volume_size –n volume_name volume_ group_name. In the following example, two 7GB volumes (named research and development) are defined in the DATA volume group:
openSUSE:~ # lvcreate -L 7G -n research DATA
. openSUSE:~ # lvcreate -L 7G -n development DATA
. openSUSE:~ # lvscan –v
.
    Volume Groups with the clustered attribute will be inaccessible.
.     Finding all logical volumes
.     ACTIVE            '/dev/DATA/research' [7.00 GiB] inherit
.     ACTIVE            '/dev/DATA/development' [7.00 GiB] inherit
.
Notice that we used the lvscan command to view the logical volumes defined on the system. Also notice that we defined two logical volumes in the volume group, which itself is created by pooling together all the storage space from two disk partitions and one entire hard disk drive. Pretty cool!
To manage your logical volumes after they have been created, you use the following commands: 
Use lvextend to increase the size of a logical volume. You must first add additional physical volumes (disks or partitions) to the volume group to do this.
Use lvreduce to reduce the size of a logical volume.
Use lvremove to remove a logical volume from the system.
. Use extreme caution when working with lvreduce and lvremove! If the file system is larger than the size you specify with lvreduce, you run risk of chopping off chunks of data. You should migrate any data you don’t
want to lose to a different logical volume if you decide to remove a logical volume with lvremove.
Once your logical volumes have been created, you need to create file systems on them and then mount them. You create a file system using mkfs, just as with traditional partitions. Use the following syntax:
mkfs –t file_system /dev/volume_group/logical_volume
You mount a logical volume using the mount command, just as you would to mount file sys-
tems on traditional partitions. Use the following syntax:
mount –t file_system /dev/volume_group/logical_volume /mount_point 

Let’s next talk about maintaining Linux file systems.

. Maintaining Linux File Systems
Just as with any other operating system, you need to monitor and maintain your Linux file systems. In this part of the chapter, you learn how to do this as we cover the following topics:
Checking partition and file usage Checking the file system integrity Checking for open files
Identifying processes using files
Checking Partition and File Usage
One of the key problems you need to avoid when maintaining Linux file systems is running out of disk space. Linux provides the df and du utilities to help you keep track of how much space is used on a partition and which files are using that space.
The df utility shows you where your hard drive partitions, optical drives, and other storage devices (such as USB drives) are mounted in the file system. It also shows the total size of the device and how much of that space is used. Running df without any options shows information about all mounted file systems, as shown here:
openSUSE:/ # df
. Filesystem     1K-blocks    Used Available Use% Mounted on
. /dev/sda2        6194480 4528228   1328540  78% /
.

You can also enter df filename at the shell prompt. This causes df to display information about the partition on which the specified file resides, as in this example: 
openSUSE:/ # df /mnt/extraspace/myfile.txt
Filesystem     1K-blocks  Used Available Use% Mounted on
/dev/sdb1        5029504 10232   4740744   1% /mnt/extraspace
Notice in the preceding examples that, by default, df displays space statistics in blocks. This is fine if you are a bit-head and like to calculate available disk space from the number of blocks. I don’t. Therefore, I always use the –h option with the df command. This causes df to display space statistics in human-readable format, such as KB, MB, and GB. An example of the same informa- tion in the preceding examples is shown next using the –h option: 
openSUSE:/ # df –h

Much better! If you want to view the type of file system used on each partition, you can add the –T option as well, as shown here: 
openSUSE:/ # df –hT

Another useful option you can use with df is –i, which lists inode usage information instead of disk space usage. Recall earlier in this chapter that when a file system is created on a partition, a fixed maximum number of inodes is assigned. If the file system stores a huge number of very small 
files, it’s possible for it to run out of inodes even though plenty of disk space is still available. You can monitor inode usage using the df –i command, as shown here: 
openSUSE:/ # df –i

Another utility you can use to monitor disk space usage is the du utility. Its function is to pro- vide you with a summary of disk space usage of each file, recursively, for a specified directory. The syntax is du directory. Some useful options you can use with du include the following: 
–c Used to calculate a grand total 
–h Used to display output in human-readable format 
--exclude filename_or_pattern Used to exclude all files that match the specified filename or pattern 
The –h option is the most useful one, in my experience. Here is an example of viewing the space used by files in the /tmp directory in human-readable format: 
openSUSE:/ # du -h /tmp


Checking the File System Integrity 
As with any other operating system, Linux partitions can sometimes encounter problems. These problems can be caused by a variety of conditions; however, my experience has been that the number-one source of partition corruption issues is a power outage that results in an unclean shutdown of the system. If your system goes down without properly unmounting a mounted partition, it’s very likely that data corruption will occur. 
This is a good argument for implementing an uninterruptible power supply (UPS) on your systems. Linux doesn’t like unexpected shutdowns! 
If something like this happens, you need to check your file systems for errors and make repairs, if necessary. This is done using the File System ChecK utility (fsck). To use this utility, you must first umount the file system you want to check. Then enter fsck device at the shell prompt. For example, if you want to check the file system on the first partition of the second hard drive in your system, you would enter fsck /dev/sdb1. The utility will then check the file system and report any errors encountered (or the lack thereof ). Here is sample output from fsck: 
openSUSE:~ # umount /dev/sdb1

fsck is really just a front end to the following utilities: 
/sbin/fsck.msdos /sbin/fsck.cramfs /sbin/fsck.ext3
/sbin/dosfsck 
/sbin/fsck.ext2
/sbin/fsck
/sbin/e2fsck
/sbin/fsck.ext4
/sbin/fsck.vfat
/sbin/fsck.minix /sbin/fsck.reiserfs 
If you run fsck without any options, it will query your /etc/fstab file to determine the file system type of the partition you want to check and then load the appropriate module for the file system. As you can see in the preceding list, versions of fsck exist for the Reiser, ext2, ext3, and ext4 file systems. 
The fsck.reiserfs utility can be run using the reiserfsck front end. 
After the check is complete, you can remount the partition using the mount command. 
One very useful feature of e2fsck is its ability to restore a damaged superblock on ext2/3/4 file systems. Remember from earlier in this chapter that the superblock is the block at the beginning of the partition that contains information about the structure of the file system. Also, most Linux file systems keep backup copies of the superblock at various locations in the partition. If the super- block gets corrupted, you can restore it using one of these backup copies. The syntax is e2fsck –f –b backup_superblock device. The –f option tells e2fsck to force a check, even if the file system seems to be clean. The –b option tells e2fsck which copy of the superblock it should use. 
Here are some other useful file system management utilities: 
tune2fs This utility is used to adjust various file system parameters on ext2/3/4 file systems. These are some of the options you can use: 
–c n Sets the number of mounts (n) after which the file system will automatically be checked by fsck.
–i interval d|m|w Sets the maximum time between file system checks in d (days), m (months), or w (weeks).
–j Adds a journal to an ext2 file system.
–L volume_label Sets the volume label of the file system.
Here is an example:
      openSUSE:/ # tune2fs -L NewVol /dev/sdb1
      tune2fs 1.42.8 (20-Jun-2013)
reiserfstune This is the Reiser equivalent of the tune2fs utility.
dumpe2fs Displays lots of useful information about ext2/3/4 file systems. Use the –h
option to make the output more readable. An example follows:
openSUSE:/ # dumpe2fs -h /dev/sdb1
debugfs The debugfs program is an interactive file system debugger. It can be used to examine or even change the state of an ext2/3/4 file system. The syntax is debugfs device. When you run this command, the debugfs: prompt is displayed, as shown here: 
  openSUSE:/sbin # debugfs /dev/sdb1
  debugfs 1.42.8 (20-Jun-2013)
  debugfs:
By default, the file system is opened in read-only mode. If you need to make changes to the file system, you need to include the –w option to open it in read-write mode. Here are some of the commands you can enter at the debugfs: prompt: 
show_super_stats Displays information equivalent to that displayed by dumpe2fs. 
write in_file out_file Allows you to copy a file from the file system to another file system. This can be extremely useful in situations where you are trying to salvage an important file from a mangled file system. 
stat file_or_directory_name Displays extended information about the file or
directory specified. An example follows for a file named myfile2.txt:
     Inode: 12   Type: regular    Mode:  0644   Flags: 0x80000
    Generation: 556089601    Version: 0x00000000:00000001
    User:     0   Group:     0   Size: 0
    File ACL: 0    Directory ACL: 0
•    Links: 1   Blockcount: 0
    Fragment:  Address: 0    Number: 0    Size: 0

      ctime: 0x4d812f93:d2d91b5c -- Wed Mar 16 15:45:55 2011
      atime: 0x4d812f93:d2d91b5c -- Wed Mar 16 15:45:55 2011
      mtime: 0x4d812f93:d2d91b5c -- Wed Mar 16 15:45:55 2011

     crtime: 0x4d812f93:d2d91b5c -- Wed Mar 16 15:45:55 2011
    Size of extra inode fields: 28
    EXTENTS:
    lines 1-12/12 (END)
list_deleted_inodes Displays a list of delete inodes in the file system.
You can do a lot with debugfs. We don’t have the time or space to cover everything here.
Have a look at the debugfs man page for more information. 

xfs_admin The xfs equivalent of tune2fs. This utility actually uses the xfs_db executable to modify various parameters of an xfs file system. The file system must be unmounted before you can make any changes. Options you can use with this utility include the following: 
–j   Enables version 2 log format, which is a journal format supporting larger log buffers. 
–l   Displays the file system’s label. 
–u   Displays the file system’s universally unique identifier (UUID).
–L label Sets the file system’s label.
–U uuid Sets the UUID of the file system.

xfs_info Displays useful information about xfs file systems.
xfs_metadump Dumps xfs file system metadata (such as filenames and file sizes) to a file.
Let’s next look at how you identify open files on a Linux file system. 

Checking for Open Files
You can use the lsof command at the shell prompt to display a list of open files. Entering lsof at the shell prompt without any options lists all open files belonging to all active processes on the system. Useful options you can use with lsof include the following:
-s Displays file sizes.
-t Specifies that lsof should produce terse output with process identifiers only and no header. This is useful if you are piping the output of lsof to the kill command.
-u user_name Displays only open files associated with the specified user.
In the next example, a list of open files associated with the tux user on the system are displayed by lsof: 
openSUSE:/sbin # lsof -u tux | more


Identifying Processes Using Files 
In addition to listing open files, you may also need to identify processes running on your Linux system that are using a particular file in the file system. The fuser command displays the PIDs of processes using the specified files or file systems. By default, each filename is followed by a letter indicating the following: 
  • Current directory 
  • e   Executable being run
  • f   An open file
  • F   A file that is open for writing 
  • The root directory
  • m    Memory mapped file or shared library


Options you can use with this command include the following: 
–a Show all files specified. By default, only files that are accessed by at least one process are shown. 
–k   Kill processes accessing the file
–i   Ask the user for confirmation before killing a process.
–u   Append the user name of the process owner to each process ID number (PID).
In the following example, you can see that the tux user is running the /usr/bin/top executable and that the process using the file has a PID of 9823: 
openSUSE:/sbin # fuser -u /usr/bin/top
/usr/bin/top:         9823e(tux)

Mounting Removable Media 
One of the more difficult aspects of working with a Linux system for those coming to Linux from Windows tends to be the subject of how to use removable media, including optical discs and USB drives. Using these types of storage on a Windows system is trivial. You just insert the disc and everything just works. 
Using removable media with a Linux system, on the other hand, takes a little more effort. However, with a little bit of practice, it will become second nature to you. In this part of the chap- ter, you’ll learn how to make it all work. Specifically, we’re going to discuss the following tasks: 
Working with optical devices
Working with USB and FireWire devices 
Let’s begin by discussing how to work with good ol’ optical discs. 
Working with Optical Devices 
Just as you mount partitions, you must also mount optical drives, such as CDs, DVDs, or Blu- ray discs, before you can use them. The only real difference is the mount point used and the file system type. For most optical discs you can use the file system type of iso9660 with the mount command. For Blu-ray discs, you specify a file system type of udf. If you’re unsure what file system type to use, just specify auto and let the mount command figure it out for you. 
You can mount optical discs anywhere you want in the file system. However, by convention they are usually mounted in /media or /mnt, depending on your distribution. 
To mount a disc on a Linux system, insert it in your optical drive. Change to your root account and then enter mount –t iso9660 device mount_point. For example, if you want to mount the disc into /media/dvd, you would enter mount –t iso9660 /dev/sr0 /media/dvd, as shown here: 
openSUSE:/ # mount -t iso9660 /dev/sr0 /media/dvd/


Most distributions also implement a symbolic link file named /dev/ cdrom that points to /dev/sr0. 
As with hard disk partitions, you can make the mounting process for optical devices easier by adding an entry to your /etc/fstab file. Consider the following: 
/dev/sr0    /media/dvd    iso9660    noauto,user,sync    0    0

Adding this line makes it so you only need to enter mount /dev/sr0 to mount an optical device. As with any mounted file system, you should use umount to unmount an optical disc before ejecting it. 

Working with USB and FireWire Devices 
All Linux distributions support external storage devices using a USB or FireWire interface. Be- cause Linux is plug-and-play compatible, it should automatically detect a new device when you connect external storage (such as a flash drive or an external hard drive). 
Most Linux distributions will address these devices as a SCSI device. Your external devices are addressed through /dev/sdx. The value of x depends on how many hard drives you already have installed in the system. It will always be addressed as the last SCSI device in your system. For example, if you have a SATA hard drive and a SATA DVD drive, your external USB or FireWire device will be /dev/sdc. 
To verify this, you can check your /var/log/messages file after connecting the device. You should see a kernel message indicating that a new SCSI disk was attached. If necessary, you can use the same procedure discussed previously to create a partition on the device and then create a file system. If a file system has already been created on the device, you only need to mount it. 
You mount USB and FireWire devices in the same way you mount a typical hard disk partition. One issue that comes up here is that you may not be entirely sure what file system is being used on the external device, especially if the drive is being used among several different computers with differ- ent operating systems. Because of this, I recommend that you use the –t auto option with the mount command. This will cause mount to try to detect for you what file system has been used on the device. 
Other than that, mounting a USB or FireWire device is relatively easy. Simply enter mount –t auto device mount_point. For example, if I want to mount an external flash drive that is the fourth device in the system in the /media/THUMBDRIVE directory, I would enter mount –t auto /dev/sdd /media/THUMBDRIVE
Backing Up Data 
One of the key roles you must perform as a Linux system administrator is to ensure that the data on the systems you are responsible for is protected. One of the best ways you can do this is to back up the data. Having a backup creates a redundant copy of the data so that if a disaster occurs, the data can be restored. 
It’s important that you remember that data is stored, for the most part, on mechanical devices in your Linux system. Hard drives have motors and other moving parts that slowly wear out over time. In fact, hard drives usually have a mean time between failures (MTBF) value assigned to them by the manufacturer. This MTBF basically gives you an estimate of how long a drive will last before it fails. Basically, it’s not a matter of if a hard drive will fail; it’s a matter of when
I relate this because I want you to take backing up seriously. Many system administrators, frankly, get very lazy about running backups. It’s an easy task to blow off, thinking “I’ll run a backup tomorrow....” Before you know it, it has been weeks since the last time a backup was run. 
If you’re employed by an organization to maintain their systems and you fail to run backups, I can just about guarantee you will lose your job if a disaster happens. Several years ago, I partici- pated on a support call with a system administrator whose server had completely died. He had called us in a last-ditch effort hoping that we would have some kind of magic solution that would get his data back. When we told him that we couldn’t and that he would have to reinstall and restore from his backups, there was silence on the other end of the phone for a long time. He got a little choked up and whispered, “I don’t have any backups. I’m going to lose my job over this.” Take my advice, develop a backup plan and stick to it religiously. 
There are several components to a backup plan. In this part of the chapter, we’re going to discuss the following: 
Selecting a backup medium Selecting a backup strategy Using Linux backup utilities 
Let’s begin by discussing how to select a backup medium. 
Selecting a Backup Medium 
Back in the “old days,” we really only had two choices for backing up data: floppies and a tape drive. Floppies didn’t hold much and were notoriously unreliable, so most system admins opted for tape drives. 
Today, some admins still use tape drives to back up their data. Tape drives use magnetic tape, much like the tape used in an 8 mm video camera, to store data. Tape drives store a lot of data and are relatively inexpensive. They are an acceptable choice for backing up data on your Linux system. 
However, tape drives have several limitations. First of all, they wear out. Tape drives have a lot of mechanical parts that wear out over time. The tape itself is run over the top of a read/write head  in the tape drive, so it wears out over time as well due to friction. Second, tapes are relatively slow. Backing up large quantities of data can take hours. 
As a result, many admins are exploring other media for backing up system data. For a while, read/write CDs and DVDs were in vogue for backups. However, modern hard drives are really big, making optical discs obsolete for running system-wide backups. Backing up a 3TB hard drive with DVDs today is similar to backing up a 100MB hard drive with floppy diskettes 20 years ago. Yes, it can be done, but it’s a painful, time-consuming process. Unless the amount of data you’re backing up is relatively small, I don’t recommend this option. 
Another option that many system administrators are exploring (and one that I really like per- sonally) is the use of removable USB hard drives. Back in the old days, we used tape drives to back up our hard drives because hard disks were extremely expensive. The general rule of thumb back in the early 1990s was “$1 per megabyte” when buying a hard disk. It was just too expensive to use hard drives for backups. In addition, we didn’t have a hot-swappable external interface for hard drives. Therefore, tape was one of the few viable options. 
Today, however, the price of hard drives has dropped dramatically. You can buy a removable USB or FireWire hard drive for less than a $0.10 per gigabyte. Using external hard drives for backups has two distinct advantages. First, they are much, much faster than tapes. Backups that took hours on a tape drive take minutes on a hard disk. Second, hard drives tend to be more reli- able than tape drives. 
Of course, using hard drives for backups isn’t a perfect solution. One of the key disadvantages is the fact that the disk partition must be mounted on the removable drive to run a backup. If something bad were to happen to the system that destroyed data on the main hard drive while the removable drive was connected, it could potentially destroy data on the removable drive as well. 
As with any backup medium, you have to weigh the benefits and drawbacks when deciding to use external hard drives for backups. 
Selecting a Backup Strategy 
When creating a backup plan, you have several different options for how you will conduct your backups. You need to consider the following: 
Selecting a backup type
Selecting a backup schedule
Determining what to back up 
Let’s start by choosing a backup type. 
Selecting a Backup Type 
Depending on the backup utility you choose, you will usually have at least three different types of backups that you can use (sometimes more). These include the following: 
Full In a full backup, all specified files are backed up, regardless of whether or not they’ve been modified since the last backup. After being backed up, each file is flagged as having been backed up.
Incremental During an incremental backup, only the files that have been modified since the last backup (full or incremental) are backed up. After being backed up, each file is flagged as having been backed up.
Differential During a differential backup, only the files that have been modified since the last full backup are backed up. Even though they have been backed up during a differential backup, the files involved are not flagged as having been backed up.
To determine your backup strategy, you need to select from the preceding backup types. For example, you could run a full backup every time. This strategy is thorough and exhaustive. It’s also the fastest strategy when you need to restore data from a backup.
However, full backups can take a very long time to complete. This is because every single file is backed up regardless of whether or not it was changed since the last backup. Therefore, many administrators mix full backups with incremental or differential backups.
. If you use a full/incremental strategy, you run a full backup once a week, usually when the sys- tem load is lightest, such as Friday night. Then you run incremental backups each of the other six days in the week. Using this strategy, you should end up with one full backup and six incremental backups for each week. The advantage of this strategy is primarily speed. Because incrementals only back up files that have changed since the last full or incremental backup, they usually run very fast. The drawback to this strategy is that you must restore six backups in exactly the correct order. The full backup is restored first, followed by the first incremental, then the second incre- mental, and so on. This can be a slow process.
Alternatively, you can also use full backups with differentials. In this strategy, you run a full backup on Friday night, usually. Then you run a differential each of the other nights of the week. Remember that a differential backup only backs up files that have changed since the last full backup, not since the last differential. Therefore, each day’s backup gets progressively bigger. The main advantage to this strategy is that restores are really fast. Instead of seven backups to restore, you only have to restore two—the last full backup first, followed by the last differential backup (and no others!). The disadvantage to this method is that the differential backups start out very fast but can become almost as long as a full backup by the time you reach the last day in the cycle. 
Whichever strategy you choose, you should be sure to keep a rotation of backups. Many administrators will rotate their backup media so that they have three to four weeks’ worth of past backups on hand. You never know when a file that was deleted two weeks ago will suddenly be needed again! 
In addition, you should be sure to verify your backups. Most backup utilities provide you with the option of checking your backup after it’s complete against the original files. If you don’t, you may have errors in your backup. 
Now that you’ve selected a backup type, you next need to set a backup schedule. 
Selecting a Backup Schedule 
You can use whatever backup schedule works best for you. However, most admins work on a weekly rotation, as discussed previously. Pick one day for your full backup and then the remaining days of the week for your incremental or differential backups. 
You should also schedule your backups to occur when the load on the system is at its lightest. Late in the evening or in the early morning are usually best, depending on your organization’s schedule. 
Finally, you need to specify what will be backed up. Let’s discuss this next. 


Determining What to Back Up 
Most Linux systems you’re going to be working with will probably consume a fairly large amount of disk space, depending on the packages you’ve installed. You need to decide how much of this consumed disk space is going to be backed up. 
One option is to back up the entire system. This is a safe, thorough option. However, it’s also somewhat slow due to the sheer amount of data involved. Many administrators choose not to do this. Instead, they only back up critical data on the system, such as user data and configuration information. The theory behind this strategy is that you could, in the event of a disaster, simply reinstall a new system and then restore the critical data to it. If you choose this strategy, you should consider backing up the following directories in your Linux file system: 
/etc
/home /opt
/root /var
/srv 

Notice that this strategy doesn’t back up your Linux system or its utilities. Instead, it only backs up your configuration files, your user data, your log files, and your web/ftp files. 
Once you’ve determined what to back up, the next part of your plan is to determine what you’ll back it up with. 
Using Linux Backup Utilities 
When working with Linux, you have a host of different utilities at your disposal to conduct a backup. Many come with the operating system; others can be obtained from third parties. For your LPIC-1/Linux+ exam, you need to be familiar with the tools that are common to most distributions and are run from the shell prompt. In this part of the chapter, we’re going to look at the following: 
Using tar
Using cpio
Using dd to copy data 
Let’s begin by looking at the venerable tar utility. 
Using tar 
The tar utility has been around for a very long time and is a very commonly used Linux backup tool. The acronym “tar” stands for tape archive. The tar utility takes a list of specified files and copies them into a single archive file (.tar). The .tar file can then be compressed with the gzip utility on your Linux system, resulting in a file with a .tar.gz extension. This is called a tarball. 
The tar utility can be used to send backup jobs to a variety of backup media, including tape drives and removable hard disk drives. The syntax for using tar to create backups is tar –cvf filename directory. The –c option tells tar to create a new archive. The –v option tells tar to work in verbose mode, displaying each file being backed up onscreen. The –f option specifies the name of the tar archive to be created. 
For example, if you wanted to create a backup of the /home directory named backup.tar on an external USB hard drive mounted in /media/usb, you would enter tar –cvf /media/usb/backup .tar /home, as shown in this example: 
openSUSE:/ # tar -cvf /media/usb/backup.tar /home

Notice in this example that a message stating tar: Removing leading ‘/’ from member names is displayed. When a tar archive is created, absolute paths are converted to relative paths by default. As a result, the leading / is removed. 
Option 
Function 
–A --concatenate 
Appends tar files to an existing archive.
–d --compare 
Identifies differences between an archive file and files in the file system.
–P --absolute-names 
Causes tar to not strip leading / from filenames. 
–c --create 
Creates a new archive file. 

-J --xz 
Does one of two things. During archive creation, it compresses the new tar archive by running it through the xz utility. During extraction, it first decompresses the tar archive using the xz utility. 
–r --append 
Adds files to the end of a tar archive. 
–t --list 
Lists the contents of an archive file.
–x --extract 
Extracts files from an archive.
–j --bzip2 --bunzip 
Does one of two things. During archive creation, it compresses the new tar archive by running it through the bzip2 utility. During extraction, it first decompresses the tar archive using the bunzip2 utility. 
–u --update 
Appends files only if they are newer than the existing files in an archive. 
–z --gzip --gunzip 
Does one of two things. During archive creation, it compresses the new tar archive by running it through the gzip utility. During extraction, it first decompresses the tar archive using the gunzip utility. 
–X filename --exclude-from 
Causes tar to exclude files listed in the text file specified. 

If you want to back up to a tape drive instead of a USB drive, you could do this by replacing the filename parameter in the tar command to the device name for your tape drive. On most distributions, the first SCSI tape drive in the system is referenced through /dev/st0. Therefore, you could enter tar –cvf /dev/st0 /home if you wanted to run the same backup as in the previous example, but send it to a SCSI tape drive instead. 
To restore a tar archive, simply enter tar –xvf filename. For example, to extract the archive we just created, you would enter tar –xvf /media/usb/backup.tar. This will extract the archive into the current working directory. If the archive has been zipped, you can also use the –z option to unzip the archive before extracting it. 
In addition to tar, you can also use cpio to create backups. Let’s discuss how it is used next. 
Using cpio 
The cpio utility can also be used to make archive files just like tar. A key difference between tar and cpio is the fact that you must provide cpio with a list of files and directories to back up from the standard input. This can be done using cat to display the contents of a file or by generating a listing using find or ls. 
For example, let’s suppose you want to back up the contents of the /home/tux/myproject directory on your Linux system. To use cpio to do this, you must somehow generate a listing of files and directories in this directory and send it to cpio. Then you must redirect the output from cpio to a file. This can be done using the find utility, discussed earlier. You could switch to /home/ tux/myproject and then enter find . –print –depth | cpio –ov > /media/usb/backup.cpio. The find utility will generate a listing of all files in the current directory. Because the –print option was used, find will print the full name of each file to the screen (the standard output). Because the –depth option was also used, find checks the contents of each directory before processing the directory itself. 
If there are no subdirectories in the MyProjects directory, an easier way to do this would be to enter ls | cpio –ov > /media/usb/backup.cpio 
The key here is the fact that we’ve piped the standard output from find to the standard input of cpio. The cpio utility uses this as a list of files and directories to archive. The –o option tells cpio to create a new archive. The –v option simply tells cpio to run verbosely, displaying the name of each file and directory as it’s processed. Finally, we have to redirect the standard output from cpio (the archive) to a file in the file system. This is done by entering > followed by the name of the archive file. 
You can also compress a cpio archive by adding the gzip utility to the pipe[md]for example, ls | cpio –ov | gzip > /media/usb/backup.cpio.gz
To restore files from a cpio archive, you run cpio from the shell prompt using the –i option and specifying the name of the archive to process. When you do, the archive files will be extracted into the current working directory. For example, we could extract the archive we just created by entering cpio –iv < /media/usb/backup.cpio, like this: 
openSUSE:/tmp # cpio -iv < /media/usb/backup.cpio
If the cpio archive was compressed with gzip, you will have to run it through the gunzip command to decompress the archive before you can extract files from it with cpio. This is shown here: 
openSUSE:/media/usb # ls
backup.cpio.gz  backup.tar
openSUSE:/media/usb # gunzip backup.cpio.gz
openSUSE:/media/usb # ls
backup.cpio  backup.tar
As you can see, the backup.cpio.gz file is decompressed by the gunzip command into the backup.cpio file, which can now be manipulated with the cpio command. Another way to do this is to add the –c option to the gunzip command and then pipe the results to cpio –i. The –c option tells gunzip to write the output of the command to the standard output, leaving the original file intact. Essentially, this just writes the uncompressed filename to the standard out, which is then sent to cpio for processing. Here is an example: 
openSUSE:/media/usb # ls
backup.cpio.gz  backup.tar
openSUSE:/media/usb # gunzip -c backup.cpio.gz | cpio –i
341 blocks
openSUSE:/media/usb # ls
accessdb  acpidump  acpisrc  backup.cpio.gz  backup.tar
As you can see, the gunzip command extracted the name of the file compressed in the gzip archive and sent it to the cpio –i command, which then extracted the three files out of the archive to the local directory. 
Using dd to Copy Data 
You can use the dd command to convert and copy files. You can copy all kinds of Linux data with this command, including entire partitions. You may be thinking, “Big deal, I already know how to use cp and mv.” Actually, the dd utility is quite useful. The key difference between dd and other file copy utilities is the fact that it copies data using records. The default size for a record is 512 bytes. 
Let’s first look at how you copy a file with dd. The syntax is dd if=input_file of=output_file. Use the if= (input file) option to specify the file to be copied. Use the of= (output file) option to specify the name of the new file. Here is an example: 
openSUSE:/ # dd if=/home/tux/MyProject/acpidump of=/home/tux/acpidump.copy

You can modify the default record size using the bs=block_size option. 
To copy an entire partition, you enter dd if=device_file of=output_file at the shell prompt. The device file of the partition is used as the input file. All the contents of the partition are written to the output file specified. In the example that follows, the dd command is used to copy the /dev/ sdb1 partition to a file named partitionbackup in the root user’s home directory: 
openSUSE:/ # dd if=/dev/sdb1 of=/root/partitionbackup

The dd command can even create an image file of an entire hard disk. The syntax again is dd if=device_file of=output_file. The difference is that you simply specify the device file of the hard disk itself instead of a partition. In the next example, the entire /dev/sdb hard drive is archived into the drivebackup file: 
openSUSE:~ # dd if=/dev/sdb of=/mnt/bigdrive/drivebackup

Another useful feature of dd is the fact that it can create a backup copy of your hard drive’s master boot record (MBR) and partition table. Again, this is possible because it looks at data as records. The syntax is dd if=device_file of=output_file bs=512 count=1. This tells dd to grab just the first 512K block of the hard drive, which is where your MBR and partition table reside. This is shown in the following example: 
openSUSE:/tmp # dd if=/dev/sda of=/root/mbrbackup bs=512 count=1


No comments:

Post a Comment