9

There are often occasions where Ubuntu users need to do a quick reboot, e.g. after a kernel update or when testing some settings that require rebooting.

I would like to have a method (e.g. a script) that allows me to save as much time as possible performing a reboot.

Those are things which can probably be avoided to reduce reboot time until the user session is ready:

  • if possible, skip BIOS/UEFI/firmware initialization and POST
  • skip GRUB menu (preselect the desired entry once and directly boot it without any timeout)
  • skip login screen (use passwordless auto-login once)

Maybe there are even more things that could be skipped for a quick reboot.

I have seen the RapidReboot spec in the Ubuntu Wiki, but the page was last edited 2010-02-03.

But even if we might not yet properly be able to use this described kexec to skip firmware initialization and stuff, temporarily preselecting the GRUB entry and using auto-login for exactly one reboot should be possible. How can I set this up?

I'm using Ubuntu 16.04, 64 bit with Unity DE, lightdm and GRUB in dual boot with Windows 10 (and soon some more Linux distros) on an Acer Aspire E5-773G machine with UEFI.

7
  • @Rinzwind I have an SSD, but 24 seconds (if systemd-analyze is correct: Startup finished in 4.130s (firmware) + 3.107s (loader) + 9.403s (kernel) + 10.658s (userspace) = 27.299s is still not fast enough >:D Firmware init, GRUB menu and entering password at the login screen still consume too much time for impatient me.
    – Byte Commander
    Aug 30, 2016 at 13:36
  • I wonder if putting your system in Legacy boot, which I am assuming is in UEFI since it has firmware boot time listed on there, if that will help. Not sure why you are getting 3.1 seconds on the loader. On my host that is older, I run a SATA II SanDisk SDSSDA240 240GB SSD, with a Legacy BIOS, and my boot time is Startup finished in 5.301s (kernel) + 12.865s (userspace) = 18.167s. Oh, and I have my system login automatically.
    – Terrance
    Aug 30, 2016 at 14:10
  • 1
    The option for grub is not going to be quicker: any change to grub conf requires a sudo update-grub and that is going to take a relatively long time.
    – Rinzwind
    Aug 30, 2016 at 14:56
  • 1
    Reboot Ubuntu without going through the BIOS POST I remember that I had instructions a work once to reboot a machine by reloading the kernel avoiding post and I read it somewhere else too but I can't find the command anymore. It probably wasn't a very clean method either.
    – LiveWireBT
    Oct 5, 2016 at 2:10
  • 1
    Grub has something called recordfail that I've never used. But if you downloaded grub code you could modify it duplicating that code into something called fastboot which would bypass countdown timer and auto select the first menu option. You can get the source code here: [ftp.gnu.org/gnu/grub/] The next step is to call grub directly from Ubuntu. As grub resides on the MBR an ASSEMBLER branch might be required to execute it after the fastboot grub environment variable has been set. Prior to that a script has to be run to set auto-login, which is automatically reversed in startup. Oct 5, 2016 at 2:34

3 Answers 3

4

Reboot times

We can procrastinate rebooting at times in this world of "overly busy" and not wanting to waste 5 or 15 seconds. When I had first looked at this question my boot times had gone from 45 seconds to 14 seconds with an SSD and systemd tweaking.

Recently I acquired a "modern" laptop with a NVMe M.2 Gen 3.0 x 4 SSD with blazing 3.4 GB/s Windows read speed and 2 GB/s Linux read speed. So it should be faster right? Um... no. It's painfully slower:

$ systemd-analyze
Startup finished in 6.823s (firmware) + 4.447s (loader) + 3.467s (kernel) + 8.412s (userspace) = 23.151s

This time is actually after tweaking. It was much worse 80 seconds because nVidia and Dell choose to cut power to HDMI audio on nVidia GTX970M graphics card which drives the HDMI port. As such a setpci command was required which caused "low grapics error" message and you had to wait 20 seconds for mouse pointer to proceed. Then it would reload all graphic drivers and restart lightdm.

Another problem with the "modern" laptop is BIOS POST was taking 15 seconds. Tweaks in BIOS to turn off hardware error checking, turn off loading boot drivers for NICs and other tweaks I can't remember just now changed BIOS POST time to 6 seconds. By comparison the "old" laptop BIOS POST time was only a couple of seconds using Legacy BIOS CSM and no BIOS tweaking.


Ubuntu's RapidReboot project from 2010

The Ubuntu RapidReboot project mention in ByteCommander's post is from 2010. The project's rationale is summed up thusly:

Rationale

There are a few cases where we can safely assume the user does not want to see the boot loader; in these cases, we should use kexec to avoid long reboots, BIOS POSTs, and boot loader time. This can take 10 seconds in optimal situations; but with SCSI or RAID BIOS and network boot roms, the time can climb to 20, 30, or even over 60 seconds, even in cases where the time between loading the kernel and seeing the log-in screen is 30-60 seconds.

The project's usage examples:

Use cases

  • Bob has just upgraded his kernel; update-notifier informs him he must reboot for the changes to take effect, and the 'Restart' button uses kexec to make this faster.
  • Alice just upgraded dbus; update-notifier tells her to restart, and uses kexec.
  • Seveas has performed several updates in the past month, and now is under three times as much memory pressure due to different programs using different copies of shared libraries. He decides to reboot to clear this up, and uses a "Quick Reboot" to make this faster.
  • Keybuk just installed Ubuntu Edgy and wants to boot into it. The LiveCD loads the target kernel and initrd with kexec; umounts all disk-based file systems; sync; and then uses kexec to start the new system rather than rebooting.

Using kexec to reboot

kexec is the magic spell we cast to reboot without BIOS post and some other low level discovery mechanisms such as RAID setup.

Kernel must be compiled with CONFIG_KEXEC flag

The first step is to ensure your kernel was compiled with the CONFIG_KEXEC=y option:

$ uname -r
4.14.2-041402-generic
$ grep KEXEC= /boot/config-`uname -r`
CONFIG_KEXEC=y

The first command uname -r is optional just to show what kernel you've booted with. As we can see on the second command the kernel is compiled by Ubuntu team with the necessary flag set. Not sure when this was added but likely <= year 2012.

Install kexec-tools

The next step is to install the kexec tools for user space:

$sudo apt install kexec-tools

After normal binary downloads you are greeted with this screen:

kexec-tools install.png

I answered No in part because of this bug report that the option is not relevant in systemd.

Then the installation continues (snippet shown below):

Unpacking kexec-tools (1:2.0.10-1ubuntu2.4) ...
Processing triggers for man-db (2.7.5-1) ...
Processing triggers for systemd (229-4ubuntu21) ...
Setting up kexec-tools (1:2.0.10-1ubuntu2.4) ...
Generating /etc/default/kexec...
Generating grub configuration file ...
  (... CUT ...)
Found Windows Boot Manager on /dev/nvme0n1p2@/EFI/Microsoft/Boot/bootmgfw.efi
Found Windows Boot Manager on /dev/sda1@/efi/Microsoft/Boot/bootmgfw.efi
Adding boot menu entry for EFI firmware configuration
done
Processing triggers for systemd (229-4ubuntu21) ...

Configuring kexec

Our cohorts at ArchLinux have great documentation for kexec but it needs massaging for Ubuntu / Debian distributions.

Basic usage are with two commands:

sudo kexec -l /boot/vmlinuz-`uname -r` --initrd=/boot/initrd.img-`uname -r` --reuse-cmdline
sudo kexec -e

from this point forward it appears every time you click reboot it automatically reloads the last kernel in fast reboot mode.

Using Systemd to load kexec

Create the file /etc/systemd/system/[email protected]:

[Unit]
Description=load %i kernel into the current kernel
Documentation=man:kexec(8)
DefaultDependencies=no
Before=shutdown.target umount.target final.target

[Service]
Type=oneshot
ExecStart=/usr/bin/kexec -l /boot/vmlinuz-%i --initrd=/boot/initrd.img-%i --reuse-cmdline

[Install]
WantedBy=kexec.target

Then enable the service file for the kernel you want to load, for example to simply set the current kernel uname -r:

$ sudo systemctl enable kexec-load@`uname -r`
Created symlink from /etc/systemd/system/kexec.target.wants/[email protected] to /etc/systemd/system/[email protected].

Then to kexec:

$ sudo systemctl kexec

If you have youtube running there might be a system inhibitor preventing you from rebooting in which case use:

$ sudo systemctl kexec -i

If you wish to load a different kernel for the next kexec, for example 4.12.2-041202-generic, disable the service for the current kernel and enable the one for the new kernel:

$ sudo systemctl disable kexec-load@`uname -r`
$ sudo systemctl enable [email protected]

More to come

I have to post this answer now as it's time to boot, fix, boot, fix, repeat. I have a complicated setup where systemd loads five nVidia drivers which I have to unload, run setpci to power on audio to hdmi, reload nVidia drivers and restart lightdm. It might take awhile to work out.

After getting the cli methods working I'll create a desktop shortcut for one-click rebooting with sudo password prompt. After that I'll create a desktop shortcut to a yad dialog box allowing you to pick from installed kernels to reboot. Similar to grub's advanced options menu.

10
  • Did you got farther? On 18.04, kexec work when I follow your basic section but when I invoke reboot graphically, it reboot normally.
    – sup
    May 23, 2018 at 17:34
  • @sup Are you trying to setup a desktop shortcut to reboot the current kernel? May 23, 2018 at 23:03
  • No, I would like the Restart menu item in the panel (I am on Unity 7 still, Gnome Shell feels weird) lead to kexec. and issuing sudo reboot could lead to it too, but I think I know how to do that already.
    – sup
    May 24, 2018 at 5:24
  • @sup It sounds like you have it under control. If you hit a stumbling block let me know, or perhaps post a new question for everyone to address (including myself). May 24, 2018 at 10:25
  • Actually, not - I do not know how to make the gui invoke a reboot. How do I change what happend when I click reboot from the menu?
    – sup
    May 24, 2018 at 14:26
1

In order to extremely fast reboot a linux machine, call the reboot kernel function. It will flush the disks (at least in kernel version 3.x) and reboot immediately. There is nothing in between, no OS functions, scripts etc... just the kernel calls.

How?

Create reboot.c with contents:

#include <stdio.h>
#include <sys/reboot.h>


int
main (int argc, char **argv)
{
    if (reboot (RB_AUTOBOOT) < 0) {
        fprintf (stderr, "%s: call to reboot() system call failed.\n",
             argv[0]);
    }

    return 0;
}

Compile reboot.c into reboot:

gcc reboot.c -o reboot

Execute reboot (it will flush data to disks and reboot, that’s it!):

./reboot

Have a nice reboot!

https://github.com/masikh/reboot

-2

If you want to partially restart your computer (and not speed-up the boot), here is my understanding : there are 3 main parts in Linux boot :

  1. Step 1 : Computer boot until boot manager / OS selection (rEFInd, Grub, ...)
  2. Step 2 : Start Linux and main services
  3. Step 3 : Start the session
    • user login
    • window manager like Cinnamon, KDE, Gnome, ...)

Restart step 3 only :

  • Type Alt-F2 and R
  • Or sudo service $(cat /etc/X11/default-display-manager) restart

Restart step 2 and 3 only :

I don't know how to restart Linux from your boot manager


Restart all steps

  • In the main menu, choose "restart"
  • From command line, type sudo reboot

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .