Add files via upload

This commit is contained in:
SkimrMe
2024-08-29 21:47:24 +08:00
committed by GitHub
parent 5b5d912866
commit f1a15bf780
39 changed files with 2590 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
#!/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" ]
}

View File

@@ -0,0 +1,20 @@
#!/bin/bash
# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
# ex: ts=8 sw=4 sts=4 et filetype=sh
check() {
require_binaries sed grep awk || return 1
return 255
}
depends() {
echo dm
return 0
}
install() {
inst_multiple sed grep awk dd sort head find basename xzcat zcat vtoydump vtoypartx vtoydmpatch vtoytool
inst_hook initqueue/settled 99 "$moddir/ventoy-settled.sh"
dracut_need_initqueue
}

View File

@@ -0,0 +1,370 @@
#!/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/>.
#
#************************************************************************************
type getarg >/dev/null 2>&1 || . /lib/dracut-lib.sh
###########################
###########################
#AUTO_INSERT_COMMON_FUNC
ventoy_check_efivars() {
if [ -e /sys/firmware/efi ]; then
if grep -q efivar /proc/mounts; then
:
else
if [ -e /sys/firmware/efi/efivars ]; then
mount -t efivarfs efivarfs /sys/firmware/efi/efivars >/dev/null 2>&1
fi
fi
fi
}
ventoy_log() {
echo "$@" >> /tmp/vtoy.log
}
ventoy_need_dm_patch() {
if vtoydump -R > /dev/null 2>&1; then
[ 1 -eq 1 ]; return
fi
if grep -q "VTOY_LINUX_REMOUNT=1" /proc/cmdline; then
[ 1 -eq 1 ]; return
fi
[ 1 -eq 0 ]
}
ventoy_check_insmod() {
if [ -f /bin/kmod ]; then
[ -f /bin/insmod ] || ln -s /bin/kmod /bin/insmod
[ -f /bin/lsmod ] || ln -s /bin/kmod /bin/lsmod
fi
}
ventoy_need_proc_ibt() {
vtKv=$(uname -r)
vtMajor=$(echo $vtKv | awk -F. '{print $1}')
vtMinor=$(echo $vtKv | awk -F. '{print $2}')
#ibt was supported since linux kernel 5.18
if [ $vtMajor -lt 5 ]; then
false; return
elif [ $vtMajor -eq 5 ]; then
if [ $vtMajor -lt 18 ]; then
false; return
fi
fi
if grep -q ' ibt=off' /proc/cmdline; then
false; return
fi
#hardware CPU doesn't support IBT
if vtoytool vtoykmod -I; then
:
else
false; return
fi
#dot.CONFIG not enabled
if grep -q ' ibt_restore$' /proc/kallsyms; then
:
else
false; return
fi
true
}
ventoy_do_dm_patch() {
vtDmPatchDebug=0
ventoy_log 'ventoy_do_dm_patch'
if [ -f /tmp/dm_patch.ko ]; then
if grep -q 'dm_patch' /proc/modules; then
ventoy_log "dm_patch already exist"
else
ventoy_log "dm_patch reinstall"
insmod /tmp/dm_patch.ko >>/tmp/vtoy.log 2>&1
if grep -q 'dm_patch' /proc/modules; then
ventoy_log "dm_patch reinstall success"
else
ventoy_log "dm_patch reinstall failed"
fi
fi
return
fi
if [ -f /bin/vtoydump ]; then
vtHeadSize=$(stat -c '%s' /bin/vtoydump)
dd if=/bin/vtoydmpatch of=/tmp/dm_patch.ko bs=1 skip=$vtHeadSize >/dev/null 2>&1
elif [ -f /sbin/vtoydump ]; then
vtHeadSize=$(stat -c '%s' /sbin/vtoydump)
dd if=/sbin/vtoydmpatch of=/tmp/dm_patch.ko bs=1 skip=$vtHeadSize >/dev/null 2>&1
else
ventoy_log 'vtoydump not found'
return
fi
if ! grep -m1 -q dm_get_table_device /proc/kallsyms; then
ventoy_log "modprobe dm_mod"
modprobe dm_mod >>/tmp/vtoy.log 2>&1
fi
cat /proc/kallsyms | sort > /tmp/kallsyms
if grep -m1 -q 'open_table_device.isra' /tmp/kallsyms; then
vtLine=$(vtoytool vtoyksym open_table_device.isra /tmp/kallsyms)
else
vtLine=$(vtoytool vtoyksym dm_get_table_device /tmp/kallsyms)
fi
get_addr=$(echo $vtLine | awk '{print $1}')
get_size=$(echo $vtLine | awk '{print $2}')
vtLine=$(vtoytool vtoyksym blkdev_get_by_dev /tmp/kallsyms)
blkdev_get_addr=$(echo $vtLine | awk '{print $1}')
vtLine=$(vtoytool vtoyksym blkdev_put /tmp/kallsyms)
blkdev_put_addr=$(echo $vtLine | awk '{print $1}')
vtLine=$(vtoytool vtoyksym bdev_open_by_dev /tmp/kallsyms)
bdev_open_addr=$(echo $vtLine | awk '{print $1}')
vtLine=$(vtoytool vtoyksym bdev_file_open_by_dev /tmp/kallsyms)
bdev_file_open_by_dev=$(echo $vtLine | awk '{print $1}')
if grep -m1 -q 'close_table_device.isra' /tmp/kallsyms; then
vtLine=$(vtoytool vtoyksym close_table_device.isra /tmp/kallsyms)
else
vtLine=$(vtoytool vtoyksym dm_put_table_device /tmp/kallsyms)
fi
put_addr=$(echo $vtLine | awk '{print $1}')
put_size=$(echo $vtLine | awk '{print $2}')
ro_addr=$(grep ' set_memory_ro$' /proc/kallsyms | awk '{print $1}')
rw_addr=$(grep ' set_memory_rw$' /proc/kallsyms | awk '{print $1}')
kprobe_reg_addr=$(grep ' register_kprobe$' /proc/kallsyms | awk '{print $1}')
kprobe_unreg_addr=$(grep ' unregister_kprobe$' /proc/kallsyms | awk '{print $1}')
if [ "$VTOY_DEBUG_LEVEL" = "01" ]; then
vtDmPatchDebug=1
fi
if grep -q 'dmpatch_debug' /proc/cmdline; then
vtDmPatchDebug=1
fi
if [ $vtDmPatchDebug -eq 1 ]; then
printk_addr=$(grep ' printk$' /proc/kallsyms | awk '{print $1}')
vtDebug="-v"
elif grep -q "vtdebug" /proc/cmdline; then
printk_addr=$(grep ' printk$' /proc/kallsyms | awk '{print $1}')
vtDebug="-v"
else
printk_addr=0
fi
if [ -z "$printk_addr" ]; then
printk_addr=$(grep ' _printk$' /proc/kallsyms | awk '{print $1}')
fi
if ventoy_need_proc_ibt; then
ventoy_log "need to proc IBT"
vtIBT='0x8888'
else
ventoy_log "NO need to proc IBT"
vtIBT='0'
fi
#printk_addr=$(grep ' printk$' /proc/kallsyms | awk '{print $1}')
#vtDebug="-v"
ventoy_log get_addr=$get_addr get_size=$get_size vtDebug=$vtDebug
ventoy_log put_addr=$put_addr put_size=$put_size
ventoy_log blkdev_get_addr=$blkdev_get_addr blkdev_put_addr=$blkdev_put_addr
ventoy_log kprobe_reg_addr=$kprobe_reg_addr kprobe_unreg_addr=$kprobe_unreg_addr
ventoy_log ro_addr=$ro_addr rw_addr=$rw_addr printk_addr=$printk_addr
ventoy_log bdev_open_addr=$bdev_open_addr bdev_file_open_by_dev=$bdev_file_open_by_dev
if [ "$get_addr" = "0" -o "$put_addr" = "0" ]; then
ventoy_log "Invalid symbol address"
return
fi
if [ "$ro_addr" = "0" -o "$rw_addr" = "0" ]; then
ventoy_log "Invalid symbol address"
return
fi
vtKv=$(uname -r)
vtKVMajor=$(echo $vtKv | awk -F. '{print $1}')
vtKVMinor=$(echo $vtKv | awk -F. '{print $2}')
vtKVSubMinor=$(echo $vtKv | awk -F. '{print $3}')
if [ ! -d /lib/modules/$vtKv ]; then
ventoy_log "No modules directory found"
return
elif [ -d /lib/modules/$vtKv/kernel/fs ]; then
vtModPath=$(find /lib/modules/$vtKv/kernel/fs/ -name "*.ko*" | head -n1)
else
vtModPath=$(find /lib/modules/$vtKv/kernel/ -name "xfs.ko*" | head -n1)
fi
if [ -z "$vtModPath" ]; then
vtModPath=$(find /lib/modules/$vtKv/kernel/ -name "*.ko*" | head -n1)
fi
vtModName=$(basename $vtModPath)
[ -f /tmp/$vtModName ] && rm -f /tmp/$vtModName
ventoy_log "template module is $vtModPath $vtModName"
if [ -z "$vtModPath" ]; then
ventoy_log "No template module found"
return
elif echo $vtModPath | grep -q "[.]ko$"; then
cp -a $vtModPath /tmp/$vtModName
elif echo $vtModPath | grep -q "[.]ko[.]xz$"; then
xzcat $vtModPath > /tmp/$vtModName
elif echo $vtModPath | grep -q "[.]ko[.]gz$"; then
zcat $vtModPath > /tmp/$vtModName
elif echo $vtModPath | grep -q "[.]ko[.]zst$"; then
zstdcat $vtModPath > /tmp/$vtModName
else
ventoy_log "unsupport module type"
return
fi
#step1: modify vermagic/mod crc/relocation
vtoytool vtoykmod -u $vtKVMajor $vtKVMinor /tmp/dm_patch.ko /tmp/$vtModName $vtDebug >>/tmp/vtoy.log 2>&1
#step2: fill parameters
vtPgsize=$(vtoytool vtoyksym -p)
vtoytool vtoykmod -f /tmp/dm_patch.ko $vtPgsize 0x$printk_addr 0x$ro_addr 0x$rw_addr $get_addr $get_size $put_addr $put_size 0x$kprobe_reg_addr 0x$kprobe_unreg_addr $vtKVMajor $vtIBT $vtKVMinor $blkdev_get_addr $blkdev_put_addr $vtKVSubMinor $bdev_open_addr $bdev_file_open_by_dev $vtDebug >>/tmp/vtoy.log 2>&1
ventoy_check_insmod
insmod /tmp/dm_patch.ko >>/tmp/vtoy.log 2>&1
if grep -q 'dm_patch' /proc/modules; then
ventoy_log "dm_patch success"
else
ventoy_log "dm_patch failed"
fi
}
ventoy_dm_patch_proc_begin() {
if ventoy_need_dm_patch; then
export vtLevel1=$(cat /proc/sys/kernel/printk | awk '{print $1}')
export vtLevel2=$(cat /proc/sys/kernel/printk | awk '{print $2}')
export vtLevel3=$(cat /proc/sys/kernel/printk | awk '{print $3}')
export vtLevel4=$(cat /proc/sys/kernel/printk | awk '{print $4}')
#suppress printk message
echo 0 $vtLevel2 0 $vtLevel4 > /proc/sys/kernel/printk
fi
}
ventoy_dm_patch_install() {
if ventoy_need_dm_patch; then
ventoy_do_dm_patch
fi
}
ventoy_dm_patch_remove() {
if ventoy_need_dm_patch; then
if grep -q 'dm_patch' /proc/modules; then
ventoy_log "remove dm_patch"
rmmod dm_patch
fi
fi
}
ventoy_dm_create_ventoy() {
ventoy_dm_patch_install
dmsetup create ventoy /ventoy_table
vret=$?
ventoy_dm_patch_remove
return $vret
}
ventoy_dm_patch_proc_end() {
if ventoy_need_dm_patch; then
#recover printk level
echo $vtLevel1 $vtLevel2 $vtLevel3 $vtLevel4 > /proc/sys/kernel/printk
fi
}
#check for efivarfs
ventoy_check_efivars
if ! vtoydump > /dev/null 2>&1; then
info 'vtoydump failed'
return
fi
#already done
if dmsetup ls | grep -q ventoy; then
info 'ventoy already exist'
return
fi
ventoy_dm_patch_proc_begin
#flush multipath before dmsetup
multipath -F > /dev/null 2>&1
vtoydump -L > /ventoy_table
if ventoy_dm_create_ventoy; then
:
else
sleep 3
multipath -F > /dev/null 2>&1
ventoy_dm_create_ventoy
fi
DEVDM=/dev/mapper/ventoy
loop=0
while ! [ -e $DEVDM ]; do
sleep 0.5
let loop+=1
if [ $loop -gt 10 ]; then
echo "Waiting for ventoy device ..." > /dev/console
fi
if [ $loop -gt 10 -a $loop -lt 15 ]; then
multipath -F > /dev/null 2>&1
ventoy_dm_create_ventoy
fi
done
for ID in $(vtoypartx $DEVDM -oNR | grep -v NR); do
PART_START=$(vtoypartx $DEVDM -n$ID -oSTART,SECTORS | grep -v START | awk '{print $1}')
PART_SECTOR=$(vtoypartx $DEVDM -n$ID -oSTART,SECTORS | grep -v START | awk '{print $2}')
echo "0 $PART_SECTOR linear $DEVDM $PART_START" > /ventoy_part_table
dmsetup create ventoy$ID /ventoy_part_table
done
rm -f /ventoy_table
rm -f /ventoy_part_table
ventoy_dm_patch_proc_end

View File

@@ -0,0 +1,134 @@
#!/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/>.
#
#************************************************************************************
. ./tools/efi_legacy_grub.sh
if [ -e /lib/dracut/dracut-install ]; then
vtmodpath=/lib/dracut/modules.d/99ventoy
else
vtmodpath=/usr/lib/dracut/modules.d/99ventoy
fi
if [ -d /etc/dracut.conf.d ]; then
dracutConfPath=/etc/dracut.conf.d
else
dracutConfPath=/usr/lib/dracut/dracut.conf.d
fi
rm -f /bin/vtoydump /bin/vtoypartx /bin/vtoytool /bin/vtoydmpatch
rm -f $dracutConfPath/ventoy.conf
rm -rf $vtmodpath
mkdir -p $vtmodpath
cp -a $vtdumpcmd /bin/vtoydump
cp -a $partxcmd /bin/vtoypartx
cp -a $vtoytool /bin/vtoytool
cat /bin/vtoydump $dmpatchko > /bin/vtoydmpatch
cp -a ./distros/$initrdtool/module-setup.sh $vtmodpath/
cp -a ./distros/$initrdtool/ventoy-settled.sh $vtmodpath/
#early centos release doesn't have require_binaries
if [ -e $vtmodpath/../../dracut-functions ]; then
if grep -q require_binaries $vtmodpath/../../dracut-functions; then
:
else
sed "/require_binaries/d" -i $vtmodpath/module-setup.sh
fi
fi
for md in $(cat ./tools/vtoydrivers); do
if [ -n "$md" ]; then
if modinfo -n $md 2>/dev/null | grep -q '\.ko'; then
extdrivers="$extdrivers $md"
fi
fi
done
#generate dracut conf file
cat >$dracutConfPath/ventoy.conf <<EOF
add_dracutmodules+=" ventoy "
force_drivers+=" $extdrivers "
EOF
echo "updating the initramfs, please wait ..."
dracut -f --no-hostonly
kv=$(uname -r)
for k in $(ls /lib/modules); do
if [ "$k" != "$kv" ]; then
echo "updating initramfs for $k please wait ..."
dracut -f --no-hostonly --kver $k
fi
done
disable_grub_os_probe
#wrapper grub-probe
echo "grub mkconfig ..."
PROBE_PATH=$(find_grub_probe_path)
EDITENV_PATH=$(find_grub_editenv_path)
MKCONFIG_PATH=$(find_grub_mkconfig_path)
echo "PROBE_PATH=$PROBE_PATH EDITENV_PATH=$EDITENV_PATH MKCONFIG_PATH=$MKCONFIG_PATH"
if [ -e "$PROBE_PATH" -a -e "$MKCONFIG_PATH" ]; then
wrapper_grub_probe $PROBE_PATH
if [ -e "$EDITENV_PATH" ]; then
wrapper_grub_editenv $EDITENV_PATH
fi
GRUB_CFG_PATH=$(find_grub_config_path)
if [ -f "$GRUB_CFG_PATH" ]; then
echo "$MKCONFIG_PATH -o $GRUB_CFG_PATH"
$MKCONFIG_PATH -o $GRUB_CFG_PATH
else
echo "$MKCONFIG_PATH null"
$MKCONFIG_PATH > /dev/null 2>&1
fi
fi
if [ -e /sys/firmware/efi ]; then
if [ -e /dev/mapper/ventoy ]; then
echo "This is ventoy enviroment"
else
update_grub_config
install_legacy_bios_grub
fi
if [ "$1" = "-s" ]; then
recover_shim_efi
else
replace_shim_efi
fi
if [ -d /boot/EFI/EFI/mageia ]; then
if ! [ -d /boot/EFI/EFI/boot ]; then
mkdir -p /boot/EFI/EFI/boot
if [ -f /boot/EFI/EFI/mageia/grubx64.efi ]; then
cp -a /boot/EFI/EFI/mageia/grubx64.efi /boot/EFI/EFI/boot/bootx64.efi
fi
fi
fi
fi

View File

@@ -0,0 +1,48 @@
#!/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_false() {
[ -z "1" ]
}
vtoy_true() {
[ -n "1" ]
}
vtoy_check_initramfs_tool() {
if which update-initramfs >/dev/null 2>&1; then
if ! [ -f /usr/share/initramfs-tools/hook-functions ]; then
vtoy_false; return
fi
if ! [ -f /usr/share/initramfs-tools/hooks/dmsetup ]; then
vtoy_false; return
fi
if ! [ -d /etc/initramfs-tools/scripts/local-top ]; then
vtoy_false; return
fi
vtoy_true
else
vtoy_false
fi
}

View File

@@ -0,0 +1,56 @@
#!/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/>.
#
#************************************************************************************
PREREQ="dmsetup"
prereqs() {
echo "$PREREQ"
}
case $1 in
prereqs)
prereqs
exit 0
;;
esac
. /usr/share/initramfs-tools/hook-functions
# Begin real processing below this line
for md in $(cat /sbin/vtoydrivers); do
if [ -n "$md" ]; then
if modinfo -n $md 2>/dev/null | grep -q '\.ko'; then
force_load $md
fi
fi
done
for ef in dd sort head find basename xzcat zcat; do
for vp in /bin /sbin /usr/bin /usr/sbin; do
if [ -f $vp/$ef ]; then
copy_exec $vp/$ef /sbin
break
fi
done
done
copy_exec /sbin/vtoytool /sbin
copy_exec /sbin/vtoydmpatch /sbin
copy_exec /sbin/vtoypartx /sbin
copy_exec /sbin/vtoydump /sbin

View File

