Tip: How to compact your virtual disks when using Linux guests

Discussion in 'Linux Guest OS Discussion' started by ralphrmartin, Sep 11, 2008.

  1. ralphrmartin

    ralphrmartin

    Messages:
    58
    Seems I posted my tip in the wrong forum! Repeated here, where it really should be; only tested on MacOS X host but ought to work anywhere:

    Here is a tip which might be helpful to people using Linux guest operating systems.

    Eventually, with use, your expanding virtual hard disk will grow quite a lot bigger than the amount of data on it. This is how to reduce the disk back down to a small size. There are 2 steps

    (1) Within Linux, you need to zero all free blocks. This can be done as follows:

    Install the "zerofree" package using whatever package manager is appropriate for your flavour of Linux.

    Boot your Linux VM in single user mode.
    Type the following commands (which switch to level 1, so we can remount the disk as read-only, check the disk before zeroing the free blocks, then finally remount the disk as read-write and shutdown).

    init 1
    mount -n -o remount,ro -t ext3 /dev/hda1 /
    fsck.ext3 -f /dev/hda1
    zerofree /dev/hda1
    mount -o remount,rw -t ext3 /dev/hda1 /
    shutdown -h now

    (2) Within your Parallels application, choose your virtual machine, click on the disk's configuration tab, and under advanced, choose "compact".

    You can now reboot your Linux VM, with its streamlined disk.
     
  2. boecko

    boecko

    Messages:
    2
    use buildmap

    That didn't work for me. (linux.hdd with ext4 on it)
    "Compact" returned immediately with no space gain.

    However it works over the command line.

    (1) step one is the same

    (2)
    - go to the Folder, where the .hdd - Directory is located (yes, it's an dir)
    e.g
    Code:
    $ cd /Users/Shared/Parallels/
     or
    $ cd /Users/Shared/Parallels/<linux name>.pvm/
    
    # check
    $ ls -ld *.hdd
    drwxrwxrwx@ 8 user  staff  272 12 Nov 11:49 linuxhome.hdd
    $ du -ksh linuxhome.hdd
    20,0G	linuxhome.hdd
    
    - compact via prl_disk_tool
    Code:
    $ prl_disk_tool compact --buildmap --hdd linuxhome.hdd/
    Operation progress 100 %   
    $ du -ksh linuxhome.hdd
    8,3G	linuxhome.hdd
    
    The important parts are --buildmap and the "/"

    The hdd-Image has been shrinked from 20GB to 8.3GB, which comes pretty close to the actual size inside the VM

    Code:
    root@yoda:~# df -h
    Filesystem            Size  Used Avail Use% Mounted on
    …
    /dev/sdb1              61G  7.4G   51G  13% /mnt/user
    
     
  3. trixie

    trixie

    Messages:
    12
    Has anyone successfully entered single user mode in Fedora Core 15? When I go into single user mode via ``init 1'', the keyboard inputs after the new shell prompt are garbled. Maybe run level one is too low for a meaningful keyboard user interface to be supported? Also, I noticed that when I unhide the grub menu in grub.conf that the menu appears maybe for a split second without providing any chance of making a selection and proceeds instantly towards a full run boot.

    Update: OK, I got it to work. I went into full screen mode, then did an ``init 2'' and then an ``init 1''.
     
    Last edited: Dec 28, 2011
  4. RobertoF1

    RobertoF1 Bit Poster

    Messages:
    9
    There is actually a much simpler way. You can zero all free bytes using dd with this:
    dd if=/dev/zero of=deleteme bs=1M
    once all the free space has been zero'd out, you'll get an error message saying:
    dd: writing 'deleteme': No space left on device

    You can now shut down the Linux guest and use Parallel's "Compress" option in the Hardware - Hard Disk options for the guest.
     
  5. TonyG3

    TonyG3 Bit Poster

    Messages:
    2
    I know it's a 9-year-old post, but does this still work for a newer version of linux? When I test the code on Ubuntu 16.04 and type in
    mount -n -o remount,ro -t ext3 /dev/hda1 /
    All I get is:
    mount: / is busy
    or did I miss anything?
     
  6. AmirA4

    AmirA4 Bit Poster

    Messages:
    1
    For the sake of anyone coming across this in the future:
    The base of the instructions are good and work on modern Ubuntu, however some things have changed which give rise to some gotchas.
    1) The OP says "boot to single user" - see here for how to do this with GRUB on modern Ubuntu.
    2) For some reason even in single user mode and after init 1 - dhclient is still running, and keeps a write file handle open (probably for log file) - kill is manually in the usual way (ps / kill).
    3) If you are still getting busy error, use fuser -v -m / to look for the culprits (it's actually better to do fuser -v -m / 2>&1 | less -S): search for those processes that in the second column the first letter is a uppercase F.
    4) Pay attention to the file system type and device, on a modern default installation Ubuntu it would be ext4 and the device on my Parallels 11 is /dev/mapper/ubuntu--vg-root.
    Apply the additional instructions by the 2nd poster (boecko): use command line prl_disk_tool.
    This was tested on an Ubuntu 16.04 Guest.
     

Share This Page