虚拟机硬盘扩容(使用LVM)

参考资料

正文

1. 扩容条件

  1. 虚拟机无快照;
  2. 虚拟机已关机;
  3. 由于这里使用LVM扩容,需要使用 vgs 命令,若可以执行则可以使用LVM扩容。
  1. (root💀kali)-[/home/kali]
  2. └─# vgs
  3. VG #PV #LV #SN Attr VSize VFree
  4. kali-vg 1 5 0 wz--n- <39.52g 40.00m

查看当前磁盘使用情况

(kali㉿kali)-[~]
└─$ df -h
文件系统                   容量  已用  可用 已用% 挂载点
udev                       1.9G     0  1.9G    0% /dev
tmpfs                      391M  1.2M  390M    1% /run
/dev/mapper/kali--vg-root   11G  8.4G  2.1G   81% /
tmpfs                      2.0G     0  2.0G    0% /dev/shm
tmpfs                      5.0M     0  5.0M    0% /run/lock
/dev/mapper/kali--vg-tmp   649M   88K  601M    1% /tmp
/dev/mapper/kali--vg-var   2.3G  858M  1.3G   41% /var
/dev/sda1                  470M   83M  363M   19% /boot
/dev/mapper/kali--vg-home   24G  3.7G   19G   17% /home
tmpfs                      391M   60K  391M    1% /run/user/1000

2. 硬盘扩容

在虚拟机配置中选择扩展磁盘大小

image-20220309124314014.png

磁盘扩展完成后,需要在系统内重新分区以及文件系统扩展。

image-20220309124228522.png

3. 划定新分区

执行 fdisk -l ,查看整体的磁盘属性

(root💀kali)-[/home/kali]
└─# fdisk -l
Disk /dev/sda: 60 GiB, 64424509440 bytes, 125829120 sectors
Disk model: VMware Virtual S
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: 0x683b7641

Device     Boot   Start      End  Sectors  Size Id Type
/dev/sda1  *       2048   999423   997376  487M 83 Linux
/dev/sda2       1001470 83884031 82882562 39.5G  5 Extended
/dev/sda5       1001472 83884031 82882560 39.5G 8e Linux LVM


Disk /dev/mapper/kali--vg-root: 11.18 GiB, 11999903744 bytes, 23437312 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/kali--vg-var: 2.33 GiB, 2499805184 bytes, 4882432 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/kali--vg-swap_1: 976 MiB, 1023410176 bytes, 1998848 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/kali--vg-tmp: 676 MiB, 708837376 bytes, 1384448 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/kali--vg-home: 24.36 GiB, 26159874048 bytes, 51093504 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

看上面高亮部分,可以看到当前的主磁盘是 LVM 分区。

执行 fdisk /dev/sda 编辑 sda 磁盘:

(root💀kali)-[/home/kali404]
└─# fdisk /dev/sda                                               

Welcome to fdisk (util-linux 2.36.1).                                                 
Changes will remain in memory only, until you decide to write them. 

Be careful before using the write command.

接下来会进入fdisk的命令行,可以使用 m 参数查看帮助信息

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

第一步,使用 p 参数(print partition table)查看分区表

Command (m for help): p
Disk /dev/sda: 60 GiB, 64424509440 bytes, 125829120 sectors
Disk model: VMware Virtual S
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: 0x683b7641

Device     Boot   Start      End  Sectors  Size Id Type
/dev/sda1  *       2048   999423   997376  487M 83 Linux
/dev/sda2       1001470 83884031 82882562 39.5G  5 Extended
/dev/sda5       1001472 83884031 82882560 39.5G 8e Linux LVM

第二步,使用 n 参数(new partition)创建新分区

Command (m for help): n

# 分区类型:p(primary)主分区,l(logical)逻辑分区
# - 此处为MBR分区表模式,最多有4个分区
# - 目前已有1个主分区,1个拓展分区,还有2个未分配的分区份额
# - 选择“p”,创建一个主分区

Partition type
   p   primary (1 primary, 1 extended, 2 free)
   l   logical (numbered from 5)
Select (default p): p

# 新分区的编号,剩下3,4未分配,这里选3

Partition number (3,4, default 3): 3

# 分配扇区区域,也即该扇区在磁盘上的开始地址和结束地址,直接按默认分配最大

First sector (83884032-125829119, default 83884032): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (83884032-125829119, default 125829119): 

Created a new partition 3 of type 'Linux' and of size 20 GiB.

再次查看分区表 p ,确认一下创建成功

Command (m for help): p
Disk /dev/sda: 60 GiB, 64424509440 bytes, 125829120 sectors
Disk model: VMware Virtual S
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: 0x683b7641

Device     Boot    Start       End  Sectors  Size Id Type
/dev/sda1  *        2048    999423   997376  487M 83 Linux
/dev/sda2        1001470  83884031 82882562 39.5G  5 Extended
/dev/sda3       83884032 125829119 41945088   20G 83 Linux
/dev/sda5        1001472  83884031 82882560 39.5G 8e Linux LVM

Partition table entries are not in disk order.

第三步,使用 t 参数(partition type)修改刚刚创建的3号分区的磁盘格式为LVM

Command (m for help): t
# 选择分区编号,这里选择刚刚创建的3号分区
Partition number (1-3,5, default 5): 3
# 输入格式的16进制编号或者是别名,可以输入L查看列表
Hex code or alias (type L to list all): 8e

Changed type of partition 'Linux' to 'Linux LVM'.

拓展一下,看看支持的磁盘格式

