Day 5 – Disk Management & LVM in Linux

This training covers:

  • Creating partitions using fdisk
  • Creating and mounting ext2, ext3, ext4 and XFS filesystems
  • Primary, extended and logical partitions
  • Expanding a partition after increasing disk size
  • Creating and managing LVM
  • Expanding LVM physical volumes, volume groups and logical volumes
  • Adding additional disks to LVM
  • Migrating LVM data using pvmove
  • Removing disks from an LVM configuration
  • Understanding Linux inodes
  • Demonstrating inode consumption
  • Creating and configuring swap
  • Checking directory and filesystem usage


2. File Systems

A filesystem provides the structure used by an operating system to store, organize, retrieve and manage files on storage devices.
The lab introduces the following filesystems:

Filesystem
FAT: Basic and simple filesystem with broad compatibility.
FAT32: FAT variant with larger capacity support but approximately 4 GB maximum per file.
NTFS: Windows filesystem providing journaling, permissions and security features. 
ext3: Linux filesystem that introduced journaling to the ext filesystem family.
ext4: Modern Linux filesystem and successor to ext3. 
XFS: Scalable, high-performance Linux filesystem commonly used for enterprise workloads.


3. Check Existing Disk Layout

First check the existing disks and partitions.
[root@rocky ~]# lsblk
Example environment:
sda           16G
├─sda1        600M    /boot/efi
├─sda2        1G      /boot
└─sda3        14.4G
  ├─rl-root   12.8G   /
  └─rl-swap   1.6G    [SWAP]

A new 4 GB disk /dev/sdb is then added to the server.


4. Create Four Partitions Using fdisk

4.1 Partition Layout
Create:
One primary partition – /dev/sdb1 – 1 GB
One extended partition – /dev/sdb2 – approximately 3 GB
Three logical partitions:
/dev/sdb5 – 1 GB
/dev/sdb6 – 1 GB
/dev/sdb7 – remaining space

The layout demonstrates the difference between primary, extended and logical partitions.

Start fdisk:

[root@rocky ~]# fdisk /dev/sdb
Inside fdisk:

Command (m for help): p
Create the first primary partition:

Command (m for help): n
Select (default p): p
Partition number (1-4, default 1): 1
First sector: <Enter>
Last sector: +1G
Create the extended partition:

Command (m for help): n
Select (default p): e
Partition number (2-4, default 2): <Enter>
First sector: <Enter>
Last sector: <Enter>
Create logical partition 5:

Command (m for help): n
First sector: <Enter>
Last sector: +1G
Create logical partition 6:

Command (m for help): n
First sector: <Enter>
Last sector: +1G
Create logical partition 7:

Command (m for help): n
First sector: <Enter>
Last sector: <Enter>
Verify:

Command (m for help): p
Write the partition table:

Command (m for help): w
The resulting layout is:

/dev/sdb1    1G     Linux
/dev/sdb2    3G     Extended
/dev/sdb5    1G     Linux
/dev/sdb6    1G     Linux
/dev/sdb7    1020M  Linux


5. Create Filesystems

Create the filesystems:
[root@rocky ~]# mkfs.ext3 /dev/sdb5
[root@rocky ~]# mkfs.ext4 /dev/sdb6
[root@rocky ~]# mkfs.xfs /dev/sdb7

Create/mount the required mount points:
[root@rocky ~]# mount /dev/sdb6 /data-ext4
[root@rocky ~]# mount /dev/sdb5 /data-ext3/
[root@rocky ~]# mount /dev/sdb1 /data-ext2
[root@rocky ~]# mount /dev/sdb7 /data-xfs/

Training observation: The supplied lab shows /dev/sdb1 as ext2 in the df output, but does not show a preceding mkfs.ext2 command. Preserve this sequence when demonstrating the supplied lab.


6. Test Filesystem Usage

Create a 900 MB file on each filesystem:
[root@rocky ~]# dd if=/dev/zero of=/data-ext3/largefile bs=1M count=900 status=progress
[root@rocky ~]# dd if=/dev/zero of=/data-ext4/largefile bs=1M count=900 status=progress
[root@rocky ~]# dd if=/dev/zero of=/data-xfs/largefile bs=1M count=900 status=progress

Check filesystem usage:
[root@rocky ~]# df -PTh /data-*
Expected result from the lab:
/dev/sdb1  ext2  1007M   24K  956M   1% /data-ext2
/dev/sdb5  ext3   975M  901M   23M  98% /data-ext3
/dev/sdb6  ext4   974M  901M  6.2M 100% /data-ext4
/dev/sdb7  xfs   1014M  940M   75M  93% /data-xfs


7. Extend a Partition After Increasing Disk Size

7.1 Important Concept
Initially /dev/sdb is 4 GB.
Increase the virtual disk size to 6 GB.

The operating system must first rescan the disk:
[root@rocky ~]# echo 1 > /sys/class/block/sdb/device/rescan

Verify:
[root@rocky ~]# lsblk

The disk now appears as 6 GB, but the existing partitions still occupy only the original 4 GB.


7.2 Why Can Only sdb7 Be Extended Directly?

The original layout is:
sdb1  → 1G
sdb2  → Extended partition
 ├─ sdb5 → 1G
 ├─ sdb6 → 1G
 └─ sdb7 → 1020M
After the disk is expanded, the additional space exists after /dev/sdb7.
Therefore:
  • /dev/sdb1 cannot directly use that space because other partitions exist after it.
  • /dev/sdb5 cannot directly use the space because /dev/sdb6 and /dev/sdb7 follow it.
  • /dev/sdb6 cannot directly use the space because /dev/sdb7 follows it.
  • /dev/sdb7 is the last partition, so the new free space is immediately after it.

This is why the supplied lab demonstrates extending /dev/sdb7.

8. Recreate the Extended Partition

Start:

[root@rocky ~]# fdisk /dev/sdb
Delete partitions in reverse order:
Command (m for help): d
Partition number: 7
Command (m for help): d
Partition number: 6
Command (m for help): d
Partition number: 5
Command (m for help): d
Partition number: 2
Do not delete /dev/sdb1.

Recreate the extended partition:

Command (m for help): n
Select (default p): e
Partition number: 2
First sector: <Enter>
Last sector: <Enter>
This creates an approximately 5 GB extended partition.

Recreate /dev/sdb5:

Command (m for help): n
First sector: <Enter>
Last sector: +1G
Do you want to remove the signature? [Y]es/[N]o: N
Recreate /dev/sdb6:

Command (m for help): n
First sector: <Enter>
Last sector: +1G
Do you want to remove the signature? [Y]es/[N]o: N
Recreate /dev/sdb7 using the remaining space:

Command (m for help): n
First sector: <Enter>
Last sector: <Enter>
Do you want to remove the signature? [Y]es/[N]o: N
Verify:

Command (m for help): p
Write:

Command (m for help): w
Synchronize:

[root@rocky ~]# sync
Check filesystems:

[root@rocky ~]# df -PTh
The kernel may continue using the old partition table until reboot, as shown in the supplied lab.


9. Grow the XFS Filesystem

The partition has now been expanded, but the filesystem must also be expanded.
Run:
[root@rocky ~]# xfs_growfs /dev/sdb7

Verify:
[root@rocky ~]# df -PTh /data-*

Expected:
/dev/sdb7  xfs  3.0G  955M  2.1G  32% /data-xfs

The ext2/ext3/ext4 filesystems remain at their previous sizes.


10. Create a Partition and Put It Under LVM

Add a new 3 GB /dev/sdb disk.

Check:
[root@rocky ~]# lsblk

Start fdisk:
[root@rocky ~]# fdisk /dev/sdb

Create one primary partition covering the disk:
Command (m for help): n
Select (default p): p
Partition number: 1
First sector: <Enter>
Last sector: <Enter>

Change the partition type:
Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e

Verify:
Command (m for help): p

