USB stick for more (K)Ubuntu performance

Blog ยป USB stick for more (K)Ubuntu performance

Posted on 12 May 2009 22:27

USB stick works 2 times faster than hard disk. Using USB sticks, you can increase the performance of your Ubuntu. Or you can make your own environment portable. Nowadays, everybody can buy a few 16G USB sticks.

My favorite configuration is:
USB stick 1 -> / root
USB stick 2 -> swapspace
USB Harddisk -> /home

That way I have speed and I don't have the nightmare of installing my next new box. When you move to another box with your system the only problem is having a different graphics card. Jaunty fixes the problem for you on failure of existing X11 configuration.

I'll try to cover a few possibilities that can be applied independently

Fresh first time (K)ubuntu

Stick your devices, pop-in live-cd and do a normal installation. Instead of "Guided Partition", go and specify the devices for / (root), /home and swap.

After booting into your new system add the last bit of optimization so that your usb system does not wear off. Add those lines to /etc/fstab

tmpfs /tmp tmpfs defaults,noatime,mode=1777 0 0
tmpfs /var/tmp tmpfs defaults,noatime,mode=1777 0 0
tmpfs /var/lock tmpfs defaults,noatime,mode=1777 0 0
tmpfs /var/log tmpfs defaults,noatime,mode=1777 0 0
tmpfs /var/run tmpfs defaults,noatime,mode=1777 0 0

Those directories are frequently written. And you don't need to keep your data. Now they are in RAM. If there is not enough ram, they'll swap out.

Apart from that there is a possibility of desktop not starting at first login. For this and other possible problems, see Installing Ubuntu directly to USB drive from installer CD

Your swap goes USB

If you don't have time to change an existing system, than at least, make sure that you use a USB stick for your swap space. It increases performance because

  1. swap is on a different device. It does not block the access to your harddisk to operate.
  2. swap performance is much faster on USB stick.

I have a SanDisk 8G as swap space. My machine is doing swaps 200 times faster!

You must have heard that USB sticks have limited writes. True. You'll replacing your $5 swap stick every 6 months.

Create a partition as you normally would. Then format it with mkfs.swap

Normally you don't create partitions? Then simply install (apt-get) and use qtparted to create your partition. Let's say you created partition sdb1. Then execute:

sudo mkswap /dev/sdb1

To see the uuid of your swap, execute

ls -l /dev/disk/by-uuid/ | grep sdb1

Mine reads, "dbceaa30-73cc-45a0-9b11-dade7131d192". So go to your /etc/fstab (sudo kate /etc/fstab) and add the line:

# usb swap
UUID=dbceaa30-73cc-45a0-9b11-dade7131d192 none            swap    sw,pri=5              0       0

Also comment out the other line which specifies the swap partition on your hard disk. You don't need it anymore. In case you are keeping it pri=5 above makes usb to take over swapping.

Enable the new swap space

sudo swapon -a

You can see the current swap configuration of your machine

swapon -s

When my swap went USB, I found out that I could launch around virtual machines for testing purposes and live them around freely. With less concern. Ubuntu has a "swappiness" setting. When it's 100, it will swap out any idle process ASAP and use the freed memory for data caches. So with a fast swap space, you can increase the storage cache memory to increase the performance of the computer once more. How useful this trick depends on how CPU intensive your processes are. But in general, things are just idling around. So give the memory to cache and get better harddisk performance.

sudo sysctl -w vm.swappiness=100

The default value of swappiness is 60. You can experiment values between 60 and 100 to get a better result depending on your CPU usage ratio.

Moving existing system applications to USB

You are not doing a clean reinstall. You have have your lovely existing system. Then the easiest way is to keep your existing boot device as it is while moving the application binaries to the USB stick.

This gives you some benefits. Data in your harddisk and applications in the stick will be read in parallel. This is simply faster boot or operation. You'll have more space left on your harddisk :)

What about write limit of USB sticks? I am sure you are not making 10000 operating system upgrades soon. And when a USB stick dies, it simply becomes read only.

Using qtparted or your other favorite, format your stick using "ext2". No "ext3" please. Having journal on USB stick will kill it faster.

Let's say you created the partition /dev/sdc1.

Check that your system will fit into the stick!

df -h /usr

Mount it, copy the files, and link to the new place

sudo mkdir /mnt/usb-root
sudo mount /dev/sdc1 /mnt/usb-root
sudo cp -aR /usr /mnt/usb-root/
sudo mv /usr /usr.bak
sudo ln -nfs /mnt/usb-root /usr
sudo rm -R /usr.bak

Notice that I am removing /usr.bak at the end only so that I have a way to rollback until the end.

A 16G stick will be sufficient for most installations. If you still have an 8G then please buy a new one. If you are screaming that your /usr is 32G then that deserves another article although possible :) . Tell me if you need it.

Uh, you need it auto mounted next boot. Discover the uuid:

ls -l /dev/disk/by-uuid/ | grep sdc1

Mine reads, "b363a9bd-9b00-44b2-99c8-772bcc9cb1bc". So go to your /etc/fstab (sudo kate /etc/fstab) and add the lines:

UUID=b363a9bd-9b00-44b2-99c8-772bcc9cb1bc /mnt/usb-root               ext2    defaults,noatime,errors=remount-ro 0       1


~Device Rate Maximum Latency CPU
HD 650 Mb/s 10 ms 69 %
USB 910 Mb/s 1.7ms 0 %

My heavy boot with many services was 2 mins. It's now 30 secs.

Have fun :)

/usr problem

On some systems (nowadays all systems in fact), applications are guarded (.e.g by apparmor) so that they can only access the expected path inside '/usr'. If you do the symlink trick above then your system will no more work. Expecially X server itself. So the work around is mounting it instead of linking it. In fstab, define a line similar to

/mnt/usb-root/usr /usr none rw,bind 0 0

And do

sudo mv /usr /usr.bak
sudo mkdir /usr
sudo mount -a

Now the /usr on stick is mounted on the original place and the system guard will not be aware of it.

If you like this page, please spread the word: diggdel.icio.usFacebook

You can contact me if you have questions or corrections.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License