@@ -0,0 +1,377 @@
#!/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/>.
#
#************************************************************************************
###########################
###########################
#AUTO_INSERT_COMMON_FUNC
ventoy_check_efivars() {
if [ -e /sys/firmware/efi ]; then
if grep -q efivar /proc/mounts; then
:
else
if [ -e /sys/firmware/efi/efivars ]; then
mount -t efivarfs efivarfs /sys/firmware/efi/efivars >/dev/null 2>&1
fi
fi
fi
}
ventoy_log() {
echo "$@" >> /tmp/vtoy.log
}
ventoy_need_dm_patch() {
if vtoydump -R > /dev/null 2>&1; then
[ 1 -eq 1 ]; return
fi
if grep -q "VTOY_LINUX_REMOUNT=1" /proc/cmdline; then
[ 1 -eq 1 ]; return
fi
[ 1 -eq 0 ]
}
ventoy_check_insmod() {
if [ -f /bin/kmod ]; then
[ -f /bin/insmod ] || ln -s /bin/kmod /bin/insmod
[ -f /bin/lsmod ] || ln -s /bin/kmod /bin/lsmod
fi
}
ventoy_need_proc_ibt() {
vtKv=$(uname -r)
vtMajor=$(echo $vtKv | awk -F. '{print $1}')
vtMinor=$(echo $vtKv | awk -F. '{print $2}')
#ibt was supported since linux kernel 5.18
if [ $vtMajor -lt 5 ]; then
false; return
elif [ $vtMajor -eq 5 ]; then
if [ $vtMajor -lt 18 ]; then
false; return
fi
fi
if grep -q ' ibt=off' /proc/cmdline; then
false; return
fi
#hardware CPU doesn't support IBT
if vtoytool vtoykmod -I; then
:
else
false; return
fi
#dot.CONFIG not enabled
if grep -q ' ibt_restore$' /proc/kallsyms; then
:
else
false; return
fi
true
}
ventoy_do_dm_patch() {
vtDmPatchDebug=0
ventoy_log 'ventoy_do_dm_patch'
if [ -f /tmp/dm_patch.ko ]; then
if grep -q 'dm_patch' /proc/modules; then
ventoy_log "dm_patch already exist"
else
ventoy_log "dm_patch reinstall"
insmod /tmp/dm_patch.ko >>/tmp/vtoy.log 2>&1
if grep -q 'dm_patch' /proc/modules; then
ventoy_log "dm_patch reinstall success"
else
ventoy_log "dm_patch reinstall failed"
fi
fi
return
fi
if [ -f /bin/vtoydump ]; then
vtHeadSize=$(stat -c '%s' /bin/vtoydump)
dd if=/bin/vtoydmpatch of=/tmp/dm_patch.ko bs=1 skip=$vtHeadSize >/dev/null 2>&1
elif [ -f /sbin/vtoydump ]; then
vtHeadSize=$(stat -c '%s' /sbin/vtoydump)
dd if=/sbin/vtoydmpatch of=/tmp/dm_patch.ko bs=1 skip=$vtHeadSize >/dev/null 2>&1
else
ventoy_log 'vtoydump not found'
return
fi
if ! grep -m1 -q dm_get_table_device /proc/kallsyms; then
ventoy_log "modprobe dm_mod"
modprobe dm_mod >>/tmp/vtoy.log 2>&1
fi
cat /proc/kallsyms | sort > /tmp/kallsyms
if grep -m1 -q 'open_table_device.isra' /tmp/kallsyms; then
vtLine=$(vtoytool vtoyksym open_table_device.isra /tmp/kallsyms)
else
vtLine=$(vtoytool vtoyksym dm_get_table_device /tmp/kallsyms)
fi
get_addr=$(echo $vtLine | awk '{print $1}')
get_size=$(echo $vtLine | awk '{print $2}')
vtLine=$(vtoytool vtoyksym blkdev_get_by_dev /tmp/kallsyms)
blkdev_get_addr=$(echo $vtLine | awk '{print $1}')
vtLine=$(vtoytool vtoyksym blkdev_put /tmp/kallsyms)
blkdev_put_addr=$(echo $vtLine | awk '{print $1}')
vtLine=$(vtoytool vtoyksym bdev_open_by_dev /tmp/kallsyms)
bdev_open_addr=$(echo $vtLine | awk '{print $1}')
vtLine=$(vtoytool vtoyksym bdev_file_open_by_dev /tmp/kallsyms)
bdev_file_open_by_dev=$(echo $vtLine | awk '{print $1}')
if grep -m1 -q 'close_table_device.isra' /tmp/kallsyms; then
vtLine=$(vtoytool vtoyksym close_table_device.isra /tmp/kallsyms)
else
vtLine=$(vtoytool vtoyksym dm_put_table_device /tmp/kallsyms)
fi
put_addr=$(echo $vtLine | awk '{print $1}')
put_size=$(echo $vtLine | awk '{print $2}')
ro_addr=$(grep ' set_memory_ro$' /proc/kallsyms | awk '{print $1}')
rw_addr=$(grep ' set_memory_rw$' /proc/kallsyms | awk '{print $1}')
kprobe_reg_addr=$(grep ' register_kprobe$' /proc/kallsyms | awk '{print $1}')
kprobe_unreg_addr=$(grep ' unregister_kprobe$' /proc/kallsyms | awk '{print $1}')
if [ "$VTOY_DEBUG_LEVEL" = "01" ]; then
vtDmPatchDebug=1
fi
if grep -q 'dmpatch_debug' /proc/cmdline; then
vtDmPatchDebug=1
fi
if [ $vtDmPatchDebug -eq 1 ]; then
printk_addr=$(grep ' printk$' /proc/kallsyms | awk '{print $1}')
vtDebug="-v"
elif grep -q "vtdebug" /proc/cmdline; then
printk_addr=$(grep ' printk$' /proc/kallsyms | awk '{print $1}')
vtDebug="-v"
else
printk_addr=0
fi
if [ -z "$printk_addr" ]; then
printk_addr=$(grep ' _printk$' /proc/kallsyms | awk '{print $1}')
fi
if ventoy_need_proc_ibt; then
ventoy_log "need to proc IBT"
vtIBT='0x8888'
else
ventoy_log "NO need to proc IBT"
vtIBT='0'
fi
#printk_addr=$(grep ' printk$' /proc/kallsyms | awk '{print $1}')
#vtDebug="-v"
ventoy_log get_addr=$get_addr get_size=$get_size vtDebug=$vtDebug
ventoy_log put_addr=$put_addr put_size=$put_size
ventoy_log blkdev_get_addr=$blkdev_get_addr blkdev_put_addr=$blkdev_put_addr
ventoy_log kprobe_reg_addr=$kprobe_reg_addr kprobe_unreg_addr=$kprobe_unreg_addr
ventoy_log ro_addr=$ro_addr rw_addr=$rw_addr printk_addr=$printk_addr
ventoy_log bdev_open_addr=$bdev_open_addr bdev_file_open_by_dev=$bdev_file_open_by_dev
if [ "$get_addr" = "0" -o "$put_addr" = "0" ]; then
ventoy_log "Invalid symbol address"
return
fi
if [ "$ro_addr" = "0" -o "$rw_addr" = "0" ]; then
ventoy_log "Invalid symbol address"
return
fi
vtKv=$(uname -r)
vtKVMajor=$(echo $vtKv | awk -F. '{print $1}')
vtKVMinor=$(echo $vtKv | awk -F. '{print $2}')
vtKVSubMinor=$(echo $vtKv | awk -F. '{print $3}')
if [ ! -d /lib/modules/$vtKv ]; then
ventoy_log "No modules directory found"
return
elif [ -d /lib/modules/$vtKv/kernel/fs ]; then
vtModPath=$(find /lib/modules/$vtKv/kernel/fs/ -name "*.ko*" | head -n1)
else
vtModPath=$(find /lib/modules/$vtKv/kernel/ -name "xfs.ko*" | head -n1)
fi
if [ -z "$vtModPath" ]; then
vtModPath=$(find /lib/modules/$vtKv/kernel/ -name "*.ko*" | head -n1)
fi
vtModName=$(basename $vtModPath)
[ -f /tmp/$vtModName ] && rm -f /tmp/$vtModName
ventoy_log "template module is $vtModPath $vtModName"
if [ -z "$vtModPath" ]; then
ventoy_log "No template module found"
return
elif echo $vtModPath | grep -q "[.]ko$"; then
cp -a $vtModPath /tmp/$vtModName
elif echo $vtModPath | grep -q "[.]ko[.]xz$"; then
xzcat $vtModPath > /tmp/$vtModName
elif echo $vtModPath | grep -q "[.]ko[.]gz$"; then
zcat $vtModPath > /tmp/$vtModName
elif echo $vtModPath | grep -q "[.]ko[.]zst$"; then
zstdcat $vtModPath > /tmp/$vtModName
else
ventoy_log "unsupport module type"
return
fi
#step1: modify vermagic/mod crc/relocation
vtoytool vtoykmod -u $vtKVMajor $vtKVMinor /tmp/dm_patch.ko /tmp/$vtModName $vtDebug >>/tmp/vtoy.log 2>&1
#step2: fill parameters
vtPgsize=$(vtoytool vtoyksym -p)
vtoytool vtoykmod -f /tmp/dm_patch.ko $vtPgsize 0x$printk_addr 0x$ro_addr 0x$rw_addr $get_addr $get_size $put_addr $put_size 0x$kprobe_reg_addr 0x$kprobe_unreg_addr $vtKVMajor $vtIBT $vtKVMinor $blkdev_get_addr $blkdev_put_addr $vtKVSubMinor $bdev_open_addr $bdev_file_open_by_dev $vtDebug >>/tmp/vtoy.log 2>&1
ventoy_check_insmod
insmod /tmp/dm_patch.ko >>/tmp/vtoy.log 2>&1
if grep -q 'dm_patch' /proc/modules; then
ventoy_log "dm_patch success"
else
ventoy_log "dm_patch failed"
fi
}
ventoy_dm_patch_proc_begin() {
if ventoy_need_dm_patch; then
export vtLevel1=$(cat /proc/sys/kernel/printk | awk '{print $1}')
export vtLevel2=$(cat /proc/sys/kernel/printk | awk '{print $2}')
export vtLevel3=$(cat /proc/sys/kernel/printk | awk '{print $3}')
export vtLevel4=$(cat /proc/sys/kernel/printk | awk '{print $4}')
#suppress printk message
echo 0 $vtLevel2 0 $vtLevel4 > /proc/sys/kernel/printk
fi
}
ventoy_dm_patch_install() {
if ventoy_need_dm_patch; then
ventoy_do_dm_patch
fi
}
ventoy_dm_patch_remove() {
if ventoy_need_dm_patch; then
if grep -q 'dm_patch' /proc/modules; then
ventoy_log "remove dm_patch"
rmmod dm_patch
fi
fi
}
ventoy_dm_create_ventoy() {
ventoy_dm_patch_install
dmsetup create ventoy /ventoy_table
vret=$?
ventoy_dm_patch_remove
return $vret
}
ventoy_dm_patch_proc_end() {
if ventoy_need_dm_patch; then
#recover printk level
echo $vtLevel1 $vtLevel2 $vtLevel3 $vtLevel4 > /proc/sys/kernel/printk
fi
}
vtoy_wait_for_device() {
while ! vtoydump > /dev/null 2>&1; do
sleep 0.5
done
}
vtoy_device_mapper_proc() {
#flush multipath before dmsetup
multipath -F > /dev/null 2>&1
vtoydump -L > /ventoy_table
if ventoy_dm_create_ventoy; then
:
else
sleep 3
multipath -F > /dev/null 2>&1
ventoy_dm_create_ventoy
fi
DEVDM=/dev/mapper/ventoy
loop=0
while ! [ -e $DEVDM ]; do
sleep 0.5
let loop+=1
if [ $loop -gt 10 ]; then
echo "Waiting for ventoy device ..." > /dev/console
fi
if [ $loop -gt 10 -a $loop -lt 15 ]; then
multipath -F > /dev/null 2>&1
ventoy_dm_create_ventoy
fi
done
for ID in $(vtoypartx $DEVDM -oNR | grep -v NR); do
PART_START=$(vtoypartx $DEVDM -n$ID -oSTART,SECTORS | grep -v START | awk '{print $1}')
PART_SECTOR=$(vtoypartx $DEVDM -n$ID -oSTART,SECTORS | grep -v START | awk '{print $2}')
echo "0 $PART_SECTOR linear $DEVDM $PART_START" > /ventoy_part_table
dmsetup create ventoy$ID /ventoy_part_table
done
rm -f /ventoy_table
rm -f /ventoy_part_table
}
case $1 in
prereqs)
exit 0
;;
esac
#check for efivarfs
ventoy_check_efivars
if vtoydump -c > /dev/null 2>&1; then
vtoy_wait_for_device
ventoy_dm_patch_proc_begin
vtoy_device_mapper_proc
ventoy_dm_patch_proc_end
fi

View File

@@ -0,0 +1,119 @@
#!/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_clean_env() {
rm -f /sbin/vtoydump /sbin/vtoypartx /sbin/vtoytool /sbin/vtoydmpatch /sbin/vtoydrivers
rm -f /usr/share/initramfs-tools/hooks/vtoy-hook.sh
rm -f /etc/initramfs-tools/scripts/local-top/vtoy-local-top.sh
}
vtoy_efi_fixup() {
if [ -d /boot/efi/EFI ]; then
for f in 'boot/bootx64.efi' 'boot/BOOTX64.efi' 'boot/BOOTX64.EFI' 'BOOT/bootx64.efi' 'BOOT/BOOTX64.efi' 'BOOT/BOOTX64.EFI'; do
if [ -f /boot/efi/EFI/$f ]; then
return
fi
done
fi
Dirs=$(ls /boot/efi/EFI)
if ! [ -d /boot/efi/EFI/boot ]; then
mkdir -p /boot/efi/EFI/boot
fi
for d in $Dirs; do
for e in 'grubx64.efi' 'GRUBX64.EFI' 'bootx64.efi' 'BOOTX64.EFI'; do
if [ -f "/boot/efi/EFI/$d/$e" ]; then
cp -a "/boot/efi/EFI/$d/$e" /boot/efi/EFI/boot/bootx64.efi
return
fi
done
done
}
vtoy_fix_elementary_partuuid() {
if grep -q 'elementary OS' /etc/os-release; then
if grep -q '^PARTUUID=.* /boot/efi ' /etc/fstab; then
part=$(grep ' /boot/efi ' /proc/mounts | awk '{print $1}')
uuid=$(blkid -s UUID -o value $part)
sed -i "s#^PARTUUID=.* /boot/efi #UUID=$uuid /boot/efi #g" /etc/fstab
echo "Fix elementary OS PARTUUID $part"
fi
fi
}
. ./tools/efi_legacy_grub.sh
vtoy_clean_env
cp -a $vtdumpcmd /sbin/vtoydump
cp -a $partxcmd /sbin/vtoypartx
cp -a $vtoytool /sbin/vtoytool
cat /sbin/vtoydump $dmpatchko > /sbin/vtoydmpatch
cp -a ./tools/vtoydrivers /sbin/vtoydrivers
cp -a ./distros/$initrdtool/vtoy-hook.sh /usr/share/initramfs-tools/hooks/
cp -a ./distros/$initrdtool/vtoy-local-top.sh /etc/initramfs-tools/scripts/local-top/
echo "updating the initramfs, please wait ..."
update-initramfs -u
disable_grub_os_probe
#wrapper grub-probe
echo "grub mkconfig ..."
PROBE_PATH=$(find_grub_probe_path)
MKCONFIG_PATH=$(find_grub_mkconfig_path)
echo "PROBE_PATH=$PROBE_PATH MKCONFIG_PATH=$MKCONFIG_PATH"
if [ -e "$PROBE_PATH" -a -e "$MKCONFIG_PATH" ]; then
wrapper_grub_probe $PROBE_PATH
GRUB_CFG_PATH=$(find_grub_config_path)
if [ -f "$GRUB_CFG_PATH" ]; then
echo "$MKCONFIG_PATH -o $GRUB_CFG_PATH"
$MKCONFIG_PATH -o $GRUB_CFG_PATH
else
echo "$MKCONFIG_PATH null"
$MKCONFIG_PATH > /dev/null 2>&1
fi
fi
#efi fixup
if [ -e /sys/firmware/efi ]; then
if [ -e /dev/mapper/ventoy ]; then
echo "This is ventoy enviroment"
else
vtoy_fix_elementary_partuuid
update_grub_config
install_legacy_bios_grub
fi
if [ "$1" = "-s" ]; then
recover_shim_efi
else
replace_shim_efi
fi
vtoy_efi_fixup
fi

View File

@@ -0,0 +1,33 @@
#!/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_mkinitcpio() {
if which mkinitcpio >/dev/null 2>&1; then
for vtfile in '/etc/mkinitcpio.conf' '/usr/lib/initcpio/install/lvm2'; do
if ! [ -f $vtfile ]; then
[ -z "1" ]; return
fi
done
[ -n "1" ]
else
[ -z "1" ]
fi
}

View File

