1

While running a bash script, I would like to programmatically re-boot from a USB stick. The bios is set up to boot from the HD. Is this possible? Any hints?

Why do I want to do this??? I'm updating to Ubuntu 20.04 on a hundred or more machines that are installed all over the country in remote locations. I have to export the database and then import it after the updates. The whole process takes hours to run. I want the entire process to happen automatically so the person doing the upgrade doesn't have to babysit.


Thanks for your generous responses. I can give more detail: The bios is set to boot from my hard drive. Grub2 then boots the normal OS.

If I plug in my USB thumb drive, I can boot into Ubuntu from it by hitting the F11 key and selecting the USB thumb drive. So, I'm thinking if I execute sudo update-grub, it should find the OS on the thumb drive and put it in the grub list.

The following are the menuentries in grub.cfg

dfr@m9kmission:~$ grep -i "menuentry '" /boot/grub/grub.cfg 
menuentry 'Ubuntu' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-simple-59e8ab4e-1d3d-4a1b-b97b-ec10766929e6' {
    menuentry 'Ubuntu, with Linux 4.15.0-112-generic' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-4.15.0-112-generic-advanced-59e8ab4e-1d3d-4a1b-b97b-ec10766929e6' {
    menuentry 'Ubuntu, with Linux 4.15.0-112-generic (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-4.15.0-112-generic-recovery-59e8ab4e-1d3d-4a1b-b97b-ec10766929e6' {
    menuentry 'Ubuntu, with Linux 4.15.0-106-generic' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-4.15.0-106-generic-advanced-59e8ab4e-1d3d-4a1b-b97b-ec10766929e6' {
    menuentry 'Ubuntu, with Linux 4.15.0-106-generic (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-4.15.0-106-generic-recovery-59e8ab4e-1d3d-4a1b-b97b-ec10766929e6' {

After I run sudo update-grub, the menuentries are the same, the OS on the thumb drive isn't added. Could it be that the os-prober thinks it shouldn't add an entry for a removeable drive? I think if I could get the update-grub to add a menuentry for the os on the thumb drive, I could run sudo grub-reboot and then when I reboot the computer, it would boot from the thumb drive. The next reboot would go back to the normal OS.

Questions:

  1. Am I thinking correctly?
  2. How can I get update-grub to make an entry for the os on my thumb drive?
  3. If I can't, should I insert my own entry into /boot/grub/grub.cfg?

The regular os menuentry is as follows. Maybe I could pattern my new entry after it: (It looks pretty scary though.)

menuentry 'Ubuntu' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-simple-59e8ab4e-1d3d-4a1b-b97b-ec10766929e6' {
recordfail
load_video
gfxmode $linux_gfx_mode
insmod gzio
if [ x$grub_platform = xxen ]; then insmod xzio; insmod lzopio; fi
insmod part_msdos
insmod ext2
set root='hd1,msdos1'
if [ x$feature_platform_search_hint = xy ]; then
  search --no-floppy --fs-uuid --set=root --hint-bios=hd1,msdos1 --hint-efi=hd1,msdos1 --hint-baremetal=ahci1,msdos1  59e8ab4e-1d3d-4a1b-b97b-ec10766929e6
else
  search --no-floppy --fs-uuid --set=root 59e8ab4e-1d3d-4a1b-b97b-ec10766929e6
fi
    linux   /boot/vmlinuz-4.15.0-112-generic root=UUID=59e8ab4e-1d3d-4a1b-b97b-ec10766929e6 ro  
initrd  /boot/initrd.img-4.15.0-112-generic

}

The normal os is on sdb1.  The thumb drive is sdc1.
    dfr@m9kmission:~$ blkid
    /dev/sda1: UUID="d210d958-9eea-4786-9d9f-8311f852b9df" TYPE="ext4" PARTUUID="98f91627-01"
    /dev/sdb1: UUID="59e8ab4e-1d3d-4a1b-b97b-ec10766929e6" TYPE="ext4" PARTUUID="4c6a03f4-01"
    /dev/sdb2: UUID="32251f52-55d0-4c14-8746-bbf48a3984c0" TYPE="ext4" PARTUUID="4c6a03f4-02"
    /dev/sdc1: LABEL="UBUNTU 18_0" UUID="7EF3-2CBA" TYPE="vfat" PARTUUID="0ba7b71d-01"

This is the directory tree on the thumb drive.

dfr@m9kmission:~$ tree -d /media/dfr/UBUNTU\ 18_0/
/media/dfr/UBUNTU 18_0/
├── boot
│   └── grub
│       └── x86_64-efi
├── casper
├── dists
│   └── bionic
│       ├── main
│       │   ├── binary-amd64
│       │   ├── binary-i386
│       │   └── source
│       └── restricted
│           ├── binary-amd64
│           ├── binary-i386
│           └── source
├── EFI
│   └── BOOT
├── install
├── isolinux
├── pics
├── pool
│   ├── main
│   │   ├── b
│   │   │   ├── b43-fwcutter
│   │   │   └── build-essential
│   │   ├── d
│   │   │   ├── dkms
│   │   │   └── dpkg
│   │   ├── f
│   │   │   └── fakeroot
│   │   ├── g
│   │   │   ├── gcc-7
│   │   │   ├── gcc-8
│   │   │   ├── gcc-defaults
│   │   │   ├── glibc
│   │   │   ├── grub2
│   │   │   └── grub2-signed
│   │   ├── l
│   │   │   ├── linux
│   │   │   └── lupin
│   │   ├── liba
│   │   │   ├── libalgorithm-diff-perl
│   │   │   ├── libalgorithm-diff-xs-perl
│   │   │   └── libalgorithm-merge-perl
│   │   ├── m
│   │   │   ├── make-dfsg
│   │   │   ├── manpages
│   │   │   └── mouseemu
│   │   ├── s
│   │   │   ├── setserial
│   │   │   ├── shim
│   │   │   └── shim-signed
│   │   └── u
│   │       ├── ubiquity
│   │       ├── ubiquity-slideshow-ubuntu
│   │       └── user-setup
│   └── restricted
│       └── b
│           └── bcmwl
├── preseed
└── System Volume Information

It looks like the Linux files are in the Casper folder:

dfr@m9kmission:~$ ll /media/dfr/UBUNTU\ 18_0/casper/
total 2121880
drwxr-xr-x  2 dfr dfr       8192 Jul 24 07:14 ./
drwxr-xr-x 13 dfr dfr       8192 Dec 31  1969 ../
-rw-r--r--  1 dfr dfr      56234 Jul 24 07:14 filesystem.manifest
-rw-r--r--  1 dfr dfr        767 Jul 24 07:14 filesystem.manifest-minimal-remove
-rw-r--r--  1 dfr dfr       3309 Jul 24 07:14 filesystem.manifest-remove
-rw-r--r--  1 dfr dfr         10 Jul 24 07:14 filesystem.size
-rw-r--r--  1 dfr dfr 2094698496 Jul 24 07:15 filesystem.squashfs
-rw-r--r--  1 dfr dfr   68622117 Jul 24 07:23 initrd
-rw-r--r--  1 dfr dfr    9371904 Jul 24 07:24 vmlinuz

Maybe I can edit the new menuentry something like this:

menuentry 'Ubuntu-rescue' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-simple-7EF3-2CBA' {
    recordfail
    load_video
    gfxmode $linux_gfx_mode
    insmod gzio
    if [ x$grub_platform = xxen ]; then insmod xzio; insmod lzopio; fi
    insmod part_msdos
    insmod squashfs
    set root='hd3,msdos1'
    if [ x$feature_platform_search_hint = xy ]; then
      search --no-floppy --fs-uuid --set=root --hint-bios=hd3,msdos1 --hint-efi=hd3,msdos1 --hint-baremetal=ahci1,msdos1  7EF3-2CBA
    else
      search --no-floppy --fs-uuid --set=root 7EF3-2CBA
    fi
        linux   /casper/vmlinuz root=UUID=7EF3-2CBA ro  
    initrd  /casper/initrd
}

