When I boot the virtual machine with the NAS system software, I usually interrupt the boot to start in single-user mode. This gives me an opportunity to:

$ echo terra > /proc/sys/kernel/hostname

so that the virtual machine comes online with a distinct network identity.

For a while, after then returning to the normal boot, once the machine was online I would quickly ssh in to terminate a handful of services (tor, plex, sonarr, &c.) as quickly as possible to prevent them from generally … interferring with the world.

After a while, I got tired of this, so I looked into creating a specific runlevel to separate multi-user from "running as a NAS".

In the traditional runlevel system, runlevels 1 (single-user), 3 (multi-user), 5 (graphical) and 6 (reboot) are spoken for, while 2 and 4 are "free" to be used for other purposes.

systemd takes a different (imho better) tack, with a bunch of semantically-defined "targets", to which other units can be associated.

In my case, I created a nas.target, which you can think of as runlevel 4 if it makes you feel better.

# /etc/systemd/systemd/nas.target
# 2018-04-06, jsled: create a new target!
[Unit]
Description=Network Attached Storage
# Documentation=nfs://earth/data/system/ChangeLog
Requires=multi-user.target
Conflicts=rescue.service rescue.target
After=multi-user.target rescue.service rescue.target
AllowIsolate=yes

$ rm /etc/systemd/system/default.target
$ ln -s /etc/systemd/system/nas.target /etc/systemd/system/default.target

At this point, the system will boot to nas.target by default.

After this, one needs to modify /lib/systemd/system/{whatever}.service to replace

[Install]
WantedBy=nas.target

And then re-systemctl enable whatever.service to get it linked appropriately.

(Unfortunately, it seems that adding /etc/systemd/system/whatever.service.d/override.conf will add the target to the WantedBy set, not replace the value. :/)

At this point, booting the virtual machine becomes:

# boot to single user: add "single" or "systemd.unit=rescue.target" to the boot line.
# wait for single-user...
$# echo terra > /proc/sys/kernel/hostname
$# systemctl isolate multi-user

Then the boot will continue to multi-user, without further loading all the services associated with nas.target.

And, yes, I should figure out how to configure grub / grub-mkconfig to add a boot-menu entry for single-user rather than editing the damn boot every time.

(And, it seems like there should be a kernel param for hostname, but I can't seem to find it.)

In my case, the following services are associated with nas.target:

root@earth [~]# ls -l /etc/systemd/system/nas.target.wants/
total 0
lrwxrwxrwx 1 root root 40 Apr  6 18:25 avahi-daemon.service -> /lib/systemd/system/avahi-daemon.service
lrwxrwxrwx 1 root root 36 Apr  6 18:25 collectd.service -> /lib/systemd/system/collectd.service
lrwxrwxrwx 1 root root 35 Apr  6 18:25 grafana.service -> /lib/systemd/system/grafana.service
lrwxrwxrwx 1 root root 38 May  6 09:02 lm_sensors.service -> /lib/systemd/system/lm_sensors.service
lrwxrwxrwx 1 root root 38 Apr  6 18:25 nfs-server.service -> /lib/systemd/system/nfs-server.service
lrwxrwxrwx 1 root root 41 Apr 12 18:01 node_exporter.service -> /etc/systemd/system/node_exporter.service
lrwxrwxrwx 1 root root 38 Apr 15 09:16 nut-driver.service -> /lib/systemd/system/nut-driver.service
lrwxrwxrwx 1 root root 38 Apr 15 09:14 nut-server.service -> /lib/systemd/system/nut-server.service
lrwxrwxrwx 1 root root 45 Apr  6 18:25 plex-media-server.service -> /lib/systemd/system/plex-media-server.service
lrwxrwxrwx 1 root root 38 Apr  6 18:25 prometheus.service -> /lib/systemd/system/prometheus.service
lrwxrwxrwx 1 root root 36 Apr  6 18:26 sabnzbd@default.service -> /lib/systemd/system/sabnzbd@.service
lrwxrwxrwx 1 root root 34 May  6 09:02 smartd.service -> /lib/systemd/system/smartd.service
lrwxrwxrwx 1 root root 32 Apr  6 18:25 smbd.service -> /lib/systemd/system/smbd.service
lrwxrwxrwx 1 root root 34 Apr  6 18:27 sonarr.service -> /lib/systemd/system/sonarr.service
lrwxrwxrwx 1 root root 31 Apr  6 18:25 tor.service -> /lib/systemd/system/tor.service
lrwxrwxrwx 1 root root 47 Apr  6 18:25 transmission-daemon.service -> /lib/systemd/system/transmission-daemon.service

Basically, everything related to physical hardware (lm_sensors, nut), or proactive networking (nfs, samba, avahi, plex, sonarr/sabnzbd, tor).


Published

Category

Posts

Tags

Contact