This repository has been archived by the owner on Oct 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cleanup
executable file
·150 lines (137 loc) · 4.1 KB
/
cleanup
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/bin/sh
# vim: set syn=sh ft=sh et sw=2 sts=2 ts=2 tw=0:
#
# This file is part of SaLT.
# Copyright: Cyrille Pontvieux <[email protected]>
# Licence: GPLv3+
# Version: 0.2.2
# Clean up script launched when coming back to the initrd of SaLT before halting or rebooting.
PATH=:/usr/sbin:/usr/bin:/sbin:/bin
export PATH
if [ -f /lib/libSaLT ]; then
. /lib/libSaLT
elif [ -f /mnt/salt/lib/libSaLT ]; then
. /mnt/salt/lib/libSaLT
else
echo "libSaLT not found, cannot proceed further" >&2
exit 1
fi
echodebug "cleanup: called with: $0 $@"
orig=/hooks/$(basename $0)
echo "$@"|grep -q -- -w
if [ $? -eq 0 ]; then
echodebug "cleanup: '-w' detected, will run: $orig $@"
"$orig" "$@"
exit $?
fi
RL=$(runlevel|cut -d' ' -f2)
[ "$RL" = "unknown" ] && RL=$RUNLEVEL
[ -z "$RL" ] && RL=$runlevel
[ -z "$RL" ] && RL=0
if [ "$RL" -ne 0 ] && [ "$RL" -ne 6 ]; then
echodebug "cleanup: not in runlevel 0 or 6, will exec: $orig $@"
exec "$orig" "$@"
exit 0
fi
cd /
if [ -z "$SALT_CLEANUP_IS_RUNNING" ]; then
if [ ! -e /mnt/salt ]; then
fumble "/mnt/salt not found. This is really weird.\nAre you running from a Live environment?"
else
SALT_CLEANUP_IS_RUNNING=1
export SALT_CLEANUP_IS_RUNNING
[ -z "$SALT_DEBUG" ] && echo -en '\e[H\e[2J'
echoinfo "*** Live system cleanup ***\n"
grep -q -w '/mnt/salt' /etc/mtab || (mount -o remount,rw / && echo 'rootfs /mnt/salt rootfs ro 0 0' >> /etc/mtab && mount -o remount,ro /)
echodebug "Rexecuting in chroot: $0 $@"
change_root_to_initrd /cleanup "$0" "$@"
fumble 'Rexecuting cleanup: this line must never be executed!'
fi
fi
CMD="$@"
export CMD
if [ ! -e /tmp/distro_infos ]; then
fumble "/tmp/distro_infos not found. This is really weird.\nAre you running from a Live environment?"
fi
# ensure / and /mnt/union are writable.
touch /.testrw 2>/dev/null
[ -e /.testrw ] && rm /.testrw || mount -o remount,rw /
debugshell
debuglog 'Remounting /mnt/union read/write'
mount -o remount,rw /mnt/union
infolog 'Untweaking target distro'
uninstall_hooks /mnt/union
debugshell
infolog 'Synchronizing memory data on disk'
sync
infolog 'Killing remaining process'
kill_all_before $$
debugshell
debuglog 'Umount sub-mounts in /mnt/union'
umount_submounts_in /mnt/union
sleep 3
debugshell
infolog 'Unloading modules...'
for m in $(ls -dr1 /mnt/modules/*); do
infolog " - Unloading $m"
unload_module "$m"
debugshell
done
debuglog 'Destroying union filesystem...'
destroy_union
rmdir /mnt/modules 2>/dev/null
rm /mnt/rw
if [ -e /mnt/ram ]; then
if [ -e /sync-persistence ]; then
dev=$(cut -d: -f1 /sync-persistence)
relpath=$(cut -d: -f2 /sync-persistence)
rm /sync-persistence # ensure the file is deleted for future live sessions
dir=$(mount_device $dev)
debugshell
if [ -e "$dir"/"$relpath" ]; then
infolog 'Syncing RAM -> persistence file...'
mkdir /mnt/sync
mount -o loop "$dir"/"$relpath" /mnt/sync
if [ $? -eq 0 ]; then
debugshell
SIZE_RAM=$(du -sm /mnt/ram|cut -f1)
SIZE_SYNC=$(sh -c 'echo $2' -- $(df -m /mnt/sync|tail -n1))
if [ $SIZE_RAM -lt $SIZE_SYNC ]; then
infolog "Syncing ${SIZE_RAM}M, please be patient and don't shutdown your computer..."
sync
cp -a /mnt/ram/* /mnt/ram/.??* /mnt/sync
sync
infolog " $dir/$relpath synchronized"
else
errorlog " Cannot sync to $dir/$relpath: not enough size (${SIZE_RAM}M >= ${SIZE_SYNC}M)"
fi
debugshell
umount -d /mnt/sync
else
errorlog " Cannot mount $dir/$relpath: bad persistence file"
fi
rmdir /mnt/sync
fi
umount_device $dev
fi
umount /mnt/ram || umount -l /mnt/ram
rmdir /mnt/ram
elif [ -e /mnt/save ]; then
umount /mnt/save || umount -l /mnt/save
rmdir /mnt/save
fi
[ -e /tmp/modules ] && rm -rf /tmp/modules 2>/dev/null
debugshell
umount_device $(cut -d: -f2- /tmp/distro_infos) >/dev/null
debugshell
eject_cd
debugshell
debuglog 'Umount what remains'
sync
umount -a -r -d -l 2>/dev/null
debugshell
echoinfo "Stopping/Rebooting: $CMD"
debugshell
sleep 1
exec "$CMD" -f
fumble 'End of cleanup: this line must never be executed!'