Re-partitioning Xubuntu

Having used Xubuntu for a while now on my transition test rig, I’ve decided to re-partition the HDD to better suit my needs. Here’s why I’m doing this:

Data security – Not encryption, but rather to ensure the data is (most likely) safe on an OS re-installation

The default swap partition is not flexible – It always takes up space even when you don’t need it to

Makes backup easier – I can simply image the small OS partitions and back them up onto several DVDs

However, the main drawback (and my original reason for not having a separate data partition) is that your HDD’s capacity is effectively reduced (Data = HDD – OS – Swap), and not to mention when your OS partition runs out of your space, you’re boned (alright, I know what you’re thinking, but let’s not get carried away with hacks like creating symbolic links to the data partition…).

First thing first, let’s figure out what the setup should look like and how much space we need, then we can explore different options when we encounter them.

Partition, swap and hibernation config

As much I want to make a complete transition over to Linux, there are some situations where Windows is required (like certain applications and firmware updates), so we need a Windows partition.

The best way to judge how much space is required is to look at the existing space usage of "%SystemRoot%" and "%ProgramFiles%" – you can use a tool like "diruse" (from the Windows XP support tools) or folder properties in explorer.exe…

Windows: In a clean(ish) install of XP SP3, %SystemRoot% + %ProgramFiles% comes to about 2.7 GiB (say, 3GiB), and allowing 1 GiB each (yeah, I know it’s rather generous) for MS Office, IDEs, .NET, JDK/JRE and AutoRoute the minimum size so far comes to about 8 GiB. Adding a 15% defrag free space requirement, overhead for old (un)installation files, system restore files, pagefile.sys, hiberfil.sys, and some free space, the minimum size for the Windows partition comes to 12 GiB (which also fits nicely onto 3 DVDs when imaged).

It is worth point out that if you have a lot of RAM and want to hibernate your Windows session, you’d obviously want more space to accommodate hiberfil.sys. A safe suggestion would be the next multiple of 4, say, 16 GiB (if you have 3 GiB of RAM) (to fit onto 4 DVDs). As for page file, it’s up to your judgement / Windows’s to determine how much is needed – and if you have a second HDD, put the page file there instead.

Linux: As for the Linux partition, it’s a little bit more difficult to estimate (seeing I haven’t really used it for that long) – but base install rounds up to about 3 GiB. For now, I’ve assigned 16 GiB to it as I plan on installing some of the software above using WINE (like MS Office), OpenOffice, IDEs. Though I’d probably use a 12 GiB partition when I partition my main system – but I’ll just have to wait and see how it goes.

As per the Xubuntu default setup, the OS(es) should reside on a primary partition of its own, with the rest being on extended, logical partitions – you can only have up to 4 primary partitions on a volume.

As for swap and hibernation (suspend to disk), I’ve opted for a 512 MiB swap file and a 512 MiB swap partition dedicated for hibernation, for the 512 MiB of RAM (though there are usage problems with this setup – I’ll talk about that next).

The reason behind using a swap file is because it is more flexible (can be resized dynamically) with no reported performance difference under the 2.6 kernel comparing to a swap partition.

As for the swap partition, it is used because there doesn’t seem to be a way to configure Xubuntu’s in-kernel hibernation implementation to use a hibernation file. The main problem with this setup is that hibernation would fail if the entire swap file had been filled and overflowed onto the hibernation swap partition. Unfortunately, swapoff-ing the swap partition will result in a hibernation failure.

The only usable hack around the the filled swap file problem is to run swapoff after boot and resume from hibernation, and swapon just before hibernation. Unfortunately, I was not able to find a way to do swapoff after resuming from hibernation (I’d welcome any suggestions on this!) – I could use upstart on boot for swapoff and modify "/usr/lib/hal/scripts/linux/hal-system-power-hibernate-linux" to run swapon (this is used by GDM (and the logout GUI) to hibernate), but I still need to run swapoff on resume from hibernation… However, saying that, you could modify and use "/etc/acpi/hibernate.sh" to do the swapon and swapoff as the script stops at "echo -n "disk" >/sys/power/state" and resumes execution afterwards on wake.

