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

Disk Partitioning Lab - Part I

Uploaded by

deboevania
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Disk Partitioning Lab - Part I

Uploaded by

deboevania
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Linux Disk Management


Alexandru Calcatinge
Dec 1, 2018 • 27 min read

In this laboratory, you will exercise using disk devices, partitions, RAID, LVM, swap and cryptsetup.

Contents:

1. Lab 1 Partitioning and formatting disks


 Lab objective 1: Using a file as a disk partition image
 Lab objective 2: Partitioning a disk image file
 Lab objective 3: Using losetup and parted
 Lab objective 4: Partitioning and formatting a real hard drive/stick
 Lab objective 5: Working with swap

2. Lab 2 Encryption and filesystem features


 Lab objective 6: Disk encryption
 Lab objective 7: Encrypted swap

3. Lab 3 Using LVM


 Lab objective 8: Managing logical volumes
 Lab objective 9: Creating a RAID device
Part I – Partitioning and formatting disks
Laboratory objective 1: Using a file as a disk partition image
Commands used: dd, mkfs, ls, mount, mkdir

Solution:

For the purpose of this exercise, you will need unpartitioned disk space available on your disk. If you don't, and
you are using your own system, you will need to shrink a partition and the filesystem on it first, and then make
it available to the system. If you don't use partitions on your system, only one large partition for the entire
operating system, the you can use the loop device mechanism with or without the parted program. In the first
exercises we will use the loop mechanism.

We are going to create a file that will be used as a container for a full hard disk partition image, and thus it can
be used as a real partitioned hard disk.

1. Create a file full of zeros that will be 1GB in length

dd if=/dev/zero of=imagefile bs=1M count=1024

For this, I created a test directory in my home directory, and the output is:

2. Put a file system on the file you just created. For convenience, I created a ext4 file system, but you can
user another one, such as ext3, vfat, xfs or btrfs.
3. Next, mount the file you created. For this, I created a new directory called mount point inside my
backup_test directory, where the image file was created
Laboratory objective 2: Partitioning a disk image file
Commands used: fdisk, umount

Solution:

In the previous exercise, you created an image file that was formatted as a disk drive. Now, the next level of
complication is to divide the container file into multiple partitions, in order to hold a file system or a swap area.
We will reuse the imagefile that we created earlier.

1. run fdisk on the imagefile

When finished with the above Lab, please make sure to unmount the imagefile using the unmount command.
Note: In Linux Mint, the unmount command did not work. I unmounted the drive using the GUI.

Next, enter the following command:


sudo fdisk imagefile

2. type m to get a list of commands


3. Create a new primary partition and make it 256MB
Command (m for help): n

4. Add a secondary primary partition also of 256MB in size


Command (m for help): n
Now print the partition table by hitting the p key

5. write the partition table to disk and exit

Laboratory objective 3: Using losetup and parted


Commands used: losetup, parted, fdisk, ls, mkfs, mount, mkdir, df, umount, rmdir

Solution:

In this exercise, you are going to experiment with loop devices, losetup and parted to partition at the command
line. You can use the previous file, or you can create a new one. For the purpose of this exercise, better create a
new one.

1. associate the image file with a loop device


With the first command, we find the first loop device that is free, just to make sure in case you already use
others on your system. With the option –a you can find all the currently used loop devices.

2. Create a disk partition label on the loop device image file

3. Create three primary partitions on the loop device

4. check the partition table


5. check if the device nodes have been created. This depends on your distribution. In open SUSE 42.x,
RHEL 7 and Ubuntu (starting 14.04), the following command will work. On older distributions, it
might not work.

6. put filesystems on the partitions

7. mount all three filesystems and show that they are available
8. after using the filesystems to your needs, you can unmount it all

*I unmounted the drives using the GUI…


Laboratory objective 4: Partitioning and formatting a hard
drive/stick
Commands used: fdisk, mount, umount, mkdir, mkfs, ls

Solution:

Now we will exercise partitioning and formatting with a real memory stick or external hard drive. Depending on
what you have available, you can use either of those.

I have a 32GB memory stick that I will use for this exercise.

1. Insert your memory stick into your computer's USB port and use fdisk to see details about it.

You will see that your memory stick is under /dev/sdb

In the example above, the memory stick is not recognized by the VM.

2. Now use fdisk for the ___________________

3. Now we will create three different partitions, as follows:


Now we have three different partitions on the external memory stick, as shown in the upper lines. The partitions
are not formatted. To view the supported filesystems on our distribution, we can run the command:

You should choose your filesystem based on your requirements and needs. Most used filesystem type is ext4,
supported by most distributions, but there also are XFS or btrfs that are also used by enterprise grade Linux
such as SUSE. For convenience I will use ext4, even though I use XFS and btrfs on my SUSE systems. I will
label the three partitions: "gbcc1", "gbcc2", "gbcc3", thus I will use the -L option for mkfs command:

5. Now lets mount the device partitions. I will use the same directory as in the previous exercise, but I will
create two other directories, for each partition.
6.

Now unmount the partitions:

I unmounted the drives using the GUI

Laboratory objective 5: Working with swap


Commands used: cat, dd, mkswap, cp, swapon, chown, chmod, swapoff, rm

Solution:

1. Examine your current swap space with the command:

2. We will now add more swap space to the system by adding a new swap file. You can add more space
with a new partition, if you have one unused.
3. Now format the file as a swap partition. First I will move the swapfile into the /backup_test directory for
more convenience.
4.

Now I will format the swap partition:

4. Activate the new swap space:

You notice that the distribution is warning us that the new swap is insecure, so we will need to take extra
measures and fix this:

5. Now make sure that it is being used:

6. If you are not using the file, you can then remove it from use and delete it from your system with the
commands:
We did an ls to make sure that the file was removed.

You might also like