I'd like to have Ubuntu installed on a USB stick, that I could plug in (almost) any computer and boot from it, and that I could use (almost) normally, as if it was installed on that computer. I'd also like to have a data partition on it that could be used by (almost) any computer, mostly to store some portable apps, just in case.
I've been told that if I installed Ubuntu in BIOS mode, I wouldn't be able to boot from a computer in UEFI mode, and vice versa.
Since I'd like to be able to boot on any computer, it's a big problem.
What can I do to be able to boot on computers in BIOS or UEFI modes in my case?
I've been searching for solutions but most of them are for specific needs that are different than mine, so it's difficult to extract the minimum requirements from those solutions to apply them to my problem.
Is there a simple, "clean" and quite generic solution to this problem?
I'd appreciate some explainations about the reasons why a specific solution is good or a specific step is needed.
My current setup:
- USB stick: SanDisk Ultra Fit (32 Go)
- OS: Xubuntu 16.04 i386
- installation: full install (from a Live USB built with Rufus)
- drivers: none proprietary
- file systems:
- partition table: GPT
- RAM:
/tmp
, tmpfs,defaults,nosuid,nodev,noexec,mode=1777
- swap:
vm.swappiness=5
- swap partition: 512 Mio, for emergency
- zRAM
- swap files:
/var/swap/hib.swp
: same size as RAM, for hibernation, generated by a script at startup/var/swap/opt.swp
: if needed, generated by a script
- overprovisioning: ~5.6%
- built-in: ~1.2 Gio (apparently)
- added: 512 Mio
- scheduler:
deadline
Best Answer
NB : Here I present the manual way to proceed. It's a bit more complicated (not impossible thought) but you can control exactly what you want/need. If you don't understand anything, maybe using a software like MultiSystem would be a better solution for you.
There are two ways to create such usb key. You can use a mdos/MBR partition table or GPT partition table. Both have advantages and drawback, I'll try to describe the mdos version first, and may come to the GPT version later when I have more time.
mdos/MBR partition table
First, be sure to have all you data backup. These operation may be dangerous if you don't know what you do, and could erase your hard drive if you do bad manipulations, so be sure to know what you are doing before typing a command.
Step 1: Find your usb device
Use the command
to find the name of your USB device. You can for example run
lsblk
, plug your key, and run one more timelsblk
to see what changed. The device that appears is your key, and will be noted has/dev/sdX
.Step 2: Create the partitions
You can use any program you like :
gparted
,fdisk
,cfdisk
(a command line tool forfdisk
)... Then do the following operationssudo gparted
orsudo fdisk /dev/sdX
depending on the program you choose)msdos
partition table (ingparted
it's in "Device/Create a new partition table", withfdisk
typeo
.If you want to create them with
fdisk
, you should have something like this :To do that follow these steps:
Step 3: Mount the partitions
Now, mount them to write on them
Step 4: Install grub on the two partitions (EFI and non-EFI)
Then, you need to install grub on the two partitions. Be sure to have grub installed, and then run :
Step 5: Copy the iso file and configure grub
Then copy your iso file in the following folder :
and put the grub configuration in the two grub configurations
/tmp/usb2/grub/grub.cfg
and/tmp/usb3/grub/grub.cfg
(NB : it can be grub2/grub.cfg depending on your grub version)NB : you should edit this file to match the good iso file, here we supposed we put the file in
/tmp/usb3/linux-iso/xubuntu-16.04.1-desktop-amd64.iso
.Reboot, and try it in both UEFI and non-UEFI mode : it should work, but you shouldn't have persistence yet !
Step 6 : add single persistence
Adding persistence is very easy. The idea is to create on the 3rd partition (in fact any FAT32 partition would be accepted) an empty file
casper-rw
that will contain some ext2 filesystem. It will be then positionned above the iso file, so that if the system is modified, the filecasper-rw
will record these modifications. If you want to record only the home, then do exactly the same steps, but with the filenamehome-rw
instead ofcasper-rw
(it may be usefull if you mind only to the document data, and if you prefer to avoid the problems with full persistence explained after). Finally, note that instead of a file, you can just create a partition in ext2 whose label iscasper-rw
orhome-rw
and it will give exactly the same result, except that you will be able to have more than 4Gb of modifications (max size in FAT32 system).To create the file, just run :
(here it's 512M, but you can change the size of the data up to 4G)
and add
persistent
in the linelinux ...
in the files/mnt/usb{2,3}/grub/grub.cfg
(modify the two files). It should looks like this :Now reboot : you should have persistence ! You can even mix persistent and non-persistant files like this :
Multiple persistence
For the moment, all the OS in the key have the same persistence file. And if it's a
casper-rw
which is shared, if can break everything (the data in one OS has nothing to do with the data in the other OS). To have one persistent file for each system, it's very easy, put the file in a given folder, for exemplepersist/xubuntu
:and add
persistent persistent-path=/persist/xubuntu
in thegrub.cfg
files :You can also note that I didn't find a way to have one
casper-rw
partition for each OS, so I don't see how to have multiple persistence AND persistence of more than 4Gb for all the OS. If anyone has an idea, I'd be glad to hear about it. Maybe partition mounting could temporary solve this problem ?Drawback of persistence
When you use persistence, you should be aware that upgrading may be dangerous. It could indeed break your system if the modifications touch the kernel, so avoid to do too much of them. Moreover, sometimes it doesn't fit well when you change the computer, some people explains that they lost internet when the internet card changed. If you want to upgrade your system, you should consider to install it directly on the usb key (like if the usb key was a disk), but this method could fail if you try your key on a computer which needs proprietary software that are not included in the installed OS. If someone has a solution that bypass these drawbacks, I would be glad to hear about it.
Sources
To configure grub to match other systems or if you want to use GPT instead of MBR, you may be interested by the following links :
Enjoy, and not hesitate to ask if you have questions.