[!WARNING]

以下操作可能会导致包括但不仅限于数据丢失、无法启动等问题,严重时您可能需要重装系统,如要继续,请确保已做好数据备份等准备工作。

Debian12根分区扩容记录

前言

云上Debian12的机器,使用 df -Th 查看磁盘分区信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
root@xxx:~# df -Th
Filesystem Type Size Used Avail Use% Mounted on
udev devtmpfs 3.8G 0 3.8G 0% /dev
tmpfs tmpfs 775M 852K 775M 1% /run
/dev/vda1 ext4 19G 5.7G 12G 33% /
tmpfs tmpfs 3.8G 0 3.8G 0% /dev/shm
tmpfs tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs tmpfs 775M 0 775M 0% /run/user/0
overlay overlay 19G 5.7G 12G 33% /var/lib/docker/overlay2/85022b6945d418b6979839b3897502dd192a7c7b11993f1a5e30b6f3af579595/merged
overlay overlay 19G 5.7G 12G 33% /var/lib/docker/overlay2/faa3aef89ddf6113d5d5805e64bc6abeeb3cd5d01da27dde5e39dc7af7f399c4/merged
overlay overlay 19G 5.7G 12G 33% /var/lib/docker/overlay2/e691e3e85e02b056a6001d2b3bffcb9033a6f6456bc5aac1c6ccc1cffbb4ead6/merged
overlay overlay 19G 5.7G 12G 33% /var/lib/docker/overlay2/4c14da4bf849dba7c6d05c5bb7a3d4721a2a436efaff3b2dc14ad86885ad4cd5/merged

可以看到根分区/dev/vda1只有19G,lsblk查看磁盘使用情况:

1
2
3
4
5
6
7
8
root@xxx:~# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sr0 11:0 1 1024M 0 rom
vda 254:0 0 20G 0 disk
├─vda1 254:1 0 19G 0 part /
├─vda2 254:2 0 1K 0 part
└─vda5 254:5 0 975M 0 part [SWAP]
vdb 254:16 0 30G 0 disk

可以看到未使用的磁盘有30G,现在想把这30G加到根分区上去,思路就是采用LVM逻辑卷管理动态扩容,建立一个逻辑卷组,把vda和vdb都加入此卷组,达到物理上是两块不同磁盘,但逻辑上合并为一块磁盘的效果。

1.将磁盘vdb初始化为物理卷

1
pvcreate /dev/vdb

期望输出

1
2
root@xxx:~# pvcreate /dev/vdb
Physical volume "/dev/vdb" successfully created.

2.新建卷组并将vdb加入此卷组

1
vgcreate vg_root /dev/vdb

期望输出

1
2
root@xxx:~# vgcreate vg_root /dev/vdb
Volume group "vg_root" successfully created

ERROR1跳转解决方法
跳转

1
2
3
root@xxx:~# pvcreate /dev/vdb
Can't initialize physical volume "/dev/vdb" of volume group "vg_root" without -ff
/dev/vdb: physical volume not initialized.

3.在卷组中创建逻辑卷

1
lvcreate -l 100%FREE -n lv_root vg_root

期望输出

1
2
3
4
root@xxx:~# lvcreate -l 100%FREE -n lv_root vg_root
WARNING: ext4 signature detected on /dev/vg_root/lv_root at offset 1080. Wipe it? [y/n]: y
Wiping ext4 signature on /dev/vg_root/lv_root.
Logical volume "lv_root" created.

ERROR2跳转解决方法
跳转

1
2
3
root@debian:~# lvcreate -l 100%FREE -n lv_root vg_root
Volume group "vg_root" not found
Cannot process volume group vg_root

可以使用lsblk命令查看vdb上已经创建了该逻辑卷:

1
2
3
4
5
6
7
8
9
root@xxx:~# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sr0 11:0 1 1024M 0 rom
vda 254:0 0 20G 0 disk
├─vda1 254:1 0 19G 0 part /
├─vda2 254:2 0 1K 0 part
└─vda5 254:5 0 975M 0 part [SWAP]
vdb 254:16 0 30G 0 disk
└─vg_root-lv_root 253:0 0 30G 0 lvm

4.使用etx4文件系统格式化此逻辑卷

1
mkfs.ext4 /dev/vg_root/lv_root

期望输出

1
2
3
4
5
6
7
8
9
10
11
12
root@xxx:~# mkfs.ext4 /dev/vg_root/lv_root
mke2fs 1.47.0 (5-Feb-2023)
Creating filesystem with 7863296 4k blocks and 1966080 inodes
Filesystem UUID: a8a07510-2b4c-4ac7-8524-cafcb4393283
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000

Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

