Installing Linux from a tar file

A logical progression of my VirtualBox post

[2008-04-17]

Let's say you've got yourself a tar backup of a Linux system. (If you tried my VirtualBox post, you'll have one.) And let's imagine that you want to do something useful with it, like restore it onto a machine. Here's how.

Just a run through of what we will need to do:

  1. Boot off a Linux live CD

  2. Create the partitions if necessary, and mount them.

  3. Copy the contents of the tar file to the destination partition(s).

  4. Change the files that are particular to this computer.

  5. Install GRUB, and reboot.

OK. Let's do it.

I'm going to run through this using my normal configuration. I have done this with Ubuntu Hardy. Other distros will differ slightly. For desktops, I use a root partition and a separate /usr/local partition. You might want something different. I hope that I can convey why I do what I do, and that you'll be able to adapt it to your situation accordingly.

Let's assume that I've got a blank drive in the machine at /dev/sda, I've booted off a live CD, and I've got my tar file on an external drive, or flash disk, and when I plug it in, it comes up as /media/disk/desktop.tar. We'll pick it up from there. ...

Using your favourite disk partition manager (I like good old fdisk) I create a 10GB partition as the root partition, a logical partition for /usr/local/ comprising the rest of the drive less 4GB, and a swap partition in the last 4GB. I make my swap partition about the same size as the amount of RAM I have (or would like to have :-) ).

Format the partitions:


$ sudo -s
# mkfs.ext3 /dev/sda1
# mkfs.ext3 /dev/sda5
# mkswap /dev/sda6

Mount them and copy everything across:


# mount /dev/sda1 /mnt
# cd /mnt
# mkdir -p usr/local
# mount /dev/sda5 usr/local
# tar -xf /media/disk/desktop.tar

I like to watch progress in a second terminal:


watch -d -n 30 df

This might be a good time for a coffee break.

When it's finished copying, we need to change all the things that make your new machine unique. If you're just restoring from a backup, you can skip this, and go to installing Grub.

You probably know that when Linux boots up, it knows what to mount by looking at the /etc/fstab file. Some distros use the device name (e.g. "/dev/sda1") to refer to partitions, and some (like Ubuntu) use the Universally Unique Identifier (UUID).

If your distro uses UUIDs (cat /etc/fstab to see), here's how to find out yours:


# vol_id /dev/sda1
# vol_id /dev/sda5
# vol_id /dev/sda6

Copy and paste the things that look like "b0f82767-1bef-4ac5-a392-a8d5a1a4e110" somewhere convenient.

Now edit /mnt/etc/fstab and search and replace with your new UUIDs. (I'm assuming your mount points are the same as what's in your fstab file already. If not, you'll need to change them, of course.)

Ubuntu uses UUIDs in /mnt/boot/grub/menu.lst too. So you'll want to search and replace those too.

Your partitions have now been taken care of. Let's do your hostname. (Thinking up hostnames is sometimes the hardest part for me. In the spirit of Debian, I started off using the names of CG cartoon characters, and it's now extended to any movie or TV character.)

Edit your machine's entry in /mnt/etc/hosts.

Debian-based distros also use /etc/hostname to remember the hostname:


# echo "mynewhostname" > /mnt/etc/hostname
# hostname -F /mnt/etc/hostname

(I know you're still using the live CD, and not your new installation, but I set the hostname because I'll need it for the next bit.)

If you are running an SSH server, change the SSH host keys. (I have two host keys; one DSA and one RSA. Don't miss the bit where I create the second one. You might have three. (ls /mnt/etc/ssh/ssh_host*.pub to see.) If you have three host keys, this post might help you create the third.)


# cd /mnt/etc/ssh/
# ssh-keygen -t rsa -f ssh_host_rsa_key -N ''
Generating public/private rsa key pair.
ssh_host_rsa_key already exists.
Overwrite (y/n)? y
Your identification has been saved in ssh_host_rsa_key.
Your public key has been saved in ssh_host_rsa_key.pub.
The key fingerprint is:
00:11:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff root@mynewhostname
# ssh-keygen -t dsa -f ssh_host_dsa_key -N ''
Generating public/private dsa key pair.
ssh_host_dsa_key already exists.
Overwrite (y/n)? y
Your identification has been saved in ssh_host_dsa_key.
Your public key has been saved in ssh_host_dsa_key.pub.
The key fingerprint is:
00:11:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff root@mynewhostname

Now we're nearly done. We're ready to install Grub.


# mount -o bind /dev /mnt/dev
# chroot /mnt /bin/bash
# mount -t proc none proc
# cat /boot/grub/device.map
(hd0) /dev/sda

Make sure your device.map file makes sense. If not, change it accordingly before running grub-install.


# grub-install /dev/sda
Searching for GRUB installation directory ... found: /boot/grub
Installation finished. No error reported.
This is the contents of the device map /boot/grub/device.map.
Check if this is correct or not. If any of the lines is incorrect,
fix it and re-run the script `grub-install'.

(hd0) /dev/sda

And now you're ready to reboot, and give your new installation a spin.

I was going to build a facility for comments on this blog before posting this article, but ... well ... I haven't yet. Sorry. Please mail me any feedback, and I'll incorporate it below.