Blog » USB stick for more (K)Ubuntu performance
Posted on 1242167271|%A: %d %B, %Y|agohover
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
- swap is on a different device. It does not block the access to your harddisk to operate.
- 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:
#/mnt/usb-root
UUID=b363a9bd-9b00-44b2-99c8-772bcc9cb1bc /mnt/usb-root ext2 defaults,noatime,errors=remount-ro 0 1
Performance
| ~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:
As well as commenting here, you can tweet or email to me








You must have some really special usb ports on your pc bcause the best usb 2.0 port theoretically manages to get 480Mb/s, in practice it doesn't even come close to that, no way in hell it's going to reach 910Mb/s unless it's cached in memory (and than your system would have to load itself completely to RAM) , until usb 3 get's available any decent Harddisk is faster then ANY usb stick. The numbers you gave for hd performance are about right except the cpu load is not normal on a modern, well configured system you get a load of about 3 %,
anyway the tips you gave are great if you want to have a really portable OS, but it will always be slower than a HD based system until USB3 that is ;)
(i've been using a usb stick based linux system the last few year's, it's great for troubleshooting, hacking, checking mails, but i would never want to use it on a permanent base, it's to slow for that, i'm using 1 fast stick (32GB) and swap is turned off, systems i work on have sufficient memory)
In my current configuration, I am keeping the OS only on the stick. Plus I am keeping journal for the OS partition on the hard disk. So it's totally different than using a USB based Linux. This is not a portable installation at all.
So read times are faster because of no fragmentation. Write times do not suffer because until the system is idle they are being written to the journal on the hard disk. And still it's a parallel device to the hard disk.
And to stress the point, in the case of moving "/usr" only, there is nothing write accessed on usb stick. The /usr is used solid read only. And as the number above indicate usb stick delivers your read only files much faster than the internal disk!
In the case of a portable USB based Linux we cannot enjoy those advantages, and it's write times are horribly slow. So I, too, do not suggest a portable USB based approach. Just using it as a partition for files that are not frequenly updated changes the picture completely.
In such a configuration, we cannot speak portablity of the OS at all because journal file and home partitions are on my harddisk and I cannot boot another machine with that stick.
I agree with your experience of totally USB based slow experience.
Summary
- You will benefit the speed of USB as long as there are no write accessed directories on usb-stick
And with regard to bus speeds, it only means your system would be faster if you could use two hard disks in parallel RAID configuration for example
To have a really slow system, on the usb stick a) keep the home files also b) keep the journal on the stick or use a non journaling file system c) let the system update the directory access times d) keep directories like /var/lock /var/log /var/run on the stick and kill it
Hi,
Instead of putting all /usr (mine is 15G big) is it possible to put at least some apps with same methods (at least the most used as: firefox, thunderbird (without mail directories), gimp etc )?
So that it can fit on my 8G usb stick ;-)
Hi
I tried to move /usr according to your method, everything was ok till I wrote: sudo ln -nfs /mnt/usb-root /usr
It answered: sudo, no directory….
Which is normal because as soon I rename /usr in /usr.bak, it doesn t fnd any program or application, then it s impossible to continue…
So what do I have to do to enable it?
Best Regards
Adrien
case A) You want to have extra applications on the stick. To have some applications on the stick modify PATH in the file "/etc/environment" so that your extra binaries directory on your stick is available, too, at all times
case B) You want to move the whole "/usr" to the stick. To prevent that things are unavailable during the migration of system critical things:
Then do the migration there. There you are "su" and both paths are available. Warning this has nothing to do with file "/etc/environment". It is about case A) above and it does not affect super user paths. For super user not to loose the commands you need to do it directly in the terminal, while you are the super user.
For example if your new "usr" directory is "/mnt/usb-root/usr", you should modify the path this way:
Actually that "sbin" directory is your headache
a) sbin contains critical commands required by sudo and sudo itself.
b) su and user environments are different. This is why you need to be su "sudo su" and modify the path there before moving /usr/sbin
To make sure you won't be repairing the system from the boot disk, check if everything is OK:
should return "root". That means you've done "sudo su"
should include both new and old usr paths for "/usr/sbin" and "/usr/bin".
Now you will be able to move the whole "/usr" in that terminal without a headache. And do it as described in "/usr problem" above. All new Linux releases have system guardians which prevents /usr to be a symlink.