5.修改/etc/fstab

首先使用blkid命令得到上面创建的vg_root-lv_root逻辑卷的UUID

1
2
3
4
5
6
root@xxx:~# blkid
/dev/vdb: UUID="Av2nxJ-l57c-uZoz-zFQ8-w90P-4s5F-2NhUZD" TYPE="LVM2_member"
/dev/mapper/vg_root-lv_root: UUID="a8a07510-2b4c-4ac7-8524-cafcb4393283" BLOCK_SIZE="4096" TYPE="ext4"
/dev/vda5: UUID="155c5478-3b21-4c5c-ab1d-5bc39961d1b7" TYPE="swap" PARTUUID="35ce1eaa-05"
/dev/vda1: UUID="63dc08fe-cf76-4097-b849-c9db03ce9b84" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="35ce1eaa-01"

示例:

nano /etc/fstab,其原内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# systemd generates mount units based on this file, see systemd.mount(5).
# Please run 'systemctl daemon-reload' after making changes here.
#
# <file system> <mount point> <type> <options> <dump> <pass>
# / was on /dev/vda1 during installation
UUID=63dc08fe-cf76-4097-b849-c9db03ce9b84 / ext4 errors=remount-ro 0 1
# swap was on /dev/vda5 during installation
UUID=155c5478-3b21-4c5c-ab1d-5bc39961d1b7 none swap sw 0 0
/dev/sr0 /media/cdrom0 udf,iso9660 user,noauto 0 0

替换/etc/fstab中的根目录挂载项通常为ext4 文件系统的UUID为上方逻辑卷的UUID,更改完成后如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# systemd generates mount units based on this file, see systemd.mount(5).
# Please run 'systemctl daemon-reload' after making changes here.
#
# <file system> <mount point> <type> <options> <dump> <pass>
# / was on /dev/vda1 during installation
UUID=a8a07510-2b4c-4ac7-8524-cafcb4393283 / ext4 errors=remount-ro 0 1
# swap was on /dev/vda5 during installation
UUID=155c5478-3b21-4c5c-ab1d-5bc39961d1b7 none swap sw 0 0
/dev/sr0 /media/cdrom0 udf,iso9660 user,noauto 0 0

6.挂载此逻辑卷并将原根目录同步到此卷

1
2
mount /dev/mapper/vg_root-lv_root /mnt
rsync -axHAWX --numeric-ids --info=progress2 / /mnt

期望输出

