Skip to content

Latest commit

 

History

History
253 lines (197 loc) · 9.56 KB

L09.md

File metadata and controls

253 lines (197 loc) · 9.56 KB
layout title <!--license
default
Working with file systems
  • TOC {: toc}

Introduction

This lab is intended to give you practice with using the fdisk, mount and find commands.

The fdisk command

  1. Execute the command

    $ ls -l /dev/sd*

    {: .code} which device has multiple partitions associated with it?

  2. Use fdisk to examine the partition table.

    $ sudo fdisk /dev/sda
    
    Welcome to fdisk (util-linux 2.27.1).
    Changes will remain in memory only, until you decide to write them.
    Be careful before using the write command.
    
    
    Command (m for help): p

    {: .code} Answer the following questions:

    • What is the total size of the drive in GiB?
    • What is the size of a sector in bytes?
    • How many sectors are there?
    • Calculate the total size of the drive in bytes, based on the number of sectors and the number of bytes per sector. Convert your answer to GiB. Does this answer match your answer above for the total size of the drive?
    • Which partitions are Windows partitions?
    • Which partitions are Linux partitions?
    • Which is the Linux swap partition?
  3. Notice that you can use fdisk with the -l option if all you want to do is list the partition information, e.g.

    student@C17775462-lnx:~$ sudo fdisk -l /dev/sda
    Disk /dev/sda: 477 GiB, 512110190592 bytes, 1000215216 sectors
    Units: sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 4096 bytes
    I/O size (minimum/optimal): 4096 bytes / 4096 bytes
    Disklabel type: dos
    Disk identifier: 0x6fce0ecf
    
    Device     Boot     Start        End   Sectors   Size Id Type
    /dev/sda1  *         2048    1023999   1021952   499M  7 HPFS/NTFS/exFAT
    /dev/sda2         1024000  601147391 600123392 286.2G  7 HPFS/NTFS/exFAT
    /dev/sda3       601149438 1000214527 399065090 190.3G  5 Extended
    /dev/sda5       601149440  933351423 332201984 158.4G 83 Linux
    /dev/sda6       933353472 1000214527  66861056  31.9G 82 Linux swap / Solaris
    
    Partition 3 does not start on physical sector boundary.

    {: .code} This is the output from a machine in one of the network labs. You will may see something different when you do this on a machine in a different environment.

  4. It is also possible to use fdisk with a disk image file, e.g.

    $ fdisk -l disk.img
    Disk disk.img: 2.5 GiB, 2684354560 bytes, 5242880 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
    Disklabel type: dos
    Disk identifier: 0x000e6d7d
    
    Device    Boot   Start     End Sectors  Size Id Type
    disk.img1 *       2048 3145727 3143680  1.5G 83 Linux
    disk.img2      3147774 5240831 2093058 1022M  5 Extended
    disk.img5      3147776 5240831 2093056 1022M 82 Linux swap / Solaris

    {: .code}

    To try this out for yourself you'll need to obtain a disk image file. The most efficient way to do this is to copy the file from the NAS drive on the off-campus network.

    Direct your browser to http://nas3.offcampusnetwork.co.uk and login using the username student and the usual password. Use the file app on the home page to browse to modules -> KF4005 and then download the file disk.img (right-click on the file name and select Download).

    Try out the fdisk command on your disk.img file, as shown above.

The mount and umount commands

  1. Create a directory to act as a mount point, e.g.

    $ mkdir mnt

    {: .code}

  2. Enter a command to mount the larger of the Windows partitions shown in the fdisk output above. Use your new mount point mnt. Remember that the mount command is used like this: sudo mount <partition name> <mount point>

  3. Enter a command to find out how many log files (files with the extension .log) there are in the Windows directory of the newly mounted partition.

  4. What happens if you try to mount the same partition again?

  5. A file system can be "unmounted" using the command umount. The umount command is used with the name of the directory where the file system is mounted, e.g. sudo umount mnt. Use this command to unmount the Windows partition that you mounted earlier.

  6. Use fdisk to examine the partition table of the raw disk image file that you downloaded earlier. Answer the following questions.

    • What is the total size of the drive in GiB?
    • What is the size of a sector in bytes?
    • How many sectors are there?
    • Calculate the total size of the drive in bytes, based on the number of sectors and the number of bytes per sector. Convert your answer to GiB. Does this answer match your answer above for the total size of the drive?
    • Which partitions are Linux partitions?
    • Which is the Linux swap partition?
  7. Enter a command to mount the first Linux partition in the raw disk image to your mount point. Remember that you need to use the loop option so that mount sees the raw disk image file as a block device. You also need to specify the offset of the partion in bytes, using the offset option. Refer to the lecture slides if you need a reminder about how to do this.

  8. How many .txt files are there in the /usr/share/doc/git directory of the file system that you have just mounted?

  9. Mounting LVM volumes from a disk image is trickier and outside the scope of this module. However, if you're interested, there's a brief tutorial here.

The find command

find is a very powerful command that can be used to explore a file system. You call it by specifying the directory from which you want the exploration to begin. For example, find / will just list all the files in the file system (you may need to use sudo to avoid permission denied errors for those directories that you do not have permission to read). If you just want to list the files starting at the /bin directory, use find /bin. Some very useful options with find are

  • -name : restrict your search to files whose names match a pattern, e.g.
$ find /usr/share -name "*.txt"

{: .code} finds all .txt files, starting at the /usr/share directory.

  • -exec : execute a command on each file that matches the search, e.g.
$ find /usr/share -name "*.txt" -exec grep Linux {} \;

{: .code} finds all .txt files, starting at the /usr/share directory, which contain the string Linux. You can print out the name of each matching file by adding the -print option, e.g.

$ find /usr/share -name "*.txt" -exec grep Linux {} \; -print

{: .code}

  • -printf : print file metadata, e.g.
$ find /bin -printf "%Ax;%p"

{: .code} prints the last access data and the file names of all of the files in the /bin directory. Use man find and look at the -printf option for all the metadata and formatting possibilities.

Develop your own ideas about useful data and how to display it. Experiment using the command line.

The tar command

The tar command stores and extracts files from a tape or disk archive. tar is a flexible command and has numerous options. As always, you can use man to find out about the full set of options. For now you should just practice the most frequent use cases.

  1. Write a complete directory (and all sub-directories to an archive), e.g.

    $ tar cf fred.tar fred

    {: .code} copies the entire contents of the fred directory to a file called fred.tar. Notice that c is the option to tar that causes an archive to be created. The f option allows you to specify the name of the file to hold the archive. By convention this file name should have the file extension .tar. The final word on the command line specifies the name of the directory that should be archived.

  2. You can cause the archive to be save in compressed form by adding an additional option to the tar command, e.g.

    $ tar zcf fred.tgz fred

    {: .code} Here the addition of the z option causes the archive to be compressed using gzip. The convention here is to give the file name the extension .tgz to indicate that it is a gzipped, tar file.

  3. You can examine the contents of a tar file using the t option, e.g.

    $ tar tf fred.tar

    {: .code} will list the contents of fred.tar, and

    $ tar ztf fred.tgz

    {: .code} will list the contents of the gzipped, tar file, fred.tgz.

  4. You can extract and verify the contents using the x option to extract and the v option to verify, e.g.

    $ tar xvf fred.tar

    {: .code} will extract the contents of fred.tar, and

    $ tar zxvf fred.tgz

    {: .code} will extract the contents of fred.tgz.

Try the following exercise...

  1. Change the current working directory to /usr/share
  2. Use ls to explore the gtk-doc directory that you'll find there.
  3. Create a gzipped, tape archive of the gtk-doc directory. Call the archive gtk-doc.tgz and make sure that it is saved in your home directory.
  4. Change your current working directory to your home directory.
  5. List the contents of gtk-doc.tgz to check it contains what you'd expect.
  6. Extract the contents of gtk-doc.tgz.
  7. Use ls to explore the contents of the directory that you've just extracted. Check that it contains the same files as in the original directory.
  8. Delete gtk-doc and gtk-doc.tgz from your home directory. {: .exercise}