Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've been provided a .tar.gz archive containing the files from a VM file system (but not the filesystem itself) and I need to untar these into a raw disk image so that I may mount them as a VM.

How do I make an empty disk image and put a filesystem on it, and then untar the archive into it?

I suspect I'll then need to install a bootloader and create a swap partition too.

share|improve this question

You have not specified that the archive is from which Virtual Machine file system. If you are working on VMWare Workstation, just add a new disk from the interface, boot the machine with any bootloader, copy the tar file inside it using any ftp s/w and then untar it. If it is an exact tar of a running virtual machine, then your machine will boot next time even without bootloader.

share|improve this answer
The files are from an alien VM environment, possibly Xen, and are to go into a KVM hypervisor, hence the use of raw disk images. I think I have a solution I'll be trying tomorrow. – Aethylred Dec 12 '12 at 8:18

This is more of a strategy than a perfect manual, but it should help.

Build your "virtual computer", i.e. create an empty hard disk. Then, from my point of view, it is the best option to find out which Linux distribution is inside your .tar.gz-file. Imagine it is an openSUSE system, version 12.1 (it should work with any other distribution in a similar way). Download an original disk (CD) image for setting up a base system of your distribution in the right version and install it into your newly created VM. So you can create file systems and partitions (including swap), install grub or similar etc. and test the VM. Once this is done, boot into your VM with a Linux life system. Go to the "/"-folder of your VM (not the life-system's) and rename a number of folders (as root): bin, boot, etc, lib, opt, root, sbin, srv, tmp, usr, var (assuming non of those are on separate partitions) into something like bin-old, boot-old ... Now untar your old VM into the very same location. There should be new boot, bin ... folders right next to the old renamed ones. Now you need to change some files of your old VM in order to boot into the old system inside the new VM. Assuming the old VM was using grub, you e.g. need to adjust the files in /boot, while using /boot-old as help.

share|improve this answer
up vote 0 down vote accepted

First I created a created a loopback (/dev/loop0) device from a 25GB blank raw disk image and partitioned it into a 20GB partition for the filesystem and the remainder for swap.

Remember to use kpartx on the loopback device to 'find' the new partitions:

    # kpartx -a /dev/loop0
    # ls /dev/mapper/
    control  loop0p1  loop0p2

I wasn't sure which file system to use so I conservatively chose to make an ext2 file system on the first partition:

    # mkfs.ext2 /dev/mapper/loop0p1
    mke2fs 1.41.12 (17-May-2010)
    Filesystem label=
    OS type: Linux
    Block size=4096 (log=2)
    Fragment size=4096 (log=2)
    Stride=0 blocks, Stripe width=0 blocks
    1313760 inodes, 5245214 blocks
    262260 blocks (5.00%) reserved for the super user
    First data block=0
    Maximum filesystem blocks=4294967296
    161 block groups
    32768 blocks per group, 32768 fragments per group
    8160 inodes per group
    Superblock backups stored on blocks:
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,

    Writing inode tables: done
    Writing superblocks and filesystem accounting information: done

    This filesystem will be automatically checked every 23 mounts or
    180 days, whichever comes first.  Use tune2fs -c or -i to override.

Create a mount point and mount the loopback device partition:

    # mkdir /mnt/disk
    # mount /dev/mapper/loop0p1 /mnt/disk

Change to the mount directory and untar the archive:

    # cd /mnt/disk
    # tar xzvf /path/to/disk_archive.tar.gz

Use chroot to mount the new disk image as a root filesystem:

    # mount -t proc none /mnt/disk/proc
    # mount -o bind /dev /mnt/disk/dev
    # chroot /mnt/disk /bin/bash

Make any required changes to the file system (like installing the grub packages!)

Exit chroot and unmount and unloop everything:

   # umount /mnt/disk/dev
   # umount /mnt/disk/proc
   # umount /mnt/disk
   # kpartx -d /dev/loop0
   # losetup -d /dev/loop0

Finally I created a VM with this as its hard drive. I used Super Grub2 Disk to resolve any further issues with the bootloader.

share|improve this answer
Some delay on this while I much about getting the grub package installed, with no network, and having to rebuild dpkg locales etc. – Aethylred Dec 13 '12 at 1:20

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.