Linux extended root file system

This article describes the expansion of the root file system through the tools that come with Linux without installing additional packages.

We can see that the root disk is 46.6G:

#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 46.6G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 19G 0 part
  ├─klas-root 253:0 0 17G 0 lvm /
  └─klas-swap 253:1 0 2G 0 lvm [SWAP]

However, the root file system and the corresponding partition are only 19G, so installing the database is not enough:

# df -h
File System Capacity Used Available % Used Mount Points
devtmpfs 7.2G 0 7.2G 0% /dev
tmpfs 7.3G 0 7.3G 0% /dev/shm
tmpfs 7.3G 9.0M 7.2G 1% /run
tmpfs 7.3G 0 7.3G 0% /sys/fs/cgroup
/dev/mapper/klas-root 17G 7.0G 11G 41% /
tmpfs 7.3G 4.0K 7.3G 1% /tmp
/dev/sda1 1014M 216M 799M 22% /boot
tmpfs 1.5G 0 1.5G 0% /run/user/992
tmpfs 1.5G 0 1.5G 0% /run/user/0

# fdisk -l /dev/sda
Disk /dev/sda: 46.59 GiB, 50010783744 bytes, 97677312 sectors
Disk model: BlockVolume
Unit: sector / 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 1048576 bytes
Disk label type: dos
Disk identifier: 0x5917cffa

Device Start Start End Sector Size Id Type
/dev/sda1 * 2048 2099199 2097152 1G 83 Linux
/dev/sda2 2099200 41943039 39843840 19G 8e Linux LVM

# pvdisplay
  --- Physical volume ---
  PV Name /dev/sda2
  VG Name klas
  PV Size <19.00 GiB / not usable 3.00 MiB
  Allocable yes (but full)
  PE Size 4.00 MiB
  Total PE 4863
  Free PE 0
  Allocated PE 4863
  PV UUID 8i1xe9-bQGw-C3UN-yXKl-RX71-BpMZ-Z5sJCW

#vgdisplay
  ---Volume group ---
  VG Name klas
  System ID
  Format lvm2
  Metadata Areas 1
  Metadata Sequence No. 3
  VG Access read/write
  VG Status resizable
  MAXLV 0
  Cur LV 2
  OpenLV2
  Max PV 0
  Cur PV 1
  Act PV 1
  VG Size <19.00 GiB
  PE Size 4.00 MiB
  Total PE 4863
  AllocPE/Size 4863/<19.00 GiB
  FreePE/Size 0/0
  VG UUID sdCaBU-DZsc-M40m-FjMc-7tcr-fOAq-OboAQX

#lvdisplay
  --- Logical volume ---
  LV Path /dev/klas/swap
  LV Name swap
  VG Name klas
  LV UUID gDn1nO-nlbE-bHvu-IVQ4-0c7p-Jss8-avOEiL
  LV Write Access read/write
  LV Creation host, time localhost, 2023-10-24 11:49:34 + 0800
  LV Status available
  # open 2
  LV Size 2.00 GiB
  Current LE 512
  Segments 1
  Allocation inherit
  Read ahead sectors auto
  - currently set to 8192
  Block device 253:1

  --- Logical volume ---
  LV Path /dev/klas/root
  LV Name root
  VG Name klas
  LV UUID eH66eX-dg12-kFwB-Nhp0-mfqs-R301-Y2dHD6
  LV Write Access read/write
  LV Creation host, time localhost, 2023-10-24 11:49:35 + 0800
  LV Status available
  # open 1
  LV Size <17.00 GiB
  Current LE 4351
  Segments 1
  Allocation inherit
  Read ahead sectors auto
  - currently set to 8192
  Block device 253:0

Delete and rebuild partition sda2 to make it larger.
The key point here is that deleting and rebuilding a partition does not affect the data in it!

# fdisk /dev/sda

Welcome to fdisk (util-linux 2.35.2).
Changes will stay in memory until you decide to write them to disk.
Think twice before using write commands.


Command (enter m for help): p
Disk /dev/sda: 46.59 GiB, 50010783744 bytes, 97677312 sectors
Disk model: BlockVolume
Unit: sector / 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 1048576 bytes
Disk label type: dos
Disk identifier: 0x5917cffa

Device Start Start End Sector Size Id Type
/dev/sda1 * 2048 2099199 2097152 1G 83 Linux
/dev/sda2 2099200 41943039 39843840 19G 8e Linux LVM

Command (enter m for help): m

help:

  DOS(MBR)
   a switch enable flag
   b Edit nested BSD disk labels
   c switch dos compatibility flag

  conventional
   d delete partition
   F lists unpartitioned free areas
   l List known partition types
   n add new partition
   p print partition table
   t Change partition type
   v Check the partition table
   i prints information about a certain partition

  Miscellaneous
   m print this menu
   u Change display/record unit
   x More features (Professionals only)

  script
   I Load disk layout from sfdisk script file
   O Dump disk layout to sfdisk script file

  Save and exit
   w Write the partition table to disk and exit
   q Exit without saving changes

  Create a new empty disk label
   g Create a new GPT partition table
   G Create a new empty GPT (IRIX) partition table
   o Create a new empty DOS partition table
   s Create a new empty Sun partition table


Command (enter m for help): d
Partition number (1,2, default 2): 2

Partition 2 has been deleted.

Command (enter m for help): n
Partition type
   p primary partition (1 primary, 0 extended, 3 free)
   e extended partition (logical partition container)
Select (default p): p
Partition number (2-4, default 2): 2
First sector (2099200-97677311, default 2099200):
Last sector, + /-sectors or + size{<!-- -->K,M,G,T,P} (2099200-97677311, default 97677311):

A new partition 2 is created, type "Linux", size 45.6 GiB.
Partition #2 contains an LVM2_member signature.

Do you want to remove this signature? Yes [Y]/No [N]: N

Command (enter m for help): w

The partition table has been adjusted.
Synchronizing disks.

Confirm, the partition has been expanded,:

# fdisk -l /dev/sda
Disk /dev/sda: 46.59 GiB, 50010783744 bytes, 97677312 sectors
Disk model: BlockVolume
Unit: sector / 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 1048576 bytes
Disk label type: dos
Disk identifier: 0x5917cffa

Device Start Start End Sector Size Id Type
/dev/sda1 * 2048 2099199 2097152 1G 83 Linux
/dev/sda2 2099200 97677311 95578112 45.6G 83 Linux

Extended pv:

# pvdisplay
  --- Physical volume ---
  PV Name /dev/sda2
  VG Name klas
  PV Size <19.00 GiB / not usable 3.00 MiB
  Allocable yes (but full)
  PE Size 4.00 MiB
  Total PE 4863
  Free PE 0
  Allocated PE 4863
  PV UUID 8i1xe9-bQGw-C3UN-yXKl-RX71-BpMZ-Z5sJCW

# pvresize /dev/sda2
  Physical volume "/dev/sda2" changed
  1 physical volume(s) resized or updated / 0 physical volume(s) not resized

# pvdisplay
  --- Physical volume ---
  PV Name /dev/sda2
  VG Name klas
  PV Size <45.58 GiB / not usable 1.00 MiB
  Allocable yes
  PE Size 4.00 MiB
  Total PE 11667
  Free PE 6804
  Allocated PE 4863
  PV UUID 8i1xe9-bQGw-C3UN-yXKl-RX71-BpMZ-Z5sJCW

You can directly expand lv:

# vgdisplay
  ---Volume group ---
  VG Name klas
  System ID
  Format lvm2
  Metadata Areas 1
  Metadata Sequence No. 4
  VG Access read/write
  VG Status resizable
  MAXLV 0
  Cur LV 2
  OpenLV2
  Max PV 0
  Cur PV 1
  Act PV 1
  VG Size 45.57 GiB
  PE Size 4.00 MiB
  Total PE 11667
  AllocPE/Size 4863/<19.00 GiB
  Free PE/Size 6804/<26.58 GiB
  VG UUID sdCaBU-DZsc-M40m-FjMc-7tcr-fOAq-OboAQX

