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
Recent Comments