Kvm之七:vm磁盘扩容

Centos7 Kvm vm 磁盘扩容

1、磁盘扩容方式
#常用的磁盘格式raw与qcow2,磁盘一般情况下只是扩大,没有缩小吧
#扩容方式:
#1.直接对原有磁盘扩容(dd-->raw)(qemu-img-->qcow2)
#2.添加新的磁盘加入vm的lvm中
2、直接扩展raw格式磁盘
#检查raw格式磁盘
[root@node71 vm]# qemu-img info ./oel3/oel3_raw.img 
image: ./oel3/oel3_raw.img
file format: raw
virtual size: 20G (21474836480 bytes)
disk size: 20G
[root@node71 vm]# virsh list
 Id    名称                         状态
----------------------------------------------------
 14    oel3                           running
#采用dd命令扩展镜像
[root@node71 vm]# dd if=/dev/zero bs=1024M count=5 >> ./oel3/oel3_raw.img 
记录了5+0 的读入
记录了5+0 的写出
5368709120字节(5.4 GB)已复制,11.2163 秒,479 MB/秒
[root@node71 vm]# qemu-img info ./oel3/oel3_raw.img 
image: ./oel3/oel3_raw.img
file format: raw
virtual size: 25G (26843545600 bytes)
disk size: 28G
[root@node71 vm]# virsh console oel3
连接到域 oel3
换码符为 ^]


Oracle Linux Server release 6.6
Kernel 3.8.13-44.1.1.el6uek.x86_64 on an x86_64

localhost.localdomain login: root
Password: 
Last login: Wed Oct 12 04:00:22 on tty1
[root@localhost ~]# fdisk -l /dev/vda
#这里是看不到扩容后的磁盘
Disk /dev/vda: 21.5 GB, 21474836480 bytes
16 heads, 63 sectors/track, 41610 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: 0x00031a78

   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       41611    20458496   8e  Linux LVM
Partition 2 does not end on cylinder boundary.
[root@localhost ~]# 
#重启一下vm
[root@node71 ~]# virsh list
 Id    名称                         状态
----------------------------------------------------
 15    oel3                           running
[root@node71 ~]# virsh console oel3
连接到域 oel3
换码符为 ^]

Oracle Linux Server release 6.6
Kernel 3.8.13-44.1.1.el6uek.x86_64 on an x86_64

localhost.localdomain login: root
Password: 
Last login: Thu Oct 13 23:15:49 on ttyS0
[root@localhost ~]# fdisk -l /dev/vda

Disk /dev/vda: 26.8 GB, 26843545600 bytes
16 heads, 63 sectors/track, 52012 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: 0x00031a78

   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       41611    20458496   8e  Linux LVM
Partition 2 does not end on cylinder boundary.
#上面可以查看到磁盘大小已经发生变化
#接下来是对磁盘分区,接着扩展vg,lv,
#在fdisk可以看到扩展后的容量,但是分区时cylinder对比显示不对,可能需要重启vm试试看
[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: 26.8 GB, 26843545600 bytes
16 heads, 63 sectors/track, 52012 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: 0x00031a78

   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       41611    20458496   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-52012, default 1): 41612
Last cylinder, +cylinders or +size{K,M,G} (41612-52012, default 52012): 
Using default value 52012

Command (m for help): p

Disk /dev/vda: 26.8 GB, 26843545600 bytes
16 heads, 63 sectors/track, 52012 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: 0x00031a78

   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       41611    20458496   8e  Linux LVM
Partition 2 does not end on cylinder boundary.
/dev/vda3           41612       52012     5242104   83  Linux

Command (m for help): t
Partition number (1-4): 3
Hex code (type L to list codes): 8e
Changed system type of partition 3 to 8e (Linux LVM)

Command (m for help): p

Disk /dev/vda: 26.8 GB, 26843545600 bytes
16 heads, 63 sectors/track, 52012 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: 0x00031a78

   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       41611    20458496   8e  Linux LVM
