Xen和KVM的guest主机热添加硬盘

简介:

Xen或者KVM线上主机的磁盘空间不足,需要增大硬盘,并且不能重启,这时候就需要热添加以扩充磁盘空间


说明:Xen和KVM主机都为CentOS 6.4  guest磁盘为lvm格式

先来说Xen吧,操作都一样的,虚拟机上测试

Xen的guest主机扩充根分区

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# xm list                     #test2主机增大磁盘空间
Name                                        ID   Mem VCPUs      State   Time(s)
Domain-0                                     0  1024     4     r-----     80.8
test2                                        1   768     4     -b----     25.8
  
# qemu-img info test2.qcow2   #现在为10G的磁盘
image: test2.qcow2
file  format : raw
virtual size: 10G (10739318784 bytes)  
disk size: 1.5G
  
# qemu-img create -f qcow2 -o size=5G,preallocation=metadata /data/testadd.qcow2  #新建一块5G的磁盘
Formatting  '/data/testadd.qcow2' fmt =qcow2 size=5368709120 encryption=off 
cluster_size=65536 preallocation= 'metadata'
#防止出错,把preallocation=metadata去掉,或者qemu-img create -f raw /data/testadd.img 5G
  
# qemu-img info testadd.qcow2 #查看新建的磁盘
image: testadd.qcow2
file  format : qcow2
virtual size: 5.0G (5368709120 bytes)
disk size: 912K
cluster_size: 65536
  
# virsh attach-disk test2 /data/testadd.qcow2 xvdb --cache=writeback --subdriver=qcow2   #对test2主机进行添加,xvdb
Disk attached successfully
  
# vim test2          #修改guest的配置文件,添加xvdb
name =  "test2"
memory =  "768"
disk = [  "file:/data/test2.qcow2,xvda,w" , "file:/data/testadd.qcow2,xvdb,w"  ]
vif = [  "bridge=br0"  ]
vcpus = 4
on_reboot =  "restart"
on_crash =  "destroy"

接下来就是进入guest内的操作,先对其分区

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# fdisk -l       #查看有没有刚添加的xvdb
Disk  /dev/xvdb : 5369 MB, 5369757696 bytes
  
255 heads, 63 sectors /track , 652 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
   
# fdisk /dev/xvdb             #进行分区,lvm格式
Command (m  for  help): n
Command action
    e   extended
    p   primary partition (1-4)
p
  
Partition number (1-4): 1
First cylinder (1-652, default 1): 
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-652, default 652): 
Using default value 652
  
Command (m  for  help): t
Selected partition 1
Hex code ( type  L to list codes): 8e
Changed system  type  of partition 1 to 8e (Linux LVM)
  
Command (m  for  help): p
  
Disk  /dev/xvdb : 5369 MB, 5369757696 bytes
255 heads, 63 sectors /track , 652 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: 0xb1ef0943
  
     Device Boot      Start         End      Blocks   Id  System
/dev/xvdb1                1         652     5237158+  8e  Linux LVM
  
Command (m  for  help): w
The partition table has been altered!
  
Calling ioctl() to re- read  partition table.
Syncing disks.

lvm磁盘分区

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
[root@localhost ~] # partx -a /dev/xvdb
BLKPG: Device or resource busy
error adding partition 1
[root@localhost ~] # kpartx /dev/xvdb
xvdb1 : 0 10474317  /dev/xvdb  63
[root@localhost ~] # partx -a /dev/xvdb1 /dev/xvdb
[root@localhost ~] # pvcreate /dev/xvdb1 
   Physical volume  "/dev/xvdb1"  successfully created
   
[root@localhost ~] # vgs             #现在为10G的磁盘
   VG        #PV #LV #SN Attr   VSize VFree
   VolGroup   1   2   0 wz--n- 9.51g    0 
   
[root@localhost ~] # vgextend VolGroup /dev/xvdb1 
   Volume group  "VolGroup"  successfully extended
   
[root@localhost ~] # vgs              #增大了5G的空间       
   VG        #PV #LV #SN Attr   VSize  VFree
   VolGroup   2   2   0 wz--n- 14.50g 4.99g
   
[root@localhost ~] # lvs
   LV      VG       Attr      LSize   Pool Origin Data%  Move Log Cpy%Sync Convert
   lv_root VolGroup -wi-ao---   8.54g                                             
   lv_swap VolGroup -wi-ao--- 992.00m                                             
   
[root@localhost ~] # df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                       8.5G  695M  7.3G   9% /
tmpfs                 371M     0  371M   0%  /dev/shm
/dev/xvda1             485M   32M  429M   7%  /boot
  
[root@localhost ~] # lvextend -l +100%FREE /dev/VolGroup/lv_root 
   Extending logical volume lv_root to 13.53 GiB
   Logical volume lv_root successfully resized
  
[root@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 3547136 (4k) blocks.
The filesystem on  /dev/VolGroup/lv_root  is now 3547136 blocks long.
  
[root@localhost ~] # df -h            #磁盘增加好了
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                        14G  698M   12G   6% /
tmpfs                 371M     0  371M   0%  /dev/shm
/dev/xvda1             485M   32M  429M   7%  /boot


KVM的guest热扩充磁盘空间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
[root@localhost ~] # virsh list
  Id    Name                           State
----------------------------------------------------
  1     bbotte                         running
  
# qemu-img info bbotte.qcow2 
image: bbotte.qcow2
file  format : qcow2
virtual size: 10G (10737418240 bytes)
disk size: 1.3G
cluster_size: 65536
  
# qemu-img create -f qcow2 -o size=5G,preallocation=metadata /var/kvm/bbotteadd.qcow2
Formatting  '/var/kvm/bbotteadd.qcow2' fmt =qcow2 size=5368709120 encryption=off cluster_size=65536 preallocation= 'metadata' 
#防止出错,把preallocation=metadata去掉,或者qemu-img create -f raw /var/kvm/bbotteadd.img 5G 
  
# virsh attach-disk bbotte /var/kvm/bbotteadd.qcow2 vdb --cache=writeback --subdriver=qcow2
Disk attached successfully
  
# virsh edit bbotte
<devices>
     <emulator> /usr/libexec/qemu-kvm < /emulator >
     <disk  type = 'file'  device= 'disk' >
       <driver name= 'qemu'  type = 'qcow2'  cache= 'writeback' />
       < source  file = '/var/kvm/bbotte.qcow2' />
       <target dev= 'vda'  bus= 'virtio' />
       <address  type = 'pci'  domain= '0x0000'  bus= '0x00'  slot= '0x05'  function = '0x0' />
     < /disk >
##下面为添加的磁盘文件
     <disk  type = 'file'  device= 'disk' >
       <driver name= 'qemu'  type = 'qcow2'  cache= 'writeback' />
       < source  file = '/var/kvm/bbotteadd.qcow2' />      #指定路径
       <target dev= 'vdb'  bus= 'virtio' />               #指定磁盘名称vdb
       <address  type = 'pci'  domain= '0x0000'  bus= '0x00'  slot= '0x07'  function = '0x0' /> 
  #slot和其他的设备序列不要相同,如果相同,保存的时候会有提示
    < /disk >

 

到guest主机里面,会看到如下提示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# pci 0000:00:07.0: BAR 1: assigned [mem 0x30000000-0x30000fff]
pci 0000:00:07.0: BAR 1:  set  to [mem 0x30000000-0x30000fff] (PCI address [0x30000000-0x30000fff]
pci 0000:00:07.0: BAR 0: assigned [io  0x1000-0x103f]
pci 0000:00:07.0: BAR 0:  set  to [io  0x1000-0x103f] (PCI address [0x1000-0x103f]
pci 0000:00:00.0: no hotplug settings from platform
pci 0000:00:00.0: using default PCI settings
pci 0000:00:01.0: no hotplug settings from platform
pci 0000:00:01.0: using default PCI settings
ata_piix 0000:00:01.1: no hotplug settings from platform
ata_piix 0000:00:01.1: using default PCI settings
piix4_smbus 0000:00:01.3: no hotplug settings from platform
piix4_smbus 0000:00:01.3: using default PCI settings
virtio-pci 0000:00:03.0: no hotplug settings from platform
virtio-pci 0000:00:03.0: using default PCI settings
uhci_hcd 0000:00:04.0: no hotplug settings from platform
uhci_hcd 0000:00:04.0: using default PCI settings
uhci_hcd 0000:00:04.1: no hotplug settings from platform
uhci_hcd 0000:00:04.1: using default PCI settings
uhci_hcd 0000:00:04.2: no hotplug settings from platform
uhci_hcd 0000:00:04.2: using default PCI settings
ehci_hcd 0000:00:04.7: no hotplug settings from platform
ehci_hcd 0000:00:04.7: using default PCI settings
virtio-pci 0000:00:05.0: no hotplug settings from platform
virtio-pci 0000:00:05.0: using default PCI settings
virtio-pci 0000:00:06.0: no hotplug settings from platform
virtio-pci 0000:00:06.0: using default PCI settings
pci 0000:00:07.0: no hotplug settings from platform
pci 0000:00:07.0: using default PCI settings
virtio-pci 0000:00:07.0: enabling device (0000 -> 0003)
virtio-pci 0000:00:07.0: PCI INT A -> Link[LNKC] -> GSI 11 (level, high) -> IRQ 11
  vdb: unknown partition table

进行磁盘扩充

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# df -h
Filesystem                    Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root   8.4G  918M  7.1G  12% /
tmpfs                         372M     0  372M   0%  /dev/shm
/dev/vda1                      485M   32M  428M   7%  /boot
  
# fdisk -l
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 ~] # lvs
   LV      VG       Attr       LSize Pool Origin Data%  Move Log Cpy%Sync Convert
   lv_root VolGroup -wi-ao---- 8.51g                                             
   lv_swap VolGroup -wi-ao---- 1.00g                                             
   
[root@localhost ~] # vgextend VolGroup /dev/vdb
   No physical volume label  read  from  /dev/vdb
   Physical volume  /dev/vdb  not found
   Physical volume  "/dev/vdb"  successfully created
   Volume group  "VolGroup"  successfully extended
  
[root@localhost ~] # lvextend -l +100%FREE /dev/mapper/VolGroup-lv_root
   Extending logical volume lv_root to 13.50 GiB
   Logical volume lv_root successfully resized
   
[root@localhost ~] # resize2fs /dev/mapper/VolGroup-lv_root
resize2fs 1.41.12 (17-May-2010)
Filesystem at  /dev/mapper/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/mapper/VolGroup-lv_root  to 3539968 (4k) blocks.
The filesystem on  /dev/mapper/VolGroup-lv_root  is now 3539968 blocks long.
   
[root@localhost ~] # df -h
Filesystem                    Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root    14G  920M   12G   8% /
tmpfs                         372M     0  372M   0%  /dev/shm
/dev/vda1                      485M   32M  428M   7%  /boot


扩充磁盘有危险,请结合自己的环境及设置调整,先在测试机操作



重启方式添加磁盘文件扩充磁盘空间:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# qemu-img create -f qcow2 -o size=100G,preallocation=metadata /var/xen/centos3.qcow2
  
# qemu-img info centos3.qcow2
image: centos3.qcow2
file  format : qcow2
virtual size: 100G (107374182400 bytes)
disk size: 49G
cluster_size: 65536
#已经是100G空间的guest主机,下面再增大100G空间
  
# qemu-img resize centos3.qcow2 +100G                 #增加100G空间
# qemu-img info centos3.qcow2
image: centos3.qcow2
file  format : qcow2
virtual size: 200G (214748364800 bytes)
disk size: 49G
cluster_size: 65536
  
#img格式增大硬盘容量
dd  if = /dev/zero  bs=8k count=125000 >>  /data/centos65 .img
125000+0 records  in
125000+0 records out
1024000000 bytes (1.0 GB) copied, 14.1461 s, 72.4 MB /s

然后进入guest主机,如上Xen主机扩充磁盘的方式进行添加。

也可以进入guest主机,把新添加的磁盘文件分区后新建文件夹,进行挂载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# fdisk -l
# fdisk /dev/xvdb
# partx -a /dev/xvdb
BLKPG: Device or resource busy
error adding partition 1
# kpartx /dev/xvdb
xvdb1 : 0 4294961622  /dev/xvdb  63
# partx -a /dev/xvdb1 /dev/xvdb
# mkfs.ext4 /dev/xvdb1
# mkdir /data
# vim /etc/fstab
/dev/xvdb1  /data  ext4 defaults 0 0
# mount -a
# df -h


另:qcow2文件只能增大不能减小,lvm磁盘格式没有ext的速度快


增大磁盘容量方法是灵活的,谨慎操作










本文转自 bbotte 51CTO博客,原文链接:http://blog.51cto.com/bbotte/1630880,如需转载请自行联系原作者
目录
相关文章
|
21天前
|
存储 运维 Linux
KVM详解(三)——KVM创建虚拟机
KVM详解(三)——KVM创建虚拟机
25 6
|
19天前
|
运维 Linux KVM
KVM详解(八)——KVM虚拟机自启动
KVM详解(八)——KVM虚拟机自启动
18 5
|
9月前
|
存储 网络安全 KVM
|
11月前
|
XML Shell API
【KVM虚拟化】· virsh管理命令
【KVM虚拟化】· virsh管理命令
259 0
|
12月前
|
网络协议 Ubuntu 安全
使用KVM服务创建虚拟机
使用KVM服务创建虚拟机
1403 0
|
移动开发 Linux 网络安全
CentOS7下使用noVNC和websockify连接QEMU/KVM虚拟机
CentOS7下使用noVNC和websockify连接QEMU/KVM虚拟机
494 0
CentOS7下使用noVNC和websockify连接QEMU/KVM虚拟机
|
KVM 虚拟化 Linux
KVM 虚拟机安装
上传操作系统 qcow2格式创建,空间为可动态增长 qemu-img create -f qcow2 test02.img 7G virt-install --name=oeltest02 --os-variant=RHEL5.
1073 0