KVM笔记之磁盘扩容


磁盘扩容

关闭KVM虚拟机

virsh shutdown vm-ubuntu-04

扩容磁盘

查看磁盘位置

# virsh domblklist vm-ubuntu-04
Target     Source
------------------------------------------------
vda        /home/kvm/images/ubuntu-04.qcow2

查看磁盘信息

# qemu-img info /home/kvm/images/ubuntu-04.qcow2
image: /home/kvm/images/ubuntu-04.qcow2
file format: qcow2
virtual size: 2.0G (2147483648 bytes)
disk size: 1.7G
cluster_size: 65536
Format specific information:
compat: 0.10
refcount bits: 16

可以看出现在虚拟机磁盘大小为 2G

扩容磁盘大小

# qemu-img resize /home/kvm/images/ubuntu-04.qcow2 +28G
Image resized.
# qemu-img info /home/kvm/images/ubuntu-04.qcow2 
image: /home/kvm/images/ubuntu-04.qcow2
file format: qcow2
virtual size: 30G (32212254720 bytes)
disk size: 1.7G
cluster_size: 65536
Format specific information:
compat: 0.10
refcount bits: 16

现在磁盘大小已增加到 30G

扩容虚拟机分区

启动虚拟机

virsh start vm-ubuntu-04

查看磁盘大小和格式

root@vm-ubuntu-04:~# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
loop0    7:0    0 88.5M  1 loop /snap/core/7270
vda    252:0    0   30G  0 disk
├─vda1 252:1    0    1M  0 part
└─vda2 252:2    0    2G  0 part /

现在跟分区只分配了 2G 的大小,而当前 vda 磁盘总量为 30G

root@vm-ubuntu-04:~# df -T
Filesystem     Type     1K-blocks    Used Available Use% Mounted on
udev           devtmpfs   4053076       0   4053076   0% /dev
tmpfs          tmpfs       816784     936    815848   1% /run
/dev/vda2      ext4       2028368 1634136    273144  86% /
tmpfs          tmpfs      4083912       0   4083912   0% /dev/shm
tmpfs          tmpfs         5120       0      5120   0% /run/lock
tmpfs          tmpfs      4083912       0   4083912   0% /sys/fs/cgroup
/dev/loop0     squashfs     90624   90624         0 100% /snap/core/7270
tmpfs          tmpfs       816780       0    816780   0% /run/user/0

注意 :不同格式的文件系统扩容磁盘命令 有所不同,比如我的根分区格式为 ext4, 以下将以 ext4 为例

扩容磁盘

  • 安装 cloud-guest-utils

    apt -y install cloud-guest-utils
  • 扩容磁盘

    root@vm-ubuntu-base:~# growpart /dev/vda 2
    CHANGED: partition=2 start=4096 old: size=4188160 end=4192256 new: size=62910431,end=62914527

    2 为跟分区所在磁盘

    root@vm-ubuntu-base:~# lsblk
    NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    loop0    7:0    0 88.5M  1 loop /snap/core/7270
    vda    252:0    0   30G  0 disk
    ├─vda1 252:1    0    1M  0 part
    └─vda2 252:2    0   30G  0 part /
  • 更新且格式化分区 resize2fs 扩容 ext4 文件系统, 其它可参考 https://computingforgeeks.com/extending-root-filesystem-using-lvm-linux/

    root@vm-ubuntu-base:~# resize2fs /dev/vda2
    resize2fs 1.44.1 (24-Mar-2018)
    Filesystem at /dev/vda2 is mounted on /; on-line resizing required
    old_desc_blocks = 1, new_desc_blocks = 4
    The filesystem on /dev/vda2 is now 7863803 (4k) blocks long.
    
    root@vm-ubuntu-base:~# lsblk
    NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    loop0    7:0    0 88.5M  1 loop /snap/core/7270
    vda    252:0    0   30G  0 disk
    ├─vda1 252:1    0    1M  0 part
    └─vda2 252:2    0   30G  0 part /
    root@vm-ubuntu-base:~# df -hT | grep /dev/vda
    /dev/vda2      ext4       30G  1.6G   27G   6% /

DONELVM磁盘扩容

CLOSED: [2020-12-17 四 10:26]

  • State "DONE" from [2020-12-17 四 10:26]

root@jsl-ml:~# lsblk
NAME                      MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                         8:0    0 14.6T  0 disk 
sdb                         8:16   0  3.5T  0 disk 
├─sdb1                      8:17   0  512M  0 part /boot/efi
├─sdb2                      8:18   0    1G  0 part /boot
└─sdb3                      8:19   0  3.5T  0 part 
  └─ubuntu--vg-ubuntu--lv 253:0    0  200G  0 lvm  /
sdc                         8:32   0 14.6T  0 disk 
sdd                         8:48   0 14.6T  0 disk 
sr0                        11:0    1 1024M  0 rom  
root@jsl-ml:~# vgdisplay 
  /dev/sde: open failed: No medium found
  /dev/sde: open failed: No medium found
  --- Volume group ---
  VG Name               ubuntu-vg
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               3.49 TiB
  PE Size               4.00 MiB
  Total PE              915322
  Alloc PE / Size       51200 / 200.00 GiB
  Free  PE / Size       864122 / <3.30 TiB
  VG UUID               EAgRRa-u0VB-0MjL-gnk5-KDCC-YlYw-vAMwOS
root@jsl-ml:~# lvdisplay 
  /dev/sde: open failed: No medium found
  /dev/sde: open failed: No medium found
  --- Logical volume ---
  LV Path                /dev/ubuntu-vg/ubuntu-lv
  LV Name                ubuntu-lv
  VG Name                ubuntu-vg
  LV UUID                PrxjnN-ELNU-peoA-JH6E-BFle-KclP-Gt07U4
  LV Write Access        read/write
  LV Creation host, time ubuntu-server, 2020-12-03 15:13:07 +0000
  LV Status              available
  # open                 1
  LV Size                200.00 GiB
  Current LE             51200
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0
root@jsl-ml:~# lvcreate -n ubuntu-lv-home --extents +100%FREE  ubuntu-vg
  /dev/sde: open failed: No medium found
  /dev/sde: open failed: No medium found
  Logical volume "ubuntu-lv-home" created.
   
root@jsl-ml:~# lvdisplay 
  /dev/sde: open failed: No medium found
  /dev/sde: open failed: No medium found
  --- Logical volume ---
  LV Path                /dev/ubuntu-vg/ubuntu-lv
  LV Name                ubuntu-lv
  VG Name                ubuntu-vg
  LV UUID                PrxjnN-ELNU-peoA-JH6E-BFle-KclP-Gt07U4
  LV Write Access        read/write
  LV Creation host, time ubuntu-server, 2020-12-03 15:13:07 +0000
  LV Status              available
  # open                 1
  LV Size                200.00 GiB
  Current LE             51200
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0
   
  --- Logical volume ---
  LV Path                /dev/ubuntu-vg/ubuntu-lv-home
  LV Name                ubuntu-lv-home
  VG Name                ubuntu-vg
  LV UUID                4dHuSb-yGla-pcdp-0tfg-hnAL-j0xZ-h2aiNr
  LV Write Access        read/write
  LV Creation host, time jsl-ml, 2020-12-04 09:37:56 +0000
  LV Status              available
  # open                 0
  LV Size                <3.30 TiB
  Current LE             864122
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1
root@jsl-ml:~# lsblk
NAME                            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                               8:0    0 14.6T  0 disk 
sdb                               8:16   0  3.5T  0 disk 
├─sdb1                            8:17   0  512M  0 part /boot/efi
├─sdb2                            8:18   0    1G  0 part /boot
└─sdb3                            8:19   0  3.5T  0 part 
  ├─ubuntu--vg-ubuntu--lv       253:0    0  200G  0 lvm  /
  └─ubuntu--vg-ubuntu--lv--home 253:1    0  3.3T  0 lvm  
