Skip to content

Programming plugins and custom phase scripts

Benn edited this page Jan 28, 2023 · 1 revision

Programming plugins and custom phase scripts

This section provides context and details about the sdm environment for writing plugins and custom phase scripts.

sdm operation principles

sdm provides plugins and custom phase scripts with 3 operating environments:

  • Phase 0, or mounted disk environment: This environment runs in the context of the host (like pretty much every other script that you run). The disk or IMG that sdm is operating on is mounted in the host OS. Since the directory name can change, sdm provides the environment variable $SDMPT that always points to the top of the directory tree.

    Thus, a correct reference to a file in the IMG in the mounted disk environment is $SDMPT/etc/rc.local If you don't include $SDMPT, you will access or modify files on your host operating system.

    System management commands such as systemctl, journalctl, and pretty much every other command affect the host OS. This is probably not what you want to do! You can, however, access the network, host storage, mounted network shares, etc.

    Keep work in this environment minimized to copying needed files from the host OS into the IMG somewhere. You can copy files directly to target locations in the IMG, prefaced by $SDMPT, or you can stage files into $SDMPT/etc/sdm/local-assets if desired.

  • Phase 1, or Container environment: This environment runs in a container that is isolated from the host OS. The container file system is,er, contained, and code running in the container cannot access the host OS file system. $SDMPT is not needed so is unset. This means that you can safely reference it in this environment, since $SDMPT/etc/rc.local and /etc/rc.local are equivalent. But it is not required, like it is for the Phase 0 mounted disk environment.

  • post-install: The post-install phase environment is the same as Phase 1. The difference between Phase 1 and the post-install phase is the timing of when they are run.

    Phase 1 starts when the container is entered. At the conclusion of Phase 1, sdm performs an apt update. After that completes sdm runs plugin and custom phase script Phase 1 code.

    After Phase 1 for plugins and custom phase scripts completes, sdm runs the apps/xapps installs, and then enters the post-install phase.

    Work done in the post-install phase is typically focused on configuring the just-installed packages. After sdm completes its post-install work, the plugin and custom phase script post-installs are called. Note that anything that can be done in the Container environment can be done in post-install. This includes installing more packages, if you determine you need do it there for some reason.

How does all this work when burning?

Think about the environments described above, rather than phases. sdm creates an environment that is the same as that of the Phase 0 environment, and then calls selected plugins to run Phase 0.

Similarly, it creates a Container environment and runs the Phase 1 and post-install code for plugins.

Most plugin code doesn't need to know that it's running as part of a burn operation. For code that does, the env var $SDMNSPAWN can be inspected. It's value will be "Burn0" when a plugin is running Phase 0 during a burn operation. It will be set to "Burn1" in Phase 1 and post-install code. (In a standard customize operation, SDMNSPAWN will be "Phase0" during Phase 0, and "Phase1" during Phase 1 and post-install phases.)

b0script/b1script use the same technique.

Building a plugin or custom phase script

Plugins

Start with the file /usr/local/sdm/plugins/sdm-plugin-template. Copy it somewhere, give it a new filename, and work on it.

Custom phase scripts

Start with the file /usr/local/sdm/sdm-customphase, and similarly, copy it somewhere with a new filename, and work on it.

Both

If you run into problems, logtoboth is your friend. It will write the string to the console and $SDMPT/etc/sdm/history.

Remember that sdm copies itself into the IMG during customization phase 0. If you change any of the sdm files, it's easiest on your brain if you start over and re-customize. More than once I've been mystified why my change didn't work, because I tried to shortcut some steps.

What variables are controlled by command line switches?

