设为首页 收藏本站
查看: 1667|回复: 0

[经验分享] KVM虚拟磁盘扩容

[复制链接]
累计签到:2 天
连续签到:1 天
发表于 2016-2-19 08:51:01 | 显示全部楼层 |阅读模式
虚拟磁盘磁盘扩容

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

[iyunv@daixuan ~]# qemu-img info /data/daixuan1.qcow2 //本身只有9G
image: /data/daixuan1.qcow2
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 10G
cluster_size: 65536
[iyunv@daixuan ~]# qemu-img resize /data/daixuan1.qcow2 +5G  //加5G
Image resized.
[iyunv@daixuan ~]# qemu-img info /data/daixuan1.qcow2  //查看已经+5G,登录fdisk -l还是10G
image: /data/daixuan1.qcow2
file format: qcow2
virtual size: 15G (16106127360 bytes)

如果需要真正生效,需要destroy,之后start,把配置重新加载
[iyunv@daixuan ~]# virsh destroy daixuan1
[iyunv@daixuan ~]# virsh start daixuan1
[iyunv@daixuan ~]# virsh console daixuan1

fdisk -l   查看已经磁盘分区已经增加
[iyunv@localhost ~]# fdisk -l
Disk /dev/vda: 16.1 GB, 16106127360 bytes

2、但是磁盘挂载的空间并没有增加,依然是8.3G
[iyunv@localhost ~]# df -h
Filesystem  Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root 8.3G  685M  7.2G   9% /  
tmpfs                246M     0  246M   0% /dev/shm
/dev/vda1             477M   33M  419M   8% /boot

(1)因为新增加的空间还没有划分使用,所以要继续分区:
[iyunv@localhost ~]# fdisk /dev/vda
[iyunv@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)
3
Invalid partition number for type `3'
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):
[iyunv@localhost ~]#
[iyunv@localhost ~]# fdisk -l
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.

Disk /dev/mapper/VolGroup-lv_root: 9168 MB, 9168748544 bytes
255 heads, 63 sectors/track, 1114 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/mapper/VolGroup-lv_swap: 1040 MB, 1040187392 bytes
255 heads, 63 sectors/track, 126 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

[iyunv@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/vda3 加入到lvm里面去:

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

[iyunv@localhost ~]# pvcreate /dev/vda3  //创建物理卷
  Physical volume "/dev/vda3" successfully created



[iyunv@localhost ~]# pvs //查看卷
  PV         VG       Fmt  Attr PSize PFree
  /dev/vda2  VolGroup     lvm2 a--  9.51g  0
  /dev/vda3            lvm2 ---  5.00g  5.00g



[iyunv@localhost ~]# vgextend VolGroup /dev/vda3  //VolGroup虚拟卷扩展,加入到Vol
  Volume group "VolGroup" successfully extended

[iyunv@localhost ~]# pvs
  PV         VG       Fmt  Attr PSize PFree
  /dev/vda2  VolGroup lvm2 a--  9.51g    0
  /dev/vda3  VolGroup lvm2 a--  5.00g    0


[iyunv@localhost ~]# vgs
  VG       #PV #LV #SN Attr   VSize  VFree
  VolGroup   2   2   0 wz--n- 14.50g 5.00g   
如何把5G加入到lv_root中?



[iyunv@localhost ~]# lvs //查看逻辑卷
  LV    VG  Attr    LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv_root VolGroup -wi-ao----   8.54g
  lv_swap VolGroup -wi-ao---- 992.00m



[iyunv@localhost ~]# lvextend -l +100%FREE /dev/VolGroup/lv_root
  Size of logical volume VolGroup/lv_root changed from 8.54 GiB (2186 extents) to 13.54 GiB (3465 extents).
  Logical volume lv_root successfully resized


[iyunv@localhost ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root 8.3G  685M  7.2G   9% /
tmpfs                246M     0  246M   0% /dev/shm
/dev/vda1              477M   33M  419M   8% /boot


[iyunv@localhost ~]# resize2fs /dev/VolGroup/lv_root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/VolGroup/lv_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.


[iyunv@localhost ~]# df -h
Filesystem              Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root 14G 687M   12G   6% /
tmpfs                 246M     0  246M   0% /dev/shm
/dev/vda1              477M   33M  419M   8% /boot

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

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

1
2
3
4
5
<disk type='file' device='disk'>     
  <driver name='qemu' type='qcow2' cache='none'/>      
  <source file='/data/daixuan1_2.qcow2'/>      
  <target dev='vdb' bus='virtio'/>      
</disk>



开启虚拟机:virsh start daixuan1
进入虚拟机:virsh console daixuan1
分区: fdisk /dev/vdb
格式化:mkfs.ext4 /dev/vdb
挂载:vim /etc/fstab,添加一行:/dev/vdb  /mnt  ext4   defaults    0 0
mount -a 然后mnt就可以使用了
[iyunv@localhost ~]# cd /mnt
[iyunv@localhost mnt]# touch 23.txt

当然也可以按照上面的思路把 /dev/vdb 加入到 lvm里面去

注: raw格式
步骤基本上和qcow2一样。如果提示 This image format does not support resize, 检查一下你qemu-img create的时候,是否有加  preallocation=metadata 选项,如果有,就不能resize了。





运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-179825-1-1.html 上篇帖子: V2V迁移(二)ESXI 到KVM迁移 下篇帖子: KVM快照管理
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表