Partition 2 does not end on cylinder boundary.
/dev/vda3           41612       52012     5242104   8e  Linux LVM

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.
[root@localhost ~]# partx -a /dev/vda
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
[root@localhost ~]# partx -a /dev/vda
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
BLKPG: Device or resource busy
error adding partition 3
[root@localhost ~]# fdisk -l | grep /dev/vd[a-z]
Disk /dev/vda: 26.8 GB, 26843545600 bytes
/dev/vda1   *           3        1018      512000   83  Linux
/dev/vda2            1018       41611    20458496   8e  Linux LVM
/dev/vda3           41612       52012     5242104   8e  Linux LVM
[root@localhost ~]# 
#接着pv,vg,lv......
3、直接扩展qcow2格式的磁盘
#qcow2格式磁盘采用qemu-img方式扩展
[root@node71 ~]# virsh domblklist oel1
目标     源
------------------------------------------------
vda        /home/data/vm/oel1/oel1_qcow2.img

[root@node71 ~]# qemu-img info /home/data/vm/oel1/oel1_qcow2.img
image: /home/data/vm/oel1/oel1_qcow2.img
file format: qcow2
virtual size: 20G (21474836480 bytes)
disk size: 2.4G
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
[root@node71 ~]# virsh start oel1
域 oel1 已开始
#vm在开机情况下扩展磁盘,增加2G
[root@node71 ~]# qemu-img resize /home/data/vm/oel1/oel1_qcow2.img +2G
Image resized.
[root@node71 ~]# qemu-img info /home/data/vm/oel1/oel1_qcow2.img
image: /home/data/vm/oel1/oel1_qcow2.img
file format: qcow2
virtual size: 22G (23622320128 bytes)
disk size: 2.4G
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
[root@node71 ~]# virsh console oel1
连接到域 oel1
换码符为 ^]


Oracle Linux Server release 6.6
Kernel 3.8.13-44.1.1.el6uek.x86_64 on an x86_64

localhost.localdomain login: root
Password: 
Last login: Thu Oct 13 22:14:45 on ttyS0
#console连接到vm,查看不到磁盘增加,应该是可以支持在线扩展磁盘容量
[root@localhost ~]# fdisk -l /dev/vda

Disk /dev/vda: 21.5 GB, 21474836480 bytes
16 heads, 63 sectors/track, 41610 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: 0x00031a78

   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       41611    20458496   8e  Linux LVM
Partition 2 does not end on cylinder boundary.
[root@localhost ~]# 
#尝试重启vm,在virsh下,如果是在vm系统下,reboot,貌似没有用,
[root@node71 ~]# virsh shutdown oel1
域 oel1 被关闭

[root@node71 ~]# virsh list --all
 Id    名称                         状态
----------------------------------------------------
 -     oel1                           关闭
 -     oel2                           关闭
 -     oel3                           关闭
 -     oel4                           关闭
 -     win7                           关闭

[root@node71 ~]# virsh start oel1
域 oel1 已开始

[root@node71 ~]# virsh console oel1
连接到域 oel1
换码符为 ^]

mount: mount point /proc/bus/usb does not exist
        Welcome to Oracle Linux Server
Starting udev: [  OK  ]
Setting hostname localhost.localdomain:  [  OK  ]
Setting up Logical Volume Management:   2 logical volume(s) in volume group "VolGroup" now active
[  OK  ]
Checking filesystems
Checking all file systems.
[/sbin/fsck.ext4 (1) -- /] fsck.ext4 -a /dev/mapper/VolGroup-lv_root 
/dev/mapper/VolGroup-lv_root: clean, 22727/1215840 files, 504495/4859904 blocks
[/sbin/fsck.ext4 (1) -- /boot] fsck.ext4 -a /dev/vda1 
/dev/vda1: clean, 44/128016 files, 81219/512000 blocks
[  OK  ]
Remounting root filesystem in read-write mode:  [  OK  ]
Mounting local filesystems:  [  OK  ]
Enabling /etc/fstab swaps:  [  OK  ]
Entering non-interactive startup
Starting monitoring for VG VolGroup:   2 logical volume(s) in volume group "VolGroup" monitored
[  OK  ]
Bringing up loopback interface:  [  OK  ]
Bringing up interface eth0:  
Determining IP information for eth0... done.
[  OK  ]
Starting auditd: [  OK  ]
Starting system logger: [  OK  ]
Starting kdump:[FAILED]
Mounting filesystems:  [  OK  ]
Starting acpi daemon: [  OK  ]
Retrigger failed udev events[  OK  ]
Starting sshd: [  OK  ]
Starting postfix: [  OK  ]
Starting crond: [  OK  ]

