Mandriva 2009 compress /usr

Introduction

The purpose of this page is to describe how I saved 1GB on my Mandriva 2009 install. I did it earlier with Mandriva 2008 and 2008.1 and I find it very usefull for NetBook (especially for my EEE-PC 4G) so here is an update of the guide.

First of all you must understand that it’s only for advanced users. It is more a hack of the installation than a modification and you can easily crash your current installation and eventually loose all data.

Description

The /usr compression rely on 2 filesystems squashfs and unionfs. Squashfs allow one to compress an y directory in a file that can be mounted as a read-only filesystem. Squashfs is used to create LiveCD (Mandriva one for instance). My 1.5GB /usr is compressed to a 500MB squashfs file.

A read-only /usr is not very useful since it would prevent you from installing new softwares. That’s where unionfs enter. Unionfs allow one to combine a read-only filesystem and a directory into a read/write mount. Unionfs is also used to create LiveCD, the directory beeing a ramdisk. Be aware that this second directory is not compressed.

So we will have a squashfs file mounted on let say /usr.sq and a unionfs mounted on /usr and using /usr.un to store modifications.

To do this we will have to hack several sensitive files: /etc/rc.d/rc.sysinit, /etc/init.d/functions and /etc/initd.d/halt.

Be aware that these files can be overwritten by updating your system (new kernel for instance). So always keep a copy of these files and check them after any update.

Setting things up

First we have to compress /usr with squashfs. For this you need to install squashfs-tools.

#urpmi squashfs-tools
#cd /usr
#mksquashfs * /somewhere/usr.sqfs

This phase will last for some time. Be patient.

Create necessary directories:

#mkdir /usr.un /usr.sq

Save your system files:

#cp /etc/rc.d/rc.sysinit /etc/rc.d/rc.sysinit.orig
#cp /etc/init.d/halt /etc/init.d/halt.orig
#cp /etc/init.d/functions /etc/init.d/functions.orig

Update system files contained in this file. Here are the differences between modified and original files.

# diff /etc/rc.d/rc.sysinit{,.orig}
833,839d832
<
< modprobe loop
< modprobe squashfs
< modprobe unionfs
< mount -o loop -t squashfs /usr.sqfs /usr.sq
< mount -o dirs=/usr.un=rw:/usr.sq=ro -t unionfs unionfs /usr

# diff /etc/init.d/functions{,.orig}
204,205c204,205
<     remaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" && $2 !~ /^\/usr/ {print $2}' /proc/mounts)
<     devremaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" && $2 !~ /^\/usr/ {print $1}' /proc/mounts)
---
>     remaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" && $2 !~ /^\/live\// {print $2}' /proc/mounts)
>     devremaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" && $2 !~ /^\/live\// {print $1}' /proc/mounts)
219,220c219,220
<         remaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" && $2 !~ /^\/usr/ {print $2}' /proc/mounts)
<         devremaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" && $2 !~ /^\/usr/ {print $1}' /proc/mounts)
---
>         remaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" && $2 !~ /^\/live\// {print $2}' /proc/mounts)
>         devremaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" && $2 !~ /^\/live\// {print $1}' /proc/mounts)

# diff /etc/init.d/halt{,.orig}
11,12d10
< # Workaroung bug 44752
< modprobe -r snd-hda-intel
152c150
<     $2 ~ /^\/usr/{next}
---
>     $2 ~ /^\/live\//{next}
200c198
< awk '$2 !~ /\/(|dev|proc|selinux|sys)$/ && $2 !~ /^\/usr/ && $1 !~ /^\/dev\/ram/ { print $2 }' \
---
> awk '$2 !~ /\/(|dev|proc|selinux|sys)$/ && $2 !~ /^\/live\// && $1 !~ /^\/dev\/ram/ { print $2 }' \
210,215d207
< # Umount compressed /usr
< fuser -k /usr
< umount -f /usr
< fuser -k /usr.sq
< umount -f /usr.sq

Switch to runlevel 3 to activate the compression:

#init 3
#fuser -k /usr

If you have enough space rename /usr to /usr.old or remove it.
Copy usr.sqfs to /.

Mount /usr:

# modprobe loop
# modprobe squashfs
# modprobe unionfs
# mount -o loop -t squashfs /usr.sqfs /usr.sq
# mount -o dirs=/usr.un=rw:/usr.sq=ro -t unionfs unionfs /usr

Reboot.

If something went wrong, you can still failsafe boot and restore everything.

Comments are closed.