🐧 stupid linux tricks - boot a USB stick in "copy to RAM" mode even if it doesn't have that feature (or boot from an SD card when the BIOS doesn't support that)
Nachrichtenbereich: 🐧 Linux Tipps
🔗 Quelle: reddit.com
I often don't want one of my USB ports tied up the whole time I'm using a USB-booted system, and I just realised that I could do this:
Boot from USB as normal, and let it start booting.
At the very beginning of initramfs, yank the USB stick out, and wait until you get an error message and get dumped to the emergency shell.
At this point, stick the USB stick back in and copy it to RAM. You'll need to know the exact size of the important filesystems for this; for example, if your boot stick is a 700 MB .iso file that you dd'd onto your USB stick, do something like "dd if=/dev/sdX bs=1M count=700 of=/run/bootstick.iso" or "head -c700M /dev/sdX > /run/bootstick.iso".
Yank the USB stick out again.
Set up a loop device pointing to that file, e.g. "losetup -rfP /run/bootstick.iso"
Exit the emergency shell.
Surprise! The loop device creation results in all the same /dev/disk/by-X links being created, because the relevant UUIDs, partition labels, etc. are all preset in the loop devices, just as they were on the USB stick.
Some tweaks may be needed, obviously; here are some general hints:
This works in EFI mode, so it can be useful for boot sticks that have "copy to RAM" in the CSM mode boot menu but not in EFI (e.g. the Arch Linux installer). (The reason a lot of previous copy-to-RAM boot don't work in EFI is that they operate very early and copy e.g. an ISO file into a RAMdisk before the kernel has even started, then chainload boot to the RAMdisk. That kind of hackery is harder to do in EFI. The method described here works because the kludge-y handoff to RAM is happening well after the BIOS has finished its role in the boot process; by the time the kernel is running stuff from initramfs, Linux has full control of the system already.)
How to tell when to yank out the USB stick: To start the early boot process, only two files need to be read: the Linux kernel itself, and the initramfs file. Once you start seeing boot messages that come from things in the initramfs, e.g. udev, the initramfs file is fully in memory, so you can yank the USB stick without worrying about e.g. I/O errors in the middle of reading something.
Further to previous, you may need to hit the "edit boot entry" key at the bootloader menu and remove "quiet" from the kernel command line (or add "noquiet"), as a lot of distros like to hide the boot messages by default.
The example in step 3 assumes that the initramfs has mounted the /run filesystem as tmpfs or similar, and that it's sufficiently large for this. This isn't guaranteed; you can probably create your own tmpfs mount though, and by default, a simple "mount -t tmpfs tmpfs /mountpoint" will create half the RAM's worth of space. (Note that the size of a tmpfs mount is only a maximum usage limit; a tmpfs filesystem only uses as much RAM as the size of the files in it, so this isn't a waste of memory.)
Some initramfs configurations don't seem to automatically create /dev/disk/by-X entries for loop devices - if your system still complains of not being able to find the root filesystem, you can do this manually, e.g.
mkdir /dev/disk/by-label
cd /dev/disk/by-label
ln -s /dev/loopXpY myrootfslabel
How to use this to boot from an SD card when the BIOS doesn't support that
Put the SD card in a USB card reader, and allow the system to start booting as above.
At step 2, yank the SD card reader out, pull the SD card out of the reader, and insert it into the system's SD card slot.
Skip all the RAM copying steps, and just exit the emergency shell now. With a little luck, the initramfs's udev or whatever will find the correct volume again and continue mounting the root filesystem as if nothing odd has happened.
In some systems this will work immediately; in others, it seems that the card reader needs a special driver or something that isn't loaded until later (e.g. an HP laptop I have). To get around this, you'll need to find out which modules make the SD card reader work (probably mmc something?) and ensure they're included when you build your initramfs.
[link] [comments] ...