Command (m for help): l
 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
 1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden or  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      40  Venix 80286     85  Linux extended  c7  Syrinx         
 5  Extended        41  PPC PReP Boot   86  NTFS volume set da  Non-FS data    
 6  FAT16           42  SFS             87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux plaintext de  Dell Utility   
 8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM       df  BootIt         
 9  AIX bootable    4f  QNX4.x 3rd part 93  Amoeba          e1  DOS access     
 a  OS/2 Boot Manag 50  OnTrack DM      94  Amoeba BBT      e3  DOS R/O        
 b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor      
 c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad hi ea  Rufus alignment
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         eb  BeOS fs        
 f  W95 Ext'd (LBA) 54  OnTrackDM6      a6  OpenBSD         ee  GPT            
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        ef  EFI (FAT-12/16/
11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f0  Linux/PA-RISC b
12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f1  SpeedStor      
14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f4  SpeedStor      
16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      f2  DOS secondary  
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fb  VMware VMFS    
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fc  VMware VMKCORE 
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fd  Linux raid auto
1c  Hidden W95 FAT3 75  PC/IX           bc  Acronis FAT32 L fe  LANstep        
1e  Hidden W95 FAT1 80  Old Minix       be  Solaris boot    ff  BBT

第四步,保存分区表的修改

Command (m for help): w
The partition table has been altered.
Syncing disks.

4. 建立PV,扩展VG

使用 pvcreate 对新创建的 sda3 分区建立 PV(Physical Volume,物理卷)

┌──(root💀kali)-[/home/kali]
└─# pvcreate /dev/sda3
  Physical volume "/dev/sda3" successfully created.

查看 PV 信息

(root💀kali)-[/home/kali]
└─# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda5
  VG Name               kali-vg
  PV Size               39.52 GiB / not usable 2.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              10117
  Free PE               10
  Allocated PE          10107
  PV UUID               3G1HfC-94fR-WgfG-7AOm-Vk5H-XMsJ-n8ZJvk

  "/dev/sda3" is a new physical volume of "20.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sda3
  VG Name               
  PV Size               20.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               KpnS49-uN86-Krm7-28Wn-XDnT-DmVK-uGLgdO

接下来扩展 VG(Volume Group),在上表可以看到这里的 VG Name 为 kali-vg

(root💀kali)-[/home/kali]
└─# vgextend kali-vg /dev/sda3
  Volume group "kali-vg" successfully extended

查看 VG 信息

(root💀kali)-[/home/kali]
└─# vgdisplay                 
  --- Volume group ---
  VG Name               kali-vg
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  7
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                5
  Open LV               5
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               <59.52 GiB
  PE Size               4.00 MiB
  Total PE              15237
  Alloc PE / Size       10107 / 39.48 GiB
  Free  PE / Size       5130 / <20.04 GiB
  VG UUID               dB3S3w-MLYi-jn91-4ldj-lCkI-Zapb-wHSbQ9

5. 扩展LV

这个是核心部分,也就是扩展我们目标的 LV (Logical Volume,逻辑卷)

这里是因为更新系统发现 var 目录空间不够,所以要扩展 /dev/kali-vg/var 分卷

先看看 lvextend 命令的参数

lvextend -h
  lvextend - Add space to a logical volume

  Extend an LV by a specified size.
  lvextend -L|--size [+]Size[m|UNIT] LV
        [ -l|--extents [+]Number[PERCENT] ]
        [ -r|--resizefs ]
        [ -i|--stripes Number ]
        [ -I|--stripesize Size[k|UNIT] ]
        [    --poolmetadatasize [+]Size[m|UNIT] ]
        [ COMMON_OPTIONS ]
        [ PV ... ]

  Extend an LV by specified PV extents.
  lvextend LV PV ...
        [ -r|--resizefs ]
        [ -i|--stripes Number ]
        [ -I|--stripesize Size[k|UNIT] ]
        [ COMMON_OPTIONS ]

  Extend a pool metadata SubLV by a specified size.
  lvextend --poolmetadatasize [+]Size[m|UNIT] LV_thinpool
        [ -i|--stripes Number ]
        [ -I|--stripesize Size[k|UNIT] ]
        [ COMMON_OPTIONS ]
        [ PV ... ]

  Extend an LV according to a predefined policy.
  lvextend --usepolicies LV_snapshot_thinpool
        [ -r|--resizefs ]
        [ COMMON_OPTIONS ]
        [ PV ... ]

  Common options for command:
        [ -A|--autobackup y|n ]
        [ -f|--force ]
        [ -m|--mirrors Number ]
        [ -n|--nofsck ]
        [    --alloc contiguous|cling|cling_by_tags|normal|anywhere|inherit ]
        [    --nosync ]
        [    --noudevsync ]
        [    --reportformat basic|json ]
        [    --type linear|striped|snapshot|mirror|raid|thin|cache|vdo|thin-pool|cache-pool|vdo-pool ]

  Common options for lvm:
        [ -d|--debug ]
        [ -h|--help ]
        [ -q|--quiet ]
        [ -v|--verbose ]
        [ -y|--yes ]
        [ -t|--test ]
        [    --commandprofile String ]
        [    --config String ]
        [    --driverloaded y|n ]
        [    --nolocking ]
        [    --lockopt String ]
        [    --longhelp ]
        [    --profile String ]
        [    --version ]

  Use --longhelp to show all options and advanced commands.

核心为 -L-l 参数

-L:指定逻辑卷的大小,单位为“kKmMgGtT”字节;
-I:指定逻辑卷的大小(LE数)。

/var 加多 2G 吧,剩下的分给 /root/home

(root💀kali)-[/home/kali]
└─# lvextend -L +2G /dev/kali-vg/var
  Size of logical volume kali-vg/var changed from <2.33 GiB (596 extents) to <4.33 GiB (1108 extents).
  Logical volume kali-vg/var successfully resized.

(root💀kali)-[/home/kali]
└─# lvextend -L +10G /dev/kali-vg/root
  Size of logical volume kali-vg/root changed from <11.18 GiB (2861 extents) to <21.18 GiB (5421 extents).
  Logical volume kali-vg/root successfully resized.

(root💀kali)-[/home/kali]
└─# lvextend -L +8G /dev/kali-vg/home
  Size of logical volume kali-vg/home changed from 24.36 GiB (6237 extents) to 32.36 GiB (8285 extents).
  Logical volume kali-vg/home successfully resized.

查看目前的 LV

(root💀kali)-[/home/kali]
└─# lvdisplay                         
  --- Logical volume ---
  LV Path                /dev/kali-vg/root
  LV Name                root
  VG Name                kali-vg
  LV UUID                JVajT3-1nDp-ZixK-1acT-wAHz-zgcg-Rnyx8k
  LV Write Access        read/write
  LV Creation host, time kali, 2021-04-12 12:00:35 +0800
  LV Status              available
  # open                 1
  LV Size                <21.18 GiB
  Current LE             5421
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:0

  --- Logical volume ---
  LV Path                /dev/kali-vg/var
  LV Name                var
  VG Name                kali-vg
  LV UUID                uBHVZw-T0yN-X0Tq-W6Zl-VszL-YPxi-05LZRX
  LV Write Access        read/write
  LV Creation host, time kali, 2021-04-12 12:00:35 +0800
  LV Status              available
  # open                 1
  LV Size                <4.33 GiB
  Current LE             1108
  Segments               3
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:1

  --- Logical volume ---
  LV Path                /dev/kali-vg/swap_1
  LV Name                swap_1
  VG Name                kali-vg
  LV UUID                AmGYAb-q1uZ-qnaa-cl9W-G3Ct-UMfO-5GCBi7
  LV Write Access        read/write
  LV Creation host, time kali, 2021-04-12 12:00:35 +0800
  LV Status              available
  # open                 2
  LV Size                976.00 MiB
  Current LE             244
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:2

  --- Logical volume ---
  LV Path                /dev/kali-vg/tmp
  LV Name                tmp
  VG Name                kali-vg
  LV UUID                l4z3uF-evbq-PqIx-iL9j-dt2Q-r0pN-BfubHD
  LV Write Access        read/write
  LV Creation host, time kali, 2021-04-12 12:00:36 +0800
  LV Status              available
  # open                 1
  LV Size                676.00 MiB
  Current LE             169
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:3

  --- Logical volume ---
  LV Path                /dev/kali-vg/home
  LV Name                home
  VG Name                kali-vg
  LV UUID                lwfJVY-bFXg-wa6k-Ey52-2cVM-SDHL-xAdl27
  LV Write Access        read/write
  LV Creation host, time kali, 2021-04-12 12:00:36 +0800
  LV Status              available
  # open                 1
  LV Size                32.36 GiB
  Current LE             8285
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:4

6. 扩展文件系统

前面完成了逻辑卷物理边界(LV)的扩展,接下来完成最后一步:扩展逻辑边界(文件系统)的大小。

┌──(root💀kali)-[/home/kali]
└─# resize2fs /dev/kali-vg/home
resize2fs 1.46.1 (9-Feb-2021)
Filesystem at /dev/kali-vg/home is mounted on /home; on-line resizing required
old_desc_blocks = 4, new_desc_blocks = 5
The filesystem on /dev/kali-vg/home is now 8483840 (4k) blocks long.

┌──(root💀kali)-[/home/kali]
└─# resize2fs /dev/kali-vg/root
resize2fs 1.46.1 (9-Feb-2021)
Filesystem at /dev/kali-vg/root is mounted on /; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 3
The filesystem on /dev/kali-vg/root is now 5551104 (4k) blocks long.

┌──(root💀kali)-[/home/kali]
└─# resize2fs /dev/kali-vg/var 
resize2fs 1.46.1 (9-Feb-2021)
Filesystem at /dev/kali-vg/var is mounted on /var; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/kali-vg/var is now 1134592 (4k) blocks long.

查看当前文件系统,可以看到已成功扩展文件系统。

┌──(root💀kali)-[/home/kali]
└─# df -h
文件系统                   容量  已用  可用 已用% 挂载点
udev                       1.9G     0  1.9G    0% /dev
tmpfs                      391M  1.2M  390M    1% /run
/dev/mapper/kali--vg-root   21G  8.4G   12G   43% /
tmpfs                      2.0G     0  2.0G    0% /dev/shm
tmpfs                      5.0M     0  5.0M    0% /run/lock
/dev/mapper/kali--vg-tmp   649M   88K  601M    1% /tmp
/dev/mapper/kali--vg-var   4.2G  868M  3.2G   22% /var
/dev/sda1                  470M   83M  363M   19% /boot
/dev/mapper/kali--vg-home   32G  3.7G   27G   13% /home
tmpfs                      391M   60K  391M    1% /run/user/1000