Skip to content

Commit

Permalink
Always use systemd-nspawn unless --chroot
Browse files Browse the repository at this point in the history
  • Loading branch information
gitbls committed Oct 11, 2023
1 parent 8b80d63 commit f61242f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 105 deletions.
16 changes: 8 additions & 8 deletions sdm
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ Command Switches for --customize and --burn or as noted
--b1script script Like --b0script, but done in nspawn (can do both b0script and b1script)
--bootscripts Run the scripts /usr/local/sdm/1piboot/0*-*.sh during first boot
--bupdate plugin Check and update sdm plugins on burned output; See documentation for details
--chroot Use chroot rather than systemd-nspawn (for use on certain host OSes)
--cscript script Custom Phase Configuration script
--csrc dir Source directory passed for Custom Phase scripts
--custom[1-4] str Can be used in Custom cscripts
Expand All @@ -310,7 +311,6 @@ Command Switches for --customize and --burn or as noted
--poptions str Set Phase 1 auto-run options
--norestart Do not restart after first boot (use on --burn command)
--os osname Specify OS in the Image (D:raspios, ubuntu)
--rclocal string Add string as a command in /etc/rc.local (can be used multiple times)
--reboot n n seconds after first boot has completed restart the system
--redact Redact passwords in /etc/sdm/cparams and /etc/sdm/history
--redo-customize If image already customized, redo without prompting
Expand All @@ -327,7 +327,7 @@ Command Switches for --customize and --burn or as noted
# Initialize and Parse the command
#
#
version="V9.3"
version="V9.4"
sdmcommandline="$0 $@" #Used in error messages
apps="" #List of apps to install in sdm-base-installs
aptcache="" #IP address of apt-cacher-ng server
Expand Down Expand Up @@ -356,6 +356,7 @@ dimgdevname="" #Set to "IMG", "Device", or "Directory" once determi
domain="" #--domain name
ecolors="blue:gray:red" #fg:bg:cursor
expandroot=0 #1=Expand root after burning it to a device
fchroot=0 #1=Use chroot instead of systemd-nspawn
fcustomize=0 #True if customizing an image (either --customize or lack of burn,mount,explore)
fbatch=0 #1=nspawn "batch" mode (non-interactive). Do Phase1 and exit
fdirtree=0 #1=source is a directory tree, not an IMG or device
Expand Down Expand Up @@ -388,15 +389,14 @@ poptions="" #Phase options
pvers=0 #Print version number
raspiosver="" #RasPiOS Debian version # (10:Buster, 11:Bullseye, 12:Bookworm)
fredact=0 #1=redact passwords in /etc/sdm/{cparams,history}
rclocal="" #Command(s) to add to /etc/rc.local
reboot=0 #1=Reboot from First Boot
rebootwait=20 #Number of seconds to wait after systemd thinks system is fully booted to reboot
regensshkeys=0 #1=Regenerate SSH keys at end of FirstBoot
runonly="" #Keyword specified for --runonly
drebootwait=$rebootwait # Used to see if rebootwait should be modified on burn
redocustomize=0 #1=Don't prompt if image already customized
sdmdir="/usr/local/sdm" #Where to put sdm when customizing. --sdmdir to change
sdmflist="sdm sdm-phase0 sdm-phase1 sdm-cparse sdm-readparams sdm-cmdsubs sdm-firstboot sdm-apt sdm-apt-cacher sdm-hotspot sdm-cportal sdm-logmsg sdm-gburn"
sdmflist="sdm sdm-phase0 sdm-phase1 sdm-cparse sdm-readparams sdm-cmdsubs sdm-firstboot sdm-apt sdm-apt-cacher sdm-cportal sdm-logmsg sdm-gburn"
swapsize=0 #Set swap size to nMB (overrides --disable swap)
vaptmaintops="|update|upgrade|autoremove|" #Options for --apt
virtmode="nspawn" #"nspawn" or "chroot"
Expand All @@ -423,11 +423,11 @@ source $src/sdm-cparse # Get function defs
#
cmdline="$0 $*"
longopts="help,1piboot:,apps:,aptcache:,apt-dist-upgrade,aptmaint:,\
autologin,b0script:,b1script:,batch,bootscripts,burn:,burnfile:,cscript:,csrc:,\
autologin,b0script:,b1script:,batch,bootscripts,burn:,burnfile:,chroot,cscript:,csrc:,\
customize,datefmt:,ddsw:,debug:,directory,domain:,ecolors:,explore,expand-root,extend,\
groups:,host:,hostname:,info,loadlocal:,locale:,logwidth:,\
mcolors:,mount,norestart,noreboot,nowait-timesync,nspawnsw:,oklive,os:,plugin-debug,\
plugin:,plugins:,poptions:,ppart,rclocal:,reboot:,redact,redo-customize,\
plugin:,plugins:,poptions:,ppart,reboot:,redact,redo-customize,\
regen-ssh-host-keys,restart,runonly:,sdmdir:,showapt,shrink,swap:,\
bupdate:,xmb:,custom1:,custom2:,custom3:,custom4:,version"

Expand All @@ -452,6 +452,7 @@ do
burndev=$2 ; shift 2 ;;
--burnfile) burnfile=1 ;
burnfilefile=$2 ; shift 2 ;;
--chroot) fchroot=1 ; shift 1 ;;
--cscript) cscript=$2 ; shift 2 ;;
--csrc) csrc=$2 ; shift 2 ;;
--customize) fcustomize=1 ; shift 1 ;;
Expand Down Expand Up @@ -481,7 +482,6 @@ do
--poptions) poptions=$(appendvalue "$poptions" "${2,,}" ",") ; shift 2 ;;
--ppart) fppart=1 ; shift 1 ;;
--redact) fredact=1 ; shift 1 ;;
--rclocal) rclocal=$(appendvalue "$rclocal" "$2" "~") ; shift 2 ;;
--reboot) rebootwait=$2 ;
reboot=1 ; shift 2 ;;
--redo-customize) redocustomize=1 ; shift 1 ;;
Expand Down Expand Up @@ -809,7 +809,7 @@ fi
# Not --extend (only). Other steps need IMG mounted
declare -x SDMPT=$(makemtpt)
domount "$dimg"
initvirt write_premsg
[ $fexplore -eq 0 ] && initvirt write_premsg || initvirt echo
#
# Handle commands --aptmaint, --customize, and --explore
#
Expand Down
23 changes: 4 additions & 19 deletions sdm-cmdsubs
Original file line number Diff line number Diff line change
Expand Up @@ -192,16 +192,15 @@ function sdm_burndevfile() {
# Burn messages are written, all further messages written with logtoboth, which will write them to the terminal
# and to $SDMPT/etc/sdm/history in the mounted image or image file
#
xsettings="autologin b0script b1script bootscripts domain expandroot exports hname fredact myuser noreboot"
xsettings="$xsettings nowaittimesync plugindebug plugins rclocal reboot rebootwait regensshkeys"
xsettings="autologin b0script b1script bootscripts domain expandroot exports fchroot hname fredact myuser noreboot"
xsettings="$xsettings nowaittimesync plugindebug plugins reboot rebootwait regensshkeys"
# Save settings made with the --burn command
# Define variable b<varname> for each <varname>
for e in $xsettings
do
eval b$e=\${!e}
done
source $SDMPT/etc/sdm/sdm-readparams # Read settings from the SD card
initvirt logtoboth
# Update settings with settings from cmd line as appropriate
[ "$bb0script" != "$b0script" ] && b0script=$bb0script
[ "$bb1script" != "$b1script" ] && b1script=$bb1script
Expand All @@ -215,9 +214,11 @@ function sdm_burndevfile() {
plugins="$bplugins"
[ $bautologin -eq 1 ] && autologin=1
expandroot=$((bexpandroot|expandroot))
fchroot=$((bfchroot|fchroot))
regensshkeys=$((bregensshkeys|regensshkeys))
nowaittimesync=$((bnowaittimesync|nowaittimesync))
plugindebug=$((bplugindebug|plugindebug))
initvirt logtoboth # Must be done after fchroot is set
# --bupdate must specified on the burn command line
[ "$bupdate" != "" ] && checkupdsdm update "$bupdate" || checkupdsdm check "$bupdate"
if [ $expandroot -eq 1 -a $regensshkeys -eq 1 ]
Expand Down Expand Up @@ -259,22 +260,6 @@ function sdm_burndevfile() {
fi
fi
fi
if [ "$brclocal" != "" ]
then
logtoboth "> Add commands to /etc/rc.local per --rclocal"
grep -v 'exit 0' $SDMPT/etc/rc.local > $SDMPT/tmp/rc.local
IFS="~" read -a citems <<< "$brclocal"
for c in "${citems[@]}"
do
logtoboth " $c"
echo "$c" >> $SDMPT/tmp/rc.local
done
echo "exit 0" >> $SDMPT/tmp/rc.local
mv $SDMPT/etc/rc.local $SDMPT/etc/rc.local.sdm.burn
mv $SDMPT/tmp/rc.local $SDMPT/etc
chmod 755 $SDMPT/etc/rc.local
[ "$rclocal" == "" ] && rclocal="$brclocal" || rclocal="${rclocal}|$brclocal"
fi
hostname="$hname" # So it gets written to updated params on SD card
[ $bootscripts -eq 1 ] && state="enabled" || state="disabled"
logtoboth "> First System Boot Custom Boot Scripts $state"
Expand Down
99 changes: 39 additions & 60 deletions sdm-cparse
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,10 @@ function getgbstr() {
# Returns the string "(nn.nnGB, mm.mmGiB)"

local nbytes=$1
local gib=$((1024*1024*1024)) gb=$((1000*1000*1000)) # Not super-efficient but meh
ngbytes=$(python3 -c "print(\"{:.1f}\".format(round($nbytes/$gb, 2)))")
ngibytes=$(python3 -c "print(\"{:.1f}\".format(round($nbytes/$gib, 2)))")
local gb=1000000000 gib=1073741824 gb2=500000000 gi2=536870912
local ngbytes ngibytes
ngbytes=$(printf %.1f "$(( ((10 * $nbytes)+$gb2) / $gb ))e-1")
ngibytes=$(printf %.1f "$(( ((10 * $nbytes)+$gi2) / $gib))e-1")
echo "(${ngbytes}GB, ${ngibytes}GiB)"
return
}
Expand Down Expand Up @@ -234,6 +235,19 @@ function cklogfreespace() {
fi
}

function do_raspiconfig() {
#
# $1=command
# $2=value
local cmd=$1 value=$2
if type -P raspi-config > /dev/null
then
SUDO_USER=${myuser:-nobody} raspi-config $cmd "$value" nonint # prefer to not block outputs! > /dev/null 2>&1
else
logtoboth "% Unable to find raspi-config for function '$cmd' with value '$value'"
fi
}

function configitemlog() {
# $1: Message
# $2: function to call
Expand Down Expand Up @@ -360,9 +374,9 @@ function writeconfig() {
echo "#Arguments passed from sdm into the IMG on $(date +'%Y-%m-%d %H:%M:%S')" > $paramfile
for e in version thishost aptcache aptdistupgrade autologin fbatch b0script b1script bootscripts \
cscript csrc datefmt debugs dimg dimgdev dimgdevname domain ecolors \
expandroot exports fdirtree hname hostname loadlocal logwidth \
expandroot exports fchroot fdirtree hname hostname loadlocal logwidth \
dgroups myuser nowaittimesync os pi1bootconf plugindebug poptions \
raspiosver rclocal reboot fredact regensshkeys noreboot rebootwait \
raspiosver reboot fredact regensshkeys noreboot rebootwait \
redocustomize sdmdir sdmflist showapt src swapsize \
timezone virtmode vqemu wificountry custom1 custom2 custom3 custom4 plugins
do
Expand Down Expand Up @@ -738,19 +752,31 @@ function initvirt() {
qf="/usr/bin/qemu-aarch64-static"
bfs=":qemu-aarch64:M:0:\x7f\x45\x4c\x46\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xb7\x00:\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff:$qf:"
fi
elif [ $fchroot -eq 1 ]
then
$msgrtn "% sdm using chroot per --chroot"
virtmode="chroot"
[[ "$wtf" =~ "32-bit" ]] && vqemu="arm" || vqemu="aarch64"
fi
else
IFS="," read wamielf wamiarch rest <<< "$wami"
$msgrtn "% Configure chroot for sdm on your '${wamiarch##\ }' computer"
$msgrtn " * sdm will run more slowly due to qemu emulation"
virtmode="chroot"
[[ "$wtf" =~ "32-bit" ]] && vqemu="arm" || vqemu="aarch64"
if [ $fchroot -eq 0 ]
then
$msgrtn "% sdm will use systemd-nspawn on this '${wamiarch##\ }' host"
$msgrtn " Retry the command with --chroot if this fails"
else
$msgrtn "% sdm will use chroot per --chroot on this '${wamiarch##\ }' host"
virtmode="chroot"
[[ "$wtf" =~ "32-bit" ]] && vqemu="arm" || vqemu="aarch64"
fi
$msgrtn " sdm will run more slowly due to qemu emulation"
fi
}

function chroot_cleanup() {
if [ "$SDMPT" != "" ]
then
grep -q 'sdm-generated' $SDMPT/etc/resolv.conf && rm -f $SDMPT/etc/resolv.conf
[ "$qemu" != "" ] && rm -f $SDMPT/usr/bin/$qemu
umount $SDMPT/{dev/pts,dev,proc,sys}
else
Expand Down Expand Up @@ -818,7 +844,7 @@ function sdm_spawn() {
[ -f $fn ] && ubf=$fn && break
done
[ ! $? ] && errexit "? Cannot find update-binfmts; try 'sudo apt install binfmt-support'"
logtoboth "% Run 'update-binfmts --enable' to enable chroot"
#logtoboth "% Run 'update-binfmts --enable' to enable chroot"
$ubf --enable
if [ "$bfs" != "" -a ! -f /proc/sys/fs/binfmt_misc/qemu-${vqemu} ]
then
Expand All @@ -832,6 +858,9 @@ function sdm_spawn() {
[ "$qemu" != "" ] && cp $qemu $SDMPT/usr/bin
declare -x OLDSDMPT="$SDMPT"
declare -x OLDSDMNSPAWN="$SDMNSPAWN"
#
# If there is no /etc/resolv.conf, fabricate one
[ -f $SDMPT/etc/resolv.conf ] || printf "# sdm-generated \nnameserver 1.1.1.1\n" > $SDMPT/etc/resolv.conf
trap "ctrlchroot" SIGINT
for fs in dev dev/pts proc sys ; do mount --bind /$fs $SDMPT/$fs ; done
declare -x SDMPT=""
Expand Down Expand Up @@ -865,43 +894,6 @@ EOF
fi
}

function getconfbool() {
#
# Grab one entry (name in $1) from cparams safely
#
local mnt="$SDMPT" pname="$1"
paramfile="/etc/sdm/cparams"
if [ -f $paramfile ]
then
pval=$((grep ${pname}: $paramfile) | (IFS=":" read kn kv ; echo $kv))
else
pval=0
fi
# del enclosing quotes on value read from cparams
pval="${pval#\"}"
pval="${pval%\"}"
cmval=${!pname}
rval=$((pval|cmval))
echo "$rval"
}

function getconfstr() {
#
# Grab one entry string (name in $1) from cparams safely
#
local mnt="$SDMPT" pname="$1"
paramfile="/etc/sdm/cparams"
if [ -f $paramfile ]
then
pval=$((grep ${pname}: $paramfile) | (IFS=":" read kn kv ; echo $kv))
else
pval=0
fi
cmval=${!pname}
[ "$cmval" != "" ] && rval=$cmval || rval=$pval
echo "$rval"
}

function updateconfigtxt() {
local c="$1" value="$2" pfx="$3" inline="$4" newsection="$2" dnm drest dsep sname sval
if [ "$c" == "section" ]
Expand Down Expand Up @@ -971,19 +963,6 @@ function updateconfigtxt() {
}
function do_raspiconfig() {
#
# $1=command
# $2=value
local cmd=$1 value=$2
if type -P raspi-config > /dev/null
then
SUDO_USER=${myuser:-nobody} raspi-config $cmd "$value" nonint # prefer to not block outputs! > /dev/null 2>&1
else
logtoboth "% Unable to find raspi-config for function '$cmd' with value '$value'"
fi
}
function copyifnewer() {
# Copy $1 to $2 if $1 is newer
# Target must have same basename as src (no rename as part of copy)
Expand Down
18 changes: 0 additions & 18 deletions sdm-phase1
Original file line number Diff line number Diff line change
Expand Up @@ -121,24 +121,6 @@ EOF
systemctl enable sdm-firstboot > /dev/null 2>&1

[ "$hname" != "" ] && updatehostname $hname # If hostname specified on customize then set it
#
# Add requested command(s) to /etc/rc.local
#
if [ "$rclocal" != "" ]
then
logtoboth "> Add commands to /etc/rc.local"
grep -v 'exit 0' /etc/rc.local > /tmp/rc.local
IFS="~" read -a citems <<< "$rclocal"
for c in "${citems[@]}"
do
logtoboth " $c"
echo "$c" >> /tmp/rc.local
done
echo "exit 0" >> /tmp/rc.local
mv /etc/rc.local /etc/rc.local.sdm
mv /tmp/rc.local /etc
chmod 755 /etc/rc.local
fi

#
# Set up apt-cacher-ng client if requested
Expand Down

0 comments on commit f61242f

Please sign in to comment.