0% found this document useful (0 votes)
5 views

Laboratory work 6

Uploaded by

tynysbekn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Laboratory work 6

Uploaded by

tynysbekn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 51

Mounting Filesystems

Introduction
As requirements for an active system grow and the number of users increases, it is often necessary to
add additional hard drive space. When you add a drive to the system, you will need to create one or
more partitions. Partitions are used to divide a hard drive into smaller chunks. Even if you don't want to
divide a hard drive into smaller chunks, you will need to at least create a single partition to make use of
the drive.
Once you have a partition, you will need to place a filesystem on the partition. After you create the
filesystem, you will make the partition available via a mount point.
You may also need to add additional swap space to the system, either in the form of a swap partition or
swap file. Swap space is virtual memory that is used by the system when RAM becomes full.

Step 1
Log in to the system using the root account. When prompted, provide the root password:

root
netlab123
Ubuntu 18.04.2 LTS ubuntu tty1

ubuntu login: root


Password:
Welcome to Ubuntu 18.04.2 LTS (GNU/Linux 4.15.0-45-generic x86_64)

* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage

The programs included with the Ubuntu system are free software:
the exact distribution terms for each program are described in the individual
files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable


law.
root@ubuntu:~#

Step 2
Use the fdisk command to display the current partitions:
fdisk -l
root@ubuntu:~# fdisk -l
Disk /dev/sda: 9.8 GiB 10485760000 bytes, 20580000 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 identifierL 0x9f6a80de

Device Boot Start End Sectors Size Id Type


/dev/sda1 * 2048 20477951 20475904 9.8G 8e Linux LVM

Disk /dev/sdb: 4 GiB, 4294967296 bytes, 8388608 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

[ 816.422399] print_req_error: I/O error, dev fd0, sector 0

[ 816.446441] print_req_error: I/O error, dev fd0, sector 0

Disk /dev/mapper/ubuntu—vg-root: 9.3 GiB, 994469784 bytes, 19423232 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 /dev/mapper/ubuntu—vg-swap_1: 512 MiB, 536870912 bytes, 1048576 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

The following describes key components of the output:

Device The specific partition that the row is describing. For example, /dev/sda1 is the first partition on the
first SATA hard drive.

Start The starting sector of the partition.


End The ending sector of the partition.

Blocks The size of the partition in blocks.

Id An identifier which is used to tell the kernel what type of filesystem should be placed on this
partition. For example, the value 83 indicates that this partition should have an ext2, ext3, or etx4
filesystem type.

System A human-readable name that indicates the type of filesystem the Id column refers to. For
example, 83 is a Linux filesystem.

Step 3
In this step, we will begin creating a new partition on the sdb drive. Execute the following command to
start this process:

fdisk /dev/sdb
root@ubuntu:~# fdisk /dev/sdb
Welcome to disk (util-linux 2.31.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.


Created a new DOS disk label with disk identifier 0xdcd69338.

Command (m for help):

Step 4
At the Command prompt, type the m command to display the help section:

m
root@ubuntu:~# fdisk /dev/sdb
Welcome to disk (util-linux 2.31.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table.
Created a new DOS disk label with disk identifier 0xdcd69338.

Command (m for help): m


DOS (MBR)
a toggle a bootable flag
b edit nested BSD disklabel
c toggle the dos compatibility

Generic
d delete a partition
F list free unpartitioned space
l list know partition types
n add a new partition
p print the partition table
t change a partition type
v verify the partition table
i print information about a partition

Misc
m print this menu
u change display/entry units
x extra functionality (experts only)

Script
l load disk layout from sfdisk script file
0 dump disk layout to sfdisk script file

Save & Exit


w write table to disk and exit
q quit without saving changes

Create a new label


g create a new empty GPT partition table
G create a new empty SGI (IRIX) partition table
o create a new empty DOS partition table
s create a new empty Sun partition table

Command (m for help):

As you can see from the output of the m command, there are many things you can do with
the fdisk utility. For this lab, we are going to focus on just two things: creating partitions and changing
partition types.

Step 5
At the Command prompt, type the p command to display the current partition table. The output should be
the same as when you previously executed the fdisk -l command:

p
Command (m for help): p
Disk /dev/sdb: 4 GiB, 4294967296 bytes, 8388608 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: 0x97566fcf

Command (m for help):

While it is not critical for you to understand all of the output of this command, a brief summary of the
more useful output is provided below:

Disk /dev/sdb Disk name (/dev/sdb) and size in MB and bytes.

Units Available units are sectors, a fixed unit of user accessible storage,
which in this case is 512 bytes, the minimum unit of storage available to
the system.

Sector size The total number of sectors is the most important since partitions are
(logical/physical) done by sectors. Note that a block and a sector are the same things, in
this case.

I/O size This is somewhat useful because if you are trying to figure out how big
an existing partition is, you can take the number of sectors and multiply
it by the value provided here. So, if a partition is 2,000 sectors in size
and there are 512 bytes per sector, then the partition is 1,024,000 bytes
in size. An easier way to think of it is that 512 bytes is 0.512 KB
(0.000512 MB). So, if a partition is 2,000 sectors in size, it is 1,024 KB
in size (or approximately 1 MB in size).
Step 6
At the Command prompt, type n to create a new partition:

n
Command (m for help): n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p):

Step 7
At the Select prompt, type the e command to specify that you are going to create an extended
partition. This will allow you to create more partitions, called logical partitions, within the extended
partition:

e
Command (m for help): n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p): e
Partition number (1-4, default 1):

Recall that you can have up to 4 primary partitions. One of those can be an extended partition. Within
the extended partition, you can create more partitions called logical partitions. It is critical to understand
this because if you use all 4 primary partitions on a hard disk, then you will be unable to create any
more partitions, even if there is unpartitioned space available on the hard disk.

Step 8
At the Partition number prompt, type 2 to specify that you are going to use the second partition
number. This will result in a partition device name of /dev/sdb2:

2
Command (m for help): n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p): e
Partition number (1-4, default 1): 2
First sector (2048-8388607, default 2048): _

Note that you never use extended partitions directly. In other words, you won't create filesystems on
extended partitions, and you won't mount them. Their sole purpose is to be a container for logical
partitions.

Step 9
The next prompt asks for the block (or sector) this partition will start on. To accept the default value,
simply press the Enter key:

Enter
Command (m for help): n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p): e
Partition number (1-4, default 2): 2
First sector (2048-8388607, default 2048):
Last sector, *sectors or +size(K,M,G,T,P) (2048-8388607, default 8388607):

Step 10
The next prompt, Last sector… asks for where the new partition will end. You can either provide the
last sector, the number of sectors or a specific size (in units of kilobytes, megabytes, gigabytes,
terabytes, or petabytes). The default value is to use the rest of the drive, which normally is what you
want to use for extended partitions. Press the Enter key to accept the default value:

Enter
Command (m for help): n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p): e
Partition number (1-4, default 2): 2
First sector (2048-8388607, default 2048):
Last sector, *sectors or +size(K,M,G,T,P) (2048-8388607, default 8388607):

Created a new partition 2 of type ‘Extended’ and of size 4 GiB.

Command (m for help):

If you wanted to specify the Last sector (think of this as the ending sector), you could just provide
the number of the sector. Unless you are going to use the rest of the drive, as you have done in this
case, specifying the Last sector is very rare. It is also fairly rare to specify +sectors (think of this as
how many sectors you want to use for this partition), as sector sizes are somewhat confusing.
In most cases, you specify the size of a partition by using +size (think of this as how much space you
want to allocate to the partition). A value of +2000K will create a 2000-kilobyte partition. A value
of +200M will create a 200-megabyte partition. A value of +2G will create a 2-gigabyte partition.

Step 11
At the Command prompt, type the p command to display the current partition table. You should see your
new partition:

p
Command (m for help): p
Disk /dev/sdb: 4 GiB, 4294967296 bytes, 8388608 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 identifierL 0xdcd69338

Device Boot Start End Sectors Size Id Type


/dev/sdb2 2048 8388607 8386560 4G 5 Extended

Command (m for help):

It is always a good idea to check your work before saving the changes. The p command shows the
current partition table, but changes haven't been made to the hard drive yet.

Step 12
Now you can create a partition that you can later format with a filesystem and access via a directory by
mounting it. To create this new partition, first type n at the Command prompt:

n
Command (m for help): n
Allspace for primary partitions is in use.
Adding logical partition 5
First sector (4096-8388607, default 4096):

Step 13
Press the Enter key to accept the default for the First sector:

Enter
Command (m for help): n
Allspace for primary partitions is in use.
Adding logical partition 5
First sector (4096-8388607, default 4096):

Step 14
At the next prompt, enter +200M to create a 200MB partition:

+200M
Command (m for help): n
Command (m for help): n
Allspace for primary partitions is in use.
Adding logical partition 5
First sector (4096-8388607, default 4096):
Last sector, +sectors or +size{K,M,G} (4096-8388607, default 8388607): +200M

Created a new partition 5 of type ‘Linux’ and of size 200 MiB.


Command (m for help):

Don't forget to type the + character before 200M.

Step 15
Type p at the Command prompt to see your new partition:

p
Command (m for help): p

Disk /dev/sdb: 4 GiB, 4294967296 bytes, 8388608 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: 0xb2c87c2e

Device Boot Start End Sectors Size Id Type


/dev/sdb1 2048 8388607 8386560 4G 5 Extended
/dev/sdb5 4096 413695 409600 200M 83 Linux
Command (m for help):

Step 16
Type w at the Command prompt to save your new partitions:

Command (m for help): w


The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

root@ubuntu:~#

Step 17
On some systems, the kernel may not know about newly created partitions yet. In these cases, the
kernel needs to be instructed to re-read the partition table that is now on the hard drive and put it into
memory. However, in this virtual environment, you can tell the partitions are immediately recognized
because of the output of the following command:

ls /dev/sd*
root@ubuntu:~# ls /dev/sd*
/dev/sda /dev/sda1 /dev/sdb /dev/sdb2 /dev/sdb5

When the kernel recognizes new partitions, this will result in new device files being automatically
created in the /dev directory. On systems where the new partitions are not recognized, the output
would be the following:

ls /dev/sd*
root@ubuntu:~# ls /dev/sd*
/dev/sda /dev/sda1 /dev/sdb

Step 18
To have the kernel recognize new partitions, reboot the system:

root@ubuntu:~# reboot

In some cases, you may be able to run a command, such as kpartx or partprobe, to have the kernel
read the new partition table. However, on this system, these commands do not exist.

Step 19
After the system finishes booting, log in to the system using the root account. When prompted, provide
the root password:

root
netlab123
Ubuntu 18.04.2 LTS ubuntu tty1
ubuntu login: root
Password:
Last login: Tue Jun 25 11:44:39 UTC 2019 on tty1
Welcome to Ubuntu 18.04.2 LTS (GNU/Linux 4.15.0-45-generic x86_64)

* Documentation: https://help.ubuntu.com/
* Management: https://landscapte.canonical.com
* Support: https://ubuntu.com/advantage

root@ubuntu:~#

Step 20
Then, verify the new files have been created in the /dev directory:

ls /dev/sd*
root@ubuntu:~# ls /dev/sd*
/dev/sda /dev/sda1 /dev/sdb /dev/sdb2 /dev/sdb5

Step 21
Execute the following command to create an ext4 partition on the /dev/sdb5 partition:

mkfs -t ext4 /dev/sdb5


root@ubuntu:~# mkfs -t ext4 /dev/sdb5
mke2fs 1.44.1 (24-Mar-2018)
Discarding device blocks: done
Creating filesystem with 204800 1k blocks and 51200 inodes
Filesystem UUID: 616fc3dc-7b69-403f-9532-289277460408
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729

Allocating group tables: done


Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
Step 22
Create a directory to use as a mount point for the new partition by using the mkdir command:

mkdir /data
root@ubuntu:~# mkdir /data
root@ubuntu:~#

Then, mount the new partition and verify that it mounted correctly by executing the mount commands as
shown:

mount /dev/sdb5 /data


mount

Step 23
Use the nano editor to edit the /etc/fstab file:

nano /etc/fstab
root@ubuntu:~# nano /etc/fstab
Add the following line to the /etc/fstab file:

/dev/sdb5 /data ext4 defaults 1 1


# /etc/fstab: static file system information.
#
# Use ‘blkid’ to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point> <type> <options> <dump> <pass>
/dev/mapper/ubuntu—vg-root / ext4 errors=remount-ro 0 1
/dev/mapper/ubuntu—vg-swap_1 none swap sw 0 0
/dev/fd0 /media/floppy0 auto rw,user,noauto,exec,utf8 0 0
/dev/sdb5 /data ext4 defaults 1 1

[ Read 10 lines ]
^G Get Help ^O Write Out ^W Where Is ^K Cut Text ^J Justify ^C Cur Pos M-U
Undo
^X Exit ^R Read File ^\ Replace ^U Uncut Text ^T To Spell ^_ Go To Line M-E
Redo

The /etc/fstab file will be used to mount filesystems automatically when the system reboots.
Type Ctrl+X to exit the nano editor. At the Save modified buffer? prompt, type the Y key.

Ctrl+x
y
Save modified buffer? (Answering "No" will DISCARD changes.)
Y Yes
N No ^C Cancel

Press Enter to save (write) the file.

Enter
File Name to Write: /etc/fstab_
^G Get Help M-D DOS Format M-A Append M-B Backup
File
^C Cancel M-M Mac Format M-P Prepend ^T
Step 24
Verify this change with the following tail command:

tail -n 1 /etc/fstab

Note that the character after the -n option is a number 1, not a lowercase letter L.

root@ubuntu:~# tail -n 1 /etc/fstab


/dev/sdb5 /data ext4 defaults 1 1

If no output is displayed for the command above, verify that the /etc/fstab file does not contain
additional empty lines and attempt the command again. Alternatively, you can increase the number of
lines value for the -n option to the tail command.

Step 25
When the system reboots, the /etc/fstab file will be used to mount filesystems automatically.
However, you don't want to reboot the system since any error in this file could cause the boot process to
fail completely. To test the new entry in this file, unmount the /data filesystem and remount it by only
specifying the mount point:

umount /data
root@ubuntu:~# umount /data
mount /data
mount
When you only specify the mount point argument when executing the mount command, the command
looks at the /etc/fstab file for the rest of the information (the partition to mount and the mount
options).
If you made a mistake in the new entry in the /etc/fstab file, you might get an error like the following:
root@ubuntu:~# mount /data
mount: can't find /data in /etc/fstab or /etc/mtab

If this happens, look at the /etc/fstab file, correct the error, and try to mount again.

Step 26
To create additional swap space, you either need to create a new partition or a new, large file. In the
first example, you will create a swap partition. Start by using the fdisk command as shown:

fdisk /dev/sdb
root@ubuntu:~# fdisk /dev/sdb

Welcome to fdisk (util-linux 2.31.1)


Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help):

Step 27
Create a new partition by entering n at the Command prompt and use the rest of the information provided
below:

n
Enter
Enter
Command (m for help): n
All space for primary partitions is in use.
Adding logical partition 6
First sector (415744-8388607, default 415744):
Last sector, +sectors or +size{K,M,G} (4415744-8388607, default 8388607):

Created a new partition 6 of type ‘Linux’ and of size 3.8 GiB.

Command (m for help):

Step 28
Change the partition type by entering the t command at the Command prompt and entering the values
provided below. When finished, enter the p command at the Command prompt to verify that your new
partition has an Id of 82:

