-------------------------------------------------
@raja
This week, I am trying to fix the issue of APorteus reboot/shutdown process.
Next week, maybe I will have some result.
Thanks.
-------------------------------------------------
@M. Eerie
Thank you very much for your helpful advice.
Now I am studying the articles that were introduced by you.
Before reading these articles, I have analyzed the issue.
[Problem]
At systemd shutdown/reboot, "unmoun"s are done in fault.
[Cause]
The file systems that will be unmount are used by systemd running.
(umount: can't unmount ....: Resource busy)
[how to fix this issue]
====< In the case of original Porteus >====
There are two stages.
stage 1: /etc/rc.d/rc.6
the process is running in the ordinary space.
In this stage, basic modules 000-, 001-, 002-, 003- are not "unmount"ed.
At the end of stage 1, the control is transfered to the stage 2.
stage 2: /mnt/live/cleanup
At first step, running space is changed to the boot initrm space.
The boot space is not constructed with 000-,001-,002-,003- modules.
These modules can be "unmount"ed in stage 2.
Therefore they are "unmount"ed in stage 2 without fault.
====< In the case of APorteus >====
Now, APorteus has nothing to care this issue.
If the same approach as Porteus will be employed,
the following corresponding was tried.
stage 1: /usr/lib/systemd/systemd-shutdown
Note: an executable program under /usr/lib/systemd/system-shutdown/
will be executed by /usr/lib/systemd/systemd-shutdown.
But its PID is not 1.
stage 2: /mnt/live/cleanup
ISO@/porteus/base/001-z.core.xzm@/usr/lib/systemd/systemd-shutdown
Code: Select all
#!/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
BLD=$(tput bold) # Bold
RED=${BLD}$(tput setaf 1) # Red
RST=$(tput sgr0) # Reset
redmsg()
{
echo -e $RED "$1" $RST
}
nfsd_stop()
{
killall rpc.mountd 2> /dev/null
killall nfsd 2> /dev/null
sleep 1
killall -9 nfsd 2> /dev/null # make sure :)
killall rpc.rquotad 2> /dev/null
exportfs -au 2> /dev/null
}
#=== save data size check ===#
if [ -a /mnt/live/memory/images/changes ]
then
SDspace=$(df -BM --output=size,used,avail,pcent /mnt/live/memory/images/changes | tail -n1)
read size used avail percent <<< $SDspace
if [ ${percent%\%} -gt 85 ]
then
echo
redmsg "Warning! save.dat file $percent full. Used: $used Free: $avail"
echo
else
echo
echo "save.dat file $percent full. Used: $used Free: $avail"
fi
fi
#=== Save random-seed to a file ===#
#dd if=/dev/urandom of=/etc/random-seed count=1 bs=$(expr `cat /proc/sys/kernel/random/poolsize` / 8) 2>/dev/null
#chmod 600 /etc/random-seed
#=== Write what's going to happen in wtmp ===#
if [ "$1" = "reboot" ]
then
command="reboot"
else
command="poweroff"
fi
$command -w
#=== Deactivate all porteus modules activated from /union (additional modules)===#
deactivate `grep ^devpts -A500 /proc/mounts | grep '.xzm ' | cut -d" " -f2` >/dev/null 2>&1
#=== Close magic-folders encrypted containers ===#
for x in `ls /dev/mapper/magic* 2>/dev/null`;
do
umount `grep -w /dev/mapper/$x /proc/mounts | cut -d " " -f2`
cryptsetup luksClose $x
done
#=== Detach loop devices used in /union ===#
losetup -a | grep /loop3 -A500 | grep -v '.xzm)' | cut -d: -f1 | xargs -n1 losetup -d 2>/dev/null
sync; echo
#=== Stop acpid/dbus/udev ===#
udevadm control --exit 2>/dev/null
killall acpid dbus-daemon udevd 2>/dev/null
#=== Stop nfsd ===#
#pidof nfsd >/dev/null && nfsd_stop
#=== Turn off swap ===#
swapoff -a
#=== Some filesystems needs to be unmounted later ===#
echo "Unmounting local file systems"
umount -at no,aufs,devtmpfs,proc,sysfs 2>/dev/null
#=== Sync what's left in buffers ===#
sync
#=== Deactivate LVM volume groups ===#
#if [ -e /mnt/live/tmp/lvm ];
#then
# echo "Deactivating LVM volume groups"
# vgchange -an --ignorelockingfailure
#fi
#=== Deactivate RAID arrays ===#
if [ -e /mnt/live/tmp/raid ];
then
echo "Deactivating RAID arrays"
mdadm -S /dev/md/* 2>/dev/null
fi
#=== Run the cleanup script ===#
cd /mnt/live/
exec ./cleanup $command <dev/console >dev/console 2>&1
#=== None return ===#
ISO@/boot/syslinux/initrd.xz@/usr/share/systemd/cleanup
Code: Select all
#!/bin/sh
#
# Script will be called instead of final reboot/shutdown
# to safely switch back to initrd root and unmount everything possible.
# Mainly needed if you use changes= boot parameter in order to correctly
# unmount the changes device/file.
#
export PATH=.:/mnt/live/bin:/usr/sbin:/usr/bin:/sbin:/bin
#=== change root ===#
# This script needs to be re-executed to free up any shell
# from the union which may be used (eg. /union/bin/bash)
if [ ! "$RE_EXEC_CLEANUP" ];
then
export RE_EXEC_CLEANUP=1
/mnt/live/bin/pivot_root . union
exec /mnt/live/bin/chroot . /cleanup "$@" <dev/console >dev/console 2>&1
echo "Something was wrong because we should never be here!"
fi
#========================================================#
save_changes_EXIT()
{
# Save 'changes=EXIT:' session:
if [ ! -e /tmp/changes-exit ]
then
return 1
fi
echo -e "Your session will be saved in [1;33m3[0m seconds.\nPress space/enter to start doing it now or any other key to skip.";
x=3
while [ $x -gt 0 ];
do
if read -s -t1 -n1 ans
then
break
else
sleep 1;
fi
let x=x-1;
done
if [ "$ans" = "" ];
then
DEST=`cat /tmp/changes-exit`;
NAME=`basename $DEST`;
MNAME=/memory/images/changes;
FOLDERS=`grep '^/' /union/etc/changes-exit.conf | sed s/^.//g`
echo "saving changes to $NAME - do not power off the PC"
cd /memory/changes;
rm -rf var/lock/subsys/* var/run/laptop-mode-tools/* `grep '^!' /union/etc/changes-exit.conf | sed s/^..//g | tr "\n" " "`
for x in `find $FOLDERS -name ".wh.*" 2>/dev/null | sed s/.wh.//g | tr ' ' '@' `;
do
x=`echo $x | tr '@' ' ' `
if test -e $MNAME/"$x"
then
rm -rf $MNAME/"$x";
fi
done
for x in `find $MNAME -name ".wh.*" 2>/dev/null | tr ' ' '@' `;
do
x=`echo $x | tr '@' ' ' `
wh=`echo $x | sed -e s^$MNAME^^g -e s/.wh.//g `;
if test -e "$wh"
then
rm "$x";
fi
done
cp -af --parents $FOLDERS $MNAME 2>/dev/null
fi
return 0
}
#========================================================#
ejectCD()
{
# Determine if we booted from CD and copy 'eject' utility:
CD=`grep /dev/sr /proc/mounts | cut -d" " -f1 | uniq`
if [ ! -b "$CD" ]
then
return
fi
if [ -n "`egrep -o " noeject( |\$)" /proc/cmdline`" ]
then
return
fi
# Eject cdrom device:
echo "ejecting $CD...";
eject $CD 2>/dev/null;
x=6
while [ $x -gt 0 ];
do
echo -en "CD tray will be closed in [1;33m$x[0m seconds - hit enter to do it now.\r"
if read -s -t1
then
break
else
sleep 1
fi
let x=x-1
done
eject -t $CD 2>/dev/null
}
#========================================================#
OLDunmount_all()
{
#=== unmounting union ===#
echo "unmounting union"
sync
umount -nl `grep /union/ /proc/mounts | cut -d" " -f2 | tr "\n" " "` 2>/dev/null
umount -l /union 2>/dev/null
if [ $? -ne 0 ];
then
x=10;
free=no
while [ $x -gt 0 -a $free = no ];
do
usleep 200000;
sync;
let x=x-1
if umount -l /union 2>/dev/null
then
echo "union unmounted successfully";
free=yes;
fi
done
if [ $? -ne 0 ];
then
kill -9 `echo $PID` >/dev/null 2>&1;
umount -l /union 2>/dev/null;
fi
if [ $? -ne 0 ];
then
echo "remounting union as read-only"
echo -e "please use '[1;33mfsck[0m' cheatcode during next boot\nto make sure that all your filesystems are consistent..."
sleep 3;
umount -l -r /union 2>/dev/null
fi
fi
#=== unmounting everything else ===#
echo "unmounting everything else"
umount -a 2>/dev/null
if [ $? -ne 0 ];
then
# Close encrypted container:
if [ -b /dev/mapper/crypt ];
then
cryptsetup luksClose crypt
losetup -d /dev/loop2
fi
umount -arl 2>/dev/null
fi
}
#========================================================#
unmount_all()
{
for i in `ls -d /memory/images/*`;
do
umount -l $i
done
}
#========================================================#
debug()
{
#if ! egrep -qo " Pdebug( |\$)" /proc/cmdline
#then
# return
#fi
if [ $# -gt 0 ]
then
echo -e "\n=====<$1>====="
else
echo -e "\n=========="
fi
echo ": Debugging started. Here is the shell for you."
echo ": Type your desired commands, hit Ctrl+D to continue booting"
echo ": or press Ctrl+Alt+Del to reboot."
sh;
echo -e "\n\n";
}
#=== Send TERM signal to processes which can be killed ===#
SELF=$$
for i in `ps |sed 's/^ *//g'|grep -v '^PID'|cut -d' ' -f1`;
do
if [ $i = $SELF ]
then
continue
fi
kill -9 $i
done
#PID=`ps | sed -n '/PID/,/{cleanup}/p' | egrep -v '\[.*\]|PID|cleanup|ntfs-3g' | sed -r "s/^ *([0-9]+).*/\\1/" | tr "\n" " "`
#kill -15 `echo $PID` >/dev/null 2>&1
#debug "STOP processe ($1) PID=$$"
#=== Will need cryptsetup for closing encrypted container ===#
[ -b /dev/mapper/crypt ] && cp /memory/images/000-kernel.xzm/usr/bin/cryptsetup /bin
#=== Determine if we booted from CD and copy 'eject' utility ===#
#CD=`grep /dev/sr /proc/mounts | cut -d" " -f1 | uniq`
#[ -b "$CD" ] && cp -f /union/usr/bin/eject /bin
#=== Remove doubled ntfs mount entries from mtab ===#
sed -i "/ fuseblk /d" /etc/mtab
#=== Save 'changes=EXIT:' session ===#
if ! save_changes_EXIT
then
if grep -q ^memory /var/log/livedbg;
then
kill -9 `echo $PID` >/dev/null 2>&1
else
killall -9 klogd NetworkManager >/dev/null 2>&1
fi
fi
#=== unmounting all ===#
unmount_all
#debug umount
#=== Eject cdrom device ===#
ejectCD
#=== Launch debugging shell if requested ===#
#debug "START $1"
#=== $1 = action, eg. poweroff or reboot ===#
$1 -f
#debug "Something was wrong because we should never be here!"
return
Thanks.