Xen或者KVM線上主機的磁盤空間不足,須要增大硬盤,而且不能重啓,這時候就須要熱添加以擴充磁盤空間vim
說明:Xen和KVM主機都爲CentOS 6.4 guest磁盤爲lvm格式centos
先來講Xen吧,操做都同樣的,虛擬機上測試bash
Xen的guest主機擴充根分區:app
# 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的磁盤 p_w_picpath: 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 #查看新建的磁盤 p_w_picpath: 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內的操做,先對其分區
dom
# 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磁盤分區ide
[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熱擴充磁盤空間測試
[root@localhost ~]# virsh list Id Name State ---------------------------------------------------- 1 bbotte running # qemu-img info bbotte.qcow2 p_w_picpath: 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主機裏面,會看到以下提示ui
# 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
進行磁盤擴充
spa
# 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
擴充磁盤有危險,請結合本身的環境及設置調整,先在測試機操做rest
重啓方式添加磁盤文件擴充磁盤空間:
# qemu-img create -f qcow2 -o size=100G,preallocation=metadata /var/xen/centos3.qcow2 # qemu-img info centos3.qcow2 p_w_picpath: 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 p_w_picpath: 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主機,把新添加的磁盤文件分區後新建文件夾,進行掛載
# 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的速度快
增大磁盤容量方法是靈活的,謹慎操做