1

Make grub boot entry to load recovery menu

Using Lubuntu 22.04 LTS

I'm trying to add a grub entry that would be able to load a menu. This menu should have 4 options:

  1. Backup sda dd if=/dev/nvme0n1 of=/mnt/data/backup/nvme0n1.img bs=4M conv=noerror status=progress
  2. Restore sda dd if=/mnt/data/backup/nvme0n1.img of=/dev/nvme0n1 bs=4M conv=noerror status=progress
  3. Back to grub root menu
  4. Reboot

I've found here how to make a bash script to make a menu that would use dd to backup or restore my system disk. Here's what I made of it:

#!/bin/sh
show_menu(){
    normal=`echo "\033[m"`
    menu=`echo "\033[36m"` #Blue
    number=`echo "\033[33m"` #yellow
    bgred=`echo "\033[41m"`
    fgred=`echo "\033[31m"`
    printf "\n${menu}*********************************************${normal}\n"
    printf "${menu}**${number} 1)${menu} Backup sda ${normal}\n"
    printf "${menu}**${number} 2)${menu} Restore sda ${normal}\n"
    printf "${menu}**${number} 3)${menu} Restart computer ${normal}\n"
    printf "${menu}**${number} 4)${menu} Back to grub ${normal}\n"
    printf "${menu}**${number} 5)${menu} Test${normal}\n"
    printf "${menu}*********************************************${normal}\n"
    printf "Please enter a menu option and enter or ${fgred}x to exit. ${normal}"
    read opt
}

option_picked(){
    msgcolor=`echo "\033[01;31m"` # bold red
    normal=`echo "\033[00;00m"` # normal white
    message=${@:-"${normal}Error: No message passed"}
    printf "${msgcolor}${message}${normal}\n"
}

clear
show_menu
while [ $opt != '' ]
    do
    if [ $opt = '' ]; then
      exit;
    else
      case $opt in
        1) clear;
            option_picked "Option 1 Picked";
            dd if=/dev/nvme0n1 of=/mnt/data/backup/nvme0n1.img bs=4M conv=noerror status=progress;
            show_menu;
        ;;
        2) clear;
            option_picked "Option 2 Picked";
            dd if=/mnt/data/backup/nvme0n1.img of=/dev/nvme0n1 bs=4M conv=noerror status=progress;
            show_menu;
        ;;
        3) clear;
            option_picked "Option 3 Picked";
            "back to root menu";
            show_menu;
        ;;
        4) clear;
            option_picked "Option 4 Picked";
            reboot;
            show_menu;
        ;;
        x)exit;
        ;;
        \n)exit;
        ;;
        *)clear;
            option_picked "Pick an option from the menu";
            show_menu;
        ;;
      esac
    fi
done

Results of this bash script on an Ubuntu server run: Results of this bash script on an Ubuntu server run It could do the trick, but it's not easy I've made a mistake with the disk and file locations probably, because my system couldn't start anymore. I was good for reinstalling it.

I would like to have such a menu entry into my default grub. Therefor I thought I could add an entry into /etc/grub.d/40.custom

#!/bin/sh
exec tail -n +3 $0
# This file provides an easy way to add custom menu entries.  Simply type the
# menu entries you want to add after this comment.  Be careful not to change
# the 'exec tail' line above.

menuentry 'Lubuntu Root Terminal' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-simple-5bba636c-5b61-4ef9-857b-c5d4bf51c2cd' {
    recordfail
    load_video
    gfxmode $linux_gfx_mode
    insmod gzio
    if [ x$grub_platform = xxen ]; then insmod xzio; insmod lzopio; fi
    insmod part_gpt
    insmod ext2
    search --no-floppy --fs-uuid --set=root 5bba636c-5b61-4ef9-857b-c5d4bf51c2cd
    linux   /boot/vmlinuz-5.15.0-37-generic root=UUID=5bba636c-5b61-4ef9-857b-c5d4bf51c2cd \ ro init=/bin/bash rw $vt_handoff
    initrd  /boot/initrd.img-5.15.0-37-generic
}

I didn't get the system to boot into terminal with this menu entry yet. I probably did something wrong.

I'm looking how to add TTY options in my grub menu trough this entry, but do not find how exactly. I would like to know how to mount the system as root and launch the shell with the made bash for example.

