ventoy / vtoyboot

Work with ventoy to support boot Linux distros in a vdisk file (vhd/vdi/raw ...)
GNU General Public License v3.0
275 stars 23 forks source link

take argument to specify kernel or handle the case when the currently running kernel is uninstalled from disk #59

Open hgkamath opened 1 year ago

hgkamath commented 1 year ago

Description

make ./vtoyboot.sh to take argument to specify kernel
and/or
handle the case when the currently running kernel is uninstalled from disk by finding and using the next-best-available or latest kernel.

The error message thrown will be something like

dracut: Cannot find module directory /lib/modules/6.0.12-300.fc37.x86_64/
dracut: and --no-kernel was not specified

Steps to reproduce

Log

[root@sirius bin]# cat /etc/os-release | head -2
NAME="Fedora Linux"
VERSION="37 (Workstation Edition)"

[root@sirius ~]$ uname -a
Linux sirius 6.0.12-300.fc37.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Dec 8 16:58:47 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
[root@sirius ~]$ dracut --list-modules | grep -Ei dm
dracut: Cannot find module directory /lib/modules/6.0.12-300.fc37.x86_64/
dracut: and --no-kernel was not specified
[root@sirius ~]$ rpm -qa | grep kernel-6
kernel-6.0.11-200.fc36.x86_64
kernel-6.0.15-300.fc37.x86_64
[root@sirius bin]# dracut --list-modules --kver 6.0.15-300.fc37.x86_64 | grep -Ei dm
rdma
dm
dmraid
dmsquash-live
dmsquash-live-autooverlay
dmsquash-live-ntfs
[root@fedora vtoyboot-1.0.25]# find distros -name check.sh
distros/initramfstool/check.sh
distros/dracut/check.sh
distros/mkinitcpio/check.sh

[root@fedora vtoyboot-1.0.25]# cat distros/dracut/check.sh 
#!/bin/sh
#************************************************************************************
# Copyright (c) 2020, longpanda <admin@ventoy.net>
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
# 
#************************************************************************************

vtoy_check_dracut() {
    if which dracut >/dev/null 2>&1; then        
        if dracut --list-modules | grep -q '^dm$'; then
            [ -n "1" ]; return
        fi                
    fi
    [ -z "1" ]
}

Workaround

Explanation of how this situation arises

As mine is a home machine, I sometimes take some liberty in upgrading the kernel in a way that may be frowned upon from a enterprise-grade robust upgrade/migration strategy point of view.

Fedora dnf package manager by default allows keeping of 3 kernels. While this number is configurable, more images take more space in the small boot partition.
When upgrading a new kernel, dnf, by default, usually first-in-first-out-pops-out (uninstalls) the oldest kernel.
Usually, I am okay with this.
I put an exclude=kernel* in the /etc/dnf/dnf.conf and manage my kernel upgrades at a time convenient to me.
Sometimes, I want get the latest kernel, but I choose to dispose of a kernel other than the oldest, because the oldest might actually be the most stable or I have built it with some customization.
So, before upgrade, I first uninstall a kernel other than the oldest, one of the first two, one of which is the current running kernel. Then I do the upgrade, ie dnf install a new kernel.
This ensures that there are again 3 kernels to choose to boot with on the next reboot. Therefore dnf does not automatically uninstall a kernel that I wanted to keep around.

Surprising though it might seem, Linux runs fine even when one has uninstalled the running kernel as it is already loaded to memory. The only trouble in doing so is that external kernel-modules no longer exist on disk to load/unload. Usually, one does not intend to remain this way for long and one intends to reboot immediately to the new kernel. But suppose one forgot and chose to keep running this way, then one will be surprised when, say, one inserts a USB dongle with ExFAT partitions and one wonders why the USB volume is not mounting. It will be because the ExFAT kernel-module does not exist in its kernel module directory to be loaded.

[root@sirius bin]# find /lib/modules/6.0.15-300.fc37.x86_64/kernel/fs/ | grep exfat
/lib/modules/6.0.15-300.fc37.x86_64/kernel/fs/exfat
/lib/modules/6.0.15-300.fc37.x86_64/kernel/fs/exfat/exfat.ko.xz

additionally print kernel version no of first kernel initramfs being generated

When running ./vtoyboot.sh, the first messages it prints includes

Current system use dracut as initramfs tool
updating the initramfs, please wait ...

It would be useful/informative to predetermine which default kernel version that dracut will prepare initramfs for and print its kernel version.
dracut proceeds to update initramfs for other kernels. The subsequent message contains their version numbers.

updating initramfs for 6.0.12-200.fc36..x86_64 please wait ... 
: