How to manage Linux File System?

Add new disk to Linux system.

After adding a disk to the service, the disk will be added automatically to the virtual machine.
The lsblk command on Linux lists block devices.

Before adding:

[root@weathered-firefly-vs ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   50G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   49G  0 part 
  ├─fedora-root 253:0    0   48G  0 lvm  /
  └─fedora-swap 253:1    0    1G  0 lvm  [SWAP]
sdb               8:16   0   50G  0 disk 
sdc               8:32   0   50G  0 disk 
sr0              11:0    1 1024M  0 rom  
sr1              11:1    1  376K  0 rom  
zram0           252:0    0  1.9G  0 disk [SWAP]

After after adding:

[root@weathered-firefly-vs ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   50G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   49G  0 part 
  ├─fedora-root 253:0    0   48G  0 lvm  /
  └─fedora-swap 253:1    0    1G  0 lvm  [SWAP]
sdb               8:16   0   50G  0 disk 
sdc               8:32   0   50G  0 disk 
sdd               8:48   0   50G  0 disk 
sr0              11:0    1 1024M  0 rom  
sr1              11:1    1  376K  0 rom  
zram0           252:0    0  1.9G  0 disk [SWAP]

Create the New Partition

  1. Using fdisk, create a new partition on the /dev/sdc device. Enter n, to create a new partition:
[root@weathered-firefly-vs ~]# fdisk /dev/sdc

Welcome to fdisk (util-linux 2.36).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x58db5961.

Command (m for help): n
  1. Now choose p to create a new primary partition.
    Note: Your system can only have 4 primary partitions on this disk. If you’ve already reached this limit, create an extended partition.
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
  1. Choose the partition number and its first and last sectors, if you hit Enter, then by default new partition will use all available disk space.
First sector (2048-104857599, default 2048): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-104857599, default 104857599): 

Created a new partition 1 of type 'Linux' and of size 50 GiB.
  1. Finally, you need to write the partitions to disk with the w command.
Command (m for help): w

The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

Check whether the partition was created with the lsblk command.

[root@weathered-firefly-vs ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   50G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   49G  0 part 
  ├─fedora-root 253:0    0   98G  0 lvm  /
  └─fedora-swap 253:1    0    1G  0 lvm  [SWAP]
sdb               8:16   0   50G  0 disk 
└─sdb1            8:17   0   50G  0 part 
  └─fedora-root 253:0    0   98G  0 lvm  /
sdc               8:32   0   50G  0 disk 
└─sdc1            8:33   0   50G  0 part 
sdd               8:48   0   50G  0 disk 
sr0              11:0    1 1024M  0 rom  
sr1              11:1    1  376K  0 rom  
zram0           252:0    0  1.9G  0 disk [SWAP]

Extend the Physical Volume, the Volume Group, the Logical Volume and XFS filesystem with the new partition

  1. First, enter the df -hT command and select the system partition from the list that you want to extend. For example /dev/mapper/fedora-root
[root@weathered-firefly-vs ~]# df -hT
Filesystem              Type      Size  Used Avail Use% Mounted on
devtmpfs                devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                   tmpfs     2.0G     0  2.0G   0% /dev/shm
tmpfs                   tmpfs     786M 1000K  785M   1% /run
/dev/mapper/fedora-root xfs        98G  2.8G   96G   3% /
tmpfs                   tmpfs     2.0G  4.0K  2.0G   1% /tmp
/dev/sda1               xfs      1014M  248M  767M  25% /boot
tmpfs                   tmpfs     393M  4.0K  393M   1% /run/user/0
  1. With the help of pvs command, you can find out how your Physical Volume is called. The PFree column shows the amount of free space.
[root@weathered-firefly-vs ~]# pvs
  PV         VG     Fmt  Attr PSize   PFree
  /dev/sda2  fedora lvm2 a--  <49.00g    0 
  /dev/sdb1  fedora lvm2 a--  <50.00g    0
  1. Now create a physical volume as the basis for your LVM. Here /dev/sdc1 is the created partition.
[root@weathered-firefly-vs ~]# pvcreate /dev/sdc1
  Physical volume "/dev/sdc1" successfully created.  
[root@weathered-firefly-vs ~]# pvs
  PV         VG     Fmt  Attr PSize   PFree  
  /dev/sda2  fedora lvm2 a--  <49.00g      0 
  /dev/sdb1  fedora lvm2 a--  <50.00g      0 
  /dev/sdc1         lvm2 ---  <50.00g <50.00g
  1. With the help of vgs command, you can find out how your Volume Group is called. The VFree column shows the amount of free space.
[root@weathered-firefly-vs ~]# vgs
  VG     #PV #LV #SN Attr   VSize  VFree
  fedora   2   2   0 wz--n- 98.99g    0
  1. Then extend that Volume Group fedora by adding the newly created physical volume to it using vgextend command, check whether the changes can be applied again with the vgs command.
[root@weathered-firefly-vs ~]# vgextend fedora /dev/sdc1
  Volume group "fedora" successfully extended
[root@weathered-firefly-vs ~]# vgs
  VG     #PV #LV #SN Attr   VSize    VFree  
  fedora   3   2   0 wz--n- <148.99g <50.00g
  1. With the help of lvs command, you can find out how your Volume Group is called.
[root@weathered-firefly-vs ~]# lvs
  LV   VG     Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root fedora -wi-ao---- 97.99g                                                    
  swap fedora -wi-ao----  1.00g
  1. To extend the logical volume execute command: lvextend
[root@weathered-firefly-vs ~]# lvextend -l +100%FREE /dev/mapper/fedora-root
  Size of logical volume fedora/root changed from 97.99 GiB (25086 extents) to <147.99 GiB (37885 extents).
  Logical volume fedora/root successfully resized.
[root@weathered-firefly-vs ~]# lvs
  LV   VG     Attr       LSize    Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root fedora -wi-ao---- <147.99g                                                    
  swap fedora -wi-ao----    1.00g
  1. Finally, resize the XFS file system to a logical volume using the xfs_growfs command. If you are using ext4 filesystem, use the utility resize2fs.
[root@weathered-firefly-vs ~]# df -hT /dev/mapper/fedora-root
Filesystem              Type  Size  Used Avail Use% Mounted on
/dev/mapper/fedora-root xfs    98G  2.8G   96G   3% /
[root@weathered-firefly-vs ~]# xfs_growfs /
meta-data=/dev/mapper/fedora-root isize=512    agcount=15, agsize=1834752 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1
data     =                       bsize=4096   blocks=25688064, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=3583, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 25688064 to 38794240

Execute df -hT to confirm that new disk size is available to the Virtual Machine.

[root@weathered-firefly-vs ~]# df -hT /dev/mapper/fedora-root
Filesystem              Type  Size  Used Avail Use% Mounted on
/dev/mapper/fedora-root xfs   148G  3.2G  145G   3% /

The list of commands to extend the ‘root’ partition Ubuntu Server

Suitable for 18.04 LTS and 20.04 LTS

> \# growpart /dev/sda/ 3
> \# pvresize /dev/sda3/
> \# lvextend -l +100%FREE /dev/mapper/ubuntu--vg-ubuntu--lv/
> \# resize2fs /dev/ubuntu-vg/ubuntu-lv/

The list of commands to extend the ‘root’ partition Ubuntu Desktop 20.04 LTS

> \# growpart /dev/sda/ 2
> \# growpart /dev/sda/ 5
> \# pvresize /dev/sda5/
> \# lvextend -l +100%FREE /dev/mapper/vgubuntu-root/
> \# resize2fs /dev/vgubuntu/root/

The list of commands to extend the ‘root’ partition Ubuntu Desktop 18.04 LTS

> \# growpart /dev/sda/ 1
> \# pvresize /dev/sda1/
> \# lvextend -l +100%FREE /dev/mapper/ubuntu--vg-root/
> \# resize2fs /dev/ubuntu-vg/root/