Saturday, November 29, 2008

How to increase number of disk mounts before next fsck at system boot

Many home users often power off their computers when they are not being used. Some do it to be green: turning idle computers off saves electricity and $$. Others do it for the extra security. To those who are hard-core Linux geeks, machine uptime is sacred, and voluntarily rebooting the machine is nothing but sacrilegious.

If you do reboot your machine from time to time, you most definitely have encountered a most annoying experience. Once in a while, while the computer is booting up, you see the message /dev/hdaN has reached maximal mount count, check forced. The check seems to take forever, and the system boot won't resume until the check is over.

The check refers to a file system check performed using the fsck command. For many Linux distributions, by default, the system will do a fsck check on a file system after it has been mounted 30 times, which means after 30 reboots. This is the maximum mount count before fsck is performed on the file system.

You can specify a maximum mount count for each individual partition on your hard drive. To find out the maximum mount count for /dev/hda1, execute this command as root:
$ tune2fs -l /dev/hda1 | grep 'Maximum mount count'
Maximum mount count: 30

Note that the tune2fs command is only applicable for ext2 and ext3 file systems.

The tune2fs command can also tell you how many times a file system has actually been mounted since the last fsck check.
$ tune2fs -l /dev/hda1 |grep 'Mount count'
Mount count: 17

To increase the maximum mount count, you will use the same tune2fs command but with the -c option.

Note that you should not modify the maximum mount count which is a file system parameter while the file system is mounted. The recommended way is to boot your system using a Linux Live CD, and then run tune2fs.

For me, I happened to have a Ubuntu 7.10 Live CD at my desk. I inserted the Live CD to boot up my system. Then, I opened a Terminal window, sudo -s, and executed the following commands.

First, I reminded myelf of how the /dev/hda disk is partitioned:
$ fdisk -l /dev/hda
Disk /dev/hda: 82.3 GB, 82348277760 bytes
255 heads, 63 sectors/track, 10011 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/hda1 * 1 31 248976 83 Linux
/dev/hda2 32 10011 80164350 5 Extended
/dev/hda5 32 10011 80164318+ 8e Linux LVM

To increase the maximum mount count to 50 for /dev/hda1:
$ tune2fs -c 50 /dev/hda1
tune2fs 1.40.2 (12-Jul-2007)
Setting maximal mount count to 50

If there are more than 1 file system on your hard drive, you should stagger the maximum mount count for the different file systems so that they don't all trigger the lengthy fsck at the same time. For example, set the maximum mount count to 40, 50 and 60 for /dev/hda1, hda2, and hda3 respectively.

In the above case, /dev/hda5 is a physical LVM volume. You cannot run tune2fs on physical LVM volumes directly.
$ tune2fs -l /dev/hda5
tune2fs 1.40.2 (12-Jul-2007)
tune2fs: Bad magic number in super-block while trying to open /dev/hda5
Couldn't find valid filesystem superblock.

You need to run tune2fs against each logical LVM volume. To find out their names, cat /etc/fstab.
$ tune2fs -c 60 /dev/mapper/myhost-root 
tune2fs 1.40.2 (12-Jul-2007)
Setting maximal mount count to 60

Saturday, November 8, 2008

How to open and close the CD DVD tray

You can open/close the CD, DVD disk tray from the command line.

Many Linux users already know about the eject command for opening the disk tray:
 $ eject

How do you close the tray?

Turns out that you can use the same eject command but with an additional -t option to close the tray.

$ eject -t 

Before I found out about the -t option, I always had to reach and press the open/close button on the drive to close the tray. With the -t option, you can now both open and close the tray from the command line.

There is another option -T (CAPITAL T, that is) you should know.

eject -T
basically closes the tray if it is open, and opens the tray if it is closed.

The man page for eject has detailed explanation about the options.

Sunday, November 2, 2008

Two additional ways to tail a log file

I want to revisit a topic: how to tail a log file. My earlier posts discussed the use of tail and less commands to tail a log file, and multitail if you need to tail multiple files at once.

This followup article discussed two other ways to tail a log file: the use of the most command, and to tail within emacs.

The most command bills itself as a replacement for less. Like its predecessors less and more, most is a file pager program.

To install most on my Debian Etch system:
$ apt-get update && apt-get install most

To page the log file say /var/log/messages:
$ most /var/log/messages

This opens the log file, and displays the first page.

To tail the file, put most into Tail Mode by pressing the F key (capital F).

You should see the following line in the status area at the bottom of the tail window.
Most Tail Mode--  MOST keys are still active.

If a new line is appended to the log file, most will automatically reposition the file to the end and display the new line.

Note that you can scroll the file or do a search when you are in Tail Mode. For example, you can press the PageUp/PageDown, UpArrow/DownArrow keys to scroll the file. Also, you can press / to search forward or ? to search backward. However, when you press a key in Tail Mode, you break out of Tail mode in the sense that newly appended lines are not automatically displayed in the window. Yet, you can hit F again to re-enter Tail Mode, or simply use the DownArrow scroll key to scroll past the end of file to fetch the new lines.

To quit most, press the q key.

For my fellow emacs users out there, another way to tail a log file is to do it right within emacs.

You need the tail-file elisp function which is in the emacs-goodies-el package. Debian users can install this package like this:
$ apt-get update && apt-get install emacs-goodies.el

To tail a file in emacs: start emacs, hit M-x (Alt and x keys together), and type tail-file. Then, enter the filename to tail. The net result is that this will spawn an external tail -f process.

Note that you can't read the entire file under emacs using tail-file, only the tail initially and the newly appended lines portion afterwards.