@@ -0,0 +1,382 @@
#!/bin/bash
#************************************************************************************
# 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/>.
#
#************************************************************************************
###########################
###########################
#AUTO_INSERT_COMMON_FUNC
ventoy_check_efivars() {
if [ -e /sys/firmware/efi ]; then
if grep -q efivar /proc/mounts; then
:
else
if [ -e /sys/firmware/efi/efivars ]; then
mount -t efivarfs efivarfs /sys/firmware/efi/efivars >/dev/null 2>&1
fi
fi
fi
}
ventoy_log() {
echo "$@" >> /tmp/vtoy.log
}
ventoy_need_dm_patch() {
if vtoydump -R > /dev/null 2>&1; then
[ 1 -eq 1 ]; return
fi
if grep -q "VTOY_LINUX_REMOUNT=1" /proc/cmdline; then
[ 1 -eq 1 ]; return
fi
[ 1 -eq 0 ]
}
ventoy_check_insmod() {
if [ -f /bin/kmod ]; then
[ -f /bin/insmod ] || ln -s /bin/kmod /bin/insmod
[ -f /bin/lsmod ] || ln -s /bin/kmod /bin/lsmod
fi
}
ventoy_need_proc_ibt() {
vtKv=$(uname -r)
vtMajor=$(echo $vtKv | awk -F. '{print $1}')
vtMinor=$(echo $vtKv | awk -F. '{print $2}')
#ibt was supported since linux kernel 5.18
if [ $vtMajor -lt 5 ]; then
false; return
elif [ $vtMajor -eq 5 ]; then
if [ $vtMajor -lt 18 ]; then
false; return
fi
fi
if grep -q ' ibt=off' /proc/cmdline; then
false; return
fi
#hardware CPU doesn't support IBT
if vtoytool vtoykmod -I; then
:
else
false; return
fi
#dot.CONFIG not enabled
if grep -q ' ibt_restore$' /proc/kallsyms; then
:
else
false; return
fi
true
}
ventoy_do_dm_patch() {
vtDmPatchDebug=0
ventoy_log 'ventoy_do_dm_patch'
if [ -f /tmp/dm_patch.ko ]; then
if grep -q 'dm_patch' /proc/modules; then
ventoy_log "dm_patch already exist"
else
ventoy_log "dm_patch reinstall"
insmod /tmp/dm_patch.ko >>/tmp/vtoy.log 2>&1
if grep -q 'dm_patch' /proc/modules; then
ventoy_log "dm_patch reinstall success"
else
ventoy_log "dm_patch reinstall failed"
fi
fi
return
fi
if [ -f /bin/vtoydump ]; then
vtHeadSize=$(stat -c '%s' /bin/vtoydump)
dd if=/bin/vtoydmpatch of=/tmp/dm_patch.ko bs=1 skip=$vtHeadSize >/dev/null 2>&1
elif [ -f /sbin/vtoydump ]; then
vtHeadSize=$(stat -c '%s' /sbin/vtoydump)
dd if=/sbin/vtoydmpatch of=/tmp/dm_patch.ko bs=1 skip=$vtHeadSize >/dev/null 2>&1
else
ventoy_log 'vtoydump not found'
return
fi
if ! grep -m1 -q dm_get_table_device /proc/kallsyms; then
ventoy_log "modprobe dm_mod"
modprobe dm_mod >>/tmp/vtoy.log 2>&1
fi
cat /proc/kallsyms | sort > /tmp/kallsyms
if grep -m1 -q 'open_table_device.isra' /tmp/kallsyms; then
vtLine=$(vtoytool vtoyksym open_table_device.isra /tmp/kallsyms)
else
vtLine=$(vtoytool vtoyksym dm_get_table_device /tmp/kallsyms)
fi
get_addr=$(echo $vtLine | awk '{print $1}')
get_size=$(echo $vtLine | awk '{print $2}')
vtLine=$(vtoytool vtoyksym blkdev_get_by_dev /tmp/kallsyms)
blkdev_get_addr=$(echo $vtLine | awk '{print $1}')
vtLine=$(vtoytool vtoyksym blkdev_put /tmp/kallsyms)
blkdev_put_addr=$(echo $vtLine | awk '{print $1}')
vtLine=$(vtoytool vtoyksym bdev_open_by_dev /tmp/kallsyms)
bdev_open_addr=$(echo $vtLine | awk '{print $1}')
vtLine=$(vtoytool vtoyksym bdev_file_open_by_dev /tmp/kallsyms)
bdev_file_open_by_dev=$(echo $vtLine | awk '{print $1}')
if grep -m1 -q 'close_table_device.isra' /tmp/kallsyms; then
vtLine=$(vtoytool vtoyksym close_table_device.isra /tmp/kallsyms)
else
vtLine=$(vtoytool vtoyksym dm_put_table_device /tmp/kallsyms)
fi
put_addr=$(echo $vtLine | awk '{print $1}')
put_size=$(echo $vtLine | awk '{print $2}')
ro_addr=$(grep ' set_memory_ro$' /proc/kallsyms | awk '{print $1}')
rw_addr=$(grep ' set_memory_rw$' /proc/kallsyms | awk '{print $1}')
kprobe_reg_addr=$(grep ' register_kprobe$' /proc/kallsyms | awk '{print $1}')
kprobe_unreg_addr=$(grep ' unregister_kprobe$' /proc/kallsyms | awk '{print $1}')
if [ "$VTOY_DEBUG_LEVEL" = "01" ]; then
vtDmPatchDebug=1
fi
if grep -q 'dmpatch_debug' /proc/cmdline; then
vtDmPatchDebug=1
fi
if [ $vtDmPatchDebug -eq 1 ]; then
printk_addr=$(grep ' printk$' /proc/kallsyms | awk '{print $1}')
vtDebug="-v"
elif grep -q "vtdebug" /proc/cmdline; then
printk_addr=$(grep ' printk$' /proc/kallsyms | awk '{print $1}')
vtDebug="-v"
else
printk_addr=0
fi
if [ -z "$printk_addr" ]; then
printk_addr=$(grep ' _printk$' /proc/kallsyms | awk '{print $1}')
fi
if ventoy_need_proc_ibt; then
ventoy_log "need to proc IBT"
vtIBT='0x8888'
else
ventoy_log "NO need to proc IBT"
vtIBT='0'
fi
#printk_addr=$(grep ' printk$' /proc/kallsyms | awk '{print $1}')
#vtDebug="-v"
ventoy_log get_addr=$get_addr get_size=$get_size vtDebug=$vtDebug
ventoy_log put_addr=$put_addr put_size=$put_size
ventoy_log blkdev_get_addr=$blkdev_get_addr blkdev_put_addr=$blkdev_put_addr
ventoy_log kprobe_reg_addr=$kprobe_reg_addr kprobe_unreg_addr=$kprobe_unreg_addr
ventoy_log ro_addr=$ro_addr rw_addr=$rw_addr printk_addr=$printk_addr
ventoy_log bdev_open_addr=$bdev_open_addr bdev_file_open_by_dev=$bdev_file_open_by_dev
if [ "$get_addr" = "0" -o "$put_addr" = "0" ]; then
ventoy_log "Invalid symbol address"
return
fi
if [ "$ro_addr" = "0" -o "$rw_addr" = "0" ]; then
ventoy_log "Invalid symbol address"
return
fi
vtKv=$(uname -r)
vtKVMajor=$(echo $vtKv | awk -F. '{print $1}')
vtKVMinor=$(echo $vtKv | awk -F. '{print $2}')
vtKVSubMinor=$(echo $vtKv | awk -F. '{print $3}')
if [ ! -d /lib/modules/$vtKv ]; then
ventoy_log "No modules directory found"
return
elif [ -d /lib/modules/$vtKv/kernel/fs ]; then
vtModPath=$(find /lib/modules/$vtKv/kernel/fs/ -name "*.ko*" | head -n1)
else
vtModPath=$(find /lib/modules/$vtKv/kernel/ -name "xfs.ko*" | head -n1)
fi
if [ -z "$vtModPath" ]; then
vtModPath=$(find /lib/modules/$vtKv/kernel/ -name "*.ko*" | head -n1)
fi
vtModName=$(basename $vtModPath)
[ -f /tmp/$vtModName ] && rm -f /tmp/$vtModName
ventoy_log "template module is $vtModPath $vtModName"
if [ -z "$vtModPath" ]; then
ventoy_log "No template module found"
return
elif echo $vtModPath | grep -q "[.]ko$"; then
cp -a $vtModPath /tmp/$vtModName
elif echo $vtModPath | grep -q "[.]ko[.]xz$"; then
xzcat $vtModPath > /tmp/$vtModName
elif echo $vtModPath | grep -q "[.]ko[.]gz$"; then
zcat $vtModPath > /tmp/$vtModName
elif echo $vtModPath | grep -q "[.]ko[.]zst$"; then
zstdcat $vtModPath > /tmp/$vtModName
else
ventoy_log "unsupport module type"
return
fi
#step1: modify vermagic/mod crc/relocation
vtoytool vtoykmod -u $vtKVMajor $vtKVMinor /tmp/dm_patch.ko /tmp/$vtModName $vtDebug >>/tmp/vtoy.log 2>&1
#step2: fill parameters
vtPgsize=$(vtoytool vtoyksym -p)
vtoytool vtoykmod -f /tmp/dm_patch.ko $vtPgsize 0x$printk_addr 0x$ro_addr 0x$rw_addr $get_addr $get_size $put_addr $put_size 0x$kprobe_reg_addr 0x$kprobe_unreg_addr $vtKVMajor $vtIBT $vtKVMinor $blkdev_get_addr $blkdev_put_addr $vtKVSubMinor $bdev_open_addr $bdev_file_open_by_dev $vtDebug >>/tmp/vtoy.log 2>&1
ventoy_check_insmod
insmod /tmp/dm_patch.ko >>/tmp/vtoy.log 2>&1
if grep -q 'dm_patch' /proc/modules; then
ventoy_log "dm_patch success"
else
ventoy_log "dm_patch failed"
fi
}
ventoy_dm_patch_proc_begin() {
if ventoy_need_dm_patch; then
export vtLevel1=$(cat /proc/sys/kernel/printk | awk '{print $1}')
export vtLevel2=$(cat /proc/sys/kernel/printk | awk '{print $2}')
export vtLevel3=$(cat /proc/sys/kernel/printk | awk '{print $3}')
export vtLevel4=$(cat /proc/sys/kernel/printk | awk '{print $4}')
#suppress printk message
echo 0 $vtLevel2 0 $vtLevel4 > /proc/sys/kernel/printk
fi
}
ventoy_dm_patch_install() {
if ventoy_need_dm_patch; then
ventoy_do_dm_patch
fi
}
ventoy_dm_patch_remove() {
if ventoy_need_dm_patch; then
if grep -q 'dm_patch' /proc/modules; then
ventoy_log "remove dm_patch"
rmmod dm_patch
fi
fi
}
ventoy_dm_create_ventoy() {
ventoy_dm_patch_install
dmsetup create ventoy /ventoy_table
vret=$?
ventoy_dm_patch_remove
return $vret
}
ventoy_dm_patch_proc_end() {
if ventoy_need_dm_patch; then
#recover printk level
echo $vtLevel1 $vtLevel2 $vtLevel3 $vtLevel4 > /proc/sys/kernel/printk
fi
}
vtoy_wait_for_device() {
local i=100
while [ $i -gt 0 ]; do
if vtoydump > /dev/null 2>&1; then
break
else
sleep 0.2
fi
i=$((i - 1))
done
}
vtoy_device_mapper_proc() {
#flush multipath before dmsetup
multipath -F > /dev/null 2>&1
vtoydump -L > /ventoy_table
if ventoy_dm_create_ventoy; then
:
else
sleep 3
multipath -F > /dev/null 2>&1
ventoy_dm_create_ventoy
fi
DEVDM=/dev/mapper/ventoy
loop=0
while ! [ -e $DEVDM ]; do
sleep 0.5
let loop+=1
if [ $loop -gt 10 ]; then
echo "Waiting for ventoy device ..." > /dev/console
fi
if [ $loop -gt 10 -a $loop -lt 15 ]; then
multipath -F > /dev/null 2>&1
ventoy_dm_create_ventoy
fi
done
for ID in $(vtoypartx $DEVDM -oNR | grep -v NR); do
PART_START=$(vtoypartx $DEVDM -n$ID -oSTART,SECTORS | grep -v START | awk '{print $1}')
PART_SECTOR=$(vtoypartx $DEVDM -n$ID -oSTART,SECTORS | grep -v START | awk '{print $2}')
echo "0 $PART_SECTOR linear $DEVDM $PART_START" > /ventoy_part_table
dmsetup create ventoy$ID /ventoy_part_table
done
rm -f /ventoy_table
rm -f /ventoy_part_table
}
run_hook() {
#check for efivarfs
ventoy_check_efivars
if vtoydump -c >/dev/null 2>/dev/null; then
vtoy_wait_for_device
if vtoydump > /dev/null 2>&1; then
ventoy_dm_patch_proc_begin
vtoy_device_mapper_proc
ventoy_dm_patch_proc_end
fi
fi
}

