- How do I change the device on which ubuntu boots?
- Move GRUB and boot partition to another disk
- Backstory
- Guide
- Determine disks
- Create partition and file system
- Chroot into OS
- Changing /boot
- Install Grub
- Final steps
- Further thoughts
- How do I change the boot partition from one drives partition to a different drives partition?
How do I change the device on which ubuntu boots?
I have a laptop with an internal SSD (let’s call it samsung-SSD), and I would like to install ununtu 12.04 on an external SSD connected via USB3 (let’s call this one vertex2). I downloaded an ubuntu iso and created a bootable usb key from it. Then I booted on the usb key, and followed the ubuntu install:
/dev/sda is the internal samsung-SSD. /dev/sdb is the usb key from which I am installing /dev/sdc is the external vertex2 on which I am installing.
The install completes, I boot ubuntu this time from the vertex2. But as the usb key is not here anymore, the vertex2 is known as /dev/sdb. Ubuntu tried to boot on /dev/sdc and fails, because /dev/sdc does not exist. So a BusyBox terminal shows up, with the message
# only vertex2 is plugged > ls /dev | grep sd /dev/sda # samsung SSD /dev/sda1 /dev/sda2 /dev/sdb # vertex2 # removing vertex2 > ls /dev | grep sd /dev/sda # samsung SSD /dev/sda1 /dev/sda2 # pluging usb > ls /dev | grep sd /dev/sda # samsung SSD /dev/sda1 /dev/sda2 /dev/sdb # usb stick # pluging vertex2 in addition to usb stick > ls /dev | grep sd /dev/sda # samsung SSD /dev/sda1 /dev/sda2 /dev/sdb # usb stick /dev/sdc # vertex2
> exit # this command tries to resume the boot process
And this way I can boot on ubuntu. So my question is: how do I configure ubuntu so that it tries to boot on /dev/sdb instead of /dev/sdc? Can I achieve that with mount and chroot ? Edit: my fstab :
# # / was on /dev/sdc2 during installation UUID=2586eee0-5f24-462b-b44d-5c9a25aebe6c / ext2 errors=remount-ro 0 1 # swap was on /dev/sdc5 during installation UUID=56498034-3361-4d7d-ae7f-dc8e69b582ed none swap sw 0 0
What I don’t understand is that in my fstab, the vertex2 is known by it’s UUID. So it shouldn’t matter if it appears as sdb or sdc. And yet I am able to boot only when it appears as sdc. I tried to replace the vertex UUID by sdb in the fstab, and it didn’t help. It’s like some other file is used to mount drives on boot. Edit 2: In the boot menu, I can edit entries. If I edit the ubuntu entry (by pressing e ), I see this text:
recordfail gfxmode $linux_gfx_mode insmod gzio insmod part_msdos insmod ext2 set root='hd2,msdos2' if [ x$feature_platform_search_hint = xy ]; then search --no-floppy --fs-uuid --set=root --hint-bios=hd2,msdos2 --hint-efi=hd2,msdos2 --hint-baremetal=ahci2,msdos2 25\ 86eee0-5f24-462b-b44d-5c9a25aebe6c else search --no-floppy --fs-uuid --set=root 2586eee0-5f24-462b-b44d-5c9a25aebe6c fi linux /boot/vmlinuz-3.5.0-17-generic root=/dev/sdc2 ro quiet splash $vt_handoff initrd /boot/initrd.img-3.5.0-17-generic
If I change sdc2 to sdb2, and press f10 to resume boot, ubuntu boots sucessfully (no BusyBox prompt). This text is located in /boot/grub/grub.cfg . But I should not edit it directly. It has this header:
# # DO NOT EDIT THIS FILE # # It is automatically generated by grub-mkconfig using templates # from /etc/grub.d and settings from /etc/default/grub #
> sudo grep -r "sdc2" /etc/grub.d/ > #no results
Where sdc2 comes from ? How do I change it permanently to sdb2 ? (The change in the boot menu is only temporary.)
Move GRUB and boot partition to another disk
This guide describes how to move GRUB and /boot to another disk in order to boot the existing system from it. It is for legacy BIOS (non UEFI) systems and Debian based Linux distributions, though the overall steps should be fairly similar.
Backstory
I have an old HP Microserver Gen 8 used as backup server. Additionally, to the 4 disks in the main drive bay I have added an SSD as boot and system drive in the ODD slot. The problem with this old hardware, and its BIOS is that in order to boot from the additional SSD, the RAID controller needs to be used as the BIOS cannot detect the SSD in SATA AHCI mode.
This means that I needed to configure RAID 0’s for all the drives in the machine in order to have them exposed as single drives to the OS to use them for the ZFS pool holding all the data. This is of course not the best idea as the RAID controller adds another level of complexity and abstraction and hides the disks from the OS to a certain degree (e.g. SMART data).
In order to finally switch over to SATA AHCI mode I needed another way to boot the system. Luckily, one can place an SD card or USB flash drive in the server which will be available as another disk and the BIOS can boot from it. Using the SD card only as /boot should keep writes to it to a minimum and thus a short lifetime due to wearout should be no issue.
Guide
Start by booting from a live CD.
Determine disks
Using lsblk you can determine which disks are present. In my example, the main OS disk is sda and the other disk (SD card) from which I want to boot is sdb .
Another way to determine which disk is which is using the output of ls -l /dev/disk/by-id . This will print certain values of each disk such as make, model and serial number and which sdX it is. In the case of the SD card in the HP Microserver it showed up as
usb-HP_iLO_Internal_SD-CARD_000002660A01-0:0 -> ../../sdb
Create partition and file system
Once we determined which disk we want to use to boot from in the future, we need to partition it with parted .
Consider the size of the new boot partition. I did set it quite large here at about 5 GiB. We need to set the boot flag for the partition so older BIOS’ will consider booting from it. This was definitely necessary for my Microserver.
parted /dev/sdb (parted) mklabel msdos (parted) mkpart primary ext4 2048s 5G (parted) set 1 boot
Next we need to format the partition with a file system.
Chroot into OS
After this we need to chroot into the installed OS from our live CD. First we need to mount the system disk and bind mount the dev , sys , and proc directory to it.
mount /dev/sda1 /mnt mount --bind /sys/ /mnt/sys/ mount --bind /proc/ /mnt/proc/ mount --bind /dev/ /mnt/dev/
Now we can chroot into the installed OS by running
Changing /boot
Right now /boot usually should be just a directory on the installed OS’ root file system.
Let’s change to the root of the installed OS.
First we rename the directory to have it on the side.
Next we create an empty directory of the same name.
After this we need to add an entry to the /etc/fstab file so that the new boot partition will be mounted automatically.
Edit it with your editor of choice, the line should look like this:
UUID=712371b3-b4de-49ab-aaed-2a7097630b3a /boot ext4 errors=remount-ro 0 1
By using the UUID of the partition we can make sure that the partition will be mounted even if the sdX naming might be changing. To find out the UUID run ls -l /dev/disk/by-uuid . You will see which UUID points to your partition, in my case sdb1 .
By running mount -a we can test if the entry in /etc/fstab works correctly. The command mount should now show a line like the following:
/dev/sdb1 on /boot type ext4 (rw,relatime,errors=remount-ro)
Finally, for this step, we need to copy over the contents of the old /boot directory to the new one.
Install Grub
To install GRUB in the MBR of the new disk run the following command:
At last, we need to run the following command:
Final steps
This should be it. Shutdown the running live CD and change the boot order in the machine’s BIOS. If you do this on an HP Microserver Gen 8, you should now change from the RAID controller to the SATA AHCI setting and change the boot order to USB.
On the next boot, the machine should load GRUB and the kernel image from the new disk.
Further thoughts
If you want to achieve something similar with an UEFI booting system, it should be enough to move the EFI partition to the new disk and use efibootmgr to create or update the boot entry on the motherboard.
How do I change the boot partition from one drives partition to a different drives partition?
I have two hard drives each with one primary partition, each with their own Ubuntu 18.04 install sda1 40gb sdb1 512gb The only purpose of sdb1 is to hold compressed backups of sda1 Normally the computer boots and operates from sda1. What I am trying to accomplish (via SSH konsole) is to change the boot device from sda1 to sdb1, then issue a reboot so that when it reboots sdb1 loads its copy of ubuntu, next I can create a backup image of sda1 and store it as a compressed image file on sdb1, after creating the backup image I would then want to change the boot drive back to sda1. The purpose of this exercise is to be able to create a backup image of sda1 while it is unmounted to sdb1 using nothing but SSH. I am familiar with changing boot devices in bios, or booting from thumb drives to create backups, I am trying to accomplish my goal without any physical interaction with the computer. (I do have physical access in-case things go wrong) I have attempted changing the UUID entry in /etc/fstab to point to the UUID of sdb1 but grub/fstab still loads sda1 on boot/reboot.
I suspect you’ll have to do this via BIOS/EUFI using whatever API/ABI they provide; which will be hardware specific & unrelated to the OS (Ubuntu) you are using. Anything on your fs (inc. /etc/fstab/) will only affect things after the system has already started to boot a drive, which doesn’t meet your requirements; ie. it needs to be your hardware (bios/eufi). Some devices (eg. hp & dell servers I have) provide a internal usb inside the machine which can be used as a boot device; if you machine has this — select (via bios/eufi) that device and modify the data on that to boot the wanted drive
@guiverc if the data on a usb device can point to the drive that I want to boot, then why cannot I have drive sda1 point to drive sdb1 as what I am wanting to boot?
@mook765 the reason for two ubuntu installs, is so that both the backup and restore of sda1 can be performed while sdb1 is the active drive, this way sda1 is unmounted, creating a backup or restore to an active drive is not a good idea. I am not looking to boot a compressed image, they are only compressed for storage purposes. My main obstacle at this point is finding a way to put some sort of instructions on SDA1 that says «NO, go boot SDB1 instead) those instructions I assumed would be modifying /etc/fstab or grub
the usb-device I’m talking about is often called DOM, and can be a SDRAM card or other device (inc. usb). It often has a 2GB limit which is plenty to bootstrap a system. It is treated differently by the machine BIOS and is different to the external devices, and is hardware specific. Your device probably doesn’t have it (consumer or low-end server) so it won’t be of help to you. You’ve now been offered two answers anyway 🙂