我在 Ubuntu 12.04 amd64 主机上通过 KVM 安装了一个 CentOS 6.4 映像。以下是 centos.img 的磁盘布局
# virt-filesystems --long -h --all -a centos.img
Name Type VFS Label MBR Size Parent
/dev/sda1 filesystem ext4 - - 500M -
**/dev/vg_centos/lv_root filesystem ext4 - - 2.5G -**
/dev/vg_centos/lv_swap filesystem swap - - 2.0G -
/dev/vg_centos/lv_root lv - - - 2.5G /dev/vg_centos
/dev/vg_centos/lv_swap lv - - - 2.0G /dev/vg_centos
/dev/vg_centos vg - - - 4.5G -
/dev/sda2 pv - - - 4.5G -
/dev/sda1 partition - - 83 500M /dev/sda
/dev/sda2 partition - - 8e 4.5G /dev/sda
/dev/sda device - - - 5.0G -
我想给 /root 添加更多空间,所以我尝试使用 virt-resize。我创建了一个 20G 的 img,然后通过以下方式调整 /root 的大小:
# truncate -s 20G centos-new.img
# virt-resize centos.img centos-new.img --expand /dev/sda2 --LV-expand /dev/vg_centos/lv_root
输出看起来不错
**********
Setting up initial partition table on centos-new.img ...
Copying /dev/sda1 ...
100% ⟦▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓⟧ 00:00
Copying /dev/sda2 ...
100% ⟦▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓⟧ 00:00
Expanding /dev/sda2 using the 'pvresize' method ...
Expanding /dev/vg_centos/lv_root using the 'resize2fs' method ...
我检查了新虚拟机的布局并且没有问题:
# virt-filesystems --long -h --all -a centos-new.img
Name Type VFS Label MBR Size Parent
/dev/sda1 filesystem ext4 - - 500M -
**/dev/vg_centos/lv_root filesystem ext4 - - 18G -**
/dev/vg_centos/lv_swap filesystem swap - - 2.0G -
/dev/vg_centos/lv_root lv - - - 18G /dev/vg_centos
/dev/vg_centos/lv_swap lv - - - 2.0G /dev/vg_centos
/dev/vg_centos vg - - - 20G -
/dev/sda2 pv - - - 20G -
/dev/sda1 partition - - 83 500M /dev/sda
/dev/sda2 partition - - 8e 20G /dev/sda
/dev/sda device - - - 20G -
调整大小操作已完成且没有错误。在删除旧磁盘之前,请仔细检查调整大小后的磁盘是否能够正常启动和运行。
然后我使用 virsh 在 KVM 上定义一个新的虚拟机。我将当前 centos.img 的配置文件复制到 /var/libvirt/qemu 中并编辑它,然后定义虚拟机。一切正常,但我无法启动 centos-new。以下是日志
# virsh start centos-new
error: Failed to start domain centos-new
error: internal error process exited while connecting to monitor: char device redirected to /dev/pts/13
kvm: -drive file=/home/tungns/VirtualBox VMs/centos-new.img,if=none,id=drive-ide0-0-0,format=qcow2: could not open disk image /home/tungns/VirtualBox VMs/centos-new.img: Invalid argument
这是最后一部分/var/log/libvirt/libvirtd.log:
2013-06-07 10:59:33.391+0000: 5553: error : qemuMonitorOpenUnix:295 : failed to connect to monitor socket: No such process
2013-06-07 10:59:33.392+0000: 5553: error : qemuProcessWaitForMonitor:1301 : internal error process exited while connecting to monitor: char device redirected to /dev/pts/13
kvm: -drive file=/home/tungns/VirtualBox VMs/centos-new.img,if=none,id=drive-ide0-0-0,format=qcow2: could not open disk image /home/tungns/VirtualBox VMs/centos-new.img: Invalid argument
以及来自/var/log/libvirt/qemu/centos-new.log:
2013-06-07 10:59:32.712+0000: starting up
LC_ALL=C PATH=/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin QEMU_AUDIO_DRV=none /usr/bin/kvm -S -M pc-1.0 -enable-kvm -m 1024 -smp 1,sockets=1,cores=1,threads=1 -name centos-new -uuid 1acf632e-cea2-4754-8f36-30799fecc3d0 -nodefconfig -nodefaults -chardev socket,id=charmonitor,path=/var/lib/libvirt/qemu/centos-new.monitor,server,nowait -mon chardev=charmonitor,id=monitor,mode=control -rtc base=utc -no-shutdown -drive file=/home/tungns/VirtualBox VMs/centos-new.img,if=none,id=drive-ide0-0-0,format=qcow2 -device ide-drive,bus=ide.0,unit=0,drive=drive-ide0-0-0,id=ide0-0-0,bootindex=1 -drive if=none,media=cdrom,id=drive-ide0-1-0,readonly=on,format=raw -device ide-drive,bus=ide.1,unit=0,drive=drive-ide0-1-0,id=ide0-1-0 -netdev tap,fd=18,id=hostnet0 -device rtl8139,netdev=hostnet0,id=net0,mac=00:16:3e:76:04:23,bus=pci.0,addr=0x3 -chardev pty,id=charserial0 -device isa-serial,chardev=charserial0,id=serial0 -usb -vnc 0.0.0.0:0 -vga cirrus -device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x4
char device redirected to /dev/pts/13
kvm: -drive file=/home/tungns/VirtualBox VMs/centos-new.img,if=none,id=drive-ide0-0-0,format=qcow2: could not open disk image /home/tungns/VirtualBox VMs/centos-new.img: Invalid argument
2013-06-07 10:59:33.392+0000: shutting down
两个虚拟机位于同一个文件夹中,具有相同的权限和所有者。centos.img 正常启动。
784460 -rw-r--r-- 1 root root 803405824 Jun 7 16:20 centos.img
5468088 -rw-r--r-- 1 root root 21474836480 Jun 7 17:17 centos-new.img
我读过很多这样的指南:http://n40lab.wordpress.com/2013/05/29/resizing-a-qemu-kvm-linux-image-using-virt-resize-in-centos-6-4/并且说明也相同。
有什么建议吗?谢谢大家。
答案1
我设法通过更改 qemu-create 的截断命令解决了这个问题。例如,
# truncate -s 20G centos-new.img
使用:
# qemu-img create -f qcow2 centos-new.img 20G
如果没有这个更改,源磁盘就是 qcow2,但新磁盘是原始的。这让 qemu 感到困惑。