Location>code7788 >text

Linux virtual machine expansion newly mounted disk to the root directory

Popularity:461 ℃/2025-03-11 17:47:41

Confirm the current root directory capacity

[root@jesus ~]# df -h
 File System Capacity Used Available Used % Mount Point
 devtmpfs 16G 0 16G 0% /dev
 tmpfs 16G 0 16G 0% /dev/shm
 tmpfs 16G 151M 16G 1% /run
 tmpfs 16G 0 16G 0% /sys/fs/cgroup
 /dev/mapper/klas-root 483G 12G 471G 3% /
 tmpfs 16G 32M 16G 1% /tmp
 /dev/vda2 1014M 153M 862M 16% /boot
 /dev/vda1 599M 6.5M 593M 2% /boot/efi
 tmpfs 3.1G 0 3.1G 0% /run/user/0

Create partitions for newly mounted disks

1. New disk ≤2TB can use the fdisk command to create partitions

[root@jesus ~]# fdisk -l /dev/vdb
 Disk /dev/vdb: 1 TiB, 1099511627776 bytes, 2147483648 sectors
 Unit: sector / 1 * 512 = 512 bytes
 Sector size (logical/physical): 512 bytes/512 bytes
 I/O size (minimum/best): 512 bytes / 512 bytes

Use the fdisk command to create a partition

[root@jesus ~]# fdisk /dev/vdb

 Welcome to fdisk (util-linux 2.35.2).
 Changes will stay in memory until you decide to write the changes to disk.
 Please think twice before using the write command.

 The device does not contain a recognizable partition table.
 A new DOS disk tag with disk identifier 0xf1ff1376 was created.

 Command (enter m to get help): m

 help:

   DOS (MBR)
    a switch startable flag
    b Edit nested BSD disk tags
    c switch dos compatibility flag

   conventional
    d Delete the partition
    F List unpartitioned free zones
    l List known partition types
    n Add a new partition
    p Print partition table
    t Change partition type
    v Check partition table
    i Print relevant information about a partition

   Miscellaneous
    m Print this menu
    u Change Display/Record Unit
    x More features (professional only)

   script
    I Load disk layout from sfdisk script file
    O Dump the disk layout into 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 to get help): n
 Partition type
    p primary partition (0 primary, 0 extended, 4 free)
    e Extended partition (logical partition container)
 Select (default p): p
 Partition number (1-4, default 1): 1
 The first sector (2048-2147483647, default 2048):
 The last sector, +/-sectors or +size{K,M,G,T,P} (2048-2147483647, default 2147483647):

 A new partition 1 was created with type "Linux" and size 1024 GiB.

 Command (enter m to get help): w
 The partition table has been adjusted.
 ioctl() will be called to reread the partition table.
 Synchronizing disks.

 [root@jesus ~]# fdisk -l /dev/vdb
 Disk /dev/vdb: 1 TiB, 1099511627776 bytes, 2147483648 sectors
 Unit: sector / 1 * 512 = 512 bytes
 Sector size (logical/physical): 512 bytes/512 bytes
 I/O size (minimum/best): 512 bytes / 512 bytes
 Disk Tag Type: dos
 Disk identifier: 0xf1ff1376

 Device Start Start Point End Sector Size Id Type
 /dev/vdb1 2048 2147483647 2147481600 1024G 83 Linux

2. Fdisk new disk>2TB Use parted command to create partition

[root@jesus ~]# fdisk -l /dev/vdb

 Disk /dev/vdb: 3 TiB, 3298534883328 bytes, 6442450944 sectors
 Unit: sector / 1 * 512 = 512 bytes
 Sector size (logical/physical): 512 bytes/512 bytes
 I/O size (minimum/best): 512 bytes / 512 bytes

Create partition using parted command

[root@jesus ~]# parted /dev/vdb
 GNU Parted 3.3
 Use /dev/vdb
 Welcome to GNU Parted!  Enter 'help' to view the command list.
 (parted) help
   align-check TYPE N check partition N for TYPE(min|opt) alignment
   help [COMMAND] Print general help information, or COMMAND help
   mklabel,mktable LABEL-TYPE Create a new disk volume label (partition table)
   mkpart Partition Type [File System Type] Start Point End Point Create a partition
   name number Name Name Name the partition that specifies "number" as "name"
   print [devices|free|list,all|number] displays partition table, available devices, remaining space, all partitions or special partitions
   quit Exit the program
   rescue start point end point rescues lost partitions near the "start point" and "terminal point"
   resizepart NUMBER END Change the size of NUMBER
   rm NUMBER Delete the partition number NUMBER
   select device Select the device to edit
   disk_set flag status Change flag on selected device
   disk_toggle [Flag] Toggle the flag status on the selected device
   set number Flag Status Change the flag of the specified "number" partition
   toggle [Number [Flag]] Toggle the "Flag" status on the "Number" partition
   unit unit Set the default "unit"
   version displays the current version and copyright information of GNU Parted
 (parted) p
 Error: /dev/vdb: Unrecognized disk volume
 Model: Virtio Block Device (virtblk)
 Disk /dev/vdb: 3299GB
 Sector size (logical/physical): 512B/512B
 Partition table: unknown
 Disk flag:
 (parted) mklabel gpt
 (parted) mkpart
 Partition name?   []? vdb1
 File system type?   [ext2]? ext4
 Starting point?  0
 End point?  3299GB
 Warning: The resulting partition is not properly aligned for best performance: 34s % 2048s != 0s
 Ignore/Ignore/Cancel? Ignore
 (parted) p
 Model: Virtio Block Device (virtblk)
 Disk /dev/vdb: 3299GB
 Sector size (logical/physical): 512B/512B
 Partition table: gpt
 Disk flag:

 Number Start point End point Size File system Name Flag
  1 17.4kB 3299GB 3299GB ext4 vdb1

 (parted) quit
 Information: You may need /etc/fstab.


 [root@jesus ~]# fdisk -l /dev/vdb

 Disk /dev/vdb: 3 TiB, 3298534883328 bytes, 6442450944 sectors
 Unit: sector / 1 * 512 = 512 bytes
 Sector size (logical/physical): 512 bytes/512 bytes
 I/O size (minimum/best): 512 bytes / 512 bytes
 Disk tag type: gpt
 Disk identifier: AA6037F0-3F9E-440D-95AE-D927D38B06A5

 Device Start Point End Sector Size Type
 /dev/vdb1 34 6442450910 6442450877 3T Linux file system

Force the kernel to reread the partition table

[root@jesus ~]# partprobe 

Format the partition to ext4 format

[root@jesus ~]# mkfs.ext4 /dev/vdb1
 mke2fs 1.45.6 (20-Mar-2020)
 Discard the device block: Finished
 Create a file system with 805306352 blocks (4k per block) and 201326592 inodes
 File system UUID: f7fbdcf1-2e46-4d7e-93ef-2129f2eb2591
 The backup of the super block is stored in the following blocks:
	 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
	 4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968,
	 102400000, 214990848, 512000000, 550731776, 644972544

 Assigning group table: Completed
 Writing to the inode table: Completed
 Creating logs (262144 blocks) completed
 Write superblock and file system account statistics: Completed

View volume group name

You can see that the volume group is named klas

[root@jesus ~]# vgdisplay 
  --- Volume group ---
  VG Name               klas
  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               1
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               498.41 GiB
  PE Size               4.00 MiB
  Total PE              127593
  Alloc PE / Size       127592 / <498.41 GiB
  Free  PE / Size       1 / 4.00 MiB
  VG UUID               hru1pG-Ndo3-5A5I-2lLn-XJaJ-kXet-lBy6Te

Physical volume (PV) creation

[root@jesus ~]# pvcreate /dev/vdb1
WARNING: ext4 signature detected on /dev/vdb1 at offset 1080. Wipe it? [y/n]: y
  Wiping ext4 signature on /dev/vdb1.
  Physical volume "/dev/vdb1" successfully created.

Volume Group (VG) Extension

[root@jesus ~]# vgextend klas /dev/vdb1 
  Volume group "klas" successfully extended

View the root partition

[root@jesus ~]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/klas/swap
  LV Name                swap
  VG Name                klas
  LV UUID                gIk5bI-gv8L-zzyv-3NS1-7hOY-NLb2-3Tg2ms
  LV Write Access        read/write
  LV Creation host, time localhost, 2025-03-01 13:14:09 +0800
  LV Status              available
  # open                 0
  LV Size                15.92 GiB
  Current LE             4076
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           252:1
   
  --- Logical volume ---
  LV Path                /dev/klas/root
  LV Name                root
  VG Name                klas
  LV UUID                CsXa7j-GO45-Y9AU-jMj6-Kjif-fod5-zWbCxN
  LV Write Access        read/write
  LV Creation host, time localhost, 2025-03-01 13:14:10 +0800
  LV Status              available
  # open                 1
  LV Size                482.48 GiB
  Current LE             123516
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           252:0

Logical volume (LV) extension

[root@jesus ~]# lvextend /dev/klas/root /dev/vdb1 
  Size of logical volume klas/root changed from 482.48 GiB (123516 extents) to 3.47 TiB (909947 extents).
  Logical volume klas/root successfully resized.

View file system format

If it is xfs, use the xfs_growf command. If it is ext2/ext3/ext4, use the resize2fs command. You can see that the xfs format is here.

[root@jesus ~]# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Sat Mar  1 05:14:31 2025
#
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
/dev/mapper/klas-root   /                       xfs     defaults        0 0
UUID=5a5507a0-0266-427e-af25-6e6c3e94a9ee /boot                   xfs     defaults        0 0
UUID=7B28-C2ED          /boot/efi               vfat    umask=0077,shortname=winnt 0 2

File system expansion

[root@jesus ~]# xfs_growfs /dev/klas/root 
meta-data=/dev/mapper/klas-root  isize=512    agcount=4, agsize=31620096 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1
data     =                       bsize=4096   blocks=126480384, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=61758, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 126480384 to 931785728

Final verification

[root@jesus ~]# df -h
 File System Capacity Used Available Used % Mount Point
 devtmpfs 16G 0 16G 0% /dev
 tmpfs 16G 192K 16G 1% /dev/shm
 tmpfs 16G 151M 16G 1% /run
 tmpfs 16G 0 16G 0% /sys/fs/cgroup
 /dev/mapper/klas-root 3.5T 34G 3.5T 1% /
 tmpfs 16G 32M 16G 1% /tmp
 /dev/vda2 1014M 153M 862M 16% /boot
 /dev/vda1 599M 6.5M 593M 2% /boot/efi
 tmpfs 3.1G 0 3.1G 0% /run/user/0