Saturday, June 8, 2013

expanding virtual machine's hard drive size

Here is my steps to expand my virtual machine's hard drive size. I use KVM. The guest OS is CentOS. My virtual machine does not use LVM.

  1. My VM does not have LVM group. great!
    lvdisplay.
  2. shutdown virtual machine and run command in host machine to resize virtual disc:
    qemu-img resize vmdisk.img +20G .
    This is probably only difference for different vitalization platform. Using different ways to resize image size.
  3. boot up virtual machine and run command to check hard disc size.
    fdisk -l .
  4. turn off swap:
    swapoff -a
  5. we use parted as partition tool. so call command
    parted
  6. check virtual hard drive's partition info as below.
    (parted) print free                                                       
    Model: ATA QEMU HARDDISK (scsi)
    Disk /dev/sda: 42.9GB
    Sector size (logical/physical): 512B/512B
    Partition Table: msdos
    
    Number  Start   End     Size    Type     File system     Flags
            32.3kB  1049kB  1016kB           Free Space
     1      1049kB  316MB   315MB   primary  ext4            boot
     2      316MB   19.4GB  19.0GB  primary  ext4
     3      19.4GB  21.5GB  2114MB  primary  linux-swap(v1)
    
            21.5GB  42.9GB  21.5GB           Free Space
    
  7. remove swap partition.
    (parted) rm 3
  8. set unit to be KB to avoid wasting too much space.
    (parted) unit kb
  9. make a primary partition in the unallocated space.
    (parted) mkpart primary 19360907kB 38949673kB
  10. Make all rest unallocated space as swap partition.
    mkpart primary 38949673kB 42949670kB
  11. reboot virtual machine
  12. turn off swap
    swapoff -a
  13. Format new created partition as ext4
    mkfs.ext4 /dev/sda3
  14. Format swap partition
    mkswap /dev/sda4
  15. I will mount /home direction to new partion (sda3). So, I backup my home directory first and create new /home directory.
    mv /home /homebackup
    mkdir home
  16. double check partition info again.
    cat /proc/partitions
  17. check the auto mount configurion.
    cat /etc/fstab
  18. Format swap partition
    mkswap /dev/sda4
  19. find out sda3 UUID
    blkid /dev/sda3
  20. add sda3 into auto mount
    vi /etc/fstab
    add line: UUID=e4805e9c-869a-48d5-8783-0bfcb5b65a3e /home ext4 defaults 1 3
  21. reboot
  22. move old home directory content into new home directory
    cp -rf /homebackup/* /home/
  23. change the ownership of content to different user account. for example,
    chown -R yiyujia:yiyujia ./yiyujia/
  24. check disc usage info,
    df

Also, adding a new disck to existing virtual guest could be good option too.
  1. cd into folder to find guest's configuration file as xml and backup it.
    /etc/libvirt/qemu
  2. create a new raw disk image.
    qemu-img create -f raw newDisk.img 20G
  3. Attach newly created disk image
    #virsh attach-disk
  4. login guest machine and partition new disck.

No comments:

Post a Comment