searchusermenu
  • 发布文章
  • 消息中心
点赞
收藏
评论
分享
原创

给虚拟机扩容

2023-10-31 08:12:30
17
0
扩容之前需要备份一下原有的磁盘,防止出现意外

一、方法一:扩容原有磁盘,重新分区

需求:原来的磁盘是40G,现在想扩容到100G

这里需要先关闭虚拟机,因为会使用qemu-img

(一) 在宿主机里增加磁盘的大小

qemu-img resize /path/disk.qcow2 +60G  # 增加60G的磁盘大小
  • 如果不知道磁盘路径的话,可以使用
virsh domblklist vm # 可以查看虚拟机上的磁盘块和路径 
  • 可能会出现问题:虚拟机有快照的存在而不让删除,因此需要先删除快照
virsh snopshot-list vm  # 查看虚拟机快照列表
virsh snapshot-delete vm <shot name> # 删除虚拟机快照

(二) 进入虚拟机内部操作分区

  1. df -Th不仅可以看到磁盘使用情况,还可以看到文件系统类型,vda的文件类型是xfs
  2. lsblk 可以看到虚拟机的块设备和分区情况。该虚拟机有2个块设备vda和vdb,其中各有一个分区
  3. vdb块设备现在在df -h里无法查到是因为该磁盘还没有被挂载
[root@localhost ~]# df -Th
Filesystem     Type      Size  Used Avail Use% Mounted on
devtmpfs       devtmpfs   32G     0   32G   0% /dev
tmpfs          tmpfs      32G     0   32G   0% /dev/shm
tmpfs          tmpfs      32G   17M   32G   1% /run
tmpfs          tmpfs      32G     0   32G   0% /sys/fs/cgroup
/dev/vda1      xfs        40G   15G   26G  36% /
tmpfs          tmpfs      32G  4.0K   32G   1% /tmp
tmpfs          tmpfs     6.4G     0  6.4G   0% /run/user/0