View File

@@ -0,0 +1,48 @@
#!/bin/bash
#************************************************************************************
# 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/>.
#
#************************************************************************************
build() {
add_binary "dd"
add_binary "sort"
add_binary "head"
add_binary "find"
add_binary "xzcat"
add_binary "zcat"
add_binary "basename"
add_binary "vtoydump"
add_binary "vtoypartx"
add_binary "vtoydmpatch"
add_binary "vtoytool"
for md in $(cat /sbin/vtoydrivers); do
if [ -n "$md" ]; then
if modinfo -n $md 2>/dev/null | grep -q '\.ko'; then
add_module $md
fi
fi
done
add_runscript
}
help() {
cat <<HELPEOF
This hook enables ventoy in initramfs.
HELPEOF
}

View File

@@ -0,0 +1,93 @@
#!/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/>.
#
#************************************************************************************
. ./tools/efi_legacy_grub.sh
vtoy_clean_env() {
rm -f /sbin/vtoydump /sbin/vtoypartx /sbin/vtoytool /sbin/vtoydmpatch /sbin/vtoydrivers
rm -f /usr/lib/initcpio/hooks/ventoy
rm -f /usr/lib/initcpio/install/ventoy
}
vtoy_clean_env
cp -a $vtdumpcmd /sbin/vtoydump
cp -a $partxcmd /sbin/vtoypartx
cp -a $vtoytool /sbin/vtoytool
cat /sbin/vtoydump $dmpatchko > /sbin/vtoydmpatch
cp -a ./tools/vtoydrivers /sbin/vtoydrivers
cp -a ./distros/$initrdtool/ventoy-install.sh /usr/lib/initcpio/install/ventoy
cp -a ./distros/$initrdtool/ventoy-hook.sh /usr/lib/initcpio/hooks/ventoy
echo "updating the initramfs, please wait ..."
if ! grep -q '^HOOKS=.*ventoy' /etc/mkinitcpio.conf; then
if grep -q '^HOOKS=.*lvm' /etc/mkinitcpio.conf; then
exthook='ventoy'
else
exthook='lvm2 ventoy'
fi
if grep -q '^HOOKS=.*encrypt' /etc/mkinitcpio.conf; then
sed "s/\(^HOOKS=.*\)encrypt/\1 $exthook encrypt/" -i /etc/mkinitcpio.conf
elif grep -q "^HOOKS=\"" /etc/mkinitcpio.conf; then
sed "s/^HOOKS=\"\(.*\)\"/HOOKS=\"\1 $exthook\"/" -i /etc/mkinitcpio.conf
elif grep -q "^HOOKS=(" /etc/mkinitcpio.conf; then
sed "s/^HOOKS=(\(.*\))/HOOKS=(\1 $exthook)/" -i /etc/mkinitcpio.conf
fi
fi
mkinitcpio -P
disable_grub_os_probe
#wrapper grub-probe
echo "grub mkconfig ..."
PROBE_PATH=$(find_grub_probe_path)
MKCONFIG_PATH=$(find_grub_mkconfig_path)
echo "PROBE_PATH=$PROBE_PATH MKCONFIG_PATH=$MKCONFIG_PATH"
if [ -e "$PROBE_PATH" -a -e "$MKCONFIG_PATH" ]; then
wrapper_grub_probe $PROBE_PATH
GRUB_CFG_PATH=$(find_grub_config_path)
if [ -f "$GRUB_CFG_PATH" ]; then
echo "$MKCONFIG_PATH -o $GRUB_CFG_PATH"
$MKCONFIG_PATH -o $GRUB_CFG_PATH
else
echo "$MKCONFIG_PATH null"
$MKCONFIG_PATH > /dev/null 2>&1
fi
fi
if [ -e /sys/firmware/efi ]; then
if [ -e /dev/mapper/ventoy ]; then
echo "This is ventoy enviroment"
else
update_grub_config
install_legacy_bios_grub
fi
if [ "$1" = "-s" ]; then
recover_shim_efi
else
replace_shim_efi
fi
fi

View File

@@ -0,0 +1,12 @@
#!/bin/sh
set -e
cat <<EOF
function linuxefi {
linux "\$@"
}
function initrdefi {
initrd "\$@"
}
EOF

View File