Ubuntu server minimal on a small partition of the same disk. From there I'll try to setup the bash to launch. The problem is that the server commands and configuration seems different. I can't get my raid-0 partition to work/map. So I do not have access to my files to test.

I tried to build a custom kernel I've made a mistake installing these packages after the building. The point was to build a kernel on the recovery partition that doesn't contain the X11 modules. It is to complicated procedure for me it seems. I had trouble with certificates and at the installed the packages on my system, what crashed everything.

TTS0 grub entry: I've also found information about a terminal TTS0 session I could add to the grub. But when I select it on boot, I just run into my system like usual.

Here is my custom grub entry TTYS0:

recordfail
load_video
gfxmode $linux_gfx_mode
insmod gzio
if [ x$grub_platform = xxen ]; then insmod xzio; insmod lzopio; fi
insmod part_gpt
insmod ext2
set root='hd0,gpt2'
if [ x$feature_platform_search_hint = xy ]; then
  search --no-floppy --fs-uuid --set=root --hint='hd0,gpt2'  adf801ee-4bd7-4e13-aeef-210e10ef58e8
else
  search --no-floppy --fs-uuid --set=root adf801ee-4bd7-4e13-aeef-210e10ef58e8
fi
linux   /boot/vmlinuz-5.15.0-25-generic root=UUID=adf801ee-4bd7-4e13-aeef-210e10ef58e8 ro  quiet splash $vt_handoff  vga=787 console=tty0 console=ttyS0,115200n8
initrd  /boot/initrd.img-5.15.0-25-generic

I understand know I have more as one question here:

  1. What is best:

    a. A grub to terminal that launches my script menu ?

    b. A separate system boot to clone partitions ?

  2. How to add a grub entry that enter TTY to load a bash script?

  3. Or how do I build custom kernel (If it is a correct alternative) to boot without desktop my same install on another partition and adding this entry to the boot.

The information I found on Help Ubuntu suggests a way to backup my system with dd. I'm looking for something already available, small and accessible trough command line with the bash.

I would like help on the make command to get the right config for my purposed build.

or

I would like help on the way to add a grub entry that uses my current system TTY to load the menu bash at boot.

Is there a way to do any of these?

I found a pretty example to start with : Clonezilla on HD

I added this to /etc/grub.d/40_custom with Grub Customizer following the instructions:

menuentry "Clonezilla" {
insmod gzio
if [ x$grub_platform = xxen ]; then insmod xzio; insmod lzopio; fi
insmod part_gpt
insmod ext2
set root=(hd0,gpt1)
linux /recovery/vmlinuz boot=live union=overlay username=user config components quiet noswap nolocales edd=on nomodeset ocs_live_run=\"ocs-live-general\" ocs_live_extra_param=\"\" keyboard-layouts= ocs_live_batch=\"no\" locales= vga=788 ip=frommedia nosplash live-media-path=/recovery bootfrom=/dev/nvme0n1 toram=recovery,syslinux,EFI
initrd /recovery/initrd.img
}

This is the Linux system disk partition information:

nvme0n1                259:0    0 119.2G  0 disk   
├─nvme0n1p1            259:1    0  26.1G  0 part   
├─nvme0n1p2            259:2    0  87.9G  0 part   /
└─nvme0n1p3            259:3    0   5.3G  0 part   /boot/efi
  • p1 is the recovery partiton
  • p2 is the lubuntu root partition
  • p3 is the efi-boot partition

When I try to start this boot entry, it creates a new one. When I try the new one, I get an error it can't find the boot files needed.

Here's the content my recovery partition:

ls /mnt/recovery
amd64-release.txt                         EFI                  live
boot                                      GPL                  lost+found
clonezilla-live-20220522-jammy-amd64.zip  home                 syslinux
Clonezilla-Live-Version                   linux-5.18.4.tar.xz  utils

I presume it comes from the grub entry path that isn't right. I'm not sure what has to filled in this value ...

bootfrom=/dev/nvme0n1

or

bootfrom=/dev/nvme0n1p1

I found the errors in the folder name, that should change to "live-hd" instead of "live" and edited the grub entry as follow:

I took the information of the Lubuntu's grub entry and changed the disk id's based on what I could see in the gnome-disk-utility and removed the commands recordfail - load_video - gfxmode $linux_gfx_mode.

