Linux — Disk and Filesystem management

William Lee
8 min readOct 10, 2023

--

The purpose of this post is to learn and review the contents of ‘Linux Bible — Chapter 12: Managing Disks and Filesystems’ by Negus C. (Practiced on RHEL 9.2— on Virtual machine 7.0.10)

Understanding Disk Storage

When you install an operating system, the disk is typically divided into one or more partitions, each of which is formatted with a filesystem. Some partitions may have specialised formats, such as those used for swap areas or LVM volumes. Disks serve as a means of permanent storage.

Hard Disk: This category includes Solid State Drives (SSD) and Hard Disk Drives (HDD). Hard disks are used for permanent data storage. They offer large storage capacities but have slower data access speeds and are relatively cost-effective.

Random Access Memory (RAM): RAM is used for temporary data storage. It provides fast data access but has limited capacity and is relatively expensive. RAM is volatile memory, meaning data is lost when the power is turned off.

Swap Area: Swap area serves as temporary storage and is typically a dedicated partition on the hard disk or a swap file. It’s used to move data in and out of RAM to prevent it from exceeding RAM’s capacity.

Logical Volume Manager (LVM): LVM allows you to create pools of storage space known as volume groups from physical volumes. Volume groups offer greater flexibility for resizing logical volumes compared to directly resizing disk partitions.

At least one disk partition is required for Linux, typically assigned to the root (/) of the entire Linux filesystem. It’s common to have separate partitions assigned to specific directories like /home, /var, and / or /tmp. Each partition is connected to the larger Linux filesystem by “mounting” it to a designated point in the filesystem. Any files added to the mount point directory or its subdirectories are stored on that partition.

Partitioning Hard Disks

Creating a partition disk

Use the parted or fdisk command with the -l option or lsblk command to identify the disk you want to create a partition on, and to list available disks and their partitions.

# parted -l /dev/sda
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sda: 53.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number Start End Size Type File system Flags
1 1049kB 1075MB 1074MB primary xfs boot
2 1075MB 53.7GB 52.6GB primary lvm
# fdisk -l /dev/sda
Disk /dev/sda: 50 GiB, 53687091200 bytes, 104857600 sectors
Disk model: VBOX HARDDISK
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: 0xbbae67db

Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 2099199 2097152 1G 83 Linux
/dev/sda2 2099200 104857599 102758400 49G 8e Linux LVM
# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda 8:0 0 50G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 49G 0 part
├─rhel-root 253:0 0 44G 0 lvm /
└─rhel-swap 253:1 0 5G 0 lvm [SWAP]
sr0 11:0 1 51M 0 rom /run/media/famishd/VBox_GAs_7.0.10

Run the fdisk command on the target disk you want to partition, and then create a new partition using the following commands:

# fdisk /dev/sda


Welcome to fdisk (util-linux 2.37.4).

Changes will remain in memory only, until you decide to write them.

Be careful before using the write command.



This disk is currently in use - repartitioning is probably a bad idea.

It's recommended to umount all file systems, and swapoff all swap

partitions on this disk.


Command (m for help): m

Help:

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

Generic
d delete a partition
F list free unpartitioned space
l list known 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
I load disk layout from sfdisk script file
O 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):

Use the mkfs command to format the new disk partition with a filesystem. In most cases, you would want to use a journaling filesystem such as ext3, ext4, or xfx.

# mkfs.ext4 /dev/sda3

To use the new filesystem, you’ll need to create a mount point and then use the mount command to mount it to the partition. You can use the df command to check information about total space and available space on a filesystem.

# mkdir /mnt/test
# mount /dev/sda3 /mnt/test
# df -h /dev/sda3
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 1014M 260M 755M 26% /mnt/test

Using Logical Volume Manager Partitions

Logical Volume Manager (LVM) offers a high degree of flexibility and efficiency when dealing with changing storage needs. With LVM, logical volumes can dynamically utilise space from volume groups as required. This provides several advantages:

Dynamic Volume Expansion: You can add more space to a logical volume from the volume group while the volume is in use. This flexibility allows for storage adjustments without downtime.

Adding Physical Volumes: When a volume group starts running low on space, you can easily add additional physical volumes, which can come from different disks. This scalability helps address growing storage demands.

Data Migration: LVM enables data migration from one physical volume to another. This capability allows you to replace smaller disks with larger ones while the filesystems remain operational, eliminating downtime.

Shrinking Filesystems: While shrinking filesystems does require unmounting the logical volume, LVM simplifies the process, making it easier to reclaim disk space.

Advanced Features: LVM supports advanced features like mirroring and cluster configurations, enhancing data redundancy and fault tolerance.

Checking an existing LVM

Checking if a Partition is used in an LVM Group:

Use the pvdisplay command to determine if a partition is allocated within an LVM group.

# pvdisplay /dev/sda2
--- Physical volume ---
PV Name /dev/sda2
VG Name rhel
PV Size <49.00 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 12543
Free PE 0
Allocated PE 12543
PV UUID A4Ah5q-VLCa-XxLL-lgHl-Ding-GKWe-0ItFXg

You can observe that the LVM physical volume represented by /dev/sda2 has a total capacity of approximately 49.00 GiB, all of which has been fully allocated to a volume group named rhel. The smallest usable storage unit from this physical volume is 4.0 MiB, referred to as a Physical Extent (PE).

Viewing Information about the Volume Group:

Use the vgdisplay command to retrieve information about the volume group.

# vgdisplay rhel
--- Volume group ---
VG Name rhel
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size <49.00 GiB
PE Size 4.00 MiB
Total PE 12543
Alloc PE / Size 12543 / <49.00 GiB
Free PE / Size 0 / 0
VG UUID FWrRrP-vEET-UvcH-FHJ0-qgjQ-Hnsv-jrc6v9

You can observe that all 12,543 Physical Extents (PEs) have been fully allocated.

Checking Allocation with lvdisplay:

Utilise the lvdisplay command to determine the allocation of these PEs:

# lvdisplay rhel
--- Logical volume ---
LV Path /dev/rhel/swap
LV Name swap
VG Name rhel
LV UUID cvVJQR-RL1S-vILf-KRal-Df6J-pe2n-6znrft
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2023-10-10 10:46:20 +1100
LV Status available
# open 2
LV Size 5.00 GiB
Current LE 1280
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:1

--- Logical volume ---
LV Path /dev/rhel/root
LV Name root
VG Name rhel
LV UUID XnuE8Y-eGfw-5ow6-2oI4-XudV-mNy2-U6mN8d
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2023-10-10 10:46:21 +1100
LV Status available
# open 1
LV Size <44.00 GiB
Current LE 11263
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0

Within the rhel volume group, there are two logical volumes that utilise storage: /dev/rhel/swap (5GB) and /dev/rhel/root (<44GB). These logical volumes are associated with device names that include the volume group name and the logical volume name, such as /dev/rhel/swap and /dev/rhel/root. You can also find corresponding devices in the /dev/mapper directory, such as rhel_root and rhel_swap. Both sets of names can be used to reference these logical volumes.

Creating LVM logical volumes

Use the lvcreate command to create a new volume group by adding a physical volume. For example, consider creating a logical volume named ‘test’ with a size of 1GB in the volume group ‘myvg’:

# lvcreate -n test -L 1G myvg

After creating the logical volume, format it with a filesystem of your choice. Next, create a directory to serve as a mount point for accessing the data stored on the logical volume. Once you’ve set up the mount point directory, use the mount command to mount the logical volume to it.

# mkfs -t ext4 /dev/mapper/myvg-test
# mkdir /mnt/test1
# mount /dev/mapper/myvg-test /mnt/test1

Growing LVM logical volumes

Utilise the lvextend command to expand the logical volume. Use the ‘+’ sign to increase the logical volume size.

# lvextend -L +1G /dev/mapper/myvg-test

After extending the logical volume, employ the resize2fs command to adjust the filesystem to match the new logical volume size:

# resize2fs -p /dev/mapper/myvg-test

Mounting Filesystems

When you install Linux, you can either let the installer set up your partitions automatically or create them manually with custom mount points.

After booting into Linux, the system automatically mounts the partitions listed in the /etc/fstab file. This file contains information about these mounted partitions and how they're configured.

You can use the mount command not just for local storage devices but also for mounting different types of filesystems on your Linux system.

Swap area

A reserved portion of your computer’s disk that Linux uses as a backup when your system’s physical memory (RAM) is fully utilised. If your RAM is completely occupied and you attempt to launch another application without a swap area, that application won’t be able to start.

With a swap area, Linux can temporarily move data from RAM to this reserved space and retrieve it when necessary. This action may slow down your system’s performance, but it prevents processes from failing due to memory shortages.

Enabling swap area

To create a swap area from a partition or a file, use the mkswap command, and to enable that swap area temporarily, you can use the swapon command.

# mkswap /dev/sda
# swapon /dev/sda

Disabling swap area

To disable a swap area, you can use the swapoff command. This is typically done when the swap area is no longer required, and you want to free up the space it was occupying. For instance, you might disable swap to remove a USB drive providing a swap partition or reclaim space used by a swap file.

Reference:

Negus, C 2020, Linux bible, 10th edition, Wiley, Canada

--

--

William Lee
William Lee

Written by William Lee

To continuously learn and document my journey towards becoming a cloud engineer.

No responses yet