[root@localhost ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
vda    253:0    0  100G  0 disk 
└─vda1 253:1    0   40G  0 part /
vdb    253:16   0   80G  0 disk 
└─vdb1 253:17   0   80G  0 part 
  1. 由上图的结果可以看到,块设备已经被我们扩容到了100G,但是挂载在跟目录下的分区还只有40G,因此接下来的需求是重新分区
  • fdisk /dev/vda:代表将打开fdisk工具,并操作块设备/dev/vda
  • 进入交互界面之后,可以进行分区管理操作:
    • p:显示分区表,列出所有分区信息
    • d:删除分区,后跟分区号(例如 d 1 为删除第一个分区,我这里只有一个,所以直接删除了)
    • n:创建新分区,可以选择主分区或者逻辑分区
    • w:保存分区表的更改并退出fdisk
  1. 有了以上知识后,来扩展分区的大小,步骤如下:
  • 输入p,查看当前分区
  • 输入d,分区后,删除原来的,即需要扩展的分区
  • 输入p,查看删除情况
  • 输入n,新建分区。新建分区时需要选择扇区起始位置和结束位置,我这里直接使用的默认值。
  • 输入p,查看新建分区的情况
  • 输入w,进行保存并推出
[root@localhost ~]# fdisk /dev/vda

Welcome to fdisk (util-linux 2.37.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

This disk is currently in use - repartitioning is probably a bad idea.
It's recommended to umount all file systems, and swapoff all swap
partitions on this disk.

Command (m for help): p

Disk /dev/vda: 100 GiB, 107374182400 bytes, 209715200 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
Disklabel type: dos
Disk identifier: 0x7d3ff994

=========以下即为显示出来的分区信息==========
Device     Boot Start      End  Sectors Size Id Type
/dev/vda1  *     2048 83886079 83884032  40G 83 Linux

Command (m for help): d
Selected partition 1
Partition 1 has been deleted.

Command (m for help): p
Disk /dev/vda: 100 GiB, 107374182400 bytes, 209715200 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
Disklabel type: dos
Disk identifier: 0x7d3ff994


=========以下即为新建分区==========
Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 
First sector (2048-209715199, default 2048): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-209715199, default 209715199): 

Created a new partition 1 of type 'Linux' and of size 100 GiB.
Partition #1 contains a xfs signature.

Do you want to remove the signature? [Y]es/[N]o: Y

The signature will be removed by a write command.
Command (m for help): p

Disk /dev/vda: 100 GiB, 107374182400 bytes, 209715200 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
Disklabel type: dos
Disk identifier: 0x7d3ff994

=========以下即为修改后的分区信息==========
Device     Boot Start       End   Sectors  Size Id Type
/dev/vda1        2048 209715199 209713152  100G 83 Linux

Filesystem/RAID signature on partition 1 will be wiped.

Command (m for help): w
The partition table has been altered.
Syncing disks.
  1. 修改完分区表之后,进行刷新
  • partprobe:可以通知操作系统重新加载分区表的命令,以便在不重启系统的情况下生效
  • lsblk:再来查看一下分区情况,这是vda1的分区已经扩展到了100G
  • 使用df -h发现分区还是没有变,因此需要再执行一步操作
[root@localhost ~]# partprobe /dev/vda

[root@localhost ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
vda    253:0    0  100G  0 disk 
└─vda1 253:1    0  100G  0 part /
vdb    253:16   0   80G  0 disk 
└─vdb1 253:17   0   80G  0 part 

[root@localhost ~]# df -Th
Filesystem     Type      Size  Used Avail Use% Mounted on
devtmpfs       devtmpfs   32G     0   32G   0% /dev
tmpfs          tmpfs      32G     0   32G   0% /dev/shm
tmpfs          tmpfs      32G   17M   32G   1% /run
tmpfs          tmpfs      32G     0   32G   0% /sys/fs/cgroup
/dev/vda1      xfs        40G   15G   26G  36% /
tmpfs          tmpfs      32G  4.0K   32G   1% /tmp
tmpfs          tmpfs     6.4G     0  6.4G   0% /run/user/0
  1. 最后一部操作,我们需要让文件系统也得到扩展以占用整个分区的空间,这里的命令与文件系统的格式有关
  • 对于xfs文件系统:可以使用xfs_growfs /dev/vda1
  • 对于ext4文件系统:需要使用resize2fs /dev/vda1
[root@localhost ~]# xfs_growfs /dev/vda1
meta-data=/dev/vda1              isize=512    agcount=4, agsize=2621376 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1
data     =                       bsize=4096   blocks=10485504, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=5119, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 10485504 to 26214144

[root@localhost ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
vda    253:0    0  100G  0 disk 
└─vda1 253:1    0  100G  0 part /
vdb    253:16   0   80G  0 disk 
└─vdb1 253:17   0   80G  0 part 

[root@localhost ~]# df -Th
Filesystem     Type      Size  Used Avail Use% Mounted on
devtmpfs       devtmpfs   32G     0   32G   0% /dev
tmpfs          tmpfs      32G     0   32G   0% /dev/shm
tmpfs          tmpfs      32G   17M   32G   1% /run
tmpfs          tmpfs      32G     0   32G   0% /sys/fs/cgroup
/dev/vda1      xfs       100G   15G   86G  15% /
tmpfs          tmpfs      32G  4.0K   32G   1% /tmp
tmpfs          tmpfs     6.4G     0  6.4G   0% /run/user/0

 8. 至此,操作结束,原有的磁盘以及被扩容成了100G

二、方法二:添加磁盘

需求:原来的磁盘是40G,我们想挂载一个新的磁盘

(一) 在宿主机里新建一个磁盘

[root@localhost wy]# qemu-img create -f qcow2 add-disk.qcow2 30G
Formatting 'add-disk.qcow2', fmt=qcow2 size=32212254720 cluster_size=65536 lazy_refcounts=off refcount_bits=16

(二) 给虚拟机添加磁盘

virsh attach-disk vm /var/wy/add-disk.qcow2 vdb --subdriver qcow2 --config  #永久添加磁盘
virsh attach-disk vm /var/wy/add-disk.qcow2 vdb --subdriver qcow2  # 临时添加磁盘
  • 添加完成之后,会发现虚拟机的xml文件改变:
<disk type='block' device='disk'>
  <driver name='qemu' type='qcow2'/>
    <source dev='/home/wy/add-disk.qcow2'/>
      <target dev='vdb' bus='virtio'/>
        <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
</disk>

(三) 查看当前虚拟机的磁盘

  • 可以看到原有的磁盘和新加的磁盘,如果看不到新加的磁盘需要重启一下虚拟机
[root@localhost wy]# virsh domblklist newy
 Target   Source
-------------------------------------------------------------
 vda      /var/wy/Ctyunos.qcow2
 vdb      /var/wy/add-disk.qcow2

(四) 进入虚拟机内部,查看虚拟机的磁盘

lsblk

(五) 格式化分区/挂载

mkfs.xfs/dev/vdb # 不分区,直接格式化

mount /dev/vdb  /mnt # 挂载硬盘
0条评论
0 / 1000
w****m
1文章数
0粉丝数
w****m
1 文章 | 0 粉丝