"apt-get dist-upgrade" in limited hard drive space

Having a small hard drive can make dist-upgrades a complete nightmare, as apt-get likes to download everything and then dist-upgrade, filling up your harddrive before the download is complete. The easiest solution is recommended in the Squeeze (6.0) release notes, Debian's official guide to the Lenny to Squeeze upgrade process:

Use a temporary /var/cache/apt/archives: You can use a temporary cache directory from another filesystem (USB storage device, temporary hard disk, filesystem already in use, ...)

Note: Do not use an NFS mount as the network connection could be interrupted during the upgrade.

For example, if you have a USB drive mounted on /media/usbkey:


      remove the packages that have been previously downloaded for installation:

      # apt-get clean


      copy the directory /var/cache/apt/archives to the USB drive:

      # cp -ax /var/cache/apt/archives /media/usbkey/


      mount the temporary cache directory on the current one:

      # mount --bind /media/usbkey/archives /var/cache/apt/archives


      after the upgrade, restore the original /var/cache/apt/archives directory:

      # umount /media/usbkey/archives


      remove the remaining /media/usbkey/archives. 

You can create the temporary cache directory on whatever filesystem is mounted on your system.

The release notes mention other methods as well.

What follows is a report of how one Debian user managed the problem. It does not appear to be simple or reliable which goes to show that while you're free to do as you wish you will likely be better served by obtaining support via offical Debian channels.

However, there is a way round it. This is how I did it. First, create a file which lists the packages that will be updated thus:

apt-get -s -u upgrade || sed -n -e ''^Inst'',/^Conf/p -e /^Conf/q || \
     cut -d" " -f2 > update.packages

This will create a file called update.packages which has a list of packages to update. Before you do anything else, have a look at it and guage whether you think it is too long. Any packages that you recognise to be heavyweights, take them out for now. If you're running really low end, you can even update it a package at a time.

Next, you need to download those packages:

for f in $(cat update.packages); do apt-get install $f; done

This runs a loop which looks at the packages in update.packages and installs them.

Next, you need to clean things up to make room for the next cycle of install with:

apt-get clean

Then repeat the process until update.packages no longer has any text in it (which means there are no other packages to update).

Finally, you can carry out apt-get dist-upgrade, which on my machine managed to work. This could still fill up your machine in theory.

I'm interested to hear if anyone can script this, so that it becomes more automatic. Please leave any suggestions here.