Resizing an Image

From CSLabsWiki
Jump to: navigation, search


This How To covers the process of resizing an image created using the steps in Create a New VM.

All of these steps are performed on the dom0 that the VM is currently running on.

Preparation

  • Shutdown the current image
    • xm shutdown IMAGENAME
  • Create Backup Copy
    • mkdir /xen/temp
    • cp /xen/images/IMAGENAME.disk /xen/temp/

Resizing the Image

  • Create image of new size
    • dd if=/dev/zero of=/xen/temp/new_IMAGENAME.disk bs=1024k count=1 seek=NEW_SIZE_IN_MB
  • Partition the new image, mount the images, and copy the files, preserving permissions
    • losetup -f to get next open loopback device. (the following commands assume it’s loopX)
    • losetup /dev/loopX /xen/temp/new_IMAGENAME.disk
    • losetup -f to get next open loopback device. (the following commands assume it’s loopY)
    • losetup /dev/loopY /xen/temp/IMAGENAME.disk
    • fdisk /dev/loopX (Create swap and root partitions. The actual commands are beyond the scope of this How-To)
    • kpartx -av /dev/loopX
    • kpartx -av /dev/loopY
    • mkswap /dev/mapper/loopXp1 (assumes swap partition is 1)
    • mkfs.ext3 /dev/mapper/loopXp2 (assumes root partition is 2)
    • mkdir /mnt/{old,new}
    • mount /dev/mapper/loopXp2 /mnt/new (assumes root partition is 2)
    • mount /dev/mapper/loopYp2 /mnt/old (assumes root partition is 2)
    • cp -pr /mnt/old/* /mnt/new
    • umount /mnt/new
    • umount /mnt/old
    • kpartx -d /dev/loopX
    • kpartx -d /dev/loopY
    • losetup -d /dev/loopX
    • losetup -d /dev/loopY

Put the new image in place

  • Move the new image into place
    • mv /xen/images/{,old_}IMAGENAME.disk
    • mv /xen/{temp/new_,images/}imagename.disk
  • Start the image
    • xm create /xen/confs/IMAGENAME

Cleanup

  • If everything works, clean up after yourself
    • rm -rf /xen/temp
    • rmdir /mnt/new
    • rmdir /mnt/old
    • rm /xen/images/old_IMAGENAME.disk