next up previous contents
Next: Windows 軟體安裝 Up: 管理 KVM 虛擬機 Previous: 管理 KVM 虛擬機   Contents


  1. 若要安裝更多套件,但不要重新安裝虛擬機,可利用下列方式加大虛擬機硬碟。
  2. 先安裝加大虛擬機硬碟所需工具套件 libguestfs-tools-c。
    [root@dywH ~]# yum install libguestfs-tools-c
  3. 如果要支援 windows 的檔案格式,還要安裝工具套件 libguestfs-winsupport,不過經過測試 ntfs 放大還有問題。因此仍以 linux 的檔案格式做例子。
    [root@dywH ~]# yum install libguestfs-winsupport
  4. 查詢 kvmusb 虛擬機的硬碟分割狀況,硬碟大小為 1.7G,目標是將其 /dev/sda2 1.2G 增大至 2.5G,也就是整顆硬碟由 1.7G 放大至 4G。
    [root@dywH ~]# virt-filesystems --long --parts --blkdevs -h -a /var/lib/libvirt/images/kvmusb.qcow2 
    Name       Type       MBR  Size  Parent
    /dev/sda1  partition  83   96M   /dev/sda
    /dev/sda2  partition  8e   1.2G  /dev/sda
    /dev/sda3  partition  8e   130M  /dev/sda
    /dev/sda   device     -    1.7G  -
  5. 先產生一個 4G 的映像檔 out.qcow2。
    [root@dywH ~]# qemu-img create -f qcow2 out.qcow2 4G
    Formatting 'out.qcow2', fmt=qcow2 size=4294967296 encryption=off cluster_size=65536
  6. 將 kvmusb 虛擬機硬碟 /dev/sda2 由 1.2G 加大到 2.5G。
    [root@dywH ~]# virt-resize --expand /dev/sda2 /var/lib/libvirt/images/kvmusb.qcow2 out.qcow2 
    Examining /var/lib/libvirt/images/kvmusb.qcow2 ...
    Summary of changes:
    /dev/sda1: This partition will be left alone.
    /dev/sda2: This partition will be resized from 1.2G to 3.8G.  The LVM 
        PV on /dev/sda2 will be expanded using the 'pvresize' method.
    /dev/sda3: This partition will be left alone.
    Setting up initial partition table on out.qcow2 ...
    Copying /dev/sda1 ...
     100% [-----------------------------------------------------------------] 00:00
    Copying /dev/sda2 ...
     100% [-----------------------------------------------------------------] 00:00
    Copying /dev/sda3 ...
     100% [-----------------------------------------------------------------] 00:00
    Expanding /dev/sda2 using the 'pvresize' method ...
    Resize operation completed with no errors.  Before deleting the old 
    disk, carefully check that the resized disk boots and works correctly.
  7. 如果就把原先的 kvmusb.qcow2 取代成 out.qcow2,虛擬機開機後查看硬碟,還是沒變,必須使用 guestfish 工具,進入互動式管理虛擬機的檔案系統。
    [root@dywH ~]# guestfish --rw -a out.qcow2 
    Welcome to guestfish, the libguestfs filesystem interactive shell for
    editing virtual machine filesystems.
    Type: 'help' for help on commands
          'man' to read the manual
          'quit' to quit the shell
    ><fs> run
    ><fs> list-filesystems 
    /dev/sda1: ext4
    /dev/vg_kvmhome/vo: ext4
    /dev/vg_kvmusb/root: ext4
    /dev/vg_kvmusb/swap: swap
    ><fs> lvresize
    lvresize       lvresize-free  
    ><fs> help lvresize-free 
        lvresize-free - expand an LV to fill free space
         lvresize-free lv percent
        This expands an existing logical volume "lv" so that it fills "pc"% of
        the remaining free space in the volume group. Commonly you would call
        this with pc = 100 which expands the logical volume as much as possible,
        using all remaining free space in the volume group.
    ><fs> lvresize-free /dev/vg_kvmusb/root 100
    ><fs> resize2fs /dev/vg_kvmusb/root 
    ><fs> e2fsck-f /dev/vg_kvmusb/root 
    ><fs> exit
  8. 查看處理後的硬碟分割狀況,out.qcow2 中的檔案系統 /dev/vg_kvmusb/root/ 已經放大至 3.7G。
    [root@dywH ~]# virt-df -h out.qcow2 
    Filesystem                                Size       Used  Available  Use%
    out.qcow2:/dev/sda1                        93M        25M        63M   28%
    out.qcow2:/dev/vg_kvmhome/vo               97M       5.6M        86M    6%
    out.qcow2:/dev/vg_kvmusb/root             3.7G       849M       2.7G   23%
  9. 將原始的映像檔 kvmusb.qcow2 移走或刪除。
    [root@dywH ~]# mv /var/lib/libvirt/images/kvmusb.qcow2 /tmp
  10. 以新的 out.qcow2 映像檔取代舊的。
    [root@dywH ~]# mv out.qcow2 /var/lib/libvirt/images/kvmusb.qcow2
  11. 啟動 kvmusb 虛擬機。
    [root@dywH ~]# virsh start kvmusb
  12. 登入查看 lv vg_kvmusb-root 已經變成 3.7G。
    [root@dywH ~]# ssh's password: 
    Last login: Thu Dec  4 19:26:47 2014 from
    [root@kvm8 ~]# df -h
    Filesystem            Size  Used Avail Use% Mounted on
                          3.7G  849M  2.7G  24% /
    tmpfs                 499M     0  499M   0% /dev/shm
    /dev/vda1              93M   26M   63M  29% /boot
                           97M  5.6M   87M   7% /home

De-Yu Wang 2019-02-23