@@ -0,0 +1 @@
"#

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,373 @@
#!/bin/sh
disable_grub_os_probe() {
for probe in 30_os-prober; do
probe_cfg=/etc/grub.d/$probe
if [ -f $probe_cfg ]; then
if grep -q 'VTOYBOOT_FLAG' $probe_cfg; then
:
else
sed "1a#VTOYBOOT_FLAG" -i $probe_cfg
sed "1aexit 0" -i $probe_cfg
fi
fi
done
}
find_grub_editenv_path() {
if which grub-editenv >/dev/null 2>&1; then
which grub-editenv
elif which grub2-editenv >/dev/null 2>&1; then
which grub2-editenv
else
echo "XXX"
fi
}
find_grub_probe_path() {
if which grub-probe >/dev/null 2>&1; then
which grub-probe
elif which grub2-probe >/dev/null 2>&1; then
which grub2-probe
else
echo "XXX"
fi
}
find_grub_mkconfig_path() {
if which grub-mkconfig >/dev/null 2>&1; then
which grub-mkconfig
elif which grub2-mkconfig >/dev/null 2>&1; then
which grub2-mkconfig
else
echo "XXX"
fi
}
find_grub_config_path() {
for i in grub.cfg grub2.cfg grub-efi.cfg grub2-efi.cfg; do
if readlink -f -e /etc/$i > /dev/null; then
cfgfile=$(readlink -f -e /etc/$i)
echo $cfgfile
return
fi
done
for t in /boot/grub/grub.cfg /boot/grub2/grub.cfg; do
if grep -q 'BEGIN' $t 2>/dev/null; then
echo $t
return
fi
done
echo "xx"
}
update_grub_config() {
if update-grub -V > /dev/null 2>&1; then
GRUB_UPDATE=update-grub
elif update-grub2 -V > /dev/null 2>&1; then
GRUB_UPDATE=update-grub2
else
vgrubcfg=$(find_grub_config_path)
mkconfig=$(find_grub_mkconfig_path)
if [ -f $mkconfig -a -f $vgrubcfg ]; then
GRUB_UPDATE="$mkconfig -o $vgrubcfg"
else
echo "update-grub no need"
return
fi
fi
UPDATE=0
if [ -f /etc/default/grub ]; then
if grep -q 'GRUB_TIMEOUT=0' /etc/default/grub; then
UPDATE=1
sed 's/GRUB_TIMEOUT=0/GRUB_TIMEOUT=30/' -i /etc/default/grub
fi
if grep -q 'GRUB_TIMEOUT_STYLE=hidden' /etc/default/grub; then
UPDATE=1
sed 's/GRUB_TIMEOUT_STYLE=hidden/GRUB_TIMEOUT_STYLE=menu/' -i /etc/default/grub
fi
fi
if [ $UPDATE -eq 1 ]; then
echo "update grub config"
$GRUB_UPDATE
fi
}
print_bios_grub_warning() {
echo -e "\033[33m[WARNING] ################################################################## \033[0m"
for i in 0 1 2 3 4 5 6 7 8 9; do
echo -e "\033[33m[WARNING] !!!! This vhd/vdi/raw file will only be bootable in UEFI mode !!!! \033[0m"
done
echo -e "\033[33m[WARNING] ################################################################## \033[0m"
sleep 3
}
install_legacy_bios_grub() {
all_modules=""
if [ -f /boot/grub/grub.cfg ]; then
PREFIX=/boot/grub
MOD_PATH=/boot/grub
CFG=grub.cfg
if [ -f /boot/efi/EFI/UOS/grub.cfg ]; then
PREFIX=/EFI/UOS
elif [ -f /boot/efi/EFI/ubuntu/grub.cfg ]; then
PREFIX=/EFI/ubuntu
fi
else
for i in grub.cfg grub2.cfg grub-efi.cfg grub2-efi.cfg; do
if readlink -f -e /etc/$i > /dev/null; then
cfgfile=$(readlink -f -e /etc/$i)
MOD_PATH=${cfgfile%/*}
PREFIX=$MOD_PATH
if echo $MOD_PATH | grep -q '^/boot/efi'; then
if mountpoint -q /boot/efi; then
PREFIX=${MOD_PATH#/boot/efi}
fi
fi
CFG=${cfgfile##*/}
echo "/etc/$i --> $cfgfile"
break
fi
done
if [ -z "$MOD_PATH" ]; then
if [ -f /boot/grub2/grub.cfg ]; then
PREFIX=/boot/grub2
MOD_PATH=/boot/grub2
CFG=grub.cfg
if [ -f /boot/efi/EFI/opensuse/grub.cfg ]; then
PREFIX=/EFI/opensuse
fi
fi
fi
if [ -z "$MOD_PATH" ]; then
echo "[WARNING] grub.cfg not found, this vhd/vdi/raw file can only be booted in UEFI mode."
print_bios_grub_warning
return
fi
fi
if grub-mkimage -V > /dev/null 2>&1; then
GRUB_CMD=grub-mkimage
CFG_CMD=grub-mkconfig
elif grub2-mkimage -V > /dev/null 2>&1; then
GRUB_CMD=grub2-mkimage
CFG_CMD=grub2-mkconfig
else
echo "[WARNING] grub-mkimage not found, package missing?"
print_bios_grub_warning
return
fi
if [ -d /usr/lib/grub/x86_64-efi ]; then
GRUB_DIR=/usr/lib/grub
elif [ -d /usr/lib/grub/i386-pc ]; then
GRUB_DIR=/usr/lib/grub
elif [ -d /usr/share/grub2/i386-pc ]; then
GRUB_DIR=/usr/share/grub2
else
echo "[WARNING] grub module directory not found, package missing?"
print_bios_grub_warning
return
fi
if ! [ -d $GRUB_DIR/i386-pc ]; then
echo "[WARNING] grub i386-pc modules not installed, package missing?"
print_bios_grub_warning
return
fi
if [ -e /dev/sda ]; then
DISK=/dev/sda
elif [ -e /dev/vda ]; then
DISK=/dev/vda
elif [ -e /dev/hda ]; then
DISK=/dev/hda
else
echo "[WARNING] disk not found"
print_bios_grub_warning
return
fi
if $vtcheckcmd $DISK; then
echo "GPT check $DISK OK ..."
else
echo "GPT check $DISK failed, code=$?"
return
fi
echo PREFIX=$PREFIX CFG=$CFG DISK=$DISK
echo MOD_PATH=$MOD_PATH
chkPrefix=$PREFIX
while [ -n "$chkPrefix" ]; do
if mountpoint -q "$chkPrefix"; then
PREFIX=${MOD_PATH#$chkPrefix}
echo "$chkPrefix is mountpoint PREFIX=$PREFIX"
break
fi
chkPrefix=${chkPrefix%/*}
done
if grep -q 'linuxefi' $MOD_PATH/$CFG; then
echo "update grub.cfg ..."
cp -a ./tools/01_linuxefi /etc/grub.d/
$CFG_CMD -o $MOD_PATH/$CFG
elif grep -q 'blscfg' $MOD_PATH/$CFG; then
echo "update grub.cfg disable bls ..."
if grep -q '^GRUB_ENABLE_BLSCFG' /etc/default/grub; then
sed 's/^GRUB_ENABLE_BLSCFG.*/GRUB_ENABLE_BLSCFG=false/g' -i /etc/default/grub
else
echo 'GRUB_ENABLE_BLSCFG=false' >> /etc/default/grub
fi
$CFG_CMD -o $MOD_PATH/$CFG
fi
cp -a ./tools/embedcfg embed.cfg
sed "s#XXX#$PREFIX#g" -i embed.cfg
sed "s#YYY#$CFG#g" -i embed.cfg
for mod in $(cat ./tools/grubmodules); do
if [ -e $GRUB_DIR/i386-pc/${mod}.mod ]; then
all_modules="$all_modules $mod"
fi
done
$GRUB_CMD -c "./embed.cfg" --prefix "$PREFIX" --output "./core.img" --format 'i386-pc' --compression 'auto' $all_modules
echo "Write loader to $DISK ..."
dd if=$GRUB_DIR/i386-pc/boot.img of=$DISK bs=1 count=440 status=none && sync
dd if=./tools/bootbin of=/dev/sda bs=1 count=1 seek=92 status=none && sync
dd if=./core.img of=/dev/sda bs=512 seek=34 status=none && sync
dd if=./tools/bootbin of=/dev/sda bs=1 count=1 skip=1 seek=17908 status=none && sync
if ! [ -d $MOD_PATH/i386-pc ]; then
cp -a $GRUB_DIR/i386-pc $MOD_PATH/
fi
rm -f ./embed.cfg
rm -f ./core.img
}
wrapper_grub_probe() {
if [ -e "${1}-bk" ]; then
if grep -q '#!' "$1"; then
rm -f "$1"
mv "${1}-bk" "$1"
else
rm -f "${1}-bk"
fi
fi
cp -a "$1" "${1}-bk"
rm -f "$1"
cp -a ./tools/grub-probe.sh "$1"
chmod +x "$1"
chmod +x "${1}-bk"
}
wrapper_grub_editenv() {
if [ -e "${1}-bk" ]; then
if grep -q '#!' "$1"; then
rm -f "$1"
mv "${1}-bk" "$1"
else
rm -f "${1}-bk"
fi
fi
cp -a "$1" "${1}-bk"
rm -f "$1"
cp -a ./tools/grub-editenv.sh "$1"
chmod +x "$1"
chmod +x "${1}-bk"
}
replace_shim_efi() {
echo "replace shim efi ..."
if [ ! -d /boot/efi/EFI ]; then
return
fi
vCnt=$(find /boot/efi/EFI -type f | grep -i /efi/boot/bootx64.efi | wc -l)
if [ $vCnt -ne 1 ]; then
echo "bootx64.efi no need $vCnt"
return
fi
vBOOTX64=$(find /boot/efi/EFI -type f | grep -i /efi/boot/bootx64.efi)
vCnt=$(find /boot/efi/EFI -type f | grep -i shimx64.efi | wc -l)
if [ $vCnt -le 0 ]; then
echo "shimx64.efi no need $vCnt"
return
fi
vSHIMX64=$(find /boot/efi/EFI -type f | grep -m1 -i shimx64.efi)
vCnt=$(find /boot/efi/EFI -type f | grep -i grubx64.efi | wc -l)
if [ $vCnt -le 0 ]; then
echo "grubx64.efi no need $vCnt"
return
fi
vGRUBX64=$(find /boot/efi/EFI -type f | grep -m1 -i grubx64.efi)
echo vBOOTX64=$vBOOTX64
echo vSHIMX64=$vSHIMX64
echo vGRUBX64=$vGRUBX64
vMD51=$(md5sum $vBOOTX64 | awk '{print $1}')
vMD52=$(md5sum $vSHIMX64 | awk '{print $1}')
if [ "$vMD51" != "$vMD52" ]; then
echo "bootx64 shimx64 not equal"
echo "$vMD51"
echo "$vMD52"
return
fi
echo "BOOT=$vBOOTX64"
echo "GRUB=$vGRUBX64"
mv $vBOOTX64 ${vBOOTX64}_VTBK
cp $vGRUBX64 $vBOOTX64
}
recover_shim_efi() {
echo "recover shim efi ..."
if [ ! -d /boot/efi/EFI ]; then
return
fi
vVTBKFILE=$(find /boot/efi/EFI -type f | grep -i '_VTBK$')
if [ -z "$vVTBKFILE" ]; then
echo "no backup file found, no need."
return
fi
if [ -f "$vVTBKFILE" ]; then
vVTRAWFILE=$(echo "$vVTBKFILE" | sed "s/_VTBK//")
if [ -f "$vVTRAWFILE" ]; then
rm -f "$vVTRAWFILE"
echo "BACK=$vVTRAWFILE"
echo "BOOT=$vVTBKFILE"
mv "$vVTBKFILE" "$vVTRAWFILE"
fi
fi
}

View File

@@ -0,0 +1,3 @@
search -f XXX/YYY -s root
set prefix=($root)XXX
configfile $prefix/YYY

View File

@@ -0,0 +1,76 @@
#!/bin/sh
if which grub-editenv-bk >/dev/null 2>&1; then
grub_editenv_cmd=grub-editenv-bk
elif which grub2-editenv-bk >/dev/null 2>&1; then
grub_editenv_cmd=grub2-editenv-bk
else
grub_editenv_cmd=grub-editenv-bk
fi
curdate=$(date)
if [ -e /dev/mapper/ventoy -a -d /etc/vtoyboot/editenv ]; then
$grub_editenv_cmd $* > /etc/vtoyboot/editenv/tmp_stdout 2>/etc/vtoyboot/editenv/tmp_stderr
code=$?
if [ $code -eq 0 ]; then
cat /etc/vtoyboot/editenv/tmp_stdout
exit 0
fi
newpara=$(echo $* | sed "s#/dev/mapper/ventoy#/dev/sda#")
echo "[$curdate] oldpara=$* newpara=$newpara" >> /etc/vtoyboot/editenv/match.log
id=1
while [ -n "1" ]; do
if [ -d /etc/vtoyboot/editenv/$id ]; then
para=$(head -n1 /etc/vtoyboot/editenv/$id/param)
if [ "$para" = "$newpara" ]; then
code=$(cat /etc/vtoyboot/editenv/$id/errcode)
cat /etc/vtoyboot/editenv/$id/stdout
if [ $code -ne 0 ]; then
cat /etc/vtoyboot/editenv/$id/stderr >&2
fi
echo "[$curdate] grub-editenv match history id=$id code=$code" >> /etc/vtoyboot/editenv/match.log
exit $code
fi
else
break
fi
id=$(expr $id + 1)
done
echo "[$curdate] grub-editenv NO match $*" >> /etc/vtoyboot/editenv/match.log
cat /etc/vtoyboot/editenv/tmp_stdout
cat /etc/vtoyboot/editenv/tmp_stderr >&2
exit $code
else
[ -d /etc/vtoyboot/editenv ] || mkdir -p /etc/vtoyboot/editenv
id=1
override=0
while [ -d /etc/vtoyboot/editenv/$id ]; do
para=$(head -n1 /etc/vtoyboot/editenv/$id/param)
if [ "$para" = "$*" ]; then
echo "[$curdate] override $id $*" >> /etc/vtoyboot/editenv/history.log
override=1
break
fi
id=$(expr $id + 1)
done
if [ $override -eq 0 ]; then
echo "[$curdate] $*" >> /etc/vtoyboot/editenv/history.log
fi
mkdir -p /etc/vtoyboot/editenv/$id
echo "$*" > /etc/vtoyboot/editenv/$id/param
$grub_editenv_cmd $* > /etc/vtoyboot/editenv/$id/stdout 2>/etc/vtoyboot/editenv/$id/stderr
code=$?
echo $code > /etc/vtoyboot/editenv/$id/errcode
cat /etc/vtoyboot/editenv/$id/stdout
cat /etc/vtoyboot/editenv/$id/stderr >&2
exit $code
fi

View File

@@ -0,0 +1,76 @@
#!/bin/sh
if which grub-probe-bk >/dev/null 2>&1; then
grub_probe_cmd=grub-probe-bk
elif which grub2-probe-bk >/dev/null 2>&1; then
grub_probe_cmd=grub2-probe-bk
else
grub_probe_cmd=grub-probe-bk
fi
curdate=$(date)
if [ -e /dev/mapper/ventoy -a -d /etc/vtoyboot/probe ]; then
$grub_probe_cmd $* > /etc/vtoyboot/probe/tmp_stdout 2>/etc/vtoyboot/probe/tmp_stderr
code=$?
if [ $code -eq 0 ]; then
cat /etc/vtoyboot/probe/tmp_stdout
exit 0
fi
newpara=$(echo $* | sed "s#/dev/mapper/ventoy#/dev/sda#")
echo "[$curdate] oldpara=$* newpara=$newpara" >> /etc/vtoyboot/probe/match.log
id=1
while [ -n "1" ]; do
if [ -d /etc/vtoyboot/probe/$id ]; then
para=$(head -n1 /etc/vtoyboot/probe/$id/param)
if [ "$para" = "$newpara" ]; then
code=$(cat /etc/vtoyboot/probe/$id/errcode)
cat /etc/vtoyboot/probe/$id/stdout
if [ $code -ne 0 ]; then
cat /etc/vtoyboot/probe/$id/stderr >&2
fi
echo "[$curdate] grub-probe match history id=$id code=$code" >> /etc/vtoyboot/probe/match.log
exit $code
fi
else
break
fi
id=$(expr $id + 1)
done
echo "[$curdate] grub-probe NO match $*" >> /etc/vtoyboot/probe/match.log
cat /etc/vtoyboot/probe/tmp_stdout
cat /etc/vtoyboot/probe/tmp_stderr >&2
exit $code
else
[ -d /etc/vtoyboot/probe ] || mkdir -p /etc/vtoyboot/probe
id=1
override=0
while [ -d /etc/vtoyboot/probe/$id ]; do
para=$(head -n1 /etc/vtoyboot/probe/$id/param)
if [ "$para" = "$*" ]; then
echo "[$curdate] override $id $*" >> /etc/vtoyboot/probe/history.log
override=1
break
fi
id=$(expr $id + 1)
done
if [ $override -eq 0 ]; then
echo "[$curdate] $*" >> /etc/vtoyboot/probe/history.log
fi
mkdir -p /etc/vtoyboot/probe/$id
echo "$*" > /etc/vtoyboot/probe/$id/param
$grub_probe_cmd $* > /etc/vtoyboot/probe/$id/stdout 2>/etc/vtoyboot/probe/$id/stderr
code=$?
echo $code > /etc/vtoyboot/probe/$id/errcode
cat /etc/vtoyboot/probe/$id/stdout
cat /etc/vtoyboot/probe/$id/stderr >&2
exit $code
fi

View File

@@ -0,0 +1,78 @@
loadenv
lvm
diskfilter
date
drivemap
blocklist
regexp
newc
vga_text
ntldr
search
at_keyboard
usb_keyboard
gcry_md5
hashsum
gzio
xzio
lzopio
lspci
pci
ext2
xfs
chain
read
halt
iso9660
linux16
test
true
sleep
reboot
echo
videotest
videoinfo
videotest_checksum
video_colors
video_cirrus
video_bochs
vga
vbe
video_fb
font
video
gettext
extcmd
terminal
linux
minicmd
help
configfile
tr
trig
boot
biosdisk
disk
ls
tar
squash4
password_pbkdf2
all_video
png
jpeg
part_gpt
part_msdos
fat
exfat
ntfs
loopback
gzio
normal
udf
gfxmenu
gfxterm
gfxterm_background
gfxterm_menu
net
tftp
http

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,28 @@
efivars
efivarfs
mptsas
mptspi
nvme
usb-storage
ehci-fsl
ehci-hcd
ehci-pci
ehci-platform
ohci-hcd
ohci-pci
ohci-platform
uhci-hcd
xhci-hcd
xhci-pci
xhci-plat-hcd
vhci-hcd
usbhid
sg
uas
pmcraid
smartpqi
megaraid
megaraid_sas
vmw_pvscsi
mvsas
aacraid

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,73 @@
#!/bin/sh
find_grub_probe_path() {
if which grub-probe >/dev/null 2>&1; then
which grub-probe
elif which grub2-probe >/dev/null 2>&1; then
which grub2-probe
else
echo "XXX"
fi
}
find_grub_mkconfig_path() {
if which grub-mkconfig >/dev/null 2>&1; then
which grub-mkconfig
elif which grub2-mkconfig >/dev/null 2>&1; then
which grub2-mkconfig
else
echo "XXX"
fi
}
find_grub_config_path() {
for i in grub.cfg grub2.cfg grub-efi.cfg grub2-efi.cfg; do
if readlink -f -e /etc/$i > /dev/null; then
cfgfile=$(readlink -f -e /etc/$i)
echo $cfgfile
return
fi
done
for t in /boot/grub/grub.cfg /boot/grub2/grub.cfg; do
if grep -q 'BEGIN' $t 2>/dev/null; then
echo $t
return
fi
done
echo "xx"
}
update_grub_config() {
GRUB_UPDATE=""
if update-grub -V > /dev/null 2>&1; then
GRUB_UPDATE=update-grub
elif update-grub2 -V > /dev/null 2>&1; then
GRUB_UPDATE=update-grub2
else
vgrubcfg=$(find_grub_config_path)
mkconfig=$(find_grub_mkconfig_path)
if [ -f $mkconfig -a -f $vgrubcfg ]; then
GRUB_UPDATE="$mkconfig -o $vgrubcfg"
else
echo "no grub.cfg found."
return
fi
fi
if [ -n "$GRUB_UPDATE" ]; then
echo "update grub config $GRUB_UPDATE"
$GRUB_UPDATE
fi
}
USER=$(whoami)
if [ "$USER" != "root" ]; then
echo "Please run this script as root or use sudo"
echo ""
exit 1
fi
update_grub_config

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,163 @@
#!/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_version=1.0.35
if echo "$*" | grep -q __vtoyloop__; then
:
else
if readlink /proc/$$/exe | grep -q dash; then
exec /bin/bash $0 $* __vtoyloop__
fi
fi
vtoy_get_initrdtool_type() {
. ./distros/initramfstool/check.sh
. ./distros/mkinitcpio/check.sh
. ./distros/dracut/check.sh
if vtoy_check_initramfs_tool; then
echo 'initramfstool'; return
elif vtoy_check_mkinitcpio; then
echo 'mkinitcpio'; return
elif vtoy_check_dracut; then
echo 'dracut'; return
else
echo 'unknown'; return
fi
}
echo ''
echo '**********************************************'
echo " vtoyboot $vtoy_version"
echo " longpanda admin@ventoy.net"
echo " https://www.ventoy.net"
echo '**********************************************'
echo ''
USER=$(whoami)
if [ "$USER" != "root" ]; then
echo "Please run this script as root or use sudo"
echo ""
exit 1
fi
if ! [ -d ./distros ]; then
echo "Please run the script in the right directory"
echo ""
exit 1
fi
if [ -e /dev/mapper/ventoy ]; then
:
else
if ls -1 /dev | grep -q '[svh]db$'; then
echo "More than one disks found. Currently only one disk is supported."
echo ""
exit 1
fi
fi
initrdtool=$(vtoy_get_initrdtool_type)
if ! [ -f ./distros/$initrdtool/vtoy.sh ]; then
echo 'Current OS is not supported!'
exit 1
fi
vtoyboot_need_proc_ibt() {
vtTool=$1
vtKv=$(uname -r)
vtMajor=$(echo $vtKv | awk -F. '{print $1}')
vtMinor=$(echo $vtKv | awk -F. '{print $2}')
#ibt was supported since linux kernel 5.18
if [ $vtMajor -lt 5 ]; then
false; return
elif [ $vtMajor -eq 5 ]; then
if [ $vtMajor -lt 18 ]; then
false; return
fi
fi
if grep -q ' ibt=off' /proc/cmdline; then
false; return
fi
#hardware CPU doesn't support IBT
if $vtTool vtoykmod -I; then
:
else
false; return
fi
#dot.CONFIG not enabled
if grep -q ' ibt_restore$' /proc/kallsyms; then
:
else
false; return
fi
true
}
#prepare vtoydump
if uname -a | grep -Eq "x86_64|amd64"; then
vtdumpcmd=./tools/vtoydump64
partxcmd=./tools/vtoypartx64
vtcheckcmd=./tools/vtoycheck64
vtoytool=./tools/vtoytool_64
dmpatchko=./tools/dm_patch_64.ko
if vtoyboot_need_proc_ibt $vtoytool; then
dmpatchko=./tools/dm_patch_ibt_64.ko
fi
elif uname -a | grep -Eq "aarch64|arm64"; then
vtdumpcmd=./tools/vtoydumpaa64
partxcmd=./tools/vtoypartxaa64
vtcheckcmd=./tools/vtoycheckaa64
vtoytool=./tools/vtoytool_aa64
dmpatchko=./tools/dm_patch_64.ko
else
vtdumpcmd=./tools/vtoydump32
partxcmd=./tools/vtoypartx32
vtcheckcmd=./tools/vtoycheck32
vtoytool=./tools/vtoytool_32
dmpatchko=./tools/dm_patch_32.ko
fi
chmod +x $vtdumpcmd $partxcmd $vtcheckcmd
for vsh in $(ls ./distros/$initrdtool/*.sh); do
chmod +x $vsh
done
echo "Current system use $initrdtool as initramfs tool"
. ./distros/$initrdtool/vtoy.sh "$@"
if [ $? -eq 0 ]; then
sync
echo ""
echo "vtoyboot process successfully finished."
echo ""
else
echo ""
echo "vtoyboot process failed, please check."
echo ""
exit 1
fi