Home Of The McMuffin

Posts Tagged ‘VM’

Shrinking a Linux VMware image disk size

VMware workstation, Infrustructure and other VWware tools have capabilities built in for resizing your disk when running a Windows VM.  However, there is no such automated support for changing especially reducing the size of your VM disk for Linux VMs.  I have recently been able to accomplish shrinking a Linux VMware disk from 80GB to 10 using the process outlined below.  I have not found a similar process documented online thus far.

Please note, that I have tested this process only twice and in no way do I standby the results.  Please make sure when doing any changes to your system that you always make backup copies!!!  If you find this useful or have alternative methods for accomplishing such changes please post them or correct me where I may have missed a step in this documentation.

Preparation for disk changes

Gracefully shutdown your real source VMware image. Copy your existing VMware image folder, to another folder.  Open the image in VMware workstation and remove all of the snapshot files.  This new copy will now be referred to as your source image/disk.

Using the VMware Workstation or similar application defragment your source virtual disk.

Download a copy of SystemRescueCD ISO. (This is a CD bootable Gentoo Linux distro with many recovery applications)

RESIZE LINUX PARTITIONS

This step does not resize VMware virtual disks, that part is further down.

You may need to first shrink the partitions on your source disk so they are the size that you want them to be iny our final VMware image. Before using the process below you must copy your existing VMware image directory to a new backup copy directory and remove any snapshots.  This will be your new source image.

Get and save current file system info from each of these commands:  df, “df -B 4k”, “df -h”, “fdisk -l”, “fdisk -s /dev/sda1″ (or equivalent root partition).

    • Shutdown the system and boot using CD ISO file: SystemRescueCD  (Knoppix also will work).
    • After shutdown use VMware Workstation and edit the source image so that the CD/DVD is added at power on.

    • Also ensure that the CD/DVD is configured to use a bootable Linux CD ISO such as SystemRescueCD or Knoppix

    • Start the VMware image. (This part is tricky as you have to be very fast): As soon as you see the black and white VMware screen click the screen with your mouse and immediately hit escape to break out into the Bios.  The VMware window only flashes up for about one second so if you don’t get it you may need to try again, although then using <ctrl+alt+ins> and then <escape> afterwards may also work.

    Once your bootable Linux is started

      • ensure that the partitions that you are going to adjust are not mounted.
      • fsck -n /dev/<partition>  (Copy output to your notes)

      • tune2fs -O ^has_journal /dev/<partition>  (Removes the journal from the partition)

      • e2fsck -f /dev/<partition>  (Copy output to your notes)

      • resize2fs /dev/<partition> 6000M   (!!!! Take note of block size and number of blocks!!!)

      • fdisk /dev/<root disk partition> {i.e. /dev/sda} (delete our partition and creates a new smaller partition)
        • type m to get a list of commands
        • d  (to delete a partition)
        • partition number: X (sda1 =1, sda2=2 ….)
        • n (for new)
        • P (for real partition)
        • partition number: X
        • first cylinder:  (value for partition we saved from fdisk -l output earlier)
        • last cylinder:  (Can specify size in KB, calculated: <amount of blocks from resize2fs output> * <size of a block (4k)> * 1.03    (answer will look like: +3164160K)
        • w (write partition table)
        • Add the ext3 journal systems back to the partition using tune2fs -j /partition/name

      COPY LINUX DISKS TO NEW VMware INSTANCE TO RESIZE DISKS (whole disk)

      NEW DESTINATION IMAGE

      • Create a new Linux VMware image in Workstation that has the disk size that you wish to end up with.

      • Edit the CD/DVD so it uses a bootable Linux ISO image to boot from (SystemRescueCD or Knoppix)

      • Boot the new image

      • Use ifconfig to set an IP, the only requirement here is that it be in the same subnet that you will have your source VMware connected to while doing the backups.

      • Assuming your destination disk is /dev/sda  run the following command to listen for and copy the backup data to your drive:

      • Set netcat listener with binding to dd. nc -l -p 10001 | dd of=/dev/sda bs=4k

      OLD SOURCE IMAGE

      • Gracefully shutdown your source VMware image

      • Using Workstation defragment your virtual drive

      • Configure the image to use the CD/DVD at power on

      • Configure the CD/DVD to mount a bootable Linux ISO  (SystemRescueCD or Knoppix)

      • Boot the system using the CD (This can be tricky.  Have to quickly click on the bios screen and hit escape.  If missed try <ctrl+shift+ins> then escape.  If not then restart again and be faster)

      • Once booted, set the interface IP address to be in the same subnet as the destination image.  (test with ping)

      • Assuming your source drive is /dev/sda, start the backup to destination using the following command

      • dd if=/dev/sda bs=4k conv=sync,noerror | nc <dst-ip-address> 10001 (This starts DD bit-by-bit copy process and pipes the bits from standard out to netcat over to a maching at <dst-ip-address> which is running a netcat listener.

      Notes:  If you can’t get network between the two, you can use a USB drive by creating a new dir i.e. /mnt/ext and mounting it to /dev/sdb1 in most cases.

      Share and Enjoy:
      • Digg
      • Twitter
      • RSS
      • Facebook
      • del.icio.us
      • email
      • StumbleUpon
      • Reddit