Linux Exercise: Linux Filesystems

This exercise assumes you've done the LVM exercise already. So you should have an ExtraVG with one LV on it, containing an XFS filesystem.

Creating and mounting filesystems

  1. Create two additional 50 MB logical volumes in your ExtraVG. Format these with the ext2 and ext4 filesystems. Create mount points for these partitions and mount them. Look at the free sizes on these filesystems to check their overhead.
    • # lvcreate -n Ext2LV -L 50M ExtraVG
    • # lvcreate -n Ext4LV -L 50M ExtraVG
    • # mkfs -t ext2 /dev/ExtraVG/Ext2LV
    • # mkfs -t ext4 /dev/ExtraVG/Ext4LV
    • # mkdir /mnt/ext2fs
    • # mkdir /mnt/ext4fs
    • # mount /dev/ExtraVG/Ext2LV /mnt/ext2fs
    • # mount /dev/ExtraVG/Ext4LV /mnt/ext4fs
    • # mount
    • # df
      ext4 and xfs filesystems have a 1-4 MB overhead due to their journaling functionality, while ext2 is able to use almost all of the available space for files.
  2. Modify your /etc/fstab file so that all filesystems are mounted upon reboot. Reboot your system and check these three filesystems are mounted.
    • # vi /etc/fstab
      /dev/ExtraVG/ExtraLV /mnt/extra xfs defaults 0 0
      /dev/ExtraVG/Ext2LV /mnt/ext2fs ext2 defaults 0 2
      /dev/ExtraVG/Ext4LV /mnt/ext4fs ext4 defaults 0 2
      Note that an XFS filesystem is checked automatically when it is mounted. It is therefore not necessary to check this during a reboot. That's why the last column is 0.
    • # reboot
    • # mount

Maintaining filesystems

  1. Perform a dump on your /dev/ExtraVG/Ext2LV filesystem. When was it last checked?
    • # dumpe2fs /dev/ExtraVG/Ext2LV
    • # dumpe2fs /dev/ExtraVG/Ext2LV | grep "Last checked"
  2. Try to check your /dev/ExtraVG/Ext2LV filesystem. Does this work?
    • # fsck /dev/ExtraVG/Ext2LV
      You should get a warning or an error because the filesystem is mounted read/write. A read/write filesystem is by definition in an inconsistent state (due to write caching in the kernel) so it's not a good idea to let fsck fix the errors.
    • # dumpe2fs /dev/ExtraVG/Ext2LV | grep "Last checked"
  3. Remount your /dev/ExtraVG/Ext2LV filesystem read-only. Can you now check this filesystem?
    • # mount -o remount,ro /dev/ExtraVG/Ext2LV
    • # fsck /dev/ExtraVG/Ext2LV
      You will get a warning about the filesystem being mounted.
    • # fsck -f /dev/ExtraVG/Ext2LV
      This time the check should work.
    • # dumpe2fs /dev/ExtraVG/Ext2LV | grep "Last checked"
  4. Unmount your /dev/ExtraVG/Ext2LV filesystem altogether. Can you now check this filesystem?
    • # umount /dev/ExtraVG/Ext2LV
    • # fsck /dev/ExtraVG/Ext2LV
      Again, this time the check should work.
    • # dumpe2fs /dev/ExtraVG/Ext2LV | grep "Last checked"

Maintaining filesystem space

  1. Make a list of all free/used/total blocks on all filesystems.
    • # df
  2. Make a list of all free/used/total inodes on all filesystems.
    • # df -i
  3. Find out which of the directories inside your / filesystem take up the most space.
    • # cd /
    • # du -k --max-depth=1
  4. Go to the directory that takes up most space, and figure out which subdirectories in here take up most space.
    • # cd /usr
    • # du -k --max-depth=1

Working with hard and symbolic links

  1. In your home directory, create a subdirectory "links". Go to this directory. Create a file "abc" with some content.
    • # mkdir ~/links
    • # cd ~/links
    • # echo "This is abc" > abc
  2. Look at the characteristics of the file abc. In particular, note its inode number, its size and its permissions.
    • # ls -li abc
    • # stat abc
  3. Create a hard link named "def" to the file abc. Look at the characteristics of this file.
    • # ln abc def
    • # ls -li abc def
    • # stat abc
    • # stat def
  4. Add some content to the file abc. Then look at the content of the file def. Is this what you expected?
    • # echo "This is some more content" >> abc
    • # cat def
  5. Modify the permissions of the file def. Then look at the permissions of abc. Is this what you expected?
    • # chmod 400 def
    • # ls -li abc def
    • # stat abc
    • # stat def
  6. Look at the filesystem characteristics and note the exact amount of free blocks and free inodes.
    • # df
    • # df -i
  7. Delete the file abc. Watch what happened to the def file.
    • # rm abc
    • # ls -li def
    • # stat def
    • # cat def
  8. Look at the filesystem characteristics again. Did the amount of free blocks and free inodes increase? Why not?
    • # df
    • # df -i
      The amount of free blocks and free inodes did not change: Deleting a file which has two links, only deletes the references to this file (which are contained in a directory), but not the inode or the file data itself.
  9. Create another hard link, this time called ghi. Create this not in the current directory but in /mnt/ext2fs. Does this work?
    • # ln def /mnt/ext2fs/ghi
      This will not work since /mnt/ext2fs is on a different filesystem. Hard links cannot work cross-filesystems since inodes are not unique across filesystems.
  10. Delete the file def.
    • # rm def
  11. Look at the filesystem characteristics again. Did the amount of free blocks and free inodes increase now? Why?
    • # df
    • # df -i
      This time the amount of free blocks and free inodes did increase: As we deleted the last reference to the file, the inode and the data blocks were added to the free list.
  12. Now perform the same steps as above, but use symbolic links.
    • # echo "This is abc" > abc
    • # ls -li abc
    • # stat abc
    • # ln -s abc def
    • # ls -li abc def
    • # stat abc
    • # stat def
    • # echo "This is some more content" >> abc
    • # cat def
    • # chmod 400 def
    • # ls -li abc def
    • # stat abc
    • # stat def
    • # df
    • # df -i
    • # rm abc
    • # ls -li def
    • # stat def
    • # cat def
    • # df
    • # df -i
    • # ln -s def /mnt/ext2fs/ghi
    • # rm def
    • # df
    • # df -i
    • # rm /mnt/ext2fs/ghi
    • # df
    • # df -i
End of exercise