The only command I haven’t had time to explore is "/usr/sbin/pmi" (called with parameters "action [hibernate|sleep]") – I found this in gdmsetup –> "Edit Commands…".

Update: 2008-05-27 @ 03:09
You can use "pmi" to put your system to sleep / hibernation and be able to do swapon and swapoff at the appropriate time – just do swapon before calling pmi, and swapoff after, since pmi doesn’t detach itself. I’ll post some scripts on how to do this later.

Hacks aside, there are actually solutions to enable proper hibernation to a file – such as TuxOnIce, but it requires patching and re-compilation of the kernel – which is something I don’t plan on doing in this project.

While we’re (kinda) still on the subject on partitions, there is something that can resolve the issues with partition sizes – LVM (Logical Volume Manager). In theory, pretty much everything (except "/boot") can be setup to use LVM, but I’ve decided against it because:

My inexperience to Linux – I think it’s best to keep it simple at this stage

Reliability – A disk failure or (somehow) a LVM Volume Group corruption would be disastrous

Performance – Since the data goes through another abstract layer, there may be issues there

For now, I’ll leave LVM alone until I need a heavy-duty file server with RAID (or equivalent) that runs only on Linux.

File system

So far, we’ve decided on the partition configuration and now, it’s time to choose the file systems.

Since the aim of this project was to re-partition the drive, I’ve decided to stick with Ext3 in Linux as I don’t see any benefits in using another FS for my needs. As for Windows (XP), it’s a no-brainer – NTFS (linux can mount that as ntfs-3g type).

Pre-requisites and tools

We’re now almost at the point where we can start re-partitioning the drive – now that we know what the disk setup is going to be.

We obviously need a re-partitioning tool, but we’d also need something to backup and restore the drive (in case anything horrible were to happen), and somewhere to store the HDD images.

Not long ago, I came across the SystemRescueCd (possibly on Hak5 or this article on linux-mag.com (free subscription required) – can’t remember exactly which…) which demoed the imaging and bare-metal restore capability of the live CD.

As well as the partimage tool on the live CD, it also contains GParted (for re-partitioning) and samba (for smb-based network backups). But there are so many more tools on this CD it is definitely worth keeping in your toolbox.

cd /etc/ && cp fstab fstab.bak && vim /etc/fstab – Backup then edit fstab according to fdisk and ls output, don’t forget to change the "pass" number for disk check – see man fstab for more information. The edited part of fstab should look something like:

(Note I’ve left the other stuff like "proc", "/dev/scd*" out in the example above – keep them in fstab!)

As you can see, I’ve set the hibernation swap partition as "pri=-2" (i.e. lower than the swap file’s) in an attempt to not use it – as mentioned before, I still need a proper workaround for this… – Use "pmi" instead – see update description above.

vim /etc/initramfs-tools/conf.d/resume – Change the UUID to that of the new "swap" partition, e.g.:

RESUME=UUID=c9e7c3c8-6f49-44be-9cdf-1387ce320b27

update-initramfs -u – Update (fixes) the boot screen

shutdown -r 0 – Make sure everything still works and boot screen fixed

Log back in as root

swapon -s – Check the output to ensure there is 1 mounted swap:

Filename Type Size Used Priority
/swap file 524280 38216 -1

cp -r /home /mnt/h/home – Copy the existing home directory

mv /home /home_old – Make way for the new home

cd /mnt/ && chown -R [user]:[group] h – Makes the new user own everything in the data partition (since I’ll be the only one using the data partition for now), then tweak it to your liking…

ln -s /mnt/h/home /home – Link the new home in – make sure you use the full path or you’ll get link errors.