sdc                               8:32   0 14.6T  0 disk 
sdd                               8:48   0 14.6T  0 disk 
sr0                              11:0    1 1024M  0 rom  
root@jsl-ml:~# mkfs.ext4 /dev/ubuntu-vg/ubuntu-lv-home
root@jsl-ml:~# mount /dev/ubuntu-vg/ubuntu-lv-home /home/data

磁盘挂载

创建磁盘

创建一块大小为 100G,格式为 qcow2 的磁盘

# qemu-img create -f qcow2 ubuntu-04-disk.qcow2 100G
Formatting 'ubuntu-04-disk.qcow2', fmt=qcow2 size=107374182400 cluster_size=65536 lazy_refcounts=off refcount_bits=16

查看磁盘大小

# qemu-img info ubuntu-04-disk.qcow2
image: ubuntu-04-disk.qcow2
file format: qcow2
virtual size: 100G (107374182400 bytes)
disk size: 196K
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: false
refcount bits: 16
corrupt: false

挂载磁盘

挂载命令

virsh 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 为新扩展磁盘的名称

卸载命令

virsh detach-disk vm-ubuntu-04 /home/kvm/images/ubuntu-04-disk.qcow2 --persistent

格式化磁盘

虚拟机内执行

格式化分区

[root@vm-ubuntu-04 ~]# fdisk /dev/vdb
欢迎使用 fdisk (util-linux 2.23.2)。

更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。

Device does not contain a recognized partition table
使用磁盘标识符 0xd01c1c86 创建新的 DOS 磁盘标签。

命令(输入 m 获取帮助):n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
分区号 (1-4,默认 1):
起始 扇区 (2048-209715199,默认为 2048):
将使用默认值 2048
Last 扇区, +扇区 or +size{K,M,G} (2048-209715199,默认为 209715199):
将使用默认值 209715199
分区 1 已设置为 Linux 类型,大小设为 100 GiB

命令(输入 m 获取帮助):w
The partition table has been altered!

Calling ioctl() to re-read partition table.
正在同步磁盘

格式化文件系统

[root@vm-ubuntu-04 ~]# lsblk
[root@vm-ubuntu-04 ~]# mkfs.ext4 /dev/vdb1

挂载磁盘

mkdir /home/data
mount /dev/vdb1 /home/data

FAQ

unexpected output in sfdisk --version

在执行 growpart /dev/vda 1 时报错

unexpected output in sfdisk --version [sfdisk,来自 util-linux 2.23.2]

解决方案

LANG=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, 会有警告

Device does not contain a recognized partition table.
The 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).

解决方案

# gdisk /dev/sdd

GPT fdisk (gdisk) version 1.0.5

Partition table scan:
  MBR: not present
  BSD: not present
  APM: not present
  GPT: not present

Creating new GPT entries in memory.

Command (? for help): o
This option deletes all partitions and creates a new protective MBR.
Proceed? (Y/N): Y

Command (? for help): n
Partition number (1-128, default 1): 
First sector (34-31251759070, default = 2048) or {+-}size{KMGTP}: 
Last sector (2048-31251759070, default = 31251759070) or {+-}size{KMGTP}: 
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300): 
Changed type of partition to 'Linux filesystem'

Command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): Y
OK; writing new GUID partition table (GPT) to /dev/sdd.
The operation has completed successfully.

然后格式化即可

mkfs.ext4 /dev/sdd1

参考链接

作者: honmaple
链接: https://honmaple.me/articles/2020/01/KVM笔记之磁盘扩容.html
版权: CC BY-NC-SA 4.0 知识共享署名-非商业性使用-相同方式共享4.0国际许可协议
wechat
alipay

加载评论