/dev/xvda1 should be checked for errors

Amazon EC2 Ubuntu instances sometimes display this message to the terminal when you ssh in:

**** /dev/xvda1 should be checked for errors ****

/dev/xvda1 refers to the first partition of the hard drive (which is actually a virtualized hard drive running under Xen). This partition (likely, the only partition) contains your machine’s file system. To run a file system check, we like to use the fsck command. But this can only be used when the file system is not in use, so we’ll tell the system to run it upon reboot. And to do that we need to temporarily change two files: /etc/fstab (the file system table), and /etc/default/rcS (a small configuration file).

About fstab:

LABEL=cloudimg-rootfs / ext4 defaults,discard 0 0
/var/swap.1 swap swap defaults 0 0

The file system table (/etc/fstab) contains information that allows the machine to automatically mount disk partitions. That is, it prepares them for access by assigning them a location (mount point) on the file system tree. For the partition that contains the file system, the table assigns it to the root directory, which is designated by a forward slash / in the second field.

The first field in an fstab entry contains the device node, in this case by it’s file system label. If you look in /dev, you’ll see many devices, including the disk (“xvda”) and its partitions (“xvda1”). You’ll also see the \dev\disk directory, and if you look in there you’ll see that you can list devices by-label or by-UUID.

Enter
ls -l /dev/disk/by-label
and you’ll see something like:

lrwxrwxrwx 1 root root 11 Jul 25 21:15 cloudimg-rootfs -> ../../xvda1

which shows that the label cloudimg-rootfs is just a link to the device /dev/xvda1.

The last field (in the fstab entry, not the ‘ls -l output) is what we’re interested in changing here. It’s the “pass number” and indicates the order in which file system checks are done. “0” tells it not to run fsck, “1” tells it to run fsck on the root partition, and “2” tell it to check other partitions. We’re going to change it to “1”.

About rcS:

The /etc/default/rcS file contains six variables that change the behavior of various boot scripts. (The “rc” term, as in .bashrc and rc.local, has some history behind it and may stand for “run commands” or “run configuration,” or something like that.) The variable we’re interested in is FSCKFIX, which, when enabled with a “yes”, will tell fsck to always repair the file systems without asking for further permission.

The repair:

  1. Edit fstab:
    sudo vim /etc/fstab
    

    Look for the line describing the root (/) file system, and change the last ‘0’ to a ‘1’.

    LABEL=cloudimg-rootfs   /    ext4   defaults,discard    0 1
    /var/swap.1 swap swap defaults 0 0
    

    To do that with vim:
    a. Enter insert mode by hitting ‘i’.
    b. Exit insert mode with the ‘esc’ key.
    c. Save and exit by typing :wq.

  2. Edit rcS:
    sudo vim /etc/default/rcS
    

    Look for the line #FSCKFIX=no and under it type:

    FSCKFIX=yes
    
  3. Add in the /forcefsck trigger.
    sudo touch /forcefsck
    

    In the script /etc/init/mountall.conf, there’s a line which looks for the /forcefsck file, and upon finding it, will direct fsck to perform a full file system check.

  4. Reboot.
    sudo reboot
    
  5. Delete the line FSCKFIX=yes in the rcS file.
  6. Change the line in /etc/fstab back to a ‘0’.
    (The /forcefsck file was deleted for you by mountall.conf.)