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

kvm虚拟机根分区数据无损扩容方法(非LVM)

2023-06-02 02:10:05
138
0

背景

 

kvm磁盘容量情况:硬盘分配了40G,系统没有使用LVM进行分区管理,根分区所挂载/dev/vda2随业务使用根分区剩余空间不足,需要对其进行数据无损的空间扩容。

 

 

p.s.:检测系统是否使用LVM进行分区管理的方法,执行下面命令即可(若没有返回信息则代表不是LVM分区方式):

pvdisplay      # 查看物理卷
vgdisplay      # 查看卷组
lvdisplay      # 查看逻辑卷

 

 

操作方法

kvm磁盘空间扩容

1、 虚拟机关机,防止磁盘数据损坏

 

2、 qemu-img命令可以通过resize选项对qcow2的格式虚拟磁盘镜像文件增加容量。

例如,对虚拟机的磁盘镜像文件扩充30G空间。

qemu-img resize system_os.qcow2 +30G

 

根分区数据无损扩容

1、虚拟机启动,此时lsblk可以看到vda磁盘的大小已经是扩容后的值,但是其分区vda1和vda2的值并没有更新。

[root@localhost ~]# lsblk 
MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
vda    252:0    0   70G  0 disk
├─vda1 252:1    0    2G  0 part /boot
└─vda2 252:2    0   38G  0 part /
vdb    252:16   0 12.7G  1 disk
[root@localhost ~]# df -h 
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs         64G     0   64G   0% /dev
tmpfs            64G     0   64G   0% /dev/shm
tmpfs            64G   29M   64G   1% /run
tmpfs            64G     0   64G   0% /sys/fs/cgroup
/dev/vda2        38G   27G  8.6G  76% /
tmpfs            64G     0   64G   0% /tmp
/dev/vda1       2.0G   65M  2.0G   4% /boot
tmpfs            13G     0   13G   0% /run/user/0
/dev/vdb       iso9660    13G   13G     0 100% /mnt/cdrom

 

2、需要用到partprobe命令,如果没有的话安装parted包。

[root@localhost ~]# partprobe --help
-bash: partprobe: command not found
[root@localhost ~]# yum -y install parted
[root@localhost ~]# partprobe /dev/vda

 

3、接下来进行根分区扩容。

fdisk -l :查看当前磁盘的分区情况

fdisk /dev/vda对该磁盘进行管理,在此首先对原根分区对应的vda2删除,然后新增分区。操作中可以关注vda2的start值是否保持一致。

fdisk 操作命令:

p:打印出当前分区的情况

d:删除分区

n:新建分区

w:将分区表写入磁盘并退出

[root@localhost ~]# fdisk -l
Disk /dev/vda: 70 GiB, 75161927680 bytes, 146800640 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: 0x247032e6

Device     Boot   Start      End  Sectors Size Id Type
/dev/vda1          2048  4194303  4192256   2G  e W95 FAT16 (LBA)
/dev/vda2       4194304 83886079 79691776  38G 83 Linux


Disk /dev/vdb: 12.66 GiB, 13578602496 bytes, 26520708 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

[root@localhost ~]# fdisk /dev/vda
Welcome to fdisk (util-linux 2.35.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p
Disk /dev/vda: 70 GiB, 75161927680 bytes, 146800640 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: 0x247032e6

Device     Boot   Start      End  Sectors Size Id Type
/dev/vda1          2048  4194303  4192256   2G  e W95 FAT16 (LBA)
/dev/vda2       4194304 83886079 79691776  38G 83 Linux

Command (m for help): d
Partition number (1,2, default 2): 2

Partition 2 has been deleted.

Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2): 2
First sector (4194304-146800639, default 4194304):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (4194304-146800639, default 146800639):

Created a new partition 2 of type 'Linux' and of size 68 GiB.
Partition #2 contains a ext4 signature.Do you want to remove the signature? [Y]es/[N]o: n

Command (m for help): p

Disk /dev/vda: 70 GiB, 75161927680 bytes, 146800640 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: 0x247032e6

Device     Boot   Start       End   Sectors Size Id Type
/dev/vda1          2048   4194303   4192256   2G  e W95 FAT16 (LBA)
/dev/vda2       4194304 146800639 142606336  68G 83 Linux

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


[root@localhost ~]# partprobe /dev/vda

#此时df看到的vda2的信息仍旧没有更新
[root@localhost ~]# df -Th
Filesystem     Type      Size  Used Avail Use% Mounted on
devtmpfs       devtmpfs   64G     0   64G   0% /dev
tmpfs          tmpfs      64G     0   64G   0% /dev/shm
tmpfs          tmpfs      64G   29M   64G   1% /run
tmpfs          tmpfs      64G     0   64G   0% /sys/fs/cgroup
/dev/vda2      ext4       38G   27G  8.6G  76% /
tmpfs          tmpfs      64G     0   64G   0% /tmp
/dev/vda1      vfat      2.0G   65M  2.0G   4% /boot
tmpfs          tmpfs      13G     0   13G   0% /run/user/0
/dev/vdb       iso9660    13G   13G     0 100% /mnt/cdrom

 

 

接下来使用resize2fs指令对分区重新进行一次扫描识别就可以看到正确的分区容量了,此时扩容成功。

 

#由于vda2的type是ext4,ext*格式使用resize2fs,如果是xfs系统使用xfs_growfs
[root@localhost ~]# resize2fs /dev/vda2resize2fs 1.45.6 (20-Mar-2020)
Filesystem at /dev/vda2 is mounted on /; on-line resizing required
old_desc_blocks = 5, new_desc_blocks = 9
The filesystem on /dev/vda2 is now 17825792 (4k) blocks long.