1
2
3
root@xxx:~# mount /dev/mapper/vg_root-lv_root /mnt
root@xxx:~# rsync -axHAWX --numeric-ids --info=progress2 / /mnt
6,075,870,604 99% 55.50MB/s 0:01:44 (xfr#62465, to-chk=0/80514)

7.绑定一些必要的系统目录和文件

1
2
3
4
5
6
7
mount --types proc /proc /mnt/proc
mount --rbind /sys /mnt/sys
mount --make-rslave /mnt/sys
mount --rbind /dev /mnt/dev
mount --make-rslave /mnt/dev
mount --rbind /run /mnt/run
mount --bind /etc/resolv.conf /mnt/etc/resolv.conf

8.chroot到新的根目录并更新grubinitramfs

1
2
3
4
chroot /mnt
grub-install /dev/vda
update-grub
update-initramfs -u -k all

期望输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
root@xxx:~# chroot /mnt
root@xxx:/# grub-install /dev/vda
Installing for i386-pc platform.
Installation finished. No error reported.

root@xxx:/# update-grub
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-6.1.0-18-amd64
Found initrd image: /boot/initrd.img-6.1.0-18-amd64
Warning: os-prober will not be executed to detect other bootable partitions.
Systems on them will not be added to the GRUB boot configuration.
Check GRUB_DISABLE_OS_PROBER documentation entry.
done

root@xxx:/# update-initramfs -u -k all
update-initramfs: Generating /boot/initrd.img-6.1.0-18-amd64

9.退出chroot并重启

1
2
3
4
5
6
exit # 确保完全退出chroot,提示符应显示主机名(不是/#)~# 
umount -l /mnt/sys
umount -l /mnt/proc
umount -l /mnt/dev
umount -l /mnt/run
reboot
1
2
3
4
5
6
7
root@xxx:/# exit
exit
root@xxx:~# umount -l /mnt/sys # 卸载之前的挂载点,下同
root@xxx:~# umount -l /mnt/proc
root@xxx:~# umount -l /mnt/dev
root@xxx:~# umount -l /mnt/run
root@xxx:~# reboot

10.扩容新的根分区

重启完成之后,应该能看到上面新建的逻辑分区被挂载到了根目录:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
root@xxx:~# df -Th
Filesystem Type Size Used Avail Use% Mounted on
udev devtmpfs 3.8G 0 3.8G 0% /dev
tmpfs tmpfs 775M 8.5M 767M 2% /run
/dev/mapper/vg_root-lv_root ext4 30G 1.1G 27G 4% /
tmpfs tmpfs 3.8G 0 3.8G 0% /dev/shm
tmpfs tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs tmpfs 775M 0 775M 0% /run/user/0
root@xxx:~# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sr0 11:0 1 368K 0 rom
vda 254:0 0 20G 0 disk
├─vda1 254:1 0 19G 0 part
├─vda2 254:2 0 1K 0 part
└─vda5 254:5 0 975M 0 part
vdb 254:16 0 30G 0 disk
└─vg_root-lv_root 253:0 0 30G 0 lvm /

接下来把原根分区所在的vda1也加入卷组vg_root:

1
2
3
4
pvcreate /dev/vda1
vgextend vg_root /dev/vda1
lvextend -l +100%FREE /dev/vg_root/lv_root
resize2fs /dev/vg_root/lv_root
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
root@xxx:~#  pvcreate /dev/vda1
WARNING: ext4 signature detected on /dev/vda1 at offset 1080. Wipe it? [y/n]: y
Wiping ext4 signature on /dev/vda1.
Physical volume "/dev/vda1" successfully created.

root@xxx:~# vgextend vg_root /dev/vda1
Volume group "vg_root" successfully extended
root@xxx:~# lvextend -l +100%FREE /dev/vg_root/lv_root
Size of logical volume vg_root/lv_root changed from <30.00 GiB (7679 extents) to <49.04 GiB (12554 extents).
Logical volume vg_root/lv_root successfully resized.

root@xxx:~# resize2fs /dev/vg_root/lv_root
resize2fs 1.47.0 (5-Feb-2023)
Filesystem at /dev/vg_root/lv_root is mounted on /; on-line resizing required
old_desc_blocks = 4, new_desc_blocks = 7
The filesystem on /dev/vg_root/lv_root is now 12855296 (4k) blocks long.

注意:此时看一下/etc/fstab,确保和上面修改过的一致,再次执行:

1
2
3
grub-install /dev/vda
update-grub
update-initramfs -u -k all

完成之后再次重启,如果能正确引导且磁盘已扩容就大功告成:

1
2
3
4
5
6
7
8
root@xxx:~# df -Th
Filesystem Type Size Used Avail Use% Mounted on
udev devtmpfs 3.8G 0 3.8G 0% /dev
tmpfs tmpfs 775M 500K 775M 1% /run
/dev/mapper/vg_root-lv_root ext4 49G 1.4G 45G 3% /
tmpfs tmpfs 3.8G 0 3.8G 0% /dev/shm
tmpfs tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs tmpfs 775M 0 775M 0% /run/user/0

11.可能遇到的问题&解决方法

error_1解决办法强制初始化(如果数据可丢失)
return_ERROR1

1
2
3
4
5
6
7
8
# 如果 /dev/vdb 没有重要数据,使用 -ff 强制初始化:
root@xxx:~# pvcreate -ff /dev/vdb
Really INITIALIZE physical volume "/dev/vdb" of volume group "vg_root" [y/n]? y
WARNING: Forcing physical volume creation on /dev/vdb of volume group "vg_root"
Physical volume "/dev/vdb" successfully created.
# 注意:这会完全清除 /dev/vdb 上的现有 LVM 元数据,不可逆!
root@xxx:~# pvcreate /dev/vdb
Physical volume "/dev/vdb" successfully created.

error_2解决办法强制初始化(如果数据可丢失)
return_ERROR2

1
2
3
4
5
6
7
8
9
10
11
12
# 卷组 vg_root 不存在,或卷组存在但未激活,或名称拼写错误
# 如果卷组确实不存在
# 创建物理卷(如果尚未创建)
pvcreate /dev/vdb
# 创建卷组(命名为 vg_root)
vgcreate vg_root /dev/vdb

# 如果卷组存在但未激活
# 激活卷组
vgchange -ay vg_root
# 确认后再次创建逻辑卷
lvcreate -l 100%FREE -n lv_root vg_root