t
6
82
p
Command (m for help: t
Partition number (2,5,6, default 6): 6
Hex code (type L to list codes): 82
Changed system type of partition 6 to 82 (Linux swap / Solaris)
Command (m for help): p

Device Boot Start End Sectors Size Id Type


/dev/sdb1 2048 8388607 8386560 4G 5 Extended
/dev/sdb5 4096 413695 409600 200M 83 Linux
/dev/sdb6 415744 8388607 7972864 3.8G 82 Linux swap / Solaris
Command (m for help):

Step 29
Type w at the Command prompt to save your new partition:

w
Command (m for help: w
The partition table has been altered.
Syncing disks.

root@ubuntu~#

Step 30
Reboot the system to have the kernel recognize the new partitions:

reboot
root@ubuntu:~# reboot

In some cases, you may be able to run a command, such as the partprobe command, to have the
kernel read the new partition table. However, on this system, this command does not exist.

Step 31
After the system finishes booting, log in to the system using the root account. When prompted, provide
the root password:

root
netlab123
Ubuntu 18.04.2 LTS ubuntu tty1

ubuntu login: root


Password:
Welcome to Ubuntu 18.04.2 LTS (GNU/Linux 4.15.0-45-generic x86_64)

* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage

The programs included with the Ubuntu system are free software:
the exact distribution terms for each program are described in the individual
files in /usr/share/doc/*/copyright.

root@ubuntu:~#

Step 32
Then, verify the new files have been created in the /dev directory:

ls /dev/sd*
root@ubuntu:~# ls /dev/sd*
/dev/sda /dev/sda1 /dev/sdb /devsdb2 /dev/sdb5 /dev/sdb6

Step 33
To format the partition as swap space with a label of myswap, execute the following mkswap command:

mkswap -L myswap /dev/sdb6


root@ubuntu:~# mkswap -L myswap /dev/sdb6
Setting up swapspace version 1, size = 3.8 GiB (4082102272 bytes)
LABEL=myswap, UUID=624b1bad-c835-44e5-3bc9f71a50a4

The UUID in the output above may be different in our virtual environment.

Step 34
Use the free command to see the current swap space for the system. Then, use the swapon command
to add the new swap partition to current swap space. Finally, use the free command again to verify the
changes:

free
swapon -a /dev/sdb6
free
root@ubuntu:~# free
total used free shared buff/cache available
Mem: 492952 44148 364068 580 84736 436084
Swap: 524284 0 524284
root@ubuntu:~# swapon -a /dev/sdb6
root@ubuntu:~# free
total used free shared buff/cache available
Mem: 492952 45228 362620 580 84804 434708
Swap: 4510712 0 4510712

Step 35
Use the nano editor to edit the /etc/fstab file:

nano /etc/fstab
root@ubuntu:~# nano /etc/fstab

To have the new swap partition enabled automatically at boot, add the following line to the bottom of
the /etc/fstab file:

LABEL=myswap none swap sw 0 0


# /etc/fstab: static file system information.
#
# Use ‘blkid’ to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point> <type> <options> <dump> <pass>
/dev/mapper/ubuntu—vg-root / ext4 errors=remount-ro 0 1
/dev/mapper/ubuntu—vg-swap_1 none swap sw 0 0
/dev/fd0 /media/floppy0 auto rw,user,noauto,exec,utf8 0 0
/dev/sdb5 /data ext4 defaults 1 1
LABEL=myswap none swap sw 0 0

^G Get Help ^O Write Out ^W Where Is ^K Cut Text ^J Justify ^C Cur Pos M-U
Undo
^X Exit ^R Read File ^\ Replace ^U Uncut Text ^T To Spell ^_ Go To Line M-E
Redo
Type Ctrl+X to exit the nano editor. At the Save modified buffer? prompt, type the Y key.

Ctrl+X
Y
Save modified buffer? (Answering “No” will DISCARD changes.)
Y Yes
N No ^C Cancel

Press Enter to save (write) the file.

Enter
File Name to Write: /etc/fstab_
^G Get Help M-D DOS Format M-A Append M-B Backup
File
^C Cancel M-M Mac Format M-P Prepend ^T

Step 36
Verify this change with the following tail command:

tail -n 1 /etc/fstab
root@ubuntu:~# tail -n 1 /etc/fstab
LABEL=myswap none swap sw 0 0

Step 37
To test the new setting in the /etc/fstab file, you could reboot the system. However, if you made any
errors, the system may end up being unbootable. A better solution for testing the new entry in
the /etc/fstab file is to execute the swapon -a after first removing the partition from swap space.
Execute the following commands:

swapon -s
swapoff /dev/sdb6
swapon -s
swapon -a
swapon -s
root@ubuntu:~# swapon -s
Filename Type Size Used Priority
/dev/dm-1 partition 524284 0 -2
/dev/sdb6 partition 3986428 0 -3
root@ubuntu:~# swapoff /dev/sdb6
root@ubuntu:~# swapon -s
Filename Type Size Used Priority
/dev/dm-1 partition 524284 0 -2
root@ubuntu:~# swapon -a
root@ubuntu:~# swapon -s
Filename Type Size Used Priority
/dev/dm-1 partition 524284 0 -2
/dev/sdb6 partition 3986428 0 -3

In the commands above, the swapon command used with the -s option demonstrates which swap
spaces are currently being used, while the swapon command used with the -a option enables all swap
devices that are listed in the /etc/fstab file.
If there were any errors in the line that you just added to this file, the swapon -a command would have
produced output error messages. For example:
root@ubuntu:~# tail -n 1 /etc/fstab
LABEL=myyswap none swap sw 0 0
root@ubuntu:~# swapon -a
swapon: cannot find the device for LABEL=myyswap

Notice that the value for LABEL was mistyped (it should only have one y, not two). If you get an error
message when executing the swapon -a command, review the entry in the /etc/fstab file, correct it
and try to execute the swapon -a command again.

Step 38
To create a new swap file, first, create a large file with the dd command. To determine a good location
for this new file, run the df -h command to see which partition has enough space to hold the swap file:

df -h
root@ubuntu:~# df -h
Filesystem Size Used Avail Use% Mounted on
udev 213M 0 213M 0% /dev
tmpfs 49M 592K 48M 2% /run
/dev/mapper/ubuntu—vg-root 9.1G 1.5G 7.1G 18% /
tmpfs 241M 0 241M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 241M 0 241M 0% /sys/fs/cgroup
/dev/sdb5 190M 1.6M 175M 1% /data
tmpfs 49M 0 49M 0% /run/user/0

Step 39
In this case, the / filesystem has plenty of space. Create a large file with the following dd command in
the /var directory (which is part of the / partition):

dd if=/dev/zero of=/var/swapfile bs=1M count=100


root@ubuntu:~# dd if=/dev/zero of=/var/swapfile bs=1M count=100
100+0 records in
100+0 records out
104857600 bytes (105 MB, 100MiB) 0.118785 s, 858MB/s

Step 40
The result should be a file that is approximately 100MB in size. Confirm this by executing the following
command:

ls -lh /var/swapfile
root@localhost:~# ls -lh /var/swapfile
-rw-r--r-- 1 root root 100M Jun 26 12:40 /var/swapfile

Step 41
Convert this file into a swap file by executing the following command:

mkswap /var/swapfile
root@ubuntu:~# mkswap /var/swapfile
mkswap: /var/swapfile: insecure permissions 0664, 0660 suggested.
Setting up swap space version 1, size = 100 MiB (104853504 bytes)
No label, UUID=fe0dfcb8-e9de-4305-bd8f-8f28c26ff997

The UUID in the output above may be different in our virtual environment

Step 42
Add the swap file to the current swap space and then confirm by executing the following commands:

swapon /var/swapfile
swapon -s
root@ubuntu:~# swapon /var/swapfile
swapon: /var/swapfile: insecure permissions 0644, 0600 suggested.
root@ubuntu:~# swapon -s
Filename Type Size Used Priority
/dev/dm-1 partition 524284 0 -2
/dev/sdb6 partition 3986428 0 -3
/var/swapfile file 102396 0 -4

Step 43
Use the nano editor to edit the /etc/fstab file:

nano /etc/fstab
root@ubuntu:~# nano /etc/fstab

To have the new swap file enabled automatically at boot, add the following line to the bottom of
the /etc/fstab file:

/var/swapfile none swap sw 0 0


# /etc/fstab: static file system information.
#
# Use ‘blkid’ to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point> <type> <options> <dump> <pass>
/dev/mapper/ubuntu—vg-root / ext4 errors=remount-ro 0 1
/dev/mapper/ubuntu—vg-swap_1 none swap sw 0 0
/dev/fd0 /media/floppy0 auto rw,user,noauto,exec,utf8 0 0
/dev/sdb5 /data ext4 defaults 1 1
LABEL=myswap none swap sw 0 0
/var/swapfile none swap sw 0 0

^G Get Help ^O Write Out ^W Where Is ^K Cut Text ^J Justify ^C Cur Pos M-U
Undo
^X Exit ^R Read File ^\ Replace ^U Uncut Text ^T To Spell ^_ Go To Line M-E
Redo

Type Ctrl+X to exit the nano editor. At the Save modified buffer? prompt, type the Y key.

Ctrl+X
Y
Save modified buffer? (Answering “No” will DISCARD changes.)
Y Yes
N No ^C Cancel

Press Enter to save (write) the file.

Enter
File Name to Write: /etc/fstab_
^G Get Help M-D DOS Format M-A Append M-B Backup
File
^C Cancel M-M Mac Format M-P Prepend ^T

Step 44
Verify this change with the following tail command:

tail -n 1 /etc/fstab
root@ubuntu:~# tail -n 1 /etc/fstab
/var/swapfile none swap sw 0 0

Step 45
To test the new setting in the /etc/fstab file, you could reboot the system. However, if you made any
errors, the system may end up being unbootable. A better solution for testing the new entry in
the /etc/fstab file is to execute the swapon -a command after first removing the swap file from swap
space. Execute the following commands to see how this is accomplished:

swapon -s
swapoff /var/swapfile
swapon -s
swapon -a
swapon -s
root@ubuntu:~# swapon -s
Filename Type Size Used Priority
/dev/dm-1 partition 524284 0 -2
/dev/sdb6 partition 3986428 0 -3
/var/swapfile file 102396 0 -4
root@ubuntu:~# swapoff /var/swapfile
root@ubuntu:~# swapon -s
Filename Type Size Used Priority
/dev/dm-1 partition 524284 0 -2
/dev/sdb6 partition 3986428 0 -3
root@ubuntu:~# swapon -a
swapon: /var/swapfile: insecure permissions 0644, 0600 suggested.
root@ubuntu:~# swapon -s
Filename Type Size Used Priority
/dev/dm-1 partition 524284 0 -2
/dev/sdb6 partition 3986428 0 -3
/var/swapfile file 102396 0 -4

Maintaining Integrity
Introduction
In order to provide the functionality you need to apply these concepts, this VM may require a minute or
two to load.
In this lab, you will explore commands that provide you with useful information regarding the directory
structure and filesystems. These commands are critical to maintaining the stability of the system.
Knowing how to monitor the usage of the filesystems allows you to make sure space is available for
both the OS and users. Determining which directories are using the most space provides you with the
knowledge to fix issues related to filesystems that become "full".
Understanding filesystem attributes and knowing how to view and modify these attributes allows you to
fine-tune the system, so it will perform better.

Step 1
Log in to the system using the root account. When prompted, provide the root password:

root
netlab123
CentOS Linux 7 (Core)
Kernel 3.10.0-957.el7.x86_64 on an x86_64

Centos login: root


Password:
[root@centos~]#

Step 2
Execute the following command to list filesystem disk space usage details:

df
[root@centos~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/VolGroup-lv_root 3997376 1020524 2750756 28% /
devtmpfs 237476 0 237476 0% /dev
tmpfs 249412 0 249412 0% /dev/shm
tmpfs 249412 4532 244880 2% /run
tmpfs 249412 0 249412 0% /sys/fs/cgroup
/dev/sda1 499656 102788 360172 23% /boot
tmpfs 49884 0 49884 0% /run/user/0

Step 3
To view the output of the df command in more human-readable format, use the -h option:

df -h
[root@centos~]# df -h
Filesystem Size Used Available Use% Mounted on
/dev/mapper/VolGroup-lv_root 3.9G 997M 2.7G 28% /
devtmpfs 232M 0 232M 0% /dev
tmpfs 244M 0 244M 0% /dev/shm
tmpfs 244M 4.5M 240M 2% /run
tmpfs 244M 0 244M 0% /sys/fs/cgroup
/dev/sda1 488M 101M 352M 23% /boot
tmpfs 49M 0 49M 0% /run/user/0

Step 4
To view filesystem inode usage, use the -i option:

df -i
[root@centos~]# df -i
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/mapper/VolGroup-lv_root 262144 27045 235099 11% /
devtmpfs 59369 358 59011 1% /dev
tmpfs 62353 1 62352 1% /dev/shm
tmpfs 62353 430 61923 1% /run
tmpfs 62353 16 62337 1% /sys/fs/cgroup
/dev/sda1 32768 334 32434 2% /boot
tmpfs 62353 1 62352 1% /run/user/0
Recall that each file needs an inode. Therefore, if a filesystem has 1,310,720 inodes then this also
represents the maximum number of files that can reside on that filesystem.

Step 5
To determine which directories are using the most disk space, use the du command. For example,
execute the following command to determine how much space the /usr directory is using:

du /usr | more
[root@centos~]# du /usr | more

Type Q to exit the more command:

Step 6
The output of the du command can be immense. To see just a summary of how much space a specific
directory structure is using, use the -s option:

du -s /usr
[root@centos~]# du -s /usr
914376 /usr

Step 7
The output is given in block sizes (1 block = 1 kilobyte, in this case). To see a more human-
readable value, use the -h option:
du -sh /usr
[root@centos~]# du -sh /usr
893M /usr

Step 8
The du command is useful because once you discover that a filesystem is close to becoming full, you
need to determine where the largest chunks of files are. A common way of doing this is to see which of
the directories under the root directory are using the most space. Execute the following command to see
a demonstration:

du -sh /*
[root@centos~]# du -sh /*
0 /bin
100M /boot
0 /dev
33M /etc
20K /home
0 /lib
0 /lib64
16K /lost+found
4.0K /media
4.0K /mnt
4.0K /opt

Some output has been omitted in the example above for brevity.

Step 9
Execute the following command to find the largest files in the /etc directory structure:

du /etc | sort -nr | head


[root@centos~]# du /etc | sort -nr | head
33636 /etc
21020 /etc/selinux
21000 /etc/selinux/targeted
15220 /etc/selinux/targeted/active
10996 /etc/selinux/targeted/active/modules
10988 /etc/selinux/targeted/active/modules/100
7772 /etc/udev
3812 /etc/selinux/targeted/policy
1932 /etc/selinux/targeted/contexts
1832 /etc/selinux/targeted/contexts/files

Step 10
In addition to displaying file space by filesystem or directory structure, you also want to know how to
display other information about filesystems. Execute the following command to display filesystem
information:

dumpe2fs /dev/sda1 | head


[root@centos~]# dumpe2fs /dev/sda1 | head
dumpe2fs 1.42.9 (28-Dec-2013)
Filesystem volume name: <none>
Last mounted on: /boot
Filesystem UUID: 5dac1ba0-cdbc-4ba2-99dd-17f298456819
Filesystem magic number: 0xEF53
Filesystem revision #: 1 (dynamic)
Filesystem features: has_journal ext_attr resize_inode dir_index filetype
needs_recovery extent 64bit flex_bg sparse_super large_file huge_file uninit_bg
dir_nlink extra_isize
Filesystem flags: signed_directory_hash
Default mount options: user_xattr acl
Filesystem state: clean
Errors behavior: Continue

Step 11
This output contains very useful filesystem information. For example, to display the number of Free
inodes (every file needs an inode, so the number of Free inodes indicates how many more files you
can place on this filesystem), execute the following command:

dumpe2fs /dev/sda1 | grep “Free inodes” | head -1

Note that the option after the head command is a number 1, not a lower-case letter L.

root@centos:~# dumpe2fs /dev/sda1 | grep "Free inodes" | head -1


dumpe2fs 1.42.9 (28-Dec-2013)
Free inodes: 32434

Once a filesystem has been created, the number of total inodes is set in stone.
Step 12
To see the default mount options of the filesystem, execute the following command:

dumpe2fs /dev/sda1 | grep “Default mount options”


[root@centos~]# dumpe2fs /dev/sda1 | grep "Default mount options"
dumpe2fs 1.42.9 (28-Dec-2013)
Default mount options: user_xattr acl

The default mount options are specified when the filesystem is initially created. Any additional mount
options are specified in the /etc/fstab file.

Step 13
Most of a filesystem's attributes can only be specified when the filesystem is created. For example, the
number of inodes cannot be changed later. However, the reserved block space can be changed.
Execute the following command to see the current value of the reserved block count:

dumpe2fs /dev/sda1 | grep "Reserved block count"


[root@centos~]# dumpe2fs /dev/sda1 | grep "Reserved block count"
dumpe2fs 1.42.9 (28-Dec-2013)
Reserved block count: 6553

The reserved block count is how much space on the filesystem is reserved for the root account or
processes that run as the root account (typically, system processes).
On some filesystems, such as /home, you may want to reduce this value since the root user doesn't use
that filesystem often. On other filesystems, you may find the need to increase the reserved block count.

Step 14
To change the reserved block count, you change the percentage of the filesystem that is reserved. This
reserved amount can only be used by the root account and is set to 5% by default. Execute the
following commands to change this value and confirm the change:

dumpe2fs /dev/sda1 | grep "Reserved block count"


tune2fs -m 10 /dev/sda1
dumpe2fs /dev/sda1 | grep "Reserved block count"
[root@centos~]# dumpe2fs /dev/sda1 | grep "Reserved block count"
dumpe2fs 1.42.9 (28-Dec-2013)
Reserved block count: 6553
[root@centos~]# tune2fs -m 10 /dev/sda1
tune2fs 1.42.9 (28-Dec-2013)
Setting reserved blocks percentage to 10% (13107 blocks)
[root@centos~]# dumpe2fs /dev/sda1 | grep "Reserved block count"
dumpe2fs 1.42.9 (28-Dec-2013)
Reserved block count: 13107

Fixing Filesystems
Introduction
In order to provide the functionality you need to apply these concepts, this VM may require a minute or
two to load.
There are circumstances in which a filesystem may break. This can occur, for example, when the
filesystem isn't properly shut down due to a power outage or a frustrated system administrator pulling
the power on the system.
Because of how filesystem changes are made, filesystem corruption can occur when a filesystem isn't
correctly unmounted. This is because changes to the filesystem metadata (information about the
filesystem) are stored in memory initially. When a filesystem is unmounted correctly, all of this data is
flushed to the hard drive.
When filesystems break, an administrator needs to know how to fix the problems. The fsck utility
provides you with the tools to fix filesystems.

Step 1
Log in to the system using the root account. When prompted, provide the root password:

root
netlab123
Ubuntu 18.04.2 LTS ubuntu tty1
ubuntu login:
Password:
Welcome to Ubuntu 18.04.2 LTS (GNU/Linux 4.15.0-45-generic x86_64)

* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
The programs included with the Ubuntu system are free software:
the exact distribution terms for each program are described in the individual
files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable


law.
root@ubuntu:~#

Step 2
In order to complete this lab, a new partition must be created. The steps to create a new partition will
essentially be the same as what you performed in a previous lab. Execute the following command to
start this process:

fdisk /dev/sdb
root@ubuntu:~# fdisk /dev/sdb
Welcome to disk (util-linux 2.31.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.


Created a new DOS disklabel with disk identifier 0xb23bc72f.

Command (m for help):

Step 3
At the Command prompt, type p to display the current partition table:

p
Command (m for help): p
Disk /dev/sdb: 4 GiB, 4294967296 bytes, 8388608 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: 0xb23bc72f

Command (m for help):

Step 4
At the Command prompt, type n to create a new partition:
n
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p):

Step 5
At the Select prompt, type e to specify that you are going to create an extended partition:

e
Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 4 free)
e extended
Select (default p): e
Partition number (1-4, default 1):

Step 6
At the Partition number prompt, type 1 to specify that you are going to use the first partition number:

1
Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 4 free)
e extended
Select (default p): e
Partition number (1-4, default 1): 1

Step 7
At the First sector prompt, accept the default value by pressing the Enter key:

Enter
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): e
Partition number (1-4, default 1): 1
First sector (2048-8388607, default 2048):

Step 8
Press the Enter key to accept the default value at the Last sector prompt:

Enter
Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): e
Partition number (1-4, default 2): 2
First sector (41943040-42967039, default 41943040):
Using default value 41943040
Last sector, +sectors or +size{K,M,G} (2048-8388607, default 8388607):

Created a new partition 1 of type ‘Extended’ and of size 4 GiB.

Command (m for help):

Step 9
At the Command prompt, type p to display the current partition table. You should see your new partition:

p
Command (m for help): p
Disk /dev/sdb: 4 GiB, 4294967296 bytes, 8388608 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 identifierL 0xb23bc72f

Device Boot Start End Sectors Size Id Type


/dev/sdb1 2048 8388607 8386560 4G 5 Extended

Command (m for help):

Step 10
Now, you can create a partition that you can later format with a filesystem and access via a directory by
mounting it. To create this new partition, first type n at the Command prompt:

n
Command (m for help): n
All space for primary partitions is in use.
Adding logical partition 5
First sector (4096-8388607, default 4096):
First sector (41945088-42967039, default 41945088):

Step 11
Press the Enter key to accept the default for the First sector:

Enter
Command (m for help): n
All space for primary partitions is in use.
Adding logical partition 5
First sector (4096-8388607, default 4096):
Last sector, +sectors or +size{K,M,G} (4096-8388607, default 8388607):

Step 12
At the next prompt, enter +200M to create a 200MB partition:

+200M
Command (m for help): n
All space for primary partitions is in use.
Adding logical partition 5
First sector (4096-8388607, default 4096):
Last sector, +sectors or +size{K,M,G} (4096-8388607, default 8388607):+200M

Created a new partition 5 of type 'Linux' and of size 200MiB.

Command (m for help):

Step 13
Type p at the Command prompt to see your new partition:

p
Command (m for help): p
Disk /dev/sdb: 4 GiB, 4294967296 bytes, 8388608 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 identifierL 0xb23bc72f

Device Boot Start End Sectors Size Id Type


/dev/sdb1 2048 8388607 8386560 4G 5 Extended
/dev/sdb5 4096 413695 409600 200M 83 Linux

Command (m for help):


Step 14
Type w at the Command prompt to save your new partitions:

w
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

root@ubuntu:~#

Step 15
Reboot the system to have the kernel recognize the new partitions:

reboot
root@ubuntu:~# reboot

In some cases, you may be able to run a command, such as kpartx or partprobe, to have the kernel
read the new partition table. However, on this system, these commands do not exist.

Step 16
After the system finishes booting, log in to the system using the root account. When prompted, provide
the root password:

root
netlab123
Ubuntu 18.04.2 LTS ubuntu tty1
ubuntu login:
Password:
Welcome to Ubuntu 18.04.2 LTS (GNU/Linux 4.15.0-45-generic x86_64)

* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage

root@ubuntu:~#

Step 17
Then, verify the new files have been created in the /dev directory:
ls /dev/sd*
root@ubuntu:~# ls /dev/sd*
/dev/sda /dev/sda1 /dev/sdb /dev/sdb1 /dev/sdb5

Step 18
Execute the following command to create an ext4 partition on the /dev/sdb5 partition:

mkfs -t ext4 /dev/sdb5


root@ubuntu:~# mke2fs -t ext4 /dev/sdb5
mke2fs 1.44.1 (24-Mar-2018)
Discarding device blocks: done
Creating filesystem with 204800 1k blocks and 51200 inodes
Filesystem UUID: ab7552f0-0802-49f5-8241-70fdc183b676
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729

Allocating group tables: done


Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

The Filesystem UUID in our virtual environment may be different than in the example terminal above.

Step 19
Create a directory to use as a mount point for the new partition by using the mkdir command. Then,
mount the new partition and verify that it mounted correctly by executing the mount commands as
shown.
Create a directory called /data to use as a mount point for the new partition by using
the mkdir command:

mkdir /data
root@ubuntu:~# mkdir /data
root@ubuntu:~#
mount /dev/sdb5 /data
mount
Step 20
Attempt to run the fsck utility on the /dev/sdb5 filesystem by executing the following command:

fsck /dev/sdb5
root@ubuntu:~# fsck /dev/sdb5
fsck from util-linux 2.31.1
e2fsck 1.44.1 (24-Mar_2018)
/dev/sdb5 is mounted.
e2sck: Cannot continue, aborting.

The fsck utility will not run on a mounted filesystem. The purpose of the fsck command is to fix
filesystem problems on filesystems that can't be mounted. If the filesystem is mounted, there are no
filesystem problems and no need to run the fsck command.

Step 21
Unmount the /dev/sda5 partition and then attempt to run the fsck utility on the /dev/sda5 filesystem
by executing the following commands:
umount /dev/sdb5
fsck /dev/sdb5
root@ubuntu:~# umount /dev/sdb5
root@ubuntu:~# fsck /dev/sdb5
fsck from util-linux 2.31.1
e2fsck 1.44.1 (24-Mar-2018)
/dev/sdb5: clean, 11/51200 files, 12115/204800 blocks

The clean value indicates that this filesystem is either new or has been correctly unmounted.
Therefore, there is no need to run fsck because the filesystem is not broken.

Step 22
One way of having the fsck utility run on a clean filesystem is to force the checking of the filesystem
with the -f option:

fsck -f /dev/sdb5
root@ubuntu:~# fsck -f /dev/sdb5
fsck from util-linux 2.31.1
e2fsck 1.44.1 (24-Mar-2018)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/sdb5: 11/51200 files (0.0% non-contiguous), 12115/204800 blocks

Step 23
Another technique that you can use is to manually switch the filesystem state from clean to not
clean. To do this, execute the following command:

debugfs -w -R "ssv state 0" /dev/sdb5


root@ubuntu:~# debugfs -w -R "ssv state 0" /dev/sdb5
debugfs 1.44.1 (24-Mar-2018)

A filesystem state of not clean just indicates that the filesystem was not properly unmounted. There
may be problems with the filesystem, but then again, there may be no problems at all. If a filesystem is
set to not clean, then the fsck utility will check the filesystem.
The debugfs command can make changes directly to the filesystem. Use this utility with caution on
filesystems that are on critical systems, as you could easily damage the filesystem with this utility.
Step 24
After switching the filesystem state, you can run the fsck command:

fsck /dev/sdb5
root@ubuntu:~# fsck /dev/sdb5
fsck from util-linux 2.31.1
e2fsck 1.44.1 (24-Mar-2018)
/dev/sdb5 1.44.1 was not cleanly unmounted, check forced.
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/sdb5: 11/51200 files (0.0% non-contiguous), 12115/204800 blocks

Step 25
The superblock is where critical filesystem data is stored. If the primary superblock is corrupted, then
you need to use a backup superblock to fix the primary superblock. To view the backup superblocks of a
filesystem, execute the following command:

dumpe2fs /dev/sdb5 | grep superblock


root@ubuntu:~# dumpe2fs /dev/sdb5 | grep superblock
dumpe2fs 1.44.1 (24-Mar-2018)
Primary superblock at 1, Group descriptors at 8194-8195
Backup superblock at 8193, Group descriptors at 24578-24579
Backup superblock at 24577, Group descriptors at 24578-24579
Backup superblock at 40961, Group descriptors at 40962-40963
Backup superblock at 57345, Group descriptors at 57346-57347
Backup superblock at 73729, Group descriptors at 73730-73731

Step 26
The following command is intended for testing purposes. Normally, you would never run this command
on a production machine. Execute the following command to force the corruption of your primary
superblock:
Warning
Be very careful when typing this command. If you don't type it exactly as shown, it may end up deleting
the entire filesystem. If that happens, the only solution is to reset the VM and restart the lab.
dd if=/dev/zero of=/dev/sdb5 bs=1024 count=1 seek=1
root@ubuntu:~# dd if=/dev/zero of=/dev/sdb5 bs=1024 count=1 seek=1
1+0 records in
1+0 records out
1024 bytes (1.0 kB, 1.0 KiB) copied, 0.00405975 s, 252 kB/s

Step 27
To verify that the filesystem now has a problem, attempt to mount the filesystem by executing the
following command:

mount /dev/sdb5 /data


root@ubuntu:~# mount /dev/sdb /data
mount: /data: wrong fs type, bad option, bad superblock on /dev/sdb, missing
codepage or helper program, or other error.

Normally, the mount command can determine the filesystem type by looking at values in the primary
superblock. However, if that information can't be accessed, you will get the error message mount: you
must specify the filesystem type.

Step 28
Execute the following command to try to mount the filesystem by specifying the filesystem type:

mount -t ext4 /dev/sdb5 /data


root@ubuntu:~# mount -t ext4 /dev/sdb5 /data
[ 1969.413457] EXT$-fs (sdb5): VFS: Can’t find ext4 filesystem
mount: /data: wrong fs type, bad option, bad superblock on /dev/sdb5, missing
codepage o

Step 29
The dmesg command can be used to see messages generated by the kernel. Execute the following
command:

dmesg | tail
root@ubuntu:~# dmesg | tail
[ 3.220384] parport0: PC-style at 0x378, irq 7 [PCSPP,TRISTATE]
[ 3.519704] SSE version of gmc_enc/dec engaged.
[ 3.626606] ppdev: user-space parallel port driver
[ 3.717314] Adding 524284k swap on /dev/mapper/ubuntu--vg-swap_1. Priority:-2
extents:1 across:524284k FS
[ 4.082803] random: crng init done
[ 4.082805] random: 7 urandom warning(s) missed due to ratelimiting
[ 1080.847807] EXT4-fs (sdb5): mounted filesystem with ordered data mode. Opts:
(null)
[ 1080.847807] sdb: sdb1 < sdb5 >
[ 1080.864385] sdb: sdb1 < sdb5 >
[ 1969.413457] EXT4-fs (sdb5): VFS: Can’t find ext4 filesystem

The highlighted lines demonstrate that without the primary superblock, the mount command has no way
of finding the ext4 filesystem on this partition.

Step 30
You can use a backup superblock to fix the primary superblock. Unfortunately, if the primary superblock
is corrupted, then you can't use the dumpe2fs command to view the location of backup superblocks:

dumpe2fs /dev/sdb5 | grep superblock


root@ubuntu:~# dumpe2fs /dev/sdb5 | grep superblock
dumpe2fs: 1.44.1 (24-Mar-2018)
dumpe2fs: Bad magic number in super-block while trying to open /dev/sdb5
Couldn’t find valid filesystem superblock.

Step 31
However, there is a second way to determine a backup superblock that will work on a filesystem that
has a corrupted primary superblock. Execute the following command:

mke2fs -n /dev/sdb5
root@ubuntu:~# mke2fs -n /dev/sdb5
mke2fs 1.44.1 (24-Mar-2018)
Creating filesystem with 240800 1k blocks and 51200 inodes
Filesystem UUID: 71b895c4-e9f4-4e7a-992f-b90f6784094f

Superblock backups stored on blocks:

8193, 24577, 40961, 57345, 73729

Important
Do not forget to include the -n option. Without the -n option, you would end up creating a new
filesystem on that partition, resulting in complete data loss.

Step 32
To fix the filesystem using the backup superblock, execute the following command. When prompted
with Fix<y>? type the letter Y:
fsck -b 8193 /dev/sdb5
y
root@ubuntu:~# fsck -b 8193 /dev/sdb5
fsck from util-linux 2.31.1
e2fsck 1.44.1 (24-Mar-2018)
/dev/sdb5 1.44.1 was not cleanly unmounted, check forced.
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
Block bitmap differences: +(8193--8451) +(24577--24835) +(40961--41219)
+(57345--57603) +(73729--73987)
Fix<y>? yes
Inode bitmap differences: Group 1 inode bitmap does not match checksum.

/dev/sdb5: ***** FILE SYSTEM WAS MODIFIED *****


/dev/sdb5: 11/51200 files (0.0% non-contiguous), 12115/204800 blocks

Step 33
Verify that the filesystem was fixed by mounting it with the first mount command and then verify that the
filesystem mounted properly with the second mount command:

mount /dev/sdb5 /data


mount
Step 34
The most common time for a filesystem error to occur is during the boot process. In the last phase of
this lab, you will create a filesystem error and fix it during the boot process. Start by using
the nano editor to edit the /etc/fstab file:

nano /etc/fstab
root@ubuntu:~# nano /etc/fstab

Add the following line to the bottom of the /etc/fstab file:

/dev/sdb5 /data ext4 defaults 0 0

If the /etc/fstab file is currently empty, make the line above the first and only line of the file.

# /etc/fstab: static file system information.


#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point> <type> <options> <dump> <pass>
/dev/mapper/ubuntu—vg-root / ext4 errors=remount-ro 0 1
/dev/mapper/ubuntu—vg-swap_1 none swap sw 0 0
/dev/fd0 /media/floppy0 auto rw,user,noauto,exec,utf8 0 0
/dev/sdb5 /data ext4 defaults 0 0

^G Get Help ^O Write Out ^W Where Is ^K Cut Text ^J Justify ^C Cur Pos M-U
Undo
^X Exit ^R Read File ^\ Replace ^U Uncut Text ^T To Spell ^_ Go To Line M-E
Redo

Type Ctrl+X to exit the nano editor. At the Save modified buffer? prompt, type the Y key.

Ctrl+x
y
Save modified buffer? (Answering "No" will DISCARD changes.)
Y Yes
N No ^C Cancel

Press Enter to save (write) the file.

Enter
File Name to Write: /etc/fstab_
^G Get Help M-D DOS Format M-A Append M-B Backup
File
^C Cancel M-M Mac Format M-P Prepend ^T

Step 35
Verify this is correct by executing the following command:

tail -n 1 /etc/fstab

Note that the character after the -n option is a number 1, not a lowercase letter L.
root@ubuntu:~# tail -n 1 /etc/fstab
/dev/sdb5 /data ext4 defaults 0 0

Step 36
To test that this line is accurate, unmount the /dev/sdb5 filesystem and remount it again by specifying
only the mount point:

umount /data
root@ubuntu:~# umount /data
mount /data
mount

Recall that when you specify only the mount point, the mount command looks at the corresponding
entry in the /etc/fstab file to determine the rest of the information needed to mount. If
the mount command is successful, the entry in the /etc/fstab file is correct.

Step 37
Next, unmount the filesystem and use the dd command to create an error in the filesystem:
umount /data
dd if=/dev/zero of=/dev/sdb5 bs=1024 count=1 seek=1
root@ubuntu:~# umount /data
root@ubuntu:~# dd if=/dev/zero of=/dev/sdb5 bs=1024 count=1 seek=1
1+0 records in
1+0 records out
1024 bytes (1.0 kB) copied, 0.00596831 s, 172 kB/s

Step 38
Verify that the filesystem is broken by attempting to mount it:

mount /data
root@ubuntu:~# mount /data
[ 3502.251839] EXT4-fs (sdb5): VFS : Can’t find ext4 filesystem
mount: /data: wrong fs type, bad option, bad superblock on /dev/sdb5, missing
codepage or helper program, or other error.

Step 39
Typically, you would want to fix the filesystem before you reboot the machine. However, in this case the
point is to learn how to fix a filesystem error that prevents a system from rebooting. Reboot the machine:

reboot
root@ubuntu:~# reboot

Step 40
The system will boot to a point, and then an error message will appear:
Because this filesystem is a non-critical filesystem (not needed to boot the system), you could press the
letter S, and the mounting process for this filesystem would be skipped. You could then login and fix the
problem by running the fsck command. However, if it was a critical filesystem, you wouldn't be able to
do that. For this lab, consider this a critical filesystem.

Step 41
Type the root password and press the Enter key. You will then be provided a command prompt:

netlab123
Enter
...
Give root password for maintenance
(or press Control-D to continue):
root@ubuntu:~#

At this prompt, type the following command to fix the filesystem:

fsck /dev/sdb5
root@ubuntu:~# fsck /dev/sdb5
fsck from util-linux 2.31.1
e2fsck 1.44.1 (24-Mar-2018)
/dev/sdb5 1.44.1 was not cleanly unmounted, check forced.
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
Inode bitmap differences: Group 1 inode bitmap does not match checksum.
FIXED.
Block bitmap differences: Group 1 block bitmap does not match checksum.
FIXED.

/dev/sdb5: ***** FILE SYSTEM WAS MODIFIED *****


/dev/sdb5: 11/51200 files (0.0% non-contiguous), 12115/204800 blocks

Unlike the fsck utility that you run when logged in as root, the fsck utility that runs when in
maintenance mode, as is the case here, knows to check backup superblocks automatically.

Step 42
Now that the filesystem has been fixed, you can continue the boot process by holding down the Ctrl+D.
You could also type the exit command:

exit
root@ubuntu:~# exit

Step 43
The system should boot the rest of the way and provide you with a login prompt. Log in to the system
using the root account. When prompted, provide the root password:

root
netlab123
Ubuntu 18.04.2 LTS ubuntu tty1
Ubuntu login: root
Last login: Sat Jun 29 21:30:29 UTC 2019 on tty1
Welcome to Ubuntu 18.04.2 LTS (GNU/Linux 4.15.0-45-generic x86_64)

* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage

root@ubuntu:~#

Step 44
Verify that the filesystem was fixed by running the mount command:

mount

Step 45
Lastly, after you have finished running the fsck command, you should look in
the lost+found directory for the filesystem to see if any lost files have been placed in that directory.
Execute the following command:

ls /data/lost+found
root@ubuntu:~# ls /data/lost+found
root@ubuntu:~#

No output is a good thing. If there are any lost files, they will not have regular names; rather, they will be
named after their inode number.

You might also like