-
Notifications
You must be signed in to change notification settings - Fork 54
/
sdm-phase1
executable file
·278 lines (256 loc) · 7.63 KB
/
sdm-phase1
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/bin/bash
# This script runs in the nspawn image
#
function upgkeepconf() {
#
# Write /etc/apt/apt.conf.d/02sdm-myconf
# Forcing apt to not query about conf file changes
# $1 = "--yes" or ""
# $2 = $showapt or 0 to not display output
# $3 = 0 [upgrade] or 1 [dist-upgrade]
#
cat > /etc/apt/apt.conf.d/02sdm-myconf <<EOF
Dpkg::Options {
"--force-confdef";
"--force-confold";
}
EOF
[ "$3" == "0" ] && upg="upgrade" || upg="dist-upgrade"
doaptrpterror "$1 $upg" "$2"
rm -f /etc/apt/apt.conf.d/02sdm-myconf
}
function p1exit() {
#
# Clean up when Phase 1 (or any of the container commands) exits
#
unadjust_initramfs_all
exit
}
function doctrlcp1() {
echo "% Caught CTRL/C. Exiting Phase 1..."
unadjust_initramfs_all #Be clean
exit 1 #Can't use poweroff since no job control in either nspawn or chroot
}
#
# Read configuration information from sdm
#
source /etc/sdm/sdm-readparams
#
# Handle commands which are run in the context of the container. They are here as a matter of
# programming convenience ;)
#
cmd="$(stripquotes "$1")"
cmdoptions="$(stripquotes "$2")"
#logtoboth "DD-1 cmd:|$cmd| cmdoptions:|$cmdoptions| 2:|$2| 3:|$3|"
# case stmt falls through if not processed within
trap "doctrlcp1" SIGINT
trap p1exit EXIT
adjust_initramfs_all
case "$cmd" in
apt)
logtoboth "* Start apt maintenance"
logfreespace "at start of 'apt $cmdoptions'"
[[ "$cmdoptions" =~ "update" ]] && logtoboth "> Perform 'apt update'" && doaptrpterror "--yes update" 0 || exit
[[ "$cmdoptions" =~ "upgrade" ]] && logtoboth "> Perform 'apt upgrade'" && upgkeepconf --yes 0 $aptdistupgrade || exit
[[ "$cmdoptions" =~ "autoremove" ]] && logtoboth "> Perform 'apt autoremove'" && doaptrpterror "--yes autoremove" 0 || exit
logfreespace "at end of 'apt $cmdoptions'"
logtoboth "* apt maintenance Completed"
exit 0
;;
b1script) # Burn time run script
fn="/etc/sdm/assets/$(basename $cmdoptions)"
if [ -f $fn ]
then
logtoboth "> Run script '$cmdoptions' [$fn] in nspawn"
/etc/sdm/assets/$(basename $cmdoptions)
else
logtoboth "? Internal error: Script '$fn' not found"
fi
exit
;;
burn-enable-lightdm-delay) # Delay lightdm enable until sdm-firstboot
runoneplugin graphics runfunction "runfunction=delaylightdmenable|runargs=burning" || exit
exit
;;
run-all-plugins) # Run Plugin phase 1 or post-install
phase=$cmdoptions
runplugins "$plugins" $phase || exit
exit
;;
run-plugin-list) # Run list of plugins phase 1 or post-install
phase=$cmdoptions
theseplugins="$(stripquotes "$3")"
runplugins "$theseplugins" $phase || exit
exit
;;
run-one-plugin) # Run Plugin phase 1 or post-install
# $cmdoptions: plugin name and args $3: Phase
p="$cmdoptions"
phase="$(stripquotes "$3")"
runonepluginx "$p" $phase || exit
exit
;;
run-command) # Run a single command in phase 1 or post-install
runcmd="$(stripquotes "$cmdoptions") $3 $4 $5 $6 $7 $8"
$runcmd || exit
exit
;;
run-command-quietly) # Run a single command in phase 1 or post-install
runcmd="$cmdoptions $3 $4 $5 $6 $7 $8"
$runcmd > /dev/null 2>&1 || exit
exit
;;
esac
logtoboth "* Start Phase 1 image customization"
logfreespace "at start of Phase 1 image customization"
#
# Set up sdm-firstboot service. This service processes some settings that can only be done
# on the first boot of the system.
#
logtoboth "> Configure and enable sdm FirstBoot service (sdm-firstboot)"
rm -f /etc/systemd/system/sdm-firstboot.service
cat > /etc/systemd/system/sdm-firstboot.service <<EOF
[Unit]
Description=sdm FirstBoot System Configuration
After=network.target
#Before=rc-local.service
[Service]
Type=forking
ExecStart=$sdmdir/sdm-firstboot
TimeoutSec=0
RemainAfterExit=yes
GuessMainPID=no
[Install]
WantedBy=multi-user.target
EOF
systemctl enable sdm-firstboot > /dev/null 2>&1
[ "$hname" != "" ] && updatehostname $hname # If hostname specified on customize then set it
#
# Set up apt-cacher-ng client if requested
#
if [ "$aptcache" != "" ]
then
logtoboth "> Set image as apt-cacher-ng client using server $aptcache"
$sdmdir/sdm-apt-cacher client $aptcache
fi
if [ $swapsize -gt 0 ]
then
logtoboth "> Set Swap Size to ${swapsize}MB"
sed -i "s/CONF_SWAPSIZE=.*/CONF_SWAPSIZE=$swapsize/" /etc/dphys-swapfile
fi
if ! [[ "$poptions" =~ "noupdate" ]]
then
logtoboth "> Start 'apt update'"
logfreespace "at start of 'apt update'"
doaptrpterror "update" $showapt
logfreespace "at end of 'apt update'"
else
logtoboth "> Skip 'apt update' per --apt-options noupdate"
fi
if [ "$cscript" != "" ]
then
csfn="$sdmdir/$(basename $cscript)"
logtoboth "> Run Custom Phase Script '$csfn' Phase 1"
$csfn 1 || exit
else
csfn=""
fi
#
# Run requested plugins Phase 1
#
runplugins "$plugins" 1 || exit
#
# Post-install Configuration
#
logtoboth "* Phase 1 post-app installation/configuration"
if ! [[ "$poptions" =~ "noupgrade" ]]
then
[ $aptdistupgrade -eq 0 ] && upg="upgrade" || upg="dist-upgrade"
logtoboth "> Start 'apt $upg'"
logfreespace "at start of 'apt $upg'"
upgkeepconf "-y" "$showapt" $aptdistupgrade
logfreespace "at end of 'apt $upg'"
else
logtoboth "> Skip 'apt upgrade' per --apt-options noupgrade"
fi
if [[ ! "$poptions" =~ "noautoremove" ]]
then
logtoboth "> Start 'apt autoremove'"
logfreespace "at start of 'apt autoremove'"
doaptrpterror "--yes autoremove" $showapt
logfreespace "at end of 'apt autoremove'"
else
logtoboth "> Skip 'apt autoremove' per --apt-options noautoremove"
fi
logfreespace "at end of Phase 1 image customization"
logtoboth "* Phase 1 Completed"
if [ "$csfn" != "" ]
then
logtoboth "> Run Custom Phase Script '$csfn' post-install"
$csfn post-install
fi
#
# Run requested plugins post-install phase
#
runplugins "$plugins" post-install || exit
#
# Complete graphics post-install if graphics plugin not selected
# This is a hack. Proper fix needs more thought
#
if ! ispluginselected graphics "$plugins"
then
logtoboth "> Run graphics post-install configuration"
source $sdmdir/plugins/graphics
gargs=$(getpluginargs graphics "$plugins")
[[ "$gargs" =~ "nodmconsole" ]] && nodmconsole=1 || nodmconsole=0
gfxcfgpostinstall yes
fi
if ! ispluginselected sshd "$plugins"
then
logtoboth "> Plugin phase1: Configure SSH"
dosshsetup service phase1
fi
resetpluginlist
logfreespace "at end of image customization"
printnotes
#
# Check for apt issues in the apt log
#
if [ -f /etc/sdm/apt.log ]
then
grep -q " E:" /etc/sdm/apt.log && logtobothex "? apt reported errors; review /etc/sdm/apt.log"
if grep -q " W: An error occurred" /etc/sdm/apt.log || grep -q " W: Failed to fetch" /etc/sdm/apt.log \
|| grep -q "signatures were invalid" /etc/sdm/apt.log || grep -q " W: Some index files failed to download" /etc/sdm/apt.log
then
[ "$aptcache" == "" ] && logtobothex "? apt reported warnings; review /etc/sdm/apt.log" \
|| logtobothex "? apt reported warnings; review /etc/sdm/apt.log and check your apt-cacher-ng server for problems"
fi
fi
#
# Report run time
#
customizeend="$(getcdate)"
logtoboth "> Customize elapsed time: $(datediff $customizestart $customizeend)"
if [ $fbatch -eq 0 ]
then
if [ -t 0 ]
then
logtoboth "* Enter Shell Command Prompt"
logtoboth " 'exit' to exit back to host system"
if [ "$ecolors" != "0" ]
then
IFS=":" read efg ebg ecursor <<< $ecolors
stermcolors "$efg" "$ebg" "$ecursor" x1
fi
bash -i
logtoboth "* Customization complete"
[ "$ecolors" != "0" ] && resetcolors x1
else
logtoboth "* Run bash on non-terminal standard input"
bash
fi
else
logtoboth "* Customization complete"
logtoboth "* Batch Mode exit"
fi
exit 0