Write:
Command (m for help): w

The supplied lab uses partition type 8e – Linux LVM.


11. Create LVM Physical Volume

Verify:
[root@rocky ~]# lsblk

Create the physical volume:
[root@rocky ~]# pvcreate /dev/sdb1

Create the volume group:
[root@rocky ~]# vgcreate vg01 /dev/sdb1

Create logical volumes:
[root@rocky ~]# lvcreate -L +1G -n lv-ext3 vg01
[root@rocky ~]# lvcreate -L +1G -n lv-ext4 vg01

The supplied lab demonstrates that attempting another full 1 GB LV can fail because only one extent is short:
[root@rocky ~]# lvcreate -L +1G -n lv-xfs vg01

Use the remaining space:
[root@rocky ~]# lvcreate -l +100%FREE -n lv-xfs vg01


12. Create Filesystems on LVM

Create ext3:
[root@rocky ~]# mkfs.ext3 /dev/vg01/lv-ext3

Create ext4:
[root@rocky ~]# mkfs.ext4 /dev/vg01/lv-ext4

Create XFS:
[root@rocky ~]# mkfs.xfs /dev/vg01/lv-xfs

Create mount points:
[root@rocky ~]# mkdir /data-ext3
[root@rocky ~]# mkdir /data-ext4
[root@rocky ~]# mkdir /data-xfs

Mount:
[root@rocky ~]# mount /dev/vg01/lv-ext3 /data-ext3
[root@rocky ~]# mount /dev/vg01/lv-ext4 /data-ext4
[root@rocky ~]# mount /dev/vg01/lv-xfs /data-xfs


13. Configure /etc/fstab

Take a backup first:

[root@rocky ~]# cp /etc/fstab /etc/fstab-backup

Edit:
[root@rocky ~]# vi /etc/fstab
Add:
/dev/mapper/vg01-lv--ext3 /data-ext3 ext3 rw,relatime 0 0
/dev/mapper/vg01-lv--ext4 /data-ext4 ext4 rw,relatime 0 0
/dev/mapper/vg01-lv--xfs /data-xfs xfs rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota 0 0

Verify:
[root@rocky ~]# df -PTH /data*


14. Test LVM Filesystems

Create 900 MB files:
[root@rocky ~]# dd if=/dev/zero of=/data-ext3/largefile bs=1M count=900 status=progress
[root@rocky ~]# dd if=/dev/zero of=/data-ext4/largefile bs=1M count=900 status=progress
[root@rocky ~]# dd if=/dev/zero of=/data-xfs/largefile bs=1M count=900 status=progress

Verify:
[root@rocky ~]# df -PTH /data*

Check files:
[root@rocky ~]# cd /data-ext3
[root@rocky ~]# ls
[root@rocky ~]# cd /data-ext4
[root@rocky ~]# ls
[root@rocky ~]# cd /data-xfs
[root@rocky ~]# ls


15. Expand the LVM Disk

Increase /dev/sdb from 3 GB to 6 GB.
Rescan:
[root@rocky ~]# echo 1 > /sys/class/block/sdb/device/rescan

Verify:
[root@rocky ~]# lsblk
The disk is now 6 GB, but /dev/sdb1 is still 3 GB.

Check LVM:
[root@rocky ~]# pvs
[root@rocky ~]# vgs
[root@rocky ~]# lvs


16. Expand the LVM Partition is (disadvantage hence we should always take disk under LVM without fdisk partition) 

Start:
[root@rocky ~]# fdisk /dev/sdb
Delete /dev/sdb1:
Command (m for help): d
Selected partition 1

Create it again using the entire disk:
Command (m for help): n
Select (default p): p
Partition number: 1
First sector: <Enter>
Last sector: <Enter>

When prompted:
Partition #1 contains a LVM2_member signature.
Do you want to remove the signature? [Y]es/[N]o: n
Verify:
Command (m for help): p
Write:
Command (m for help): w
Check:
[root@rocky ~]# lsblk /dev/sdb


