Thursday, 28 March 2013

RHCSA: Mounting Filesystems with /etc/fstab

The /etc/fstab file is used on Linux systems to control the mounting of filesystems, both at boot time and as they are introduced to the system by a user, such as a USB flash drive. It's a file that used to hold great mystery to me and in my earlier days as a Linux user causing me to inadvertantly screw up the filesystem mounts and trash a few systems when I didn't know how to troubleshoot it correctly. Fortunately, once you have spent some time looking at the /etc/fstab file it really isn't so difficult to understand.

Here's a sample /etc/fstab:

 [root@cruithne ~]# cat /etc/fstab   
 #  
 # /etc/fstab  
 # Created by anaconda on Wed Mar 27 21:22:12 2013  
 #  
 # Accessible filesystems, by reference, are maintained under '/dev/disk'  
 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info  
 #  
 /dev/mapper/vg_cruithne-lv_root /            ext4  defaults    1 1  
 UUID=59376313-6474-445e-8af7-b175d9ea4fb5 /boot          ext4  defaults    1 2  
 /dev/mapper/vg_cruithne-lv_home /home          ext4  defaults    1 2  
 /dev/mapper/vg_cruithne-LogVol02 swap          swap  defaults    0 0  
 tmpfs          /dev/shm        tmpfs  defaults    0 0  
 devpts         /dev/pts        devpts gid=5,mode=620 0 0  
 sysfs          /sys          sysfs  defaults    0 0  
 proc          /proc          proc  defaults    0 0  

Admittedly, it doesn't look that easy to understand initially. It's a bit easier once you know that the file is simply a line per filesystem with six fields on each line:

  • Field 1: The device or filesystem to be mounted. 
  • Field 2: Mount point, ie where you would like the device/filesystem to appear on your box.
  • Field 3: Filesystem Format: Examples here are ext3, ext4, ReiserFS, iso9660, smb, swap and nfs.
  • Field 4: Mount options. You can put options in here to set if the filesystem should be mounted at boot, noauto option prevents a filesystem from being automatically mounted with the mount -a command, acl enables access control lists on a filesystem. Too many options to list here but fully documents in the man pages for fstab.
  • Field 5: Dump Value. Determines the archiving frequency on the filesystem. Set this to 1 for daily, 2 for every other day etc. Setting to 0 disables the feature.
  • Field 6: Filesystem Checking. This field determines the order that filesystems are checked by fsck at boot time. Root should be 1, other local filesystems 2 and removable devices set to 0 to disable fsck checking.
So now you know what those fields mean, it might be useful to have an example. I've plugged in a USB stick to my machine, I'd like it to always appear at /mnt/USB. The first thing I like to to is check to see what it has been identified by as by the kernel. I like to use the command dmesg | tail as below:

 [root@cruithne ~]# dmesg | tail  
 sd 4:0:0:0: [sdb] Write Protect is off  
 sd 4:0:0:0: [sdb] Mode Sense: 23 00 00 00  
 sd 4:0:0:0: [sdb] Assuming drive cache: write through  
 sd 4:0:0:0: [sdb] Assuming drive cache: write through  
  sdb: sdb1  
 sd 4:0:0:0: [sdb] Assuming drive cache: write through  
 sd 4:0:0:0: [sdb] Attached SCSI removable disk  
 EXT4-fs (sdb1): recovery complete  
 EXT4-fs (sdb1): mounted filesystem with ordered data mode. Opts:   
 SELinux: initialized (dev sdb1, type ext4), uses xattr  

We can see quite clearly that it has been identified as /dev/sdb with a single partition /dev/sdb1. Let's check it out with fdisk:

 [root@cruithne ~]# fdisk -cul /dev/sdb  
 Disk /dev/sdb: 4027 MB, 4027580416 bytes  
 168 heads, 62 sectors/track, 755 cylinders, total 7866368 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 identifier: 0x00090022  
   Device Boot   Start     End   Blocks  Id System  
 /dev/sdb1      2048   7866367   3932160  83 Linux  
 [root@cruithne ~]#   

At the bottom there we can see that the partition type is 83, suitable for using on Linux systems. Let's format it with ext4 so that we can start using it to save files:

 [root@cruithne ~]# mkfs.ext4 /dev/sdb1  
 mke2fs 1.41.12 (17-May-2010)  
 Filesystem label=  
 OS type: Linux  
 Block size=4096 (log=2)  
 Fragment size=4096 (log=2)  
 Stride=0 blocks, Stripe width=0 blocks  
 245760 inodes, 983040 blocks  
 49152 blocks (5.00%) reserved for the super user  
 First data block=0  
 Maximum filesystem blocks=1006632960  
 30 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  
 Writing inode tables: done                
 Creating journal (16384 blocks): done  
 Writing superblocks and filesystem accounting information: done  
 This filesystem will be automatically checked every 39 mounts or  
 180 days, whichever comes first. Use tune2fs -c or -i to override.  
 [root@cruithne ~]#   

Now we need to mount it in our folder:

 [root@cruithne ~]# mkdir /mnt/usb  
 [root@cruithne ~]# mount /dev/sdb1 /mnt/usb  
 [root@cruithne ~]# mount | grep /dev/sdb1  
 /dev/sdb1 on /mnt/usb type ext4 (rw)  
 [root@cruithne ~]#   

Ok, we can see that it's successfully mounted in there. This isn't going to survive a reboot though, to do that we'll need to open our /etc/fstab and enter the line at the bottom:

 # /etc/fstab  
 # Created by anaconda on Wed Mar 27 21:22:12 2013  
 #  
 # Accessible filesystems, by reference, are maintained under '/dev/disk'  
 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info  
 #  
 /dev/mapper/vg_cruithne-lv_root /            ext4  defaults    1 1  
 UUID=59376313-6474-445e-8af7-b175d9ea4fb5 /boot          ext4  defaults    1 2  
 /dev/mapper/vg_cruithne-lv_home /home          ext4  defaults    1 2  
 /dev/mapper/vg_cruithne-LogVol02 swap          swap  defaults    0 0  
 tmpfs          /dev/shm        tmpfs  defaults    0 0  
 devpts         /dev/pts        devpts gid=5,mode=620 0 0  
 sysfs          /sys          sysfs  defaults    0 0  
 proc          /proc          proc  defaults    0 0  
 /dev/sdb1        /mnt/usb        ext4  defaults    0 0  

In short, we are mounting /dev/sdb1 at mount point /mnt/usb, it's formatted with ext4 and will be using the default mount options. We've added 0 0 at the end to disable the dump option and fsck checking on this drive.

Let's see if that's worked, unmount the drive and then use the mount -a command to remount anything in fstab that uses the auto option (included in the "defaults" directive we used).

 [root@cruithne ~]# umount /mnt/usb/  
 [root@cruithne ~]# mount | grep /dev/sdb1  
 [root@cruithne ~]# mount -a   
 [root@cruithne ~]# mount | grep /dev/sdb1  
 /dev/sdb1 on /mnt/usb type ext4 (rw)  
 [root@cruithne ~]#   

In the commands above, I've unmounted the drive from its mount point and then used grep to see if it is still mounted. I've used mount -a to remount anything listed in /etc/fstab and then used grep to check again. We can see this time that /dev/sdb1 has been mounted at /mnt/usb as we specified.

That's a very basic run-through of what we can do. We can also use /etc/fstab to automatically mount remote filesystems such as Samba and ftp shares. I'll cover these in a part 2 when I get round to doing it.



No comments:

Post a Comment