This is not intended for the beginner, it is not difficult to do but will result in a broken install if done wrong.
The stock ubuntu kernel contains both squashfs and aufs, the former is support for a compressed (7z-style compression) read-only file system. The latter is a "union filesystem", it allows to stack directories and we will use it to keep our /usr read-write.
The idea comes from me on the gentoo forums, and other related forum discussion.
Here is what we will do:
compress /usr with squashfs and mount a read-write union on top of it to keep it writable.
It is safer because you keep a read-only backup of /usr
It is faster because searching through the squashfs image is faster than through the actual file system, see benchmarks
It saves battery life because it reduces physical head movements of the hard drive.
Now, if you do not feel comfortable with the command line and do not know what a file system is, this tip is not for you. /usr is crucial to your linux and messing with it is risky. It is risky if you do not know what you are doing. And I do not take responsibility if you break your ubuntu. OK?
Let's go.
Everything is done at the terminal.
Install squashfs-tools and aufs-tools (aufs-tools is not required, though)
Code:
sudo aptitude install squashfs-tools aufs-tools
Compress /usr, as root to avoid permission issue
Code:
cd ~
sudo mksquashfs /usr /.usr.sqfs -check_data
or
Code:
sudo mksquashfs /usr /.usr.sqfs -check_data -info -no-progress
Everything OK?
reboot, when grub starts (a few white lines telling you you can press ESC) press ESC. At the grub prompt, the entry on top should be selected, press e (edit) at the kernel line. Replace "quiet splash" with "S" at the end of the line. Press ENTER and b (boot).
After the kernel has been loaded, it will ask you if you want to "drop to a root shell" and yes, you want, select this option.
The system will finish booting and actually drop you to the root shell.
Move /usr out of the way
Code:
cd /
mv /usr /usr-old
mkdir /usr
mkdir -p /var/squashed/{ro,rw}
and edit /etc/fstab
add the following lines
Code:
/.usr.sqfs /var/squashed/ro squashfs loop,ro 0 0
aufs /usr aufs br:/var/squashed/rw=rw:/var/squashed/ro=ro 0 0
then you try it
any error? you have made a mistake in /etc/fstab
and have a look whether the filesystems are mounted correctly (two lines, one for the squashfs filesystem and one for the aufs union.)
if that works too, you should be all set. Reboot. If it does not go till the end, you may want to try to reboot and remove the quiet splash from the grub entry to see what fails.
Once back in gnome or KDE or... if all is working right, you can also remove /usr-old, you should probably make sure everything is mounted correctly again.
You can reconstitute the squashed image from time to time if you install a lot of things and /var/usr becomes big. You need to make it again also when you upgrade to the next version of Ubuntu.
If you are using LVM2, like me, you can even put the squashfs image in its own volume, there is nothing wrong with that. And having root on LVM2 is not a problem for this "how to" either, provided you keep /boot out of LVM.