sdm keeps all the context in /etc/sdm/cparams, which is read by each module (via $SDMPT/etc/sdm/sdm-readparams), so that all variables are defined.

  • --1piboot conf-file — $pi1bootconf
  • --apps applist — $apps
  • --xapps applist — $xapps
  • --apip IPADDR — $apip
  • --apssid ssidname — $apssid
  • --apt-dist-upgrade — $aptdistupgrade
  • --aptcache IPADDR — $aptcache
  • --aptmaint options — $aptmaint
  • --autologin — $autologin
  • --batch — $batch
  • --b0script script — $b0script
  • --b1script script — $b1script
  • --bootadd key:value,key:value,... — $bootadd
  • --bootconfig key:value,key:value,... — $bootconfig
  • --bootset key:value,key:value,.. — $bootset
  • --bootscripts — $bootscripts
  • --cron-d file — $crond
  • --cron-daily file — $crondaily
  • --cron-hourly file — $cronhourly
  • --cron-monthly file — $cronmonthly
  • --cron-weekly file — $cronweekly
  • --cron-systemd — $cronsystemd
  • --cscript script — $cscript
  • --csrc dir — $csrc
  • --custom[1-4] str — $custom[1-4]
  • --datefmt str — $datefmt
  • --ddsw str — $ddsw
  • --debug apt — $debugs
  • --disable arg,arg — $disables
  • --directory — $fdirtree=1
  • --dhcpcd file — $dhcpcd
  • --dhcpcdwait — $dhcpcdwait
  • --domain name — $domain
  • --dtoverlay — $dtoverlay
  • --dtparam — $dtparam
  • --ecolors fg:bg:cur — $ecolors
  • --eeprom str — $eeprom
  • --expand-root — $expandroot=1
  • --exports file — $exports
  • --extend — $fextend=1
  • --fstab file — $fstab
  • --groups list — $groups
  • --hdmi-force-hotplug — $hdmiforcehotplug=1
  • --hdmi-ignore-edid — $hdmiignoreedid=1
  • --hdmigroup n — $hdmigroup=n
  • --hdmimode n — $hdmimode=n
  • --host hostname — $hname
  • --hotspot config — $hotspot
  • --journal type — $journal
  • --keymap keymapname — $keymap
  • --L10n — $loadl10n=1
  • --loadlocal args — $loadlocal
  • --locale localename — $locale
  • --logwidth N — $logwidth
  • --lxde-config files — $lxdeconfig
  • --mcolors fg:bg:cur — $mcolors
  • --modprobe file — $modprobe
  • --motd file — $motd
  • --mouse left — $fmouse=1
  • --nouser — $nouser=1
  • --nowait-timesync — $nowaittimesync=1
  • --nspawnsw str — $nspawnsw
  • --password-pi pwd — $passwordpi
  • --password-user pwd — $passworduser
  • --password-root pwd — $passwordroot
  • --password-same y|n — $samepwd
  • --plugin pname:"args" — $plugins
  • --plugin-debug — $plugindebug
  • --poptions str — $poptions
  • --redact — $redact=1
  • --norestart — $noreboot=1
  • --rclocal string — $rclocal
  • --reboot n — $rebootwait=n, $reboot=1
  • --redact — $redact=1
  • --redo-customize — $redocustomize=1
  • --regen-ssh-host-keys — $regensshkeys=1
  • --restart — $rebootwait=20, $reboot=1
  • --rootpwd — $rootpwd
  • --sdmdir /path/to/sdm — $sdmdir
  • --showapt — $showapt=1
  • --showpwd — $showpwd=1
  • --ssh none|socket|service — $ssh
  • --swap n — $swapsize
  • --svcdisable svc1,svc2,... — $svcdisable
  • --svc-disable svc1,svc2,... — $svcdisable
  • --svcenable svc1,svc2,... — $svcenable
  • --svc-enable svc1,svc2,... — $svcenable
  • --sysctl file — $sysctl
  • --systemd-config item:file,... — $systemdconfig
  • --timezone tzname — $timezone
  • --udev file — $udev
  • --uid uid — $myuid
  • --user username — $myuser
  • --vncbase n — $vncbase
  • --wifi-country country — $wificountry
  • --wpa wpaconf — $wpa
  • --nowpa — $fnowpa=1
  • --xmb n — $imgext