Partitioning and Formatting A Disk Drive in Linux
Partitioning and Formatting A Disk Drive in Linux
Blocks
Blocks are a layer of storage devices that allow individual access to each independently.
They allow programs to access storage without worrying about whether the underlying
hardware device is a hard drive, solid state drive, flash drive, etc.
In Linux, you can view block devices and file systems attached to your system using
the lsblk command. This command gathers information about all devices attached to
the system, and prints them out using a tree-like structure. To view the devices attached
to your VM, use the lsblk command.
Lsblk
A first disk, sda, is also available, but it's not mounted. In this lab, you'll divide this disk
into two partitions. You'll then mount one of these partitions onto the file system, so you
can start accessing files from it.
Note: These may be swapped for you, and your VM may be mounted on sda instead of sdb. This
will change the commands used in the lab, so when you see \[MOUNT DRIVE\] replace it with
your mount drive (sda or sdb) and when you see \[SECOND DRIVE\] replace it with the other
one. If your VM is mounted on sda, the screenshots will also be flipped from what you will see.
Optionally, you can view disks mounted on the system using the df command. This
command is normally used to display the amount of space available on the file system.
It lists all block devices with the available space on them. Use the -h option to display
file sizes in human readable format.
df -h
Partitions
Instead of using a storage block as a whole, it's common practice to divide a storage
block into different partitions. Partitions can be different sizes, and formatted to
different filesystems. This allows you to use a single storage device for different
purposes.
You can display partition information using the fdisk command. You can also use the -l
option to list partitions in the block. You can pass a device name to the fdisk command
to list the partitions contained in that device.
sudo fdisk -l
Caution!: Modifying partitions is destructive, and can lead to loss of data. Not good! Remember
to always backup your data before modifying partitions on a live system.
Mount and umount
Mounting and unmounting mean making devices available or unavailable on a Linux file
system. This is accomplished by the commands mount and umount. Before modifying a
disk, you should first unmount it from the system, using the umount command. When
modifications on the disk are done, you should mount it back onto the system. For this
exercise, since the device we're partitioning isn't initially mounted, you can proceed with
partitioning.
Go ahead and start fdisk in interactive mode by passing the name of the disk you want
to partition. In this lab, we'll partition /dev/sda
Note: We will partition the disk that's not currently mounted. You should select dev/sdb if
/dev/sda is where the operating system is mounted, and /dev/sda otherwise. You can still
partition the disk even when the operating system is running from it, but a reboot will be required
in order for the partition changes you make to take place.
Start fdisk by passing the disk you want to partition as the parameter.
Creating Partitions
You'll now create new partitions using fdisk. You'll partition the second drive into two
partitions: one swap partition of size 1GB, and another of size 9GB. The file system type
on the second partition will be ext4.
Use the d command control to delete the default partitions. When you issue
the d command control, fdisk asks you to enter the number of partitions you want to
delete. Since you have twelve partitions, fdisk automatically selects the last partition by
default, and pressing Enter deletes the last partition. Repeat this process until you
delete all the twelve partitions.
You'll then need to provide the starting sector (memory location) of the new partition,
from where you want to allocate. Here, press Enter to select the default value 2048.
Before committing your changes, you'll change the second partition to a different
partition type. You'll change the first partition type to a Linux swap type. Enter command
control t to change the partition type, and select the first partition.
Head's up: Some of the characters in the partition type name Linux swap are truncated.
Up to this point, you've just been editing the partition table in memory. You can use
the q command here to quit fdisk without committing changes to the disk. You can also
update your partitions by using the d and n commands to remove and add new
partitions.
You can also use the v command here to verify your changes before proceeding.
The second disk device is now made up of two partitions of 1GB and 9GB, respectively.
To do this, use lsblk again to find the disk you want to create the file system type in.
Lsblk
.5 (15-Dec-2018)
Discarding device blocks: done
Creating filesystem with 2359035 4k blocks and 589824 inodes
Filesystem UUID: 3e68d65f-3029-4232-8f45-b924de3862bd
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632
Allocating group tables: done
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done
You can now mount /dev/sda2 to a location on the file system to start accessing files
on it. Mount it on the directory /home/my_drive.
You can verify the file systems and block devices attached to your system
using lsblk command.
Lsblk