Tuesday, July 26, 2011

Grubbing About in Ubuntu

I've gotten a little time to do some maintenance and backup tasks on several PCs and have had to try to get up to speed with recent developments in Ubuntu.

First, my main work laptop is triple-boot: Windows 7, Ubuntu 8.04 LTS, and Ubuntu 10.04 LTS. It wasn't sufficient to just upgrade my Ubuntu, because I needed to work with some build tools for work that had not been successfully ported to the newer Ubuntu yet. However, everything is now migrated so I've been meaning to do some cleanup in order to remove some partitions and reclaim some drive space.

My build server is in a similar situation with the exception that it never had any version of Windows installed, so it has only the Ubuntu versions to contend with.

My tool of choice for backing up my laptop is the Paragon Hard Disk Manager 2011 Suite. It's been good to me and since I had purchased an early version of Partition Manager standalone program, I got it for $29.95. I like the Paragon tool because it will make a bootable rescue CD, and you can also create a bootable image on a USB stick. I used it to make backups of my partitions so when this laptop drive fails, I shouldn't need to try to reconstruct my whole Windows 7 world from scratch.

I fell down a rabbit hole a bit when trying to clean up my boot menu. Between 8.04 and 10.04 Ubuntu apparently switched to Grub 2, which is a big change. When I added 10.04 to my server, the install CD seems to have updated the MBR (boot record) to use the new Grub. When I'm booted into 10.04 I see a different root file system from a different partition. It isn't so easy anymore to clean up unwanted boot options, since the boot menu is actually generated via script every time the the update-grub script is run. The current set of boot options is listed in your /boot/grub/grub.cfg file.

I removed some uwanted 10.04 kernels by manually deleting files in my /boot directory (this is a touchy operation, so be careful; you can disable your system). I deleted all but a couple of the most recent kernels, and ran update-grub. I also tried using Synaptic package manager to remove kernels, and the useful Ubuntu-tweak tool that will remove kernels. I'll talk about these tools a little more in a bit. To make a long story even longer, I was hoping one of these options would get rid of the extra kernels I kept seeing in the boot menu.

It turned out that the update-grub script was discovering 8.04 kernels and adding them to the menu even though they existed only on my old unmounted sda1 partition. It is possible to manually edit the generated file but this is not advisable as your changes will be overwritten. It is also possible to disable the script that does this search for additional kernels but that seemed ugly as well. Some details for those interested: the script that searches for a variety of other OSes, on unmounted partitions, including Windows partitions, can be found under /etc/grub.d/30_os_prober. I'm not really a big bash guy, and it seems kind of obfuscated to boot (so to speak), so for now I'll just say I don't really understand how it works. But you can easily see what it does: just run it. If you're in the grub.d directory, I think you can just type ./30_os_prober. It will just generate some output; it won't overwrite any files.

Anyway, after I figured out where this script was finding 8.04, which took me a painfully long time, I settled on actually booting from a gparted live CD and completely removing my first partition, as there was nothing valuable I wanted in my 8.04 installation, then expanding my 10.04 partition to use this available space. (Please make sure your data is backed up before attempting an operation like this). In previous versions of Linux this might very well have completely disabled the boot; LILO and even the previous versions of Grub were notoriously fragile and I managed to break my systems frequently when messing with dual-boot configurations (fortunately I never broke a system so badly that could not eventually get it working again or lost critical data).

After removing a whole partition, the Grub 2 boot still worked flawlessly. In fact I'm quite impressed with it, although it makes me a bit nervous because my approach, which might be considered "old school," is that I want to always know what the GUI tools and automatic scripts and installers are doing under the hood. This is because based on experience, I know there is a very good chance I might need to manually fix any magical "it just works" configuration at some point. I felt compelled to study the new Grub enough to reassure myself that I could figure out more-or-less how it worked. Still, I can't claim to understand all the details - for example, the partition numbers now start at sda2, and there are "holes" in the partition numbers that showed up when the Ubuntu installer added partitions for 10.04. They seem to be of no real significance, but it would be interesting to confirm where the partition numbers are stored (presumably in the partition map) and whether they can be changed without actually modifying the partition contents. I suspect they can be but perhaps only with the older non-visual parted tool, and this may also be a risky change; for one thing, it might break my mount points -- although perhaps that fact that Ubuntu now seems to use UUIDs to identify partitions might ensure that everything still worked. Obviously there are still limits to my understanding.

There is a bit of an oddity with gparted. When I booted from the live CD in order to change my partitions, gparted initially showed free space after my last partition, which is a swap, but would not allow me to expand the swap partition to use this space. But after I adjusted the other partitions, without touching the swap partition, and booted up into my 10.04 system and ran gparted again, it showed a 1 MiB unused block next to each of my other partitions, but not my swap partition. Then when rebooting using the gparted live CD, it showed none of these free space blocks at all. Booting from the Paragon live rescue CD and running the partition manager, it also didn't show any of these unused blocks. Tides go in, tides go out, and you can't explain that. Hmmm.
I'm not sure if it these 1 MiB blocks are actually there, or apparently there due to round-off error, or padding that is normally hidden. I'm going to pretend it's not actually there since otherwise my generally obsessive-compulsive personality would compel me to try to fix it.

Anyway, if you're having a similar problem or just want to learn more about a boot issue, look for the Ubuntu "Boot Info Script," which you can find on SourceForge. (I'm running on Ubuntu 10.04; by the time you read this, things are likely to have changed, so poke around and make sure it's compatible with your distribution and version). If you want to manage a backlog of too many old kernel versions without having to use the command line and possibly deleting the wrong file, you can use Synaptic, but you might have to hunt through a long list of packages looking for the ones that are actually installed and the ones you want to remove, including headers. If you do a search on, in my case, "2.6.32," you'll see packages like linux-headers-2.6.32-24-generic and linux-image-2.6.32-24-generic. Use the "Mark for Complete Removal" option and then apply your changes. Synaptic will actually re-run the Grub update script for you. If you sort by "Installed version," it will be easier to find the ones that are installed. Make sure you don't remove your current kernel! (Identify your current kernel with a terminal window, using the "uname -r" command). You might want to keep one or two older kernels on hand in case you come across an issue with your current one.

You might also look for Ubuntu Tweaker which has the advantage of not allowing you to accidentally remove your active kernel (and it also gives you lots of other options to experiment with, although please be sure you know what you are doing, since I'm not sure what a lot of them actually do). I think you can set up Ubuntu Tweaker by executing sudo add-apt-repository ppa:tualatrix/ppa, then just running sudo sudo apt-get install ubuntu-tweak. That was all it took to get it into my system menu. Once it is installed, you'll want to "unlock" and then look for the menu item to clean up kernels. I'm not sure if it removes the associated headers or not.

Finally, there is a "Grub Customizer" tool that looks quite elaborate, but I have not gone down the rabbit hole quite that far (for example, it will let you supply a background image). Probably best not to start playing with it if my actual goal is to get work done...