menuentry "Clonezilla" {
insmod gzio
if [ x$grub_platform = xxen ]; then insmod xzio; insmod lzopio; fi
insmod part_gpt
insmod ext2
set root=(hd0,gpt1)
if [ x$feature_platform_search_hint = xy ]; then
  search --no-floppy --fs-uuid --set=root --hint='hd0,gpt1'  22d397bb-9bf4-4486-a84f-e2b193a49e85
else
  search --no-floppy --fs-uuid --set=root 22d397bb-9bf4-4486-a84f-e2b193a49e85
fi
linux /live-hd/vmlinuz boot=live union=overlay username=user config components quiet noswap nolocales edd=on nomodeset ocs_live_run=\"ocs-live-general\" ocs_live_extra_param=\"\" keyboard-layouts= ocs_live_batch=\"no\" locales= vga=788 ip=frommedia nosplash live-media-path=/live-hd bootfrom=/dev/nvme0n1p1 toram=live-hd,syslinux,EFI
initrd /live-hd/initrd.img
}

default grub with Clonezilla entry

After that, I selected Clonezilla what made a new entry in the grub menu with the same name:

Double Clonezilla entry

So I tried to boot on the new entry, and it worked fine, but why does it make this second entry first ?

The point is it boots, and I could clone the system partition of the disk where the Clonezilla is installed as recovery partition.

At top of that, I was gladly surprised that Clonezilla has detected my FakeRaid and the partitions on it without any additional settings. I could save the image on it without inconvenience.

Clonezilla partition to file

It took approximately 10 minutes with the verification included. Now I'm still searching how to make it a menu to choose between backup or restore only, with always the same source and destination alternatively. I presume this must be possible, so I continue my research.

6
  • 1
    Have you called sudo update-grub to actually generate a new GRUB menu? Also consider using grub-emu to check everything without rebooting, which is also a safer way. unix.stackexchange.com/questions/405515/…
    – ethcz
    Jun 13, 2022 at 11:57
  • 2
    dd is not considered a good backup method. It is slow as it copies all the blank space. Also many suggest it is better to plan on new install & restore of data, so no need to backup Ubuntu system, just settings, apps list & data. Also incremental data backup is a lot quicker, so will be done more often. Better to use rsync, not dd askubuntu.com/questions/1331348/… Several alternatives. First suggests dd or dc3dd as image backup askubuntu.com/questions/569679/…
    – oldfred
    Jun 13, 2022 at 14:40
  • I do not think it's needed, because it's for small installations. I get rapidly stuck with my tests. So I would like to prevent reinstalling the all version, and instead just reload the partition where it was before the potential crash, like a sort of Time Machine on Apple but with a simple command line. It took me less then 1 hour to image the disk with the example command. Thanks for your suggestion, I'll take a look. @oldfred
    – user849355
    Jun 13, 2022 at 15:06
  • I have yet to use a virtual install, but have seen that suggested for perhaps your use case. And/or use LVM or one of the "newer" formats that has backups. I only use ext4, so do not know details. I keep data separate from /home. So just do a new install & mount my data partition. Then my main working install is not modified. New install from RAM to SSD with existing partition is often 10 to 12 minutes. Longest time then is reinstalling apps from exported list or script depending on what I want in that install. Also dd really should be run from a live installer or other install to avoid issue
    – oldfred
    Jun 13, 2022 at 15:50
  • I found an existing version of Time Machine like for Linux. it's called Timeshift: itsfoss.com/backup-restore-linux-timeshift But it's to heavy to me.
    – user849355
    Jun 13, 2022 at 17:52

1 Answer 1

0

I followed the procedure to install Clonezilla on hard-drive and succeeded to boot it from the grub menu.

  1. I had to extract this zip download of Clonezilla and extracted it in my partition.

  2. I had to identify the UUID and the partition number of my disk, what I simple did by opening gnome-disk-utility.

  3. I compared the difference between my default grub system entry and the one provided by default by Clonezilla and adapted it with my disk and partition identified. (See question for details)

  4. I saved the changes to grub and updated it with sudo update-grub and restarted my system.

  5. I've made a backup of the partition containing my system to an image on my Intel RAID that was automatically recognized with my Windows made NTFS DATA partition.

  6. I tested a property driver from Nvidia that crashed my kernel. That made me a good reason to test the restore, that has been done with success:

restoring disk image

With this, I've found a correct answer to make a recovery menu entry at boot, by using Clonezilla. Now I can choose to save or restore the partition I wanted to.

I also found the steps to create and use script with Clonezilla here and an example of a script here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy