0% found this document useful (0 votes)
75 views18 pages

Unit 9 - Managing Hard Disk Partitions - Modified 11-10-2021

This document describes how to create a virtual hard disk on a Oracle Virtual Machine and use the Ubuntu 20.04 Operating System to how to create a File System on this new disk partition. This document also illustrates various Linux Utilities for managing and troubleshooting issues with disk partitions.

Uploaded by

George Nossa
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)
75 views18 pages

Unit 9 - Managing Hard Disk Partitions - Modified 11-10-2021

This document describes how to create a virtual hard disk on a Oracle Virtual Machine and use the Ubuntu 20.04 Operating System to how to create a File System on this new disk partition. This document also illustrates various Linux Utilities for managing and troubleshooting issues with disk partitions.

Uploaded by

George Nossa
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/ 18

Unit 9: Managing Disk Partitions

Document Version: 2021-11-10

Navigating the Linux directory tree and manipulating files are common tasks that are performed daily by all
users. However, Administrators must provide this directory tree for users, as well as manage and fix the disk
devices that support it. In this lab, you learn about the various device files that represent disk devices; the
different filesystems that can be placed on those devices and the use of System Administration and Disk
Management utilities.
Objective
In this lab, you will perform the following tasks:

1. Create a new virtual disk and attach it to the Filesystem


2. Create a new Linux partition on the new virtual disk
3. Format the partition using mkfs on the new virtual disk
4. Display filesystem information on the new virtual disk
5. Use fsck to maintain filesystem integrity on the new virtual disk
6. Mount the newly created partition
7. Create a swap filesystem on the new virtual disk
Background
A partition is a section of a hard disk with a device name, (like /dev/sda1), to address it separately from other
hard disk sections. Before programs can write data to a partition, a data structure called a filesystem, needs to
be written to the partition. When the Ubuntu installer creates a partition, it writes a filesystem to the partition
(just like the Windows OS Installer Utility on a .ISO image)
A Linux system must have a / (root) partition. It is advisable to set up a swap partition as well. Linux temporarily
stores programs and data on a swap partition when it does not have enough RAM to hold all the information it
is processing. The size of the swap partition is one or two times the size of the RAM in the system, with a
minimum size of 256 megabytes. For example, a system with 1 gigabyte of RAM should have a 1 to 2 gigabyte
swap partition. Although a swap partition is not required, most Linux systems perform better with one.
First Steps
To create a new partition on our Virtual Box, we will need to add a new virtual disk and attach this new disk to
our Ubuntu Filesystem. The steps required are as follows:
1. With the Virtual Machine OFF, click Settings, then click Storage, then click Controller SATA and
choose Disk
2. Now click the Create New Disk Image icon (see below):

3. On the next menu (see below) choose VDI (Virtual Disk Image) and click Next

4. On the next menu (see below) choose Dynamically Allocated and click Next

5. On the next menu (see below) select about 500 Mbytes on the slide bar and click Create

This work by the National Information Security and Geospatial Technologies Consortium (NISGTC), and except where otherwise
noted, is licensed under the Creative Commons Attribution 3.0 Unported License.
6. On this next menu, select the 500 Mbyte Virtual disk that was just created and click Choose

7. Notice the second dvi Disk under the SATA controller below:

8. The Virtual Machine should be similar to the image below. Notice that a second disk drive is attached to
the SATA drive.

This work by the National Information Security and Geospatial Technologies Consortium (NISGTC), and except where otherwise
noted, is licensed under the Creative Commons Attribution 3.0 Unported License.
And you are now ready to complete the remaining part of this lab.
1 Create a New Partition
In this task, we will explore commands to create a new partition on disk sdc.

1. Start the Oracle virtual machine and open the Console Terminal.

2. Check for available drives on the system by typing the following command from the terminal:

sudo ls –l /dev/sd*

When you use the * wildcard symbol at the end of this command, the listing will contain ALL files that begin with
the preceding letters. In this case, all files that start with “sd” will be listed.

In this listing, the devices available are considered to be SCSI devices. This is indicated by the “s” in sdc. If they
were IDE devices, they would be listed as hda, hdb, etc.

3. Next, type the fdisk command on /dev/sdb:

sudo fdisk /dev/sdb

This work by the National Information Security and Geospatial Technologies Consortium (NISGTC), and except where otherwise
noted, is licensed under the Creative Commons Attribution 3.0 Unported License.
The warnings regarding the invalid partition table can be ignored since we will be creating a new partition and formatting the partition.

4. Once you start the fdisk command, the fdisk program executes. To see available commands, type: m, then press Enter.

5. In this case, we first want to “print” the partition table to the screen. Within the fdisk program, type: p,
then press Enter.

To create a new partition, type: n, then press Enter.


a. Because this is a newly installed drive, you will be asked if you want to create a primary partition or
an extended partition. In this case, we want a primary partition, so at the prompt, type: p, then
press Enter
b. Type 1 for the partition number.
c. Accept the default starting position for the first sector by pressing the Enter key.
d. Make the partition 200MB in size by typing +200M. Press Enter to create the partition.

This work by the National Information Security and Geospatial Technologies Consortium (NISGTC), and except where otherwise
noted, is licensed under the Creative Commons Attribution 3.0 Unported License.
e. Then, confirm that the partition was created by pressing p again to print the partition table. Notice
that a new partition is on the system!

If you make a mistake, do not panic! You can exit fdisk without saving the changes by
pressing q + Enter at the command prompt. Then, start the process again.

6. To “write” the changes to the partition table, press: w

7. Repeat the ls –l /dev/sd* command to view the changes made.

Use mkfs to Create a Filesystem

In this task, we will explore the command mkfs to add a filesystem on the newly created partition.
1. From the terminal, type the following command to create an ext4 filesystem on the newly created partition:

This work by the National Information Security and Geospatial Technologies Consortium (NISGTC), and except where otherwise
noted, is licensed under the Creative Commons Attribution 3.0 Unported License.
sudo mkfs –t ext4 /dev/sdb1
Your output should be similar to the following:

Using the -t option to the mkfs command, you can create different types of filesystems:
Command Result
-t vfat Creates a vfat (Microsoft) filesystem
-t ext3 Creates an ext3 filesystem
-t xfs Creates an xfs filesystem
-t reiserfs Creates a reiserfs v3 filesystem

Note: If the -t option is not provided, an ext2 filesystem is created.

2 Mount the Newly Created Filesystem

In this task, we will explore options to mount the filesystem, including mount on startup.

1. In Linux, partitions are mounted to directories. The first step in mounting a filesystem is to create a
directory to which it can be mounted. To create a new directory, type the following from the terminal:

sudo mkdir /new

2. To mount the partition to the newly created /new directory, type:

sudo mount /dev/sdb1 /new

3. To verify that the partition is correctly mounted, run the ls command on the directory:

ls –l /new

A new filesystem will have a lost+found directory by default.

This work by the National Information Security and Geospatial Technologies Consortium (NISGTC), and except where otherwise
noted, is licensed under the Creative Commons Attribution 3.0 Unported License.
Note that the mount command only temporarily mounts the filesystem. If the system were rebooted, the filesystem
would no longer be mounted. To enable automatic mounting during the boot process, you need to edit the
/etc/fstab file.
If you know how to use the vi editor, modify the /etc/fstab file by adding the following line at the bottom of the
file:
/dev/sdb1 /new ext4 defaults 1 1
(use 4 spaces to represent tabs):
If you are not comfortable with the vi editor, then follow these instructions:
Type sudo nano /etc/fstab

Use arrow down to reach the last line and add the following line at the bottom of the file:
/dev/sdb1 /new ext4 defaults 1 1
(use 4 spaces to represent tabs):

Use ^X (cntl-X) to exit, the prompt below will follow.. to save the file, Press Enter to save..

a. Type vi /etc/fstab in a terminal window and press Enter.

This work by the National Information Security and Geospatial Technologies Consortium (NISGTC), and except where otherwise
noted, is licensed under the Creative Commons Attribution 3.0 Unported License.
b. Press upper case G to go to the end of the document.
c. Type o to open a new line at the bottom of the document
d. Enter the following data into the document (use 4 spaces to represent tabs):

/dev/sdb1 /new ext4 defaults 1 1

e. Press the Escape key.


f. Type :wq and press Enter to save and quit the document.

This work by the National Information Security and Geospatial Technologies Consortium (NISGTC), and except where otherwise
noted, is licensed under the Creative Commons Attribution 3.0 Unported License.
Each line in the /etc/fstab file contains six fields of data:
Field example Meaning
/dev/sdc1 The partition to mount
/new The mount point ext4
The filesystem type defaults
The mount options
1 Used by the dump command for backups
1 Used by the fsck command for filesystem checks

You can learn more about these fields by executing man fstab in a terminal window.

4. It is important to test your /etc/fstab entry prior to rebooting the system.


To do this, first unmount the filesystem with the following command:

sudo umount /new

5. Then, mount the filesystem with the following command:

sudo mount /new


If you only use the mount point to mount the filesystem, the /etc/fstab file will be "consulted" for the rest of the
information. In this case, if there are no errors in your /etc/fstab file, the mount command will produce no output:

If there are any errors, you will see a message like the following:

If you get this sort of error, double-check your work and try again.

3 Display Filesystem Data

In this task, we will explore filesystem data.

This work by the National Information Security and Geospatial Technologies Consortium (NISGTC), and except where otherwise
noted, is licensed under the Creative Commons Attribution 3.0 Unported License.
1. The df command will display filesystem information such as the size of the filesystem and how much
space is being used. Run the following command to display filesystem information for the filesystem
mounted under /new:

sudo df /new
Your output should be similar to the following:

For a more "human readable" size, try using the -h option to the df command.

2. The du command will display how much space (combined size of all files) a specific directory structure
contains. Run the following command to display how much space the /usr/lib/xorg directory
contains:

sudo du -h /usr/lib/xorg
The output should be similar to the following:

The default size is in blocks. For a more "human readable" size, try using the -h option to the du command.

By default, the du command provides information for each subdirectory. For a summary, try using the -s option
to the du command.

3. The tune2fs command will provide detailed information about a filesystem, including filesystem
features and the inode count (number of possible files in the filesystem). Run the following command to
display filesystem information for the /dev/sdb1 filesystem:

sudo tune2fs -l /dev/sdb1 | more

Your output should be similar to the following:

This work by the National Information Security and Geospatial Technologies Consortium (NISGTC), and except where otherwise
noted, is licensed under the Creative Commons Attribution 3.0 Unported License.
The dumpe2fs command also displays filesystem information in a very similar format to tune2fs. Typically,
system administrators use dumpe2fs to display filesystem information while using tune2fs to make changes to
a filesystem. However, either can be used to display filesystem information

This work by the National Information Security and Geospatial Technologies Consortium (NISGTC), and except where otherwise
noted, is licensed under the Creative Commons Attribution 3.0 Unported License.
Most of these filesystem attributes are created when you use the mkfs or mke2fs commands to create the
filesystem. If you want to learn more about how to specify different attributes when you create a filesystem,
execute the command man mkfs or man mke2fs.

4. The debugfs utility allows you to enter an interactive mode to display or modify a filesystem state. It is
a highly complex command with many options. The following will give you an idea of a few of the tasks
that you can perform with this utility.

Follow these instructions to use the debugfs utility:

a. In a terminal window, enter the following command to unmount the /dev/sdb1 partition:

sudo umount /dev/sdb1

b. Enter the following command to enter the debugfs interactive mode:

sudo debugfs /dev/sdb1

c. At the debugfs: prompt , enter ? and then press the Enter key to display a menu option. Output should be similar to:

d. Read through the different options, pressing the space bar to scroll down, until you are presented
with the debugfs: prompt again.

