Tuesday, August 25, 2009

Getting sources, source keys, and package lists out of an installed Ubuntu from a Live CD

When reinstalling Ubuntu, you may often find a large quantity of time becomes taken up afterwards as you look for applications that you had installed but failed to install after you finished reinstalling the OS.

There are ways to export and save this data before running reinstalling, backing up /etc/apt/sources.list is most commonly referenced, which gets you all the repositories you had, but you still have to reinstall the individual packages, and obtain the verification keys for them.

From a working system, you'd just

# If you want to preserve deinstalled packages, just omit the grep command 
$ cp /etc/apt/sources.list ~/sources.list
$ dpkg --get-selections | grep -v deinstall > ~/packages.list
$ sudo apt-key exportall > ~/sources.keys

In a Live CD, a situation I recently found myself in (I borked GRUB and decided that halfway through the potential fix was a good time to prepare for a possible reinstall,) it's a little harder. Those commands will get you the sources, packages, and keys from the Live CD rather than your install. Obviously, /etc/apt/sources.list is a simple copy-to-backup-partition away, but the other two are behind applications.

The solution (hijacked from ubuntuforums user 5-HT and pieced together with this Upgrade or Reinstall post by Daniel Bo) is to chroot into the old install, and then run those commands. Here's how to do that:

# /dev/sdb2 and ext3 was my install partition and type, replace those values with your own.
ubuntu@ubuntu:~$ sudo mkdir /mnt/root
ubuntu@ubuntu:~$ sudo mount -t ext3 /dev/sdb2 /mnt/root
ubuntu@ubuntu:~$ sudo mount -t proc none /mnt/root/proc
ubuntu@ubuntu:~$ sudo mount -o bind /dev /mnt/root/dev
ubuntu@ubuntu:~$ sudo chroot /mnt/root /bin/bash

Now you are root in your install partition. Now you need to mount the partition that you will store these on. If your home directory is on a different partition, you can use that/ If it's not, get another drive and back up your home partition to that drive as well as these files. You can't use nautilus for this, so here's what I did as a hint:

# Again, /dev/sda1 is for me, replace that value with the one applicable to your system.
root@ubuntu:~$ sudo mkdir /media/disk
root@ubuntu:~$ sudo mount /dev/sda1 /media/disk

Once you have the partition mounted (I'm going to assume /media/disk as described above,) you can simply run the commands:

root@ubuntu:~$ cp /etc/apt/sources.list /media/disk/sources.list
root@ubuntu:~$ dpkg --get-selections | grep -v deinstall > /media/disk/packages.list
root@ubuntu:~$ sudo apt-key exportall > /media/disk/sources.keys

If you have any other files in the filesystem you need, such as apache2 configs or the /etc/hosts files, now is a good time to get them.

Now, when you have your fresh install, you can restore the data by replacing or adding to the new sources.list file (remember to change the lines if you changed Ubuntu versions,) adding the keys back into apt, and re-installing all your packages. Ironically, the hardest part now (possibly) is the sources file if you need to alter it. If it's the same, just drag it into the Third-Party tab in Software Sources app (System > Administration > Software Sources) and have it add them.

The keys file can be done the same way, except in the Authentication tab.

Now that the repositories and their keys have been added, reinstall all your packages with:

$ dpkg --set-selections < /path/to/packages.list && sudo apt-get dselect-upgrade

Or with Synaptic, File > Read Markings and press Apply.