17. Resize the LVM Physical Volume

Before resizing:
[root@rocky ~]# pvs

Resize:
[root@rocky ~]# pvresize /dev/sdb1

Verify:
[root@rocky ~]# pvs
[root@rocky ~]# vgs
[root@rocky ~]# lvs

The supplied lab shows approximately:
/dev/sdb1   <6.00g   3.00g free

So the VG now has approximately 3 GB additional free space.


18. Extend ext3 Logical Volume

Increase the LV:
[root@rocky ~]# lvextend -L +1G /dev/mapper/vg01-lv--ext3

Resize the filesystem:
[root@rocky ~]# resize2fs /dev/mapper/vg01-lv--ext3

19. Extend ext4 Logical Volume

Increase the LV:
[root@rocky ~]# lvextend -L +1G /dev/mapper/vg01-lv--ext4

Resize the filesystem:
[root@rocky ~]# resize2fs /dev/mapper/vg01-lv--ext4

20. Extend XFS Logical Volume

Increase the XFS LV:
[root@rocky ~]# lvextend -L 1.9G /dev/mapper/vg01-lv--xfs

Grow the XFS filesystem:
[root@rocky ~]# xfs_growfs /dev/mapper/vg01-lv--xfs

Verify all filesystems:
[root@rocky ~]# df -PTH /data*
Expected result is approximately:
ext3  2.1G
ext4  2.1G
xfs   2.1G

The supplied lab demonstrates the complete sequence of disk expansion → partition expansion → pvresize → LV expansion → filesystem expansion.


21. Add a New Disk to LVM

Add a new 3 GB /dev/sdc disk.

Verify:
[root@rocky ~]# lsblk

Create a physical volume directly on /dev/sdc:
[root@rocky ~]# pvcreate /dev/sdc

Check:
[root@rocky ~]# pvs
[root@rocky ~]# vgs
[root@rocky ~]# lvs


Add the new PV to vg01:
[root@rocky ~]# vgextend vg01 /dev/sdc

Verify:
[root@rocky ~]# pvs
[root@rocky ~]# vgs

22. Extend LVs Using New Disk Space

The supplied lab uses -r, which automatically resizes the filesystem after extending the LV.
[root@rocky ~]# lvextend -L +1G /dev/mapper/vg01-lv--ext3 -rn
[root@rocky ~]# lvextend -L +1G /dev/mapper/vg01-lv--ext4 -rn
[root@rocky ~]# lvextend -L 2.9G /dev/mapper/vg01-lv--xfs -rn


Or, as demonstrated in the supplied lab, these can be executed as one command line:
[root@rocky ~]# lvextend -L +1G /dev/mapper/vg01-lv--ext3 -rn ; lvextend -L +1G /dev/mapper/vg01-lv--ext4 -rn ; lvextend -L 2.9G /dev/mapper/vg01-lv--xfs -rn

Verify:
[root@rocky ~]# df -PTH /data*
Expected sizes are approximately:
ext3  3.2G
ext4  3.2G
xfs   3.2G


23. LVM Data Migration Using pvmove

Objective
Migrate LVM data from:
/dev/sdb1
/dev/sdc
to:
/dev/sdd

The supplied lab adds a 12 GB /dev/sdd disk.

Check:
[root@rocky ~]# lsblk

Create the new PV:
[root@rocky ~]# pvcreate /dev/sdd

Add it to the VG:
[root@rocky ~]# vgextend vg01 /dev/sdd

Verify:
[root@rocky ~]# pvs


23.1 Move Data from sdc to sdd

[root@rocky ~]# pvmove /dev/sdc /dev/sdd

Verify:
[root@rocky ~]# pvs
The expected result is that /dev/sdc becomes free while the allocated extents are moved to /dev/sdd.

23.2 Move Data from sdb1 to sdd

[root@rocky ~]# pvmove /dev/sdb1 /dev/sdd
Verify:
[root@rocky ~]# pvs
At this point the original PVs can be shown as having free extents.

Check the VG in detail:
[root@rocky ~]# vgdisplay -v vg01
The supplied lab shows:
VG Name        vg01
Format         lvm2
VG Status      resizable
Cur LV         3
Cur PV         3
VG Size        <20.99 GiB
Free PE/Size   3094 / <12.09 GiB

The three logical volumes remain:
lv-ext3
lv-ext4
lv-xfs


24. Remove Old Physical Volumes

Remove /dev/sdc from the volume group:
[root@rocky ~]# vgreduce vg01 /dev/sdc
Remove /dev/sdb1:
[root@rocky ~]# vgreduce vg01 /dev/sdb1
Verify:
[root@rocky ~]# vgs
[root@rocky ~]# pvs
Remove the LVM labels:
[root@rocky ~]# pvremove /dev/sdc
Verify:
[root@rocky ~]# pvs
Then:
[root@rocky ~]# pvremove /dev/sdb1
Verify:
[root@rocky ~]# pvs
The supplied lab demonstrates that the old PVs are then no longer members of vg01.


25. Remove the Old sdb Partition

Start:
[root@rocky ~]# fdisk /dev/sdb
Display the partition table:
Command (m for help): p
Delete partition 1:
Command (m for help): d
Verify:
Command (m for help): p
Write:
Command (m for help): w
Check:
[root@rocky ~]# lsblk
The expected final LVM storage is on /dev/sdd.
Verify mounted filesystems: [root@rocky ~]# df -PTh /data* The supplied lab shows: /dev/mapper/vg01-lv--ext3 ext3 3.0G 901M 2.0G 32% /data-ext3 /dev/mapper/vg01-lv--ext4 ext4 3.0G 901M 1.9G 32% /data-ext4 /dev/mapper/vg01-lv--xfs xfs 2.9G 954M 2.0G 33% /data-xfs


26. Understanding Inodes

An inode is a filesystem data structure that stores metadata about a file.
The inode contains information such as:
  • File type
  • File permissions
  • Owner
  • Group
  • File size
  • Timestamps
  • Links
  • Locations of filesystem data blocks

A filesystem can have free disk space while still running out of inodes.

Check inode usage:

[root@rocky ~]# df -iPTH /data*

Example from the lab:

Filesystem                Type Inodes IUsed IFree IUse%
/dev/mapper/vg01-lv--ext3 ext3   197k    12  197k    1%
/dev/mapper/vg01-lv--ext4 ext4   197k    12  197k    1%
/dev/mapper/vg01-lv--xfs  xfs    1.6M     4  1.6M    1%

27. Demonstrate Inode Consumption

Create 100,000 files on ext3:

[root@rocky ~]# for i in {1..100000}; do
>     touch /data-ext3/file_$i
> done

Create 100,000 files on ext4:

[root@rocky ~]# for i in {1..100000}; do
>     touch /data-ext4/file_$i
> done

Create 1,000,000 files on XFS:

[root@rocky ~]# for i in {1..1000000}; do
>     touch /data-xfs/file_$i
> done

Check inode consumption:

[root@rocky ~]# df -iPTH /data*

The supplied lab demonstrates increased inode usage:

ext3   197k → 101k used
ext4   197k → 101k used
xfs    1.6M → 522k used

28. Demonstrate Inode Increase After Filesystem Expansion

Check the volume group:

[root@rocky ~]# vgs

Extend ext3 by 1 GB:

[root@rocky ~]# lvextend -L +1G /dev/mapper/vg01-lv--ext3

The lab then extends it again with automatic filesystem resizing:

[root@rocky ~]# lvextend -L +1G /dev/mapper/vg01-lv--ext3 -rn

Check inode usage:

[root@rocky ~]# df -iPTH /data*
The supplied output shows ext3 increasing from approximately 197k inodes to 328k inodes after filesystem expansion.
This demonstrates that filesystem expansion can also provide additional inode capacity, depending on filesystem design.

29. Configure Swap Partition

Add a 2 GB /dev/sdc disk.

Check:

[root@rocky ~]# lsblk
[root@rocky ~]# fdisk /dev/sdc
Command (m for help): n
Select (default p): p
Partition number: 1
First sector: <Enter>
Last sector: <Enter>
Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 82
Command (m for help): p
Command (m for help): w

Start fdisk:
Create a primary partition:
Change the partition type:
Verify:
Write:

30. Create and Enable Swap

Check current swap:

[root@rocky ~]# free -h
[root@rocky ~]# mkswap /dev/sdc1
[root@rocky ~]# swapon /dev/sdc1
[root@rocky ~]# free -h

Create the swap filesystem:
Enable it:
Verify:
The supplied lab shows swap increasing from approximately 1.6 GiB to 3.6 GiB after enabling /dev/sdc1.

31. Make Swap Persistent

Check /etc/fstab:

[root@rocky ~]# cat /etc/fstab | grep -v "#"
/dev/sdc1 none swap defaults 0 0

The supplied lab adds:
This ensures the swap partition is configured to be activated during boot.

32. Check Directory Size


[root@rocky ~]# du -sh /var
[root@rocky ~]# du -sh /var/log

To check the total size of /var:
To check a specific directory:

33. Identify Large Directories

To identify which directories under /var are consuming the most space:

[root@rocky ~]# du -h --max-depth=1 /var | sort -hr
8.5G /var
5.2G /var/log
2.1G /var/cache
800M /var/lib

Example:
This is useful when troubleshooting a filesystem that is running out of space.

34. Important Commands – Quick Reference

Disk and Partition
[root@rocky ~]# lsblk
[root@rocky ~]# fdisk /dev/sdb
[root@rocky ~]# echo 1 > /sys/class/block/sdb/device/rescan

Filesystems
[root@rocky ~]# mkfs.ext3 /dev/sdb5
[root@rocky ~]# mkfs.ext4 /dev/sdb6
[root@rocky ~]# mkfs.xfs /dev/sdb7
[root@rocky ~]# df -PTh
[root@rocky ~]# df -PTh /data-*

LVM
[root@rocky ~]# pvcreate /dev/sdb1
[root@rocky ~]# vgcreate vg01 /dev/sdb1
[root@rocky ~]# vgextend vg01 /dev/sdc
[root@rocky ~]# pvresize /dev/sdb1
[root@rocky ~]# pvs
[root@rocky ~]# vgs
[root@rocky ~]# lvs

Logical Volume
[root@rocky ~]# lvcreate -L +1G -n lv-ext3 vg01
[root@rocky ~]# lvextend -L +1G /dev/mapper/vg01-lv--ext3
[root@rocky ~]# lvextend -L +1G /dev/mapper/vg01-lv--ext4
[root@rocky ~]# lvextend -L 1.9G /dev/mapper/vg01-lv--xfs

Filesystem Expansion
[root@rocky ~]# resize2fs /dev/mapper/vg01-lv--ext3
[root@rocky ~]# resize2fs /dev/mapper/vg01-lv--ext4
[root@rocky ~]# xfs_growfs /dev/mapper/vg01-lv--xfs

LVM Migration
[root@rocky ~]# pvmove /dev/sdc /dev/sdd
[root@rocky ~]# pvmove /dev/sdb1 /dev/sdd
[root@rocky ~]# vgreduce vg01 /dev/sdc
[root@rocky ~]# vgreduce vg01 /dev/sdb1
[root@rocky ~]# pvremove /dev/sdc
[root@rocky ~]# pvremove /dev/sdb1

Inodes
[root@rocky ~]# df -iPTH /data*

Swap
[root@rocky ~]# mkswap /dev/sdc1
[root@rocky ~]# swapon /dev/sdc1
[root@rocky ~]# free -h

Disk Usage
[root@rocky ~]# du -sh /var
[root@rocky ~]# du -sh /var/log
[root@rocky ~]# du -h --max-depth=1 /var | sort -hr




No comments:

Post a Comment