This work by the National Information Security and Geospatial Technologies Consortium (NISGTC), and except where otherwise
noted, is licensed under the Creative Commons Attribution 3.0 Unported License.
e. Type the ls command to display information about the files in the top-level directory of the
filesystem. The output should be similar to the following:

f. The numbers next to the file/directory names are inode numbers. Each file/directory in the
filesystem has a unique inode number. To get detailed information about the lost+found directory,
use command stat lost+found. The output should be similar to the following:

g. Exit the debugfs utility by typing the q command, then Enter.

4 Repair Filesystems

In this task, we will explore repairing filesystems with the fsck utility.
Filesystems may break if they are not properly unmounted (for example, during a power outage). The fsck utility is
used to make these repairs.
Typically, these repairs will occur when the system boots up. An error will appear on the screen regarding filesystem
"corruption" or a filesystem error and you will be prompted to enter the "filesystem maintenance password" to fix the
problem. This is really the root password for the system.
Because it is difficult to force a filesystem error, this lab will have you run fsck on a filesystem that has no errors to fix.
To begin with, it is important to unmount a filesystem before running fsck, as the following demonstrates (DO NOT run
the command displayed in the following example yet):

This work by the National Information Security and Geospatial Technologies Consortium (NISGTC), and except where otherwise
noted, is licensed under the Creative Commons Attribution 3.0 Unported License.
If you ever get this warning, type n to exit the fsck utility.

1. Before continuing, make sure the partition is not mounted.

sudo umount /new

2. Next, execute the following command to have fsck scan and potentially fix filesystem errors:

sudo fsck -f /dev/sdb1

The output should be similar to the following:

Note that the -f option "forced" fsck to run on the filesystem. Since the filesystem was unmounted cleanly, fsck
would not run in this case.

The fsck command will run a command called e2fsck. Typically, fsck will provide all that you need to fix
filesystem problems, however be aware that the e2fsck command has more features and might be useful when
there are complicated filesystem errors to fix.

5 Create a Swap Filesystem

In this task, you will create a swap filesystem for the Linux install.

1. To create a swap filesystem, first create a regular partition using the fdisk utility:

a. Type the following command in a terminal window:

This work by the National Information Security and Geospatial Technologies Consortium (NISGTC), and except where otherwise
noted, is licensed under the Creative Commons Attribution 3.0 Unported License.
sudo fdisk /dev/sdb

b. Press p to view the current partitions.


c. Press n to create a new partition.
d. Press p to create a primary partition.
e. Type 2 for the partition number.
f. Accept the default starting position for the first sector by pressing the Enter key.
g. Make the partition 128MB in size by typing +128M. Press Enter to create the partition.
h. Save your changes by typing w at the prompt.

If you make a mistake, do not panic! You can exit fdisk without saving the changes by pressing q then Enter at
the prompt. Then start the process again.

This work by the National Information Security and Geospatial Technologies Consortium (NISGTC), and except where otherwise
noted, is licensed under the Creative Commons Attribution 3.0 Unported License.
PLEASE NOTE ON THE SCREEN ABOVE, I DIDN’T HAVE SPACE FOR A 2ND PARTITION…
THE WORK BELOW IS FROM Mr. RAHMAN WHICH IS DONE PERFECTLY.
2. To convert this partition into swap space, first use the mkswap command as shown below. To add this
new swap space into the current swap space, use the swapon command. This new swap space can be
verified by using the -s option to the swapon command:

sudo mkswap /dev/sdb2


sudo swapon /dev/sdb2
sudo swapon –s
Your output should be similar to the following:

If you reboot the machine, the new partition would not automatically be used as a swap partition. To make this a
permanent swap partition, you would add the following line to the /etc/fstab file:

/dev/sdb2 swap swap defaults 0 0


Note: This environment will not save those changes.

This work by the National Information Security and Geospatial Technologies Consortium (NISGTC), and except where otherwise
noted, is licensed under the Creative Commons Attribution 3.0 Unported License.
This work by the National Information Security and Geospatial Technologies Consortium (NISGTC), and except where otherwise
noted, is licensed under the Creative Commons Attribution 3.0 Unported License.

You might also like