我在 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 命令解決了這個問題。範例,而不是
# truncate -s 20G centos-new.img
使用:
# qemu-img create -f qcow2 centos-new.img 20G
如果沒有進行此更改,來源磁碟是 qcow2,但新磁碟是原始磁碟。這讓qemu很困惑。