newspaint

Documenting Problems That Were Difficult To Find The Answer To

Moving Linux Boot Partition to USB Drive

I have a Xubuntu (Ubuntu) Precise 12.04.3 LTS (long term support) installation on two hard drives, each encrypted using cryptsetup (LUKS) and then mirrored using mdadm.

To date I’ve had a separate /boot partition on each magnetic spinning disk. However I wished to move that /boot partition to USB drive so I wouldn’t need to rely on booting from magnetic disk anymore.

These are the steps I took to copy the /boot partition from my existing partition (/dev/sda2) onto a new partition on a USB flash drive (/dev/sdc).

In this example I took a USB stick (8GB in size) and created a 512 megabyte boot partition formatted in ext4 (/dev/sdc1) and also created a FAT32/VFAT partition with the rest of the space on the USB drive.

Check USB Vendor/Model

$ cat /sys/block/sdc/device/vendor
SanDisk
$ cat /sys/block/sdc/device/model
Cruzer Facet

Partition USB Stick

$ fdisk /dev/sdc # (assume sdc is USB stick)

d (delete partition)

n (new partition)
  p (primary)
    1 (partition number 1)
      [enter] (first sector, default 2048)
      +512M (last sector)
a (toggle bootable flag)
  1 (parition number 1)
n (new partition)
  p (primary)
    2 (partition number 2)
      [enter] (first sector, default 1050624)
      [enter] (last sector, whole of disk)
t (change parition type)
  2 (partition number 2)
    c (W95 FAT32 LBA)
w (write partition table to disk)

Format Partitions

mkfs.ext4 -L "usb_boot" /dev/sdc1
mkfs.vfat -n "USB_FAT32" /dev/sdc2

Copy Existing Boot

mkdir /mnt/sdc1
mount /dev/sdc1 /mnt/sdc1 -t ext4
rsync -avP /boot/* /mnt/sdc1/

Install Grub

cat /boot/grub/device.map
grub-install --recheck --debug /dev/sdc

Note that if your device.map file is somewhere else you may want to do the following instead (for example if your /boot was on /dev/sdc1 and that had been mounted at /media/myuser/boot_usb):

grub-install --root-directory /media/myuser/boot_usb /dev/sdc

Reboot

shutdown -r now

What happens, though, if you’re confronted with a text Grub menu (because it hasn’t found your boot USB drive)? It will display something like:

                        GNU GRUB

    Minimal BASH-like line editing is supported. For the first word,
    TAB lists possible command completions. Anywhere else TAB lists
    possible device or file completions. ESC at any time exits.

grub>

In this case you’ll need to find the partition containing your grub and Linux/initramfs files. To do that you can try the following:

grub> ls
(hd0,0) (hd0,1) (hd1,0)
grub> ls (hd0,0)/
grub> ls (hd0,1)/
grub> ls (hd1,0)/

… until it displays files like vmlinuz etc. Once you figure out what device has your files you can then prepare to boot by loading the Linux image (kernel) and initramfs file.

e.g.

grub> set root=(hd0,0)
grub> linux /vmlinuz-4.4.0-166-generic root=ZFS=rpool/ROOT ro boot=zfs nosplash
grub> initrd /initrd.img-4.4.0-166-generic
grub> boot

Now, if, after doing this, the Linux boot sequence comes up with the following:

Welcome to emergency mode! After logging in, type "journalctl -xb" to view
system logs, "systemctl reboot" to reboot, "systemctl default" or ^D to
try again to boot into default mode.
Give root password for maintenance
(or press Control-D to continue):

… then enter the root password. And enter “journalctl -xb” as instructed. If you see something about a dev-disk-by-uuid not timing out then your boot USB may not have the same UUID as it did previously (especially if you rebuilt the partition table). In this case enter:

# ls -l /dev/disk/by-uuid

to find out the ID of your boot disk. Then update/edit /etc/fstab with the correct UUID, and try rebooting again.

See Also

2 responses to “Moving Linux Boot Partition to USB Drive

  1. #359 January 14, 2016 at 8:55 am

    thank you for a great explenation! this was the only manual that actually worked for me to move my /boot partition to USB.

    however, in the section “Install Grub” i had to use “grub-mkdevicemap” first, since device.map file doesn’t exist in the latest Ubuntu versions (15.10).

    still i have a problem/question: i don’t want to have a second FAT32/VFAT partition on the USB where the /boot is. i want the rest of the space on USB to be to be unallocated to maybe (if in need) later format it as a Truecrypt partition. but whatever i tried, my computer couldn’t boot if the USB wasn’t partitioned EXACTLY how you described. if i changed second partition to cleared, unformatted, even EXT4, my computer couldn’t boot from USB – the first partition was only described as UEFI (and thus bootable) if there was also a second FAT32/VFAT partition present.

    do you have any suggestion how to solve this problem. thanks!

  2. #359 January 20, 2016 at 8:07 am

    after the latest Ubuntu kernel updates i figured that the above solution actually haven’t worked for me. i started my computer by booting from USB where i put my /boot partition according to the instructions above but when i installed the kernel updates with apt-get they went directly to the old /boot location on my hard disk and not to /mnt/sdb1

    i tested this by rebooting my computer from USB. the grub UI offered me the new kernel image that wasn’t on my USB but on the old /boot on hard disk so i’m sure that somehow i don’t boot from USB.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: