CTS 2301C (Unix/Linux Administration I) Project #4
Working with Filesystems

 

Due: by the start of class on the date shown on the syllabus

Description:

This project assignment is designed to give you experience in creating, formatting, mounting, and unmounting Linux filesystems. 1  You will create a disk image file and mount it.  The commands you will use (and which you should lookup in the man pages) are dd, df, fsck, losetup (LOopback SETUP), mkfs, mount, umount, tune2fs, fuser, lsof, and the file /etc/fstab (also documented in the man pages).

Disk image files can be used to create bootable media and RAM disks. 2  An image of an entire CD (or of many CDs) can be stored on a hard disk.  (This is how CD duplicators or juke-boxes work.)

Make sure you keep an accurate system journal of any and all changes you make to your system!  You will need to turn this in, along with the answers to the questions asked below.

Requirements:

Answer the following questions and perform the following tasks:

Part I

  1. Create an image file, let's call it “myfs.img”.  An image file is just regular file that holds a copy of a filesystems.  They must be carefully created and formatted.  To create a 10 MB filesystem you start by creating a 10 MB file.  (Verify you have 10 MB of free disk space before starting this!  If not, use a smaller count of say 1000.)  Use the dd command for this:
    dd if=/dev/zero of=/tmp/myfs.img bs=1k count=10000
    

    In this example the image file was created in /tmp.  By default on many systems, that will be a RAM disk.  That is no problem, as long as you complete this project without a reboot (or the image file will be lost).  If that may be an issue, make sure to create the image file some place that isn't a RAM disk, such as “~/tmp/myfs.img”.  Where ever you put it, make sure you have sufficient disk space for it!  In the rest of this project, we will assume you put it in /tmp.

  2. Attach the image file to a loopback device using the losetup (LOopback SETUP) command, which must be run as root.  The various disk commands work with devices, not files.  So use /dev/loop0 (or another available loopback device) which will be associated with the image file:
    losetup /dev/loop0 /tmp/myfs.img
    

    It is unlikely in our classroom setup, but loop0 may be in use by some other program.  Use the command “losetup -la” to see the status of any in-use loopback devices.  If necessary use a different one such as /dev/loop1.  The directions below assume you are using /dev/loop0, so you should substitute the acutal one you used.

  3. At this point, the system will see /dev/loop0 as an unformatted disk.  (You can see this by running lsblk.)  Use the mkfs command to format it (and thus the attached image file) as an ext4 filesystem.  (The type for a CD would be iso for Linux and HSFS for Solaris.  ISO image files can also be created and mounted, usually read-only though.)  The command mkfs is actually a common front-end for a family of related commands, one for each filesystem type.  What is the actual command ultimately invoked to create an ext4 filesystem?  (Use the man pages to determine this.)
        mkfs -t ext4 /dev/loop0 10000
    

    Examine the output.  Did the command succeed?  How many inodes were created?
  4. Now we are ready to mount our new filesystem.  The first step is to create a new directory to serve as its mount point.  Then the loop device previously associated with the image file can be mounted from the command line:
        mkdir /mnt/myfs
        mount -t ext4 /dev/loop0 /mnt/myfs
    
  5. Run the df command.  How much free space shows for your filesystem?  Why is this less than 10 MB?
  6. Change your current directory to the root of the new filesystem, using “cd /mnt/myfs”.  Run the command “ls -a”.  What files and/or directories were created automatically?  What is their purpose?
  7. Create a file on your filesystem using vi (or some other method):
        cd /mnt/myfs
        echo hello > foo
    

    Now run the df command again.  Why is the size is more (or less) than the original size minus the six bytes of file foo?

    Finally, run the command “ls -li”.  What is the inode number of your new file (“foo”)?

  8. Next, try to unmount the filesystem by running this command:
        umount /dev/loop0
    

    What happened?  It didn't work, because you cannot unmount a filesystem if it is in use.  If the current working directory of any process (such as your shell) is any directory in that filesystem, or if any process has any file open from that filesystem (such as vi) then the device will be considered busy.

    Use the commands fuser and lsof to see what files are in use and what processes are using them.  (See the man pages for details on these commands.)  Run the commands “fuser -cuv /mnt/myfs” and “lsof -Rw +D /mnt/myfs”.  What was the output?  What is the meaning of the options used?

  9. It is likely that the only process keeping your filesystem busy is your shell, since /mnt/myfs is your current directory.  Try to unmount the filesystem again, after changing the current working directory to your home directory:
        cd
        umount /dev/loop0
    

    What happened this time?

  10. Run the fsck command on your filesystem.  The fsck command is actually a common front-end for a family of related commands, one for each type of file system.  What is the actual command that is run?  (Use the man pages to determine this.)

    Run this command and note the output:
        fsck -CVfp /dev/loop0
    
    What is the purpose (or meaning) of each of these options used with fsck?  (Hint: some options are for the filesystem-specific command, not the general fsck command, so look at both man pages to determine the meaning of the options.)
  11. Now we will corrupt the image deliberately, and then re-run fsck to fix it.  There are many ways to corrupt the image, such as using a tool such as hexedit on the image file.  But it is hard to know exactly where to corrupt the image, in a way that matters and that can be detected and repaired.

    Instead you will use a tool “debugfs” that can be used to examine and edit image files.  You will change the link count field of your new file from “1” to “2”.  You will need to know your file's inode number in order to run this command (learned in a previous step).  In the command line below, the inode number for the file foo is “13”.  If you inode number is different, use that number instead.  Once you have unmounted the filesystem (which you did in a previous step), run this command, including the quotes and the angle-brackets:

        debugfs -wR 'sif <13> links_count 2' /dev/loop0
    

    If this works, there is no output.  (This command is part of the “e2fsprogs” package; you can install it as root with “dnf install e2fsprogs”.)  Now re-run the same fsck command as before.  What was the output this time?

  12. The tune2fs command is used to examine and optionally modify the superblock of ext[234] filesystems.  Use the man page to determine how to use this command to list the default values used in your filesystem.  What are the default values for ext4 filesystems for:
    • max mount counts (how many mounts are allowed before a fsck check is forced)
    • interval between checks (How much time is allowed to pass before a fsck check is forced)
    (Note this information is also reported when the filesystem was created.)
  13. To break the association (“detach”) between your image file and the loopback device use:
         losetup -d /dev/loop0
    
  14. To have your filesystem mounted automatically at boot time and to allow easy mounting without specifying all the details, you must edit the /etc/fstab file.  Each line of this file contains what to mount, the mount point (where to mount it), the mount options to use, and other information.  To automatically mount your filesystem at boot time add this entry to the /etc/fstab file (make a copy of the file before editing it.):
        /tmp/myfs.img    /mnt/myfs     ext4   owner,loop  0 0
    
    What is the meaning of the fields and options used?  (Read the man page for fstab to find out.)  What is the meaning of the mount options used?  (Read the man page for mount to find out.)
  15. Try out your changes to fstab and verify they work, but using a simple command to mount your filesystem:
        mount /mnt/myfs
    
    Note that even without the /etc/fstab line, the command
        mount -t ext4 -o loop,owner /tmp/myfs.img /mnt/myfs
    
    will work.  (But who wants to type that each time?)  In this case mount will automatically associate /tmp/myfs.img with an unused loop device, and umount will automatically break the connection (detach the file from the loopback device).  Earlier we merely took advantage of the fact that /dev/loop0 was already associated with /tmp/myfs.img, so different mount options were used.

    Files in /tmp eventually will be automatically deleted, either during a reboot or from a cron job.  What will happen if you don't remove the line from /etc/fstab before a reboot?  Would that still happen, if you added the mount option “noauto” to the fstab entry?  Go ahead and remove that entry now, to avoid any future problems.
  16. Suppose you had a large number of CDs (and a very large hard disk).  How can you make all the CD contents available on the system, as a jukebox?
  17. Using the techniques learned above, explore some of the image files on your Install/Live CDWhat are the pathnames of some of the image files?  Why might there be more than one image file?  (The file command can be helpful to determine the type of these image files, so you can use the right tools to examine them.)

