Red Hat Linux 7 Unleashed

Red Hat Linux 7 Unleashed

By William Ball

When the System Crashes

The best time to deal with a system crash is before the crash happens. This means being prepared with a good backup plan, good backups, emergency boot disks, and copies of important files. These issues are covered in this section, along with tips and hints for maintaining your filesystem integrity and system security.

Here are some Do's to help you avoid problems:

Here are some Don'ts that will help you avoid errors:

Running as root

Don't use Linux as root all of the time. Although you might be tempted, some very good reasons exist for not doing this. First, even though you might have aliased the rm command to rm -i in your .bashrc file, a simple rm -fr /* will wipe out not only your Linux system but also any DOS or Windows partitions mounted under /mnt. Instead, create a user for yourself and use the su command when you need to do things as the root operator.

Creating a Boot Disk

One of the most important things to do during your Red Hat Linux install is to make a boot disk. You should always have a working copy on hand.

If you skipped creating a boot disk, don't worry! Because you're a Red Hat Linux user, you'll find Erik Troan's handy mkbootdisk command installed on your system. Use mkbootdisk to create an emergency boot disk you can use to boot Linux in case LILO or your boot manager gets wiped out.

First, log in as the root operator. Next, use the uname command with the -r, or release number, option on the command line of your console or X11 terminal window like this:


   # uname -r
2.2.16-22

As you can see, the uname has returned 2.2.16-22 as the release number of the currently running Linux kernel. Next, use mkbootdisk with its --device option, followed by the device name of your floppy drive and the release number of your Linux kernel returned by the uname command:


   # mkbootdisk --device /dev/fd0 2.2.16-22

Insert a blank floppy disk and press the Enter key. You then see this:

Insert a disk in /dev/fd0. Any information on the disk will be lost.
Press <Enter> to continue or ^C to abort:

Press Enter to make your boot disk. When the command finishes, remove the floppy, label it, and place it in a safe place.

Another method of safeguarding your system is to make a secondary backup set of two emergency disks that use a minimal filesystem. These disks can help get you started on the road to recovery if you run into problems. Some excellent guides, scripts, and software are available to help you create your own diskette set (see "For More Information," at the end of this chapter). Generally, the approach is to create two disks, one containing a kernel and the other containing a compressed filesystem with a minimal directory of files, including file utilities.

The first disk is used to boot a kernel Linux, while the second is used as a "root" diskette with a minimal filesystem and a set of utilities (such as any required kernel modules, a text editor, tape restore commands, and the mount command). The approach is to then try to mount your existing Linux filesystem to make a repair.

Ackpht! Argggh! I've Deleted My Document!

If you accidentally delete a text file, don't panic. Here's a handy tip, called "Desperate person's text file undelete," from Paul Anderson's The Linux Tips HOWTO, courtesy of Michael Hamilton.

Assuming you remember some of the text, know which partition the file was on, and have a spare partition with some room, you should be able to recover a good portion of the file. Hamilton's approach uses the egrep and strings commands. For example, if you lose a 100-line file with the phrase Xena, followed by Lawless, and have room on your DOS partition, use this code:


   # egrep -100 'Xena.+Lawless'/dev/hda3 > /mnt/dos/lucy

Then you can look for the text with this included:


   # strings /mnt/dos/lucy | less

This approach to file recovery uses the egrep command to search for a string of text directly on your Linux partition. (Remember that everything under Linux is a file.) Each 100 lines of text before and after any match are saved into the designated file. Note, however, that this technique won't help you recover or undelete a binary file.

Your File Toolbox

You should also learn about and know how to use some of the file tools included with Red Hat Linux. Some of these tools, such as the e2fsck command, will be run automatically during booting as a diagnostic or if there is structural damage to any filesystem being mounted. Other commands, such as dumpe2fs and debugfs, can be used to provide detailed technical information concerning your Linux filesystem, while still others, such as badblocks, can be helpful if you have a non-IDE hard drive.

The following sections describe just some of the programs available.

e2fsck

Most Linux users choose to use ext2, the second extended filesystem, and with good reason: ext2 is robust, efficient, speedy, and relatively impervious to fragmentation. The staple companion utility, e2fsck, has nearly 20 command-line options that can help you check and repair a problem filesystem.

For safety's sake, unmount the problem partition first. To diagnose and repair a device such as /dev/hda3, try this:


   # e2fsck -p /dev/hda3

badblocks

The badblocks command searches a device for bad blocks and also has a number of options. Beware of the -w option; it is a "write-mode" test and will destroy data on a partition.

fsck

The fsck command, a front-end program for other filesystem commands such as e2fsck, checks and repairs Linux filesystems. Be sure to read its man page, because the -P option can be harmful.

dump and restore

The dump command can be used for filesystem backup because it can search for files that need to be backed up. dump can also do remote backups. The companion program is restore, which also works across networks. These commands may be used as preventive measure to guard against data loss.

