Add a cleanup service for Ventoy udev rules files
Ventoy CI / build (push) Canceled after 0s
Mirror GitHub to Gitee / Sync-GitHub-to-Gitee (push) Canceled after 0s

When we boot an ISO file to the live system through Ventoy.
The ISO partition (e.g. /dev/sda1) is hold by the device-mapper which will block it to be mounted.
So Ventoy will create a mirror device-mapper /dev/mapper/VentoyPart for /dev/sda1.
Most live systems with GUI environment will auto mount Ventoy ISO partition by udisk.
So in 1.1.18 Venoty will inject an udev rule in the Live system in order to:
1. Block the original ISO partition (e.g. /dev/sda1) to be auto-mounted.
2. Force /dev/mapper/VentoyPart to be auto-mounted.

But the udev rule file was injected to /etc directory of the Live system, this will make the rule mistakenly inherited into the final installed system.
Originally, the /run directory would have been the most suitable location, but in some Live systems /run is not a static directory but an overlayfs which is created dynamically during boot which make it a little bit complex.
So we add a cleanup systemd/sysvinit service.
This commit is contained in:
longpanda
2026-11-23 20:26:11 +08:00
parent 8308a527ac
commit 2248d1f807
15 changed files with 313 additions and 53 deletions
@@ -9,7 +9,7 @@ vtlog "####### $0 $* ########"
vtoy_udev_auto_hook() {
if grep -q 'break.*=.*postmount' /init; then
vtlog "udev auto before postmount"
sed "/break.*=.*postmount/i\\$BUSYBOX_PATH/sh $VTOY_PATH/hook/manjaro/ventoy-uauto-exec.sh" -i /init
sed "/break.*=.*postmount/i\\$BUSYBOX_PATH/sh $VTOY_PATH/hook/arch/ventoy-uauto-exec.sh" -i /init
elif [ -d $VT_DRACUT_HOOKS/pre-pivot ]; then
vtlog "dracut distro"
ventoy_dracut_pivot_udev_rule
@@ -6,6 +6,10 @@ After=systemd-udevd.service
Type=oneshot
ExecStart=mv /dev/VTISOPART /dev/bk_VTISOPART
ExecStart=cp -a /dev/DMXXX /dev/VTISOPART
ExecStartPost=-/bin/sh -c '\
rm -f /etc/systemd/system/ventoy-clonezilla-cheat.service;\
rm -f /etc/systemd/system-preset/90-ventoy-clonezilla.preset;\
rm -f /etc/systemd/system/sysinit.target.wants/ventoy-clonezilla-cheat.service;\
'
[Install]
WantedBy=sysinit.target
@@ -3,6 +3,12 @@ Description=Ventoy subiquity curtin Workaround
[Service]
Type=oneshot
ExecStart=/etc/ventoy/ventoy-curtin-wa.sh
ExecStart=-/bin/sh /etc/ventoy_curtin/ventoy-curtin-wa.sh
ExecStartPost=-/bin/sh -c '\
rm -f /etc/systemd/system/ventoy-curtin-wa.service;\
rm -f /etc/systemd/system/snap.ubuntu-desktop-bootstrap.subiquity-server.service.d/10-ventoy-curtin-wa.conf;\
rm -rf /etc/ventoy_curtin;\
'
PrivateMounts=no
ProtectSystem=no
@@ -7,16 +7,16 @@ PATH=$BUSYBOX_PATH:$VTOY_PATH/tool:$PATH
vtlog "####### $0 $* ########"
subiquity_curtin_workaround() {
mkdir -p /root/etc/ventoy
ventoy_copy_file /ventoy/hook/debian/ventoy-curtin-wa.sh /root/etc/ventoy/ventoy-curtin-wa.sh 0755
ventoy_copy_file /ventoy/hook/debian/ventoy-curtin-wa.conf /root/etc/ventoy/ventoy-curtin-wa.conf 0644
ventoy_copy_file /ventoy/hook/debian/ventoy-curtin-wa.service /root/etc/ventoy/ventoy-curtin-wa.service 0644
mkdir -p /root/etc/ventoy_curtin
ventoy_copy_file /ventoy/hook/debian/ventoy-curtin-wa.sh /root/etc/ventoy_curtin/ventoy-curtin-wa.sh 0755
ventoy_copy_file /ventoy/hook/debian/ventoy-curtin-wa.conf /root/etc/ventoy_curtin/ventoy-curtin-wa.conf 0644
ventoy_copy_file /ventoy/hook/debian/ventoy-curtin-wa.service /root/etc/ventoy_curtin/ventoy-curtin-wa.service 0644
ln -sf /etc/ventoy/ventoy-curtin-wa.service /root/etc/systemd/system/ventoy-curtin-wa.service
ln -sf /etc/ventoy_curtin/ventoy-curtin-wa.service /root/etc/systemd/system/ventoy-curtin-wa.service
vtDepDir=/root/etc/systemd/system/snap.ubuntu-desktop-bootstrap.subiquity-server.service.d
mkdir -p $vtDepDir
ln -sf /etc/ventoy/ventoy-curtin-wa.conf $vtDepDir/10-ventoy-curtin-wa.conf
ln -sf /etc/ventoy_curtin/ventoy-curtin-wa.conf $vtDepDir/10-ventoy-curtin-wa.conf
}
@@ -6,7 +6,9 @@ ACTION=="add|change", SUBSYSTEM=="block", KERNEL=="DMXXX", \
ENV{UDISKS_SYSTEM}="0", \
ENV{UDISKS_REMOVABLE}="1", \
ENV{UDISKS_AUTO}="1", \
ENV{UDISKS_IGNORE}="0", \
ENV{UDISKS_AUTOMOUNTABLE}="1"
KERNEL=="yyy", SUBSYSTEM=="block", ENV{UDISKS_IGNORE}="1"
@@ -0,0 +1,22 @@
#!/bin/sh
rm -f /etc/systemd/system-preset/80-ventoy-udev.preset
rm -f /etc/systemd/system/sysinit.target.wants/ventoy-udev*.service
rm -f /etc/systemd/system/ventoy-udev*.service
if [ -f /etc/systemd/system/snap.ubuntu-desktop-bootstrap.subiquity-server.service.d/10-ventoy-curtin-wa.conf ]; then
if [ -e /etc/systemd/system/snap.ubuntu-desktop-bootstrap.subiquity-server.service ]; then
:
else
rm -f /etc/systemd/system/ventoy-curtin-wa.service
rm -f /etc/systemd/system/snap.ubuntu-desktop-bootstrap.subiquity-server.service.d/10-ventoy-curtin-wa.conf
rm -rf /etc/ventoy_curtin
fi
fi
mkdir -p /run/udev/rules.d
mv /etc/udev/rules.d/*ventoy* /run/udev/rules.d/
rm -f $0
@@ -4,8 +4,12 @@ After=systemd-udevd.service
[Service]
Type=oneshot
ExecStart=-udevadm trigger --subsystem-match=block --sysname-match=yyy
ExecStart=-udevadm trigger --subsystem-match=block --sysname-match=DMXXX
ExecStart=udevadm settle
ExecStart=-udevadm settle
ExecStartPost=-/bin/sh /etc/ventoy-udev-cleanup.sh
RemainAfterExit=no
[Install]
WantedBy=sysinit.target
@@ -0,0 +1,57 @@
#!/bin/sh
set +e
rm -f /etc/boot.d/S01-ventoy-udev-trigger*
rm -f /etc/runlevels/sysinit/ventoy-udev-trigger*
rm -f /etc/runlevels/default/ventoy-udev-trigger*
rm -f /etc/dinit.d/boot.d/ventoy-udev-trigger*
rm -f /etc/dinit.d/default/ventoy-udev-trigger*
rm -f /etc/runit/runsvdir/sysinit/ventoy-udev-trigger*
rm -f /etc/runit/runsvdir/default/ventoy-udev-trigger*
rm -f /etc/s6/adminsv/sysinit/contents.d/ventoy-udev-trigger*
rm -f /etc/init.d/ventoy-udev-trigger*
rm -f /etc/dinit.d/ventoy-udev-trigger*
rm -rf /etc/runit/sv/ventoy-udev-trigger*
rm -rf /etc/s6/sv/ventoy-udev-trigger*
if ls -l /etc/udev/rules.d/ | grep -q ventoy; then
mkdir -p /run/udev/rules.d
mv /etc/udev/rules.d/*ventoy* /run/udev/rules.d/
fi
if [ -f /etc/rc.local ]; then
if grep -q ventoy /etc/rc.local; then
sed -i '/ventoy/d' /etc/rc.local
fi
fi
if [ -f /etc/s6/rc.local ]; then
if grep -q ventoy /etc/s6/rc.local; then
sed -i '/ventoy/d' /etc/s6/rc.local
fi
fi
setsid sh -c '
count=0
while [ $count -lt 60 ]; do
if pidof udisksd >/dev/null 2>&1; then
udevadm trigger --action=change --subsystem-match=block --sysname-match=yyy
udevadm trigger --action=change --subsystem-match=block --sysname-match=DMXXX
sleep 1
break
fi
count=$((count + 1))
sleep 2
done
' &
rm -f $0
@@ -0,0 +1,4 @@
type = scripted
command = /etc/ventoy-udev-cleanup.sh
after = local.target
@@ -0,0 +1,16 @@
#!/usr/bin/openrc-run
depend() {
after local
}
start() {
ebegin ""
/bin/sh /etc/ventoy-udev-cleanup.sh
eend 0
}
stop() {
ebegin ""
eend 0
}
@@ -0,0 +1,2 @@
#!/bin/sh
exec /etc/ventoy-udev-cleanup.sh
@@ -1,9 +0,0 @@
#!/bin/sh
for i in 0 1 2 3 4 5 6 7 8 9; do
[ -b /dev/DMXXX ] && break
sleep 1
done
udevadm trigger --subsystem-match=block --sysname-match=DMXXX
udevadm settle
+64 -20
View File
@@ -1026,7 +1026,7 @@ ventoy_new_root_dir() {
done
echo ''
}
ventoy_copy_udev_auto_rules() {
if ! [ -e $VTOY_PATH/ventoy_raw_dm_done ]; then
vtlog "ventoy_raw_dm_done not exist, no need for udev auto rule"
@@ -1042,40 +1042,84 @@ ventoy_copy_udev_auto_rules() {
vtlog "new root is $vroot"
fi
cp -a /ventoy/hook/default/90-ventoy-auto.rules /ventoy/udevtmp.rules
# Resolve device mapping info
vtDM=$($VTOY_PATH/tool/dmsetup info VentoyPart | $GREP Major | $SED "s/.*[^0-9]\([0-9][0-9]*\)$/\1/")
$SED "s/DMXXX/dm-${vtDM}/g" -i /ventoy/udevtmp.rules
vtRAWDISKNAME=$($HEAD -n1 $VTOY_PATH/ventoy_raw_table | $AWK '{print $4}')
$SED "s/yyy/${vtRAWDISKNAME#/dev/}/g" -i /ventoy/udevtmp.rules
vtRAWDISK_SYSNAME="${vtRAWDISKNAME#/dev/}"
vtlog "VentoyPart is /dev/dm-${vtDM} map to $vtRAWDISKNAME NewRoot is $vroot"
mkdir -p $vroot/etc/udev/rules.d
ventoy_copy_file /ventoy/udevtmp.rules $vroot/etc/udev/rules.d/90-ventoy-auto.rules 0644
cp -a /ventoy/hook/default/90-ventoy-auto.rules /ventoy/udevtmp.rules
$SED "s/DMXXX/dm-${vtDM}/g" -i /ventoy/udevtmp.rules
$SED "s/yyy/${vtRAWDISK_SYSNAME}/g" -i /ventoy/udevtmp.rules
ventoy_copy_file /ventoy/udevtmp.rules $vroot/etc/udev/rules.d/9999-ventoy-auto.rules 0644
ventoy_copy_file /ventoy/udevtmp.rules $vroot/etc/udev/rules.d/11-ventoy-auto.rules 0644
if $GREP -q 'init=/lib/sysvinit' /proc/cmdline; then
SYSVINIT=1
else
SYSVINIT=0
fi
if $GREP -q 'init=.*sysvinit' /proc/cmdline; then
vtlog "copy sysvinit udev trigger script"
cp -a /ventoy/hook/default/ventoy-udev-trigger.rc /ventoy/udevtmp.rc
$SED "s/DMXXX/dm-${vtDM}/g" -i /ventoy/udevtmp.rc
if [ -d $vroot/etc/systemd/system -a "$SYSVINIT" = "0" ]; then
vtlog "systemd inject hook"
ventoy_copy_file /ventoy/hook/default/systemd/ventoy-udev-cleanup.sh $vroot/etc/ventoy-udev-cleanup.sh 0755
else
vtlog "sysvinit inject hook"
ventoy_copy_file /ventoy/hook/default/sysvinit/ventoy-udev-cleanup.sh $vroot/etc/ventoy-udev-cleanup.sh 0755
fi
$SED "s/DMXXX/dm-${vtDM}/g" -i $vroot/etc/ventoy-udev-cleanup.sh
$SED "s/yyy/${vtRAWDISK_SYSNAME}/g" -i $vroot/etc/ventoy-udev-cleanup.sh
mkdir -p $vroot/etc/boot.d
ventoy_copy_file /ventoy/udevtmp.rc $vroot/etc/boot.d/S01-ventoy-udev-trigger 0755
if [ "$SYSVINIT" = "1" ]; then
vtlog "copy sysvinit udev trigger script"
echo "/etc/ventoy-udev-cleanup.sh" >> $vroot/etc/rc.local
elif [ -d $vroot/etc/systemd/system ]; then
vtlog "copy systemd udev trigger service"
mkdir -p $vroot/etc/systemd/system/sysinit.target.wants
cp -a /ventoy/hook/default/ventoy-udev-trigger.service /ventoy/udevtmp.service
$SED "s/DMXXX/dm-${vtDM}/g" -i /ventoy/udevtmp.service
ventoy_copy_file /ventoy/udevtmp.service $vroot/etc/systemd/system/ventoy-udev-trigger.service 0644
if [ -d $vroot/etc/systemd/system/sysinit.target.wants ]; then
vtlog "$vroot/etc/systemd/system/sysinit.target.wants exist ok"
else
mkdir -p $vroot/etc/systemd/system/sysinit.target.wants
fi
mkdir -p $vroot/etc/systemd/system-preset
cp -a /ventoy/hook/default/systemd/ventoy-udev-trigger.service /ventoy/udevtmp.service
$SED "s/DMXXX/dm-${vtDM}/g" -i /ventoy/udevtmp.service
$SED "s/yyy/${vtRAWDISK_SYSNAME}/g" -i /ventoy/udevtmp.service
ventoy_copy_file /ventoy/udevtmp.service $vroot/etc/systemd/system/ventoy-udev-trigger.service 0644
if [ -d $vroot/etc/systemd/system-preset ]; then
vtlog "$vroot/etc/systemd/system-preset exist ok"
else
mkdir -p $vroot/etc/systemd/system-preset
fi
echo 'enable ventoy-udev-trigger.service' > $vroot/etc/systemd/system-preset/80-ventoy-udev.preset
$BUSYBOX_PATH/ln -sf /etc/systemd/system/ventoy-udev-trigger.service $vroot/etc/systemd/system/sysinit.target.wants/ventoy-udev-trigger.service
elif [ -d $vroot/etc/init.d ] && [ -d $vroot/etc/runlevels/default ]; then
vtlog "copy sysvinit openrc udev trigger script"
ventoy_copy_file /ventoy/hook/default/sysvinit/ventoy-udev-trigger-cleanup.openrc $vroot/etc/init.d/ventoy-udev-trigger-cleanup 0755
$BUSYBOX_PATH/ln -sf /etc/init.d/ventoy-udev-trigger-cleanup $vroot/etc/runlevels/default/ventoy-udev-trigger-cleanup
elif [ -d $vroot/etc/dinit.d/boot.d ]; then
vtlog "copy sysvinit dinit udev trigger script"
ventoy_copy_file /ventoy/hook/default/sysvinit/ventoy-udev-trigger-cleanup.dinit $vroot/etc/dinit.d/ventoy-udev-trigger-cleanup 0644
$BUSYBOX_PATH/ln -sf /etc/dinit.d/ventoy-udev-trigger-cleanup $vroot/etc/dinit.d/boot.d/ventoy-udev-trigger-cleanup
elif [ -d $vroot/etc/runit/sv ] && [ -d $vroot/etc/runit/runsvdir/default ]; then
vtlog "copy sysvinit runit udev trigger script"
mkdir -p $vroot/etc/runit/sv/ventoy-udev-trigger-cleanup
ventoy_copy_file /ventoy/hook/default/sysvinit/ventoy-udev-trigger-cleanup.runit $vroot/etc/runit/sv/ventoy-udev-trigger-cleanup/run 0755
$BUSYBOX_PATH/ln -sf /etc/runit/sv/ventoy-udev-trigger-cleanup $vroot/etc/runit/runsvdir/default/ventoy-udev-trigger-cleanup
elif [ -d $vroot/etc/s6/sv ]; then
vtlog "copy sysvinit s6 udev trigger script"
echo "/etc/ventoy-udev-cleanup.sh" >> $vroot/etc/s6/rc.local
elif [ -f $vroot/etc/rc.local ]; then
vtlog "copy sysvinit udev trigger script"
echo "/etc/ventoy-udev-cleanup.sh" >> $vroot/etc/rc.local
else
vtlog "sysinit wants dir not exist"
vtlog "unsupported init system"
fi
}