Part II:  Using XFS

XFS is the Red Hat default filesystem for enterprise systems, and is popular with large storage systems.  While the concepts of storage volumes you learned for ext4 apply, the command are different, and some parameters cannot be changed.  (Disk quotas also work differently.)

  1. To practice XFS, you must make sure you have installed The XFS utilities.  You will also need a larger image file (or XFS will refuse to format it.)  If not already installed, run this command:
    dnf install xfsprogs
    

    Then, after verifying you have sufficient space, create a larger image file:

    dd if=/dev/zero of=/tmp/myfs.img bs=1k count=100000
    
  2. As before, connect the image file to the loopback block device /dev/loop0, then format as type XFSWhat are the exact commands you used?  Examine the output.  What is the block size?  How large are inodes?
  3. Find the XFS command that allows you to view and change the volume's label and UUID, and run it.  What is the current volume label for the volume?  What is the UUID?  (Another way to view this information is by using the command “lsblk --fs”.)

    Note that disk partitions (but not logical volumes) also can have labels and UUIDs that are independent of the ones for the volume.  You can view these with the command (run as root) “lsblk --fs -o +PARTLABEL,PARTUUID”.

  4. Change the volume label to “xfsvolume”, and confirm the new label is set correctly, and that the UUID hasn't changed.  What were the exact commands you used?
  5. Now mount your device by specifying its label, not the device name (so, don't use /dev/loop0 in your command).  Now unmount the volume.  Finally, mount it again by specifying the UUID and not the device name.  What were the exact commands you used?  Which one is commonly preferred, and why?
  6. Finally, unmount the volume, detach the loop device from your image files, and delete both image files.  (Don't forget to remove the entry for your ext4 image from the /etc/fstab file.)

Part III:  Using BtrFS

BtrFS is the Fedora default filesystem for workstations since Fedora 33.  Unlike ext4 or XFS, BtrFS is different and incorporates many LVM concepts.  In this part, create a BtrFS volume as you did in parts I and II and explore various commands (as root):

  1. btrfs filesystem show
  2. btrfs subvolume list /
  3. btrfs subvolume create /tmp/foo
    btrfs subvolume list /
  4. btrfs subvolume delete /tmp/foo
    btrfs subvolume list /
  5. useradd --btrfs-subvolume-home auser; passwd auser
    btrfs subvolume list /
  6. btrfs quota enable /home/auser
    btrfs subvolume list / # note the quota group (qgroup) number
    btrfs qgroup create gqroup number
    btrfs quota rescan /
    btrfs qgroup show /
    btrfs qgroup limit 1G /home/auser
    btrfs qgroup show -re /
  7. userdel -r auser

Check online and in the man pages to understand these commands.

To be turned in:

The answers and journal entries from the above list describing the steps you have taken to create and use filesystems.

You can submit your project as email to .  Please see your syllabus for more information about submitting projects.

 


Footnotes:

  1. Although the concepts apply to Unix systems as well as Linux systems, some commands and kernel features (such as ramfs, ext4 tools, and loop devices) are different or not available on most Unix systems.  For Solaris, see the “lofiadm” command to work with the loop devices, “/dev/lofi/num”, where num is 1, 2, 3, ...      Back
  2. Creating a true RAM disk is rarely done anymore, except when booting (see “man initrd”).  This is because the virtual memory system is so efficient.  To create a real RAM disk is easy provided you kernel is configured to include the “ramfs” and/or the “tmpfs” filesystem.  Then:
            mkdir /mnt/ramdisk
            mount -t tmpfs none /mnt/ramdisk
    
    That's it, the RAM disk is already formatted!  You can now create files there.  All files will be lost when the ram disk is unmounted, of course.  To preserve them, you could use dd to make an image file first.       Back