# lvextend -r -L + 6804 /dev/klas/root
  Size of logical volume klas/root changed from <17.00 GiB (4351 extents) to 23.64 GiB (6052 extents).
  Logical volume klas/root successfully resized.
meta-data=/dev/mapper/klas-root isize=512 agcount=4, agsize=1113856 blks
         = sectsz=512 attr=2, projid32bit=1
         = crc=1 finobt=1, sparse=1, rmapbt=0
         =reflink=1
data = bsize=4096 blocks=4455424, imaxpct=25
         = sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log bsize=4096 blocks=2560, version=2
         = sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 4455424 to 6197248

# lvextend -r -l + 5103 /dev/klas/root
  Size of logical volume klas/root changed from 23.64 GiB (6052 extents) to 43.57 GiB (11155 extents).
  Logical volume klas/root successfully resized.
meta-data=/dev/mapper/klas-root isize=512 agcount=6, agsize=1113856 blks
         = sectsz=512 attr=2, projid32bit=1
         = crc=1 finobt=1, sparse=1, rmapbt=0
         =reflink=1
data = bsize=4096 blocks=6197248, imaxpct=25
         = sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log bsize=4096 blocks=2560, version=2
         = sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 6197248 to 11422720

#vgdisplay
  ---Volume group ---
  VG Name klas
  System ID
  Format lvm2
  Metadata Areas 1
  Metadata Sequence No. 6
  VG Access read/write
  VG Status resizable
  MAXLV 0
  Cur LV 2
  OpenLV2
  Max PV 0
  Cur PV 1
  Act PV 1
  VG Size 45.57 GiB
  PE Size 4.00 MiB
  Total PE 11667
  AllocPE/Size 11667/45.57 GiB
  FreePE/Size 0/0
  VG UUID sdCaBU-DZsc-M40m-FjMc-7tcr-fOAq-OboAQX

[root@kylinos ~]# lvdisplay
  --- Logical volume ---
  LV Path /dev/klas/swap
  LV Name swap
  VG Name klas
  LV UUID gDn1nO-nlbE-bHvu-IVQ4-0c7p-Jss8-avOEiL
  LV Write Access read/write
  LV Creation host, time localhost, 2023-10-24 11:49:34 + 0800
  LV Status available
  # open 2
  LV Size 2.00 GiB
  Current LE 512
  Segments 1
  Allocation inherit
  Read ahead sectors auto
  - currently set to 8192
  Block device 253:1

  --- Logical volume ---
  LV Path /dev/klas/root
  LV Name root
  VG Name klas
  LV UUID eH66eX-dg12-kFwB-Nhp0-mfqs-R301-Y2dHD6
  LV Write Access read/write
  LV Creation host, time localhost, 2023-10-24 11:49:35 + 0800
  LV Status available
  # open 1
  LV Size 43.57 GiB
  Current LE 11155
  Segments 1
  Allocation inherit
  Read ahead sectors auto
  - currently set to 8192
  Block device 253:0

[root@kylinos ~]# df -h
File System Capacity Used Available % Used Mount Points
devtmpfs 7.2G 0 7.2G 0% /dev
tmpfs 7.3G 12K 7.3G 1% /dev/shm
tmpfs 7.3G 9.0M 7.2G 1% /run
tmpfs 7.3G 0 7.3G 0% /sys/fs/cgroup
/dev/mapper/klas-root 44G 7.2G 37G 17% /
tmpfs 7.3G 4.0K 7.3G 1% /tmp
/dev/sda1 1014M 216M 799M 22% /boot
tmpfs 1.5G 0 1.5G 0% /run/user/992
tmpfs 1.5G 0 1.5G 0% /run/user/0

Done, the entire process does not restart.