raw磁盘扩展和qcow2磁盘格式扩展
添加一块raw格式的磁盘加入kvm虚拟机,然后通过虚拟机系统lvm可进行扩展管理。直接通过dd命令扩展现有虚拟机磁盘大小,扩展之后,原磁盘大小增大,进入虚拟机系统后,直接通过fdisk分区新添加的空间,然后将该分区并入lvm逻辑卷中,扩大磁盘空间。
qcow2格式扩展可以采用raw磁盘格式磁盘的扩展方式一致的方式进行。qcow2格式磁盘,直接通过qemu-img 直接扩展qcow2磁盘, 新添加一块raw格式的磁盘加入到KVM虚拟机,然后通过虚拟机系统lvm逻辑卷管理方式进行管理,扩展磁盘空间。
下面通过qcow2格式添加磁盘
先查看虚拟机磁盘格式
[root@e3 images]# virsh edit centos6.5 #查看虚拟机硬盘格式 <disk type='file' device='disk'> <driver name='qemu' type='qcow2' cache='none'/> <source file='/data/kvm/images/centos6.5.qcow2'/>
查看虚拟机硬盘情况
[root@e3 images]# virt-df -d centos6.5 Filesystem 1K-blocks Used Available Use% centos6.5:/dev/sda1 487652 25447 432509 6% centos6.5:/dev/VolGroup/lv_root 19003260 682092 17332804 4% #硬盘一共20G大小
创建一块虚拟硬盘并且添加
[root@e3 images]# qemu-img create -f qcow2 centos6.5-add.qcow2 5G #生成5G大小的qcow2 Formatting 'centos6.5-add.qcow2', fmt=qcow2 size=5368709120 encryption=off cluster_size=65536 lazy_refcounts=off [root@e3 images]# ll 总用量 11813828 -rw-r--r--. 1 root root 197120 1月 21 17:08 centos6.5-add.qcow2 #生成的 -rwxr-xr-x. 1 root root 21474836480 1月 21 15:05 centos6.5.img -rwxr-xr-x. 1 root root 21474836480 1月 19 16:13 CENTOS6.5.img -rw-r--r--. 1 qemu qemu 1552154624 1月 21 17:08 centos6.5.qcow2 -rwxr-xr-x. 1 qemu qemu 21474836480 1月 21 13:12 centos6.6.img -rwxr-xr-x. 1 root root 21474836480 1月 21 12:36 kvm001.img -rwxr-xr-x. 1 root root 21474836480 1月 21 11:31 kvm002.img -rwxr-xr-x. 1 root root 21474836480 1月 21 14:57 kvm003.img -rwxr-xr-x. 1 root root 21474836480 1月 21 11:26 template.img 关闭虚拟机添加硬盘信息到配置文件 [root@e3 images]# virsh list --all Id 名称 状态 ---------------------------------------------------- 29 centos6.6 running 35 centos6.5 running - kvm001 关闭 - kvm002 关闭 - kvm003 关闭 - template 关闭 [root@e3 images]# virsh shutdown centos6.5 域 centos6.5 被关闭 [root@e3 images]# virsh destroy centos6.5 域 centos6.5 被删除 [root@e3 images]# virsh list --all Id 名称 状态 ---------------------------------------------------- 29 centos6.6 running - centos6.5 关闭 - kvm001 关闭 - kvm002 关闭 - kvm003 关闭 - template 关闭 [root@e3 images]# virsh edit centos6.5 #编辑主配置文件添加如下类容 <disk type='file' device='disk'> <driver name='qemu' type='qcow2' cache='none'/> <source file='/data/kvm/images/centos6.5-add.qcow2'/> #注意路径 <target dev='hdb' bus='ide'/> <address type='drive' controller='0' bus='0' target='0' unit='1'/> </disk>
启动虚拟机
[root@e3 images]# virsh start centos6.5 #启动虚拟机 域 centos6.5 已开始 [root@e3 images]# virsh console centos6.5 #连接虚拟机 连接到域 centos6.5 换码符为 ^] Freeing initrd memory: 15772k freed [root@kvm003 ~]# fdisk -l #查看你的分区情况 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: 0x000903e2 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. Disk /dev/sda: 5368 MB, 5368709120 bytes #看到添加的5G了,识别成scsi设备 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 Disk /dev/mapper/VolGroup-lv_root: 19.9 GB, 19906166784 bytes 255 heads, 63 sectors/track, 2420 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
配置lvm扩展磁盘
#分区 [root@kvm003 ~]# fdisk /dev/sda Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0xe7901370. Changes will remain in memory only, until you decide to write them. After that, of course, the previous content won't be recoverable. Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite) 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): 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): L 0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris 1 FAT12 39 Plan 9 82 Linux swap / So c1 DRDOS/sec (FAT- 2 XENIX root 3c PartitionMagic 83 Linux c4 DRDOS/sec (FAT- 3 XENIX usr 40 Venix 80286 84 OS/2 hidden C: c6 DRDOS/sec (FAT- 4 FAT16 <32M 41 PPC PReP Boot 85 Linux extended c7 Syrinx 5 Extended 42 SFS 86 NTFS volume set da Non-FS data 6 FAT16 4d QNX4.x 87 NTFS volume set db CP/M / CTOS / . 7 HPFS/NTFS 4e QNX4.x 2nd part 88 Linux plaintext de Dell Utility 8 AIX 4f QNX4.x 3rd part 8e Linux LVM df BootIt 9 AIX bootable 50 OnTrack DM 93 Amoeba e1 DOS access a OS/2 Boot Manag 51 OnTrack DM6 Aux 94 Amoeba BBT e3 DOS R/O b W95 FAT32 52 CP/M 9f BSD/OS e4 SpeedStor c W95 FAT32 (LBA) 53 OnTrack DM6 Aux a0 IBM Thinkpad hi eb BeOS fs e W95 FAT16 (LBA) 54 OnTrackDM6 a5 FreeBSD ee GPT f W95 Ext'd (LBA) 55 EZ-Drive a6 OpenBSD ef EFI (FAT-12/16/ 10 OPUS 56 Golden Bow a7 NeXTSTEP f0 Linux/PA-RISC b 11 Hidden FAT12 5c Priam Edisk a8 Darwin UFS f1 SpeedStor 12 Compaq diagnost 61 SpeedStor a9 NetBSD f4 SpeedStor 14 Hidden FAT16 <3 63 GNU HURD or Sys ab Darwin boot f2 DOS secondary 16 Hidden FAT16 64 Novell Netware af HFS / HFS+ fb VMware VMFS 17 Hidden HPFS/NTF 65 Novell Netware b7 BSDI fs fc VMware VMKCORE 18 AST SmartSleep 70 DiskSecure Mult b8 BSDI swap fd Linux raid auto 1b Hidden W95 FAT3 75 PC/IX bb Boot Wizard hid fe LANstep 1c Hidden W95 FAT3 80 Old Minix be Solaris boot ff BBT 1e Hidden W95 FAT1 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/sda: 5368 MB, 5368709120 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: 0xe7901370 Device Boot Start End Blocks Id System /dev/sda1 1 652 5237158+ 8e Linux LVM Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. sda: sda1 Syncing disks. #扩展lvm卷 [root@kvm003 ~]# pvcreate /dev/sda1 # 创建PV Physical volume "/dev/sda1" successfully created [root@kvm003 ~]# pvs # 查看pv PV VG Fmt Attr PSize PFree /dev/sda1 lvm2 --- 4.99g 4.99g /dev/vda2 VolGroup lvm2 a-- 19.51g 0 [root@kvm003 ~]# vgextend VolGroup /dev/sda1 # 把pv增加到VolGroup Volume group "VolGroup" successfully extended [root@kvm003 ~]# lvs # LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lv_root VolGroup -wi-ao---- 18.54g lv_swap VolGroup -wi-ao---- 992.00m [root@kvm003 ~]# lvextend -l +100%FREE /dev/VolGroup/lv_root # 增加剩余的所用空间 Size of logical volume VolGroup/lv_root changed from 18.54 GiB (4746 extents) to 23.53 GiB (6024 extents). Logical volume lv_root successfully resized [root@kvm003 ~]# 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 = 2, new_desc_blocks = 2 Performing an on-line resize of /dev/VolGroup/lv_root to 6168576 (4k) blocks. The filesystem on /dev/VolGroup/lv_root is now 6168576 blocks long. [root@kvm003 ~]# df -TH #查看大小 Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root ext4 25G 699M 23G 3% / tmpfs tmpfs 258M 0 258M 0% /dev/shm /dev/vda1 ext4 500M 27M 448M 6% /boot 或者这样查看 [root@e3 images]# virt-df -d centos6.5 Filesystem 1K-blocks Used Available Use% centos6.5:/dev/sda1 487652 25447 432509 6% centos6.5:/dev/VolGroup/lv_root 24156028 682160 22228340 3%
直接扩展
删除刚在配置文件里的信息
[root@e3 images]# virsh shutdown centos6.5 域 centos6.5 被关闭 [root@e3 images]# virsh destroy centos6.5 域 centos6.5 被删除 删除刚才配置文件里添加的硬盘信息并恢复虚拟机 [root@e3 images]# virsh snapshot-list centos6.5 名称 生成时间 状态 ------------------------------------------------------------ 1421829141 2015-01-21 16:32:21 +0800 shutoff 1421829551 2015-01-21 16:39:11 +0800 shutoff [root@e3 images]# virsh snapshot-current centos6.5 <domainsnapshot> <name>1421829141</name> 我恢复到1421829551这个点 [root@e3 images]# virsh snapshot-revert centos6.5 1421829551 [root@e3 images]# virsh snapshot-current centos6.5 <domainsnapshot> <name>1421829551</name> #可以看到恢复到了这个点 [root@e3 images]# virsh edit centos6.5 <devices> <emulator>/usr/libexec/qemu-kvm</emulator> <disk type='file' device='disk'> <driver name='qemu' type='qcow2' cache='none'/> # <source file='/data/kvm/images/centos6.5.qcow2'/> # <target dev='vda' bus='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/> </disk> #查看现在虚拟机磁盘的大小 [root@e3 images]# virt-df -d centos6.5 Filesystem 1K-blocks Used Available Use% centos6.5:/dev/sda1 487652 25447 432509 6% centos6.5:/dev/VolGroup/lv_root 19003260 682092 17332804 4% [root@e3 images]# qemu-img info centos6.5.qcow2 image: centos6.5.qcow2 file format: qcow2 virtual size: 20G (21474836480 bytes) disk size: 1.4G cluster_size: 65536 Snapshot list: ID TAG VM SIZE DATE VM CLOCK 1 1421829141 0 2015-01-21 16:32:21 00:00:00.000 2 1421829551 0 2015-01-21 16:39:11 00:00:00.000 Format specific information: compat: 1.1 lazy refcounts: false [root@e3 images]# qemu-img resize centos6.5.qcow2 +10G #这个要删除快照,才可以直接扩展 Image resized. [root@e3 images]# qemu-img info centos6.5.qcow2 image: centos6.5.qcow2 file format: qcow2 virtual size: 30G (32212254720 bytes) #扩展成功 disk size: 1.4G cluster_size: 65536 Format specific information: compat: 1.1 lazy refcounts: false [root@kvm003 ~]# fdisk -l #查看好像直接扩展了10G Disk /dev/vda: 32.2 GB, 32212254720 bytes #本来是20G,现在是30多G 16 heads, 63 sectors/track, 62415 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: 0x000903e2
然后分区,扩展这就不多说了