dumpe2fs

The dumpe2fs command dumps your filesystem information. You'll get the inode count, block count, block size, last mount, and write time. Running dumpe2fs on a 450MB partition generates a 26,000-character report. An interesting part of the report is the mount and maximum mount count, which determines when e2fsck is run on a partition when Linux starts.

tune2fs

If you just have to mess with your system's performance, you can use the tune2fs command to adjust its tunable parameters—but only if you have an ext2 filesystem. Use this command, along with its -c option, followed by a number, to adjust when e2fsck is run on a particular ext2 partition, but don't do it when the partition is mounted.

mke2fs

Linux hackers will be familiar with the mke2fs program, which creates a Linux second-extended filesystem on a partition. You might need it, too, if you want to create compressed filesystems on emergency disks or if you install a new hard drive.

debugfs

debugfs is an ext2 filesystem debugger, with 35 built-in commands. If you call it with the following you can examine your filesystem in read-only mode:


   # debugfs /dev/hda3

Each of these utilities can help you maintain, diagnose, and repair a filesystem. What if you can't boot? Read on.

Red Hat to the Rescue! When the System Won't Boot

A Linux system might not boot for any of a number of reasons. The rdev command sets the root device following a kernel rebuild. If you use this command incorrectly after a rebuild, trying to use LILO or LOADLIN to load the new kernel won't work, and you could have problems booting. You'll also have problems if you've rebuilt the kernel and hard-coded in the wrong root device in your /etc/lilo.conf file.

I told you earlier that you'll appreciate being a Red Hat user. Here's another good reason: You get a set of emergency boot disks with your Red Hat distribution. If your system won't boot, here's how you might be able to recover your system:

First, always make a couple of emergency boot disks. Use the mkbootdisk command to create a boot disk with your Linux kernel. Then use the dd command to create a second disk containing the rescue.img file from the Red Hat Linux CD-ROM. Use the mount command to mount the CD-ROM:


   # mount /mnt/cdrom

Log in as the root operator, insert a blank floppy disk in your computer (label it rescue ), and then create the rescue disk, using dd to copy the rescue.img file:


   # dd if=/mnt/cdrom/images/rescue.img of=/dev/fd0 bs=1440k

Boot Linux from your Red Hat Linux boot disk. Next, at the boot: prompt, type rescue , which loads a kernel from the disk. Follow the prompts and, when asked, eject the boot disk and insert the second rescue disk. You'll end up with a # bash shell prompt.

Under the /bin directory, you'll find a minimal set of programs to help you recover your files. Remember, the idea is to at least get you where you can try to check your existing partitions and possibly mount your drive. For example, if you have a Linux partition on /dev/hda5, you can try to first create a mount point by using the mkdir command, and then use mnt to mount your partition:


   # mkdir /mnt/linux

   # mount -t ext2 /dev/hda5 /mnt/linux

Your Linux partition will be found under /mnt/linux, and you can then attempt a fix or copy important files.

If you've installed Red Hat Linux and for some reason your system won't boot—and you don't have your Red Hat boot disks—you can also try booting directly from the first Red Hat Linux CD-ROM. (You might have to change your BIOS settings to alter the boot device sequence from your hard drive or floppy drive.)

You can also try to reboot your computer to DOS, change the directory to the CD-ROM and then DOSUTILS, and then type AUTOBOOT , which will execute the AUTOBOOT.BAT batch file and put you into the Red Hat installation process.

For More Information

For more information about the linuxconf tool, navigate to http://www.solucorp.qc.ca/linuxconf.

To subscribe to the linuxconf mailing list, put the following text into the body of an email message addressed to linuxconf-request@solucorp.qc.ca:


   subscribe linuxconf

For information regarding the Linux boot process, a host of handy tips on building boot disks, pointers to bootdisk packages, and a number of helpful scripts, see Tom Fawcett and Graham Chapman's Bootdisk HOWTO at http://www.linuxdoc.org.

You should also look for the following rescue packages and other helpful utilities at http://metalab.unc.edu/pub/Linux/system/recovery:

Read the man pages for the following commands on your Red Hat Linux system:

If you ever lose or destroy your copies of the Red Hat Linux boot disks, you can get replacements at http://www.redhat.com/download/mirrors.html.

For details on how 4.4BSD boots, see Tabbed Section 1 of the 4.4BSD System Manager's Manual. For details about other UNIX boot processes, see UNIX Unleashed: System Administrator's Edition.

For loads of tips on maintaining your system and background information about various Linux filesystems, see Lars Wirzenius's Linux System Administrators' Guide 0.6.2. You'll find a copy through http://www.linuxdoc.org.

If you're interested in a Linux filesystem defragmenter, check out Stephen Tweedie and Alexei Vovenko's defragmenter, which you'll find at http://metalab.unc.edu/pub/ Linux/system/filesystems/defrag-0.70.tar.gz.

Share ThisShare This

Informit Network