KVM笔记之磁盘扩容
磁盘扩容
关闭KVM虚拟机
1virsh shutdown vm-ubuntu-04
扩容磁盘
查看磁盘位置
1# virsh domblklist vm-ubuntu-04 2Target Source 3------------------------------------------------ 4vda /home/kvm/images/ubuntu-04.qcow2
查看磁盘信息
1# qemu-img info /home/kvm/images/ubuntu-04.qcow2 2image: /home/kvm/images/ubuntu-04.qcow2 3file format: qcow2 4virtual size: 2.0G (2147483648 bytes) 5disk size: 1.7G 6cluster_size: 65536 7Format specific information: 8compat: 0.10 9refcount bits: 16
可以看出现在虚拟机磁盘大小为 2G
扩容磁盘大小
1# qemu-img resize /home/kvm/images/ubuntu-04.qcow2 +28G 2Image resized.
1# qemu-img info /home/kvm/images/ubuntu-04.qcow2 2image: /home/kvm/images/ubuntu-04.qcow2 3file format: qcow2 4virtual size: 30G (32212254720 bytes) 5disk size: 1.7G 6cluster_size: 65536 7Format specific information: 8compat: 0.10 9refcount bits: 16
现在磁盘大小已增加到 30G
扩容虚拟机分区
启动虚拟机
1virsh start vm-ubuntu-04
查看磁盘大小和格式
1root@vm-ubuntu-04:~# lsblk 2NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT 3loop0 7:0 0 88.5M 1 loop /snap/core/7270 4vda 252:0 0 30G 0 disk 5├─vda1 252:1 0 1M 0 part 6└─vda2 252:2 0 2G 0 part /
现在跟分区只分配了 2G 的大小,而当前 vda 磁盘总量为 30G
1root@vm-ubuntu-04:~# df -T 2Filesystem Type 1K-blocks Used Available Use% Mounted on 3udev devtmpfs 4053076 0 4053076 0% /dev 4tmpfs tmpfs 816784 936 815848 1% /run 5/dev/vda2 ext4 2028368 1634136 273144 86% / 6tmpfs tmpfs 4083912 0 4083912 0% /dev/shm 7tmpfs tmpfs 5120 0 5120 0% /run/lock 8tmpfs tmpfs 4083912 0 4083912 0% /sys/fs/cgroup 9/dev/loop0 squashfs 90624 90624 0 100% /snap/core/7270 10tmpfs tmpfs 816780 0 816780 0% /run/user/0
注意 :不同格式的文件系统扩容磁盘命令 有所不同,比如我的根分区格式为 ext4, 以下将以 ext4 为例
扩容磁盘
-
安装 cloud-guest-utils
1apt -y install cloud-guest-utils -
扩容磁盘
1root@vm-ubuntu-base:~# growpart /dev/vda 2 2CHANGED: partition=2 start=4096 old: size=4188160 end=4192256 new: size=62910431,end=62914527
2 为跟分区所在磁盘
1root@vm-ubuntu-base:~# lsblk 2NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT 3loop0 7:0 0 88.5M 1 loop /snap/core/7270 4vda 252:0 0 30G 0 disk 5├─vda1 252:1 0 1M 0 part 6└─vda2 252:2 0 30G 0 part /
-
更新且格式化分区 resize2fs 扩容 ext4 文件系统, 其它可参考 https://computingforgeeks.com/extending-root-filesystem-using-lvm-linux/
1root@vm-ubuntu-base:~# resize2fs /dev/vda2 2resize2fs 1.44.1 (24-Mar-2018) 3Filesystem at /dev/vda2 is mounted on /; on-line resizing required 4old_desc_blocks = 1, new_desc_blocks = 4 5The filesystem on /dev/vda2 is now 7863803 (4k) blocks long. 6 7root@vm-ubuntu-base:~# lsblk 8NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT 9loop0 7:0 0 88.5M 1 loop /snap/core/7270 10vda 252:0 0 30G 0 disk 11├─vda1 252:1 0 1M 0 part 12└─vda2 252:2 0 30G 0 part / 13root@vm-ubuntu-base:~# df -hT | grep /dev/vda 14/dev/vda2 ext4 30G 1.6G 27G 6% /
DONELVM磁盘扩容
CLOSED: [2020-12-17 四 10:26]
-
State "DONE" from [2020-12-17 四 10:26]
1root@jsl-ml:~# lsblk 2NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT 3sda 8:0 0 14.6T 0 disk 4sdb 8:16 0 3.5T 0 disk 5├─sdb1 8:17 0 512M 0 part /boot/efi 6├─sdb2 8:18 0 1G 0 part /boot 7└─sdb3 8:19 0 3.5T 0 part 8 └─ubuntu--vg-ubuntu--lv 253:0 0 200G 0 lvm / 9sdc 8:32 0 14.6T 0 disk 10sdd 8:48 0 14.6T 0 disk 11sr0 11:0 1 1024M 0 rom
1root@jsl-ml:~# vgdisplay 2 /dev/sde: open failed: No medium found 3 /dev/sde: open failed: No medium found 4 --- Volume group --- 5 VG Name ubuntu-vg 6 System ID 7 Format lvm2 8 Metadata Areas 1 9 Metadata Sequence No 2 10 VG Access read/write 11 VG Status resizable 12 MAX LV 0 13 Cur LV 1 14 Open LV 1 15 Max PV 0 16 Cur PV 1 17 Act PV 1 18 VG Size 3.49 TiB 19 PE Size 4.00 MiB 20 Total PE 915322 21 Alloc PE / Size 51200 / 200.00 GiB 22 Free PE / Size 864122 / <3.30 TiB 23 VG UUID EAgRRa-u0VB-0MjL-gnk5-KDCC-YlYw-vAMwOS
1root@jsl-ml:~# lvdisplay 2 /dev/sde: open failed: No medium found 3 /dev/sde: open failed: No medium found 4 --- Logical volume --- 5 LV Path /dev/ubuntu-vg/ubuntu-lv 6 LV Name ubuntu-lv 7 VG Name ubuntu-vg 8 LV UUID PrxjnN-ELNU-peoA-JH6E-BFle-KclP-Gt07U4 9 LV Write Access read/write 10 LV Creation host, time ubuntu-server, 2020-12-03 15:13:07 +0000 11 LV Status available 12 # open 1 13 LV Size 200.00 GiB 14 Current LE 51200 15 Segments 1 16 Allocation inherit 17 Read ahead sectors auto 18 - currently set to 256 19 Block device 253:0
1root@jsl-ml:~# lvcreate -n ubuntu-lv-home --extents +100%FREE ubuntu-vg 2 /dev/sde: open failed: No medium found 3 /dev/sde: open failed: No medium found 4 Logical volume "ubuntu-lv-home" created. 5 6root@jsl-ml:~# lvdisplay 7 /dev/sde: open failed: No medium found 8 /dev/sde: open failed: No medium found 9 --- Logical volume --- 10 LV Path /dev/ubuntu-vg/ubuntu-lv 11 LV Name ubuntu-lv 12 VG Name ubuntu-vg 13 LV UUID PrxjnN-ELNU-peoA-JH6E-BFle-KclP-Gt07U4 14 LV Write Access read/write 15 LV Creation host, time ubuntu-server, 2020-12-03 15:13:07 +0000 16 LV Status available 17 # open 1 18 LV Size 200.00 GiB 19 Current LE 51200 20 Segments 1 21 Allocation inherit 22 Read ahead sectors auto 23 - currently set to 256 24 Block device 253:0 25 26 --- Logical volume --- 27 LV Path /dev/ubuntu-vg/ubuntu-lv-home 28 LV Name ubuntu-lv-home 29 VG Name ubuntu-vg 30 LV UUID 4dHuSb-yGla-pcdp-0tfg-hnAL-j0xZ-h2aiNr 31 LV Write Access read/write 32 LV Creation host, time jsl-ml, 2020-12-04 09:37:56 +0000 33 LV Status available 34 # open 0 35 LV Size <3.30 TiB 36 Current LE 864122 37 Segments 1 38 Allocation inherit 39 Read ahead sectors auto 40 - currently set to 256 41 Block device 253:1
1root@jsl-ml:~# lsblk 2NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT 3sda 8:0 0 14.6T 0 disk 4sdb 8:16 0 3.5T 0 disk 5├─sdb1 8:17 0 512M 0 part /boot/efi 6├─sdb2 8:18 0 1G 0 part /boot 7└─sdb3 8:19 0 3.5T 0 part 8 ├─ubuntu--vg-ubuntu--lv 253:0 0 200G 0 lvm / 9 └─ubuntu--vg-ubuntu--lv--home 253:1 0 3.3T 0 lvm 10sdc 8:32 0 14.6T 0 disk 11sdd 8:48 0 14.6T 0 disk 12sr0 11:0 1 1024M 0 rom 13root@jsl-ml:~# mkfs.ext4 /dev/ubuntu-vg/ubuntu-lv-home
1root@jsl-ml:~# mount /dev/ubuntu-vg/ubuntu-lv-home /home/data
磁盘挂载
创建磁盘
创建一块大小为 100G,格式为 qcow2 的磁盘
1# qemu-img create -f qcow2 ubuntu-04-disk.qcow2 100G 2Formatting 'ubuntu-04-disk.qcow2', fmt=qcow2 size=107374182400 cluster_size=65536 lazy_refcounts=off refcount_bits=16
查看磁盘大小
1# qemu-img info ubuntu-04-disk.qcow2 2image: ubuntu-04-disk.qcow2 3file format: qcow2 4virtual size: 100G (107374182400 bytes) 5disk size: 196K 6cluster_size: 65536 7Format specific information: 8compat: 1.1 9lazy refcounts: false 10refcount bits: 16 11corrupt: false
挂载磁盘
挂载命令
1virsh attach-disk vm-ubuntu-04 /home/kvm/images/ubuntu-04-disk.qcow2 --driver qemu --subdriver qcow2 vdb --persistent
注:
-
vm-ubuntu-04 为需要扩展的虚拟机名称
-
/home/kvm/images/ubuntu-04-disk.qcow2 为刚创建好的扩展磁盘
-
vdb 为新扩展磁盘的名称
卸载命令
1virsh detach-disk vm-ubuntu-04 /home/kvm/images/ubuntu-04-disk.qcow2 --persistent
格式化磁盘
虚拟机内执行
格式化分区
1[root@vm-ubuntu-04 ~]# fdisk /dev/vdb 2欢迎使用 fdisk (util-linux 2.23.2)。 3 4更改将停留在内存中,直到您决定将更改写入磁盘。 5使用写入命令前请三思。 6 7Device does not contain a recognized partition table 8使用磁盘标识符 0xd01c1c86 创建新的 DOS 磁盘标签。 9 10命令(输入 m 获取帮助):n 11Partition type: 12 p primary (0 primary, 0 extended, 4 free) 13 e extended 14Select (default p): p 15分区号 (1-4,默认 1): 16起始 扇区 (2048-209715199,默认为 2048): 17将使用默认值 2048 18Last 扇区, +扇区 or +size{K,M,G} (2048-209715199,默认为 209715199): 19将使用默认值 209715199 20分区 1 已设置为 Linux 类型,大小设为 100 GiB 21 22命令(输入 m 获取帮助):w 23The partition table has been altered! 24 25Calling ioctl() to re-read partition table. 26正在同步磁盘
格式化文件系统
1[root@vm-ubuntu-04 ~]# lsblk 2[root@vm-ubuntu-04 ~]# mkfs.ext4 /dev/vdb1
挂载磁盘
1mkdir /home/data 2mount /dev/vdb1 /home/data
FAQ
unexpected output in sfdisk --version
在执行 growpart /dev/vda 1 时报错
1unexpected output in sfdisk --version [sfdisk,来自 util-linux 2.23.2]
解决方案
1LANG=en_US.UTF-8 growpart /dev/vda 1
DOS partition table format cannot be used on drives for volumes larger than 2T
在使用 fdisk /dev/sdd 时, 当/dev/sdd的大小超过2T, 会有警告
1Device does not contain a recognized partition table. 2The size of this disk is 14.6 TiB (16000900661248 bytes). DOS partition table format cannot be used on drives for volumes larger than 2199023255040 bytes for 512-byte sectors. Use GUID partition table format (GPT).
解决方案
1# gdisk /dev/sdd 2 3GPT fdisk (gdisk) version 1.0.5 4 5Partition table scan: 6 MBR: not present 7 BSD: not present 8 APM: not present 9 GPT: not present 10 11Creating new GPT entries in memory. 12 13Command (? for help): o 14This option deletes all partitions and creates a new protective MBR. 15Proceed? (Y/N): Y 16 17Command (? for help): n 18Partition number (1-128, default 1): 19First sector (34-31251759070, default = 2048) or {+-}size{KMGTP}: 20Last sector (2048-31251759070, default = 31251759070) or {+-}size{KMGTP}: 21Current type is 8300 (Linux filesystem) 22Hex code or GUID (L to show codes, Enter = 8300): 23Changed type of partition to 'Linux filesystem' 24 25Command (? for help): w 26 27Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING 28PARTITIONS!! 29 30Do you want to proceed? (Y/N): Y 31OK; writing new GUID partition table (GPT) to /dev/sdd. 32The operation has completed successfully.
然后格式化即可
1mkfs.ext4 /dev/sdd1