[root@localhost ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs         64G     0   64G   0% /dev
tmpfs            64G     0   64G   0% /dev/shm
tmpfs            64G   29M   64G   1% /run
tmpfs            64G     0   64G   0% /sys/fs/cgroup
/dev/vda2        67G   27G   37G  43% /
tmpfs            64G     0   64G   0% /tmp
/dev/vda1       2.0G   65M  2.0G   4% /boot
tmpfs            13G     0   13G   0% /run/user/0
/dev/vdb         13G   13G     0 100% /mnt/cdrom
0条评论
0 / 1000
刘****欣
6文章数
1粉丝数
刘****欣
6 文章 | 1 粉丝
原创

kvm虚拟机根分区数据无损扩容方法(非LVM)

2023-06-02 02:10:05
138
0

背景

 

kvm磁盘容量情况:硬盘分配了40G,系统没有使用LVM进行分区管理,根分区所挂载/dev/vda2随业务使用根分区剩余空间不足,需要对其进行数据无损的空间扩容。

 

 

p.s.:检测系统是否使用LVM进行分区管理的方法,执行下面命令即可(若没有返回信息则代表不是LVM分区方式):

pvdisplay      # 查看物理卷
vgdisplay      # 查看卷组
lvdisplay      # 查看逻辑卷

 

 

操作方法

kvm磁盘空间扩容

1、 虚拟机关机,防止磁盘数据损坏

 

2、 qemu-img命令可以通过resize选项对qcow2的格式虚拟磁盘镜像文件增加容量。

例如,对虚拟机的磁盘镜像文件扩充30G空间。

qemu-img resize system_os.qcow2 +30G

 

根分区数据无损扩容

1、虚拟机启动,此时lsblk可以看到vda磁盘的大小已经是扩容后的值,但是其分区vda1和vda2的值并没有更新。

[root@localhost ~]# lsblk 
MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
vda    252:0    0   70G  0 disk
├─vda1 252:1    0    2G  0 part /boot
└─vda2 252:2    0   38G  0 part /
vdb    252:16   0 12.7G  1 disk
[root@localhost ~]# df -h 
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs         64G     0   64G   0% /dev
tmpfs            64G     0   64G   0% /dev/shm
tmpfs            64G   29M   64G   1% /run
tmpfs            64G     0   64G   0% /sys/fs/cgroup
/dev/vda2        38G   27G  8.6G  76% /
tmpfs            64G     0   64G   0% /tmp
/dev/vda1       2.0G   65M  2.0G   4% /boot
tmpfs            13G     0   13G   0% /run/user/0
/dev/vdb       iso9660    13G   13G     0 100% /mnt/cdrom

 

2、需要用到partprobe命令,如果没有的话安装parted包。

[root@localhost ~]# partprobe --help
-bash: partprobe: command not found
[root@localhost ~]# yum -y install parted
[root@localhost ~]# partprobe /dev/vda

 

3、接下来进行根分区扩容。

fdisk -l :查看当前磁盘的分区情况

fdisk /dev/vda对该磁盘进行管理,在此首先对原根分区对应的vda2删除,然后新增分区。操作中可以关注vda2的start值是否保持一致。

fdisk 操作命令:

p:打印出当前分区的情况

d:删除分区

n:新建分区

w:将分区表写入磁盘并退出

[root@localhost ~]# fdisk -l
Disk /dev/vda: 70 GiB, 75161927680 bytes, 146800640 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: 0x247032e6

Device     Boot   Start      End  Sectors Size Id Type
/dev/vda1          2048  4194303  4192256   2G  e W95 FAT16 (LBA)
/dev/vda2       4194304 83886079 79691776  38G 83 Linux


Disk /dev/vdb: 12.66 GiB, 13578602496 bytes, 26520708 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

[root@localhost ~]# fdisk /dev/vda
Welcome to fdisk (util-linux 2.35.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p
Disk /dev/vda: 70 GiB, 75161927680 bytes, 146800640 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: 0x247032e6

Device     Boot   Start      End  Sectors Size Id Type
/dev/vda1          2048  4194303  4192256   2G  e W95 FAT16 (LBA)
/dev/vda2       4194304 83886079 79691776  38G 83 Linux

Command (m for help): d
Partition number (1,2, default 2): 2

Partition 2 has been deleted.

Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2): 2
First sector (4194304-146800639, default 4194304):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (4194304-146800639, default 146800639):

Created a new partition 2 of type 'Linux' and of size 68 GiB.
Partition #2 contains a ext4 signature.Do you want to remove the signature? [Y]es/[N]o: n

Command (m for help): p

Disk /dev/vda: 70 GiB, 75161927680 bytes, 146800640 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: 0x247032e6

Device     Boot   Start       End   Sectors Size Id Type
/dev/vda1          2048   4194303   4192256   2G  e W95 FAT16 (LBA)
/dev/vda2       4194304 146800639 142606336  68G 83 Linux

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


[root@localhost ~]# partprobe /dev/vda

#此时df看到的vda2的信息仍旧没有更新
[root@localhost ~]# df -Th
Filesystem     Type      Size  Used Avail Use% Mounted on
devtmpfs       devtmpfs   64G     0   64G   0% /dev
tmpfs          tmpfs      64G     0   64G   0% /dev/shm
tmpfs          tmpfs      64G   29M   64G   1% /run
tmpfs          tmpfs      64G     0   64G   0% /sys/fs/cgroup
/dev/vda2      ext4       38G   27G  8.6G  76% /
tmpfs          tmpfs      64G     0   64G   0% /tmp
/dev/vda1      vfat      2.0G   65M  2.0G   4% /boot
tmpfs          tmpfs      13G     0   13G   0% /run/user/0
/dev/vdb       iso9660    13G   13G     0 100% /mnt/cdrom

 

 

接下来使用resize2fs指令对分区重新进行一次扫描识别就可以看到正确的分区容量了,此时扩容成功。

 

#由于vda2的type是ext4,ext*格式使用resize2fs,如果是xfs系统使用xfs_growfs
[root@localhost ~]# resize2fs /dev/vda2resize2fs 1.45.6 (20-Mar-2020)
Filesystem at /dev/vda2 is mounted on /; on-line resizing required
old_desc_blocks = 5, new_desc_blocks = 9
The filesystem on /dev/vda2 is now 17825792 (4k) blocks long.


[root@localhost ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs         64G     0   64G   0% /dev
tmpfs            64G     0   64G   0% /dev/shm
tmpfs            64G   29M   64G   1% /run
tmpfs            64G     0   64G   0% /sys/fs/cgroup
/dev/vda2        67G   27G   37G  43% /
tmpfs            64G     0   64G   0% /tmp
/dev/vda1       2.0G   65M  2.0G   4% /boot
tmpfs            13G     0   13G   0% /run/user/0
/dev/vdb         13G   13G     0 100% /mnt/cdrom
文章来自个人专栏
Qemu
2 文章 | 1 订阅
0条评论
0 / 1000
请输入你的评论
0
0