Lazy optimize running Linux from USB stick
Installing a Linux distro on a USB stick is as easy as installing it on a normal HDD / SDD.
Making that Linux work well on that USB stick was quite hard, as there is little or no information on the Internet.
Note: booting and running Linux from a USB stick will be slow and it will most likely shorten the life of your USB stick in the long run.
My setup consisted of a i7 Lenovo laptop and a 128GB Kingston HyperX Savage USB 3.0, which supposedly has 350MB / 250MB sequential read / write speed.
I chose Ubuntu 19.10 ISO netboot, which I flashed using Rufus on another USB 2.0 stick.
The following bash commands were run under root user after I successfully installed Ubuntu 19.10 on the 128GB Kinston HyperX Savage USB 3.0 stick.
#!/bin/bash # Disable swap # Having swap enabled is overkill for a USB stick, # as the disk will be overwhelmed by lots of useless # random reads / writes. # you can add this command to /etc/rc.local file swapoff -a # Change virtual memory settings using sysctl # Dirty memory is the memory that has not been # synced on the disk. # Increasing the dirty memory ratio will # make the kernel sync less often, thus # leading to less random reads / writes. cat > /etc/sysctl.conf <<- EOM vm.dirty_ratio=80 vm.dirty_background_ratio=80 vm.dirty_expire_centisecs=0 vm.dirty_writeback_centisecs=0 vm.swappiness=0 EOM # Apply the sysctl config sysctl -p # Disable ext4 journal and enable writeback # mode for the same reason: # less often random reads / writes # The following commands need to be run # either from a live iso, either from # single user mode. You cannot change the # ext4 journaling for a in-use device. # To run from single user mode, # at boot time you need to alter # the kernel boot line and # add S to the end of it. # After Linux boots in single user # mode you need to unmount the USB device tune2fs -o journal_data_writeback /dev/sdXn # where X is the USB stick device and n is # the root partition number tune2fs -O ^has_journal /dev/sdXn # After you reboot in normal user mode, # change /etc/fstab so that the root # mount line looks like: UUID=b6273073-5159-4fc5-914e-79f4f349e8c9 / ext4 defaults,data=writeback,discard,noatime 0 0 # Note the additional # data=writeback,discard,noatime
That's all, folks!Tweet