With the above edits, The Ubuntu-rescue is offered in the grub menu. When I boot it, I get a message that says:

error: file `boot/grub/i386-pc/squashfs.mod' not found

I notice on the thumb drive there is a file:

./boot/grub/x86_64-efi/squash4.mod

If I can get grub to look for the file there, it might be getting close.

Any ideas???

Thanks!

  • Doesn't look easy, especially if the hardware is not standardized, but this might be a start: https://superuser.com/questions/1148731/how-to-read-write-all-bios-settings-from-linux-cli – Brian Z Jul 31 '20 at 19:22
  • If booting with grub you may be able to use this: https://askubuntu.com/questions/859335/grub-menu-gets-skipped-when-using-hdmi-cable/859530#859530 or if systemD boot: https://github.com/systemd/systemd/blob/master/docs/ENVIRONMENT.md – oldfred Jul 31 '20 at 19:56
  • Couldn't you deploy a script on the installed system that does the post-reboot procedure? – danzel Jul 31 '20 at 21:18
  • Ypu can try efibootmgr https://linux.die.net/man/8/efibootmgr – turbulence Aug 01 '20 at 04:10

1 Answers1

1

Success! If I put this in /boot/grub/grub.cfg as the first menuentry, the computer boots from the thumb drive: (Of course you have to replace DRIVE_UUID with the real UUID of the thumb drive.)

menuentry "Boot from LIVE USB Drive" {
   search --set=root --fs-uuid DRIVE_UUID
   linux ($root)/casper/vmlinuz boot=casper quiet splash --
   initrd ($root)/casper/initrd.lz
}

In my case, DRIVE_UUID is 7EF3-2CBA

dfr@ubuntu:~$ blkid /dev/sda1: UUID="d210d958-9eea-4786-9d9f-8311f852b9df" TYPE="ext4" PARTUUID="98f91627-01" /dev/sdb1: UUID="59e8ab4e-1d3d-4a1b-b97b-ec10766929e6" TYPE="ext4" PARTUUID="4c6a03f4-01" /dev/sdb2: UUID="32251f52-55d0-4c14-8746-bbf48a3984c0" TYPE="ext4" PARTUUID="4c6a03f4-02" /dev/sdc1: LABEL="usi-utility" UUID="E2F2EA5AF2EA3309" TYPE="ntfs" PARTLABEL="My Passport" PARTUUID="a01d9bcb-63db-4837-a4b0-5bdde565421e" /dev/sdd1: LABEL="UBUNTU 18_0" UUID="7EF3-2CBA" TYPE="vfat" PARTUUID="0ba7b71d-01"

The answer came from this post: How to add a GRUB2 menu entry for booting installed Ubuntu on a USB drive?

After I boot with the thumb drive, I put grub.cfg back the way it was so the computer will reboot with its normal os.