栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

linux为已有磁盘扩容 kvm,KVM虚拟磁盘扩容

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

linux为已有磁盘扩容 kvm,KVM虚拟磁盘扩容

KVM虚拟磁盘扩容

1、磁盘扩容分为raw和qcow2两种扩容方式,命令相同,区别是后缀名
查看当前信息

[root@localhost ~]# qemu-img info /data/kvm_name.qcow2 //本身只有500G
p_w_picpath: /data/kvm_name.qcow2
file format: qcow2
virtual size: 500G (536,870,912,000bytes)
disk size: 500G
cluster_size: 65536

修改信息 增加容量

[root@localhost ~]# qemu-img resize /data/kvm_name.qcow2 +100G  //加100G
Image resized.

查看修改后的信息

[root@localhost ~]# qemu-img info /data/kvm_name.qcow2 //查看已经+100G,登录虚拟机后fdisk -l还是500G
p_w_picpath: /data/kvm_name.qcow2
file format: qcow2
virtual size: 600G (16106127360 bytes)

如果需要真正生效,需要destroy,之后start,把配置重新加载

[root@localhost ~]# virsh destroy kvm_name
[root@localhost ~]# virsh start kvm_name

进入kvm

[root@localhost ~]# virsh console kvm_name

fdisk -l 列出系统中所有的磁盘设备和分区表,这里磁盘设备容量已经增加100G

[root@localhost ~]# fdisk -l
磁盘 /dev/vda:644.2 GB, 644245094400 字节,1258291200 个扇区

2、但是磁盘挂载的空间并没有增加,依然是170G

[root@localhost ~]# df -h //查看可用磁盘总容量和使用容量
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root  170G  170G  517M  100% /
devtmpfs                  16G     0   16G    0% /dev
tmpfs                     16G     0   16G    0% /dev/shm
tmpfs                     16G  9.3M   16G    1% /run
tmpfs                     16G     0   16G    0% /sys/fs/cgroup
/dev/vda1               1014M  145M  870M   15% /boot
/dev/mapper/centos-home   26G  307M   25G    2% /home
tmpfs                    3.2G     0  3.2G    0% /run/user/0

因已存在vda3 分区使用vda4

(1)因为新增加的空间还没有划分使用,所以要继续分区:

[root@localhost ~]# fdisk /dev/vda
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
Command (m for help): p
Disk /dev/vda: 16.1 GB, 16106127360 bytes
16 heads, 63 sectors/track, 31207 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000dca27
Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *           3        1018      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/vda2            1018       20806     9972736   8e  Linux LVM
Partition 2 does not end on cylinder boundary.
Command (m for help): n
Command action
e   extended
p   primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (1-31207, default 1): 20807
Last cylinder, +cylinders or +size{K,M,G} (20807-31207, default 31207): 31207
Command (m for help): p
Disk /dev/vda: 16.1 GB, 16106127360 bytes
16 heads, 63 sectors/track, 31207 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000dca27
Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *           3        1018      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/vda2            1018       20806     9972736   8e  Linux LVM
Partition 2 does not end on cylinder boundary.
/dev/vda3           20807       31207     5242104   83  Linux
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource                                                      busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

(2)然后再把这个/dev/vda4 加入到lvm里面去:

[root@localhost ~]# ls /dev/vda4 //如果没有这个分区,需要重启一下

[root@localhost ~]# reboot
[root@localhost ~]# pvcreate /dev/vda4  //创建物理卷
Physical volume "/dev/vda4" successfully created
[root@localhost ~]# pvs //查看卷
  PV         VG     Fmt  Attr PSize    PFree  
  /dev/vda2  centos lvm2 a--   <79.00g      0 
  /dev/vda3  centos lvm2 a--  <120.00g 100.00m
  /dev/vda4         lvm2 ---   400.00g 400.00g
[root@localhost ~]# vgextend centos /dev/vda4  //centos虚拟卷扩展  ,vda4加入到VolGroup卷组 centos
Volume group "centos" successfully extended

[root@localhost ~]# pvs
  PV         VG     Fmt  Attr PSize    PFree   
  /dev/vda2  centos lvm2 a--   <79.00g       0 
  /dev/vda3  centos lvm2 a--  <120.00g  100.00m
  /dev/vda4  centos lvm2 a--  <400.00g <400.00g

此步骤是pvs的VG列较之前变的有值了

[root@localhost ~]# vgs    //VFree中400G  如何把400G加入到root中?
  VG     #PV #LV #SN Attr   VSize    VFree  
  centos   3   3   0 wz--n- <598.99g 400.09g
[root@localhost ~]# lvs //查看逻辑卷
  LV   VG     Attr       LSize   Pool Origin Data%  meta%  Move Log Cpy%Sync Convert
  home centos -wi-ao---- <25.12g                                                    
  root centos -wi-ao---- 169.90g                                                    
  swap centos -wi-ao----  <3.88g    
[root@localhost ~]# lvextend -l +100%FREE /dev/centos/root  //扩展卷 centos 与 root 也和上方的“值对应关系”一致
  Size of logical volume centos/root changed from 169.90 GiB (43495 extents) to <570.00 GiB (145919 extents).
  Logical volume centos/root successfully resized.
[root@localhost ~]# df -h   # 当前容量未发生变化
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root  170G  170G  379M  100% /
devtmpfs                  16G     0   16G    0% /dev
tmpfs                     16G     0   16G    0% /dev/shm
tmpfs                     16G  9.3M   16G    1% /run
tmpfs                     16G     0   16G    0% /sys/fs/cgroup
/dev/vda1               1014M  145M  870M   15% /boot
/dev/mapper/centos-home   26G  307M   25G    2% /home
tmpfs                    3.2G     0  3.2G    0% /run/user/0
[root@localhost ~]# resize2fs /dev/centos/root #如resize2fs执行失败则改为xfs_growfs
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/centos/root is mounted on /; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/VolGroup/lv_root to 3548160 (4k) blocks.
The filesystem on /dev/VolGroup/lv_root is now 3548160 blocks long.

[root@localhost ~]# df -h			# 容量已发生变化
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root  570G  170G  401G   30% /
devtmpfs                  16G     0   16G    0% /dev
tmpfs                     16G     0   16G    0% /dev/shm
tmpfs                     16G  9.3M   16G    1% /run
tmpfs                     16G     0   16G    0% /sys/fs/cgroup
/dev/vda1               1014M  145M  870M   15% /boot
/dev/mapper/centos-home   26G  307M   25G    2% /home
tmpfs                    3.2G     0  3.2G    0% /run/user/0

如果提示 This p_w_picpath format does not support resize, 检查一下你qemu-img create的时候,是否有加 preallocation=metadata 选项,如果有,就不能resize了。

(3)另外,如果是增加磁盘,思路是创建磁盘: qemu-img create -f qcow2 /data/kvm_name.qcow2 5G关闭虚拟机: virsh destroy kvm_name编辑配置文件: virsh edit kvm_name

复制增加如下:注意是vdb,qcow2

开启虚拟机:virsh start kvm_name
进入虚拟机:virsh console kvm_name
分区: fdisk /dev/vdb
格式化:mkfs.ext4 /dev/vdb

挂载:vim /etc/fstab,添加一行:/dev/vdb /mnt ext4 defaults 0 0

mount -a 然后mnt就可以使用了

[root@localhost ~]# cd /mnt

[root@localhost mnt]# touch 23.txt

当然也可以按照上面的思路把 /dev/vdb 加入到 lvm里面去,测试成功,这里省略步骤注: raw格式步骤基本上和qcow2一样。如果提示 This p_w_picpath format does not support resize, 检查一下你qemu-img create的时候,是否有加 preallocation=metadata 选项,如果有,就不能resize了。

转自:https://blog.csdn.net/weixin_35583709/article/details/116819328

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/642421.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号