Oracle Linux Server release 6.6
Kernel 3.8.13-44.1.1.el6uek.x86_64 on an x86_64

localhost.localdomain login: root
Password: 
Last login: Fri Oct 14 04:29:09 on ttyS0
[root@localhost ~]# 
[root@localhost ~]# fdisk -l /dev/vda
#查看到磁盘容量已经扩大
Disk /dev/vda: 25.8 GB, 25769803776 bytes
16 heads, 63 sectors/track, 49932 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: 0x00031a78

   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       41611    20458496   8e  Linux LVM
Partition 2 does not end on cylinder boundary.
#对磁盘进行分区
[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: 25.8 GB, 25769803776 bytes
16 heads, 63 sectors/track, 49932 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: 0x00031a78

   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       41611    20458496   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-49932, default 1): 41612
Last cylinder, +cylinders or +size{K,M,G} (41612-49932, default 49932): 
Using default value 49932

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.
[root@localhost ~]# partx -a /dev/vda
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
[root@localhost ~]# partx -a /dev/vda
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
BLKPG: Device or resource busy
error adding partition 3
[root@localhost ~]# fdisk -l /dev/vd[a-z]

Disk /dev/vda: 25.8 GB, 25769803776 bytes
16 heads, 63 sectors/track, 49932 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: 0x00031a78

   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       41611    20458496   8e  Linux LVM
Partition 2 does not end on cylinder boundary.
/dev/vda3           41612       49932     4193784   83  Linux
[root@localhost ~]# 
#已经分区完
4、通过新增加一块磁盘对vm进行扩容
#增加一块磁盘,然后在编辑vm的xml配置文件添加磁盘
[root@node71 ~]# qemu-img create -f qcow2 /home/data/vm/oel1/oel1_qcow2_1.img 5G
Formatting '/home/data/vm/oel1/oel1_qcow2_1.img', fmt=qcow2 size=5368709120 encryption=off cluster_size=65536 lazy_refcounts=off 
[root@node71 ~]# virsh edit oel1
编辑了域 oel1 XML 配置。
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='/home/data/vm/oel1/oel1_qcow2.img'/>
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
    </disk>
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='/home/data/vm/oel1/oel1_qcow2_1.img'/>
      <target dev='vdb' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
    </disk>
#需要重新像kvm注册vm的配置信息
[root@node71 ~]# virsh define /etc/libvirt/qemu/oel1.xml 
定义域 oel1(从 /etc/libvirt/qemu/oel1.xml)
#启动虚拟机检查
[root@node71 ~]# virsh start oel1
域 oel1 已开始

[root@node71 ~]# virsh console oel1
连接到域 oel1
换码符为 ^]


Oracle Linux Server release 6.6
Kernel 3.8.13-44.1.1.el6uek.x86_64 on an x86_64

localhost.localdomain login: root
Password: 
Last login: Fri Oct 14 04:31:00 on ttyS0
[root@localhost ~]# fdisk -l /dev/vd[a-b]

Disk /dev/vda: 25.8 GB, 25769803776 bytes
16 heads, 63 sectors/track, 49932 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: 0x00031a78

   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       41611    20458496   8e  Linux LVM
Partition 2 does not end on cylinder boundary.
/dev/vda3           41612       49932     4193784   83  Linux

Disk /dev/vdb: 5368 MB, 5368709120 bytes
16 heads, 63 sectors/track, 10402 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: 0x00000000

[root@localhost ~]# 
#以上可以看到新添加的磁盘已经被vm识别到,可以进行pv,vg,lv扩展
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,937评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,503评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,712评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,668评论 1 276
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,677评论 5 366
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,601评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,975评论 3 396
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,637评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,881评论 1 298
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,621评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,710评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,387评论 4 319
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,971评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,947评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,189评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 44,805评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,449评论 2 342

推荐阅读更多精彩内容