You are not logged in.

#1 2022-04-08 11:28:15

eom.dev
Member
Registered: 2022-02-11
Posts: 25

[SOLVED] ordering systemd units for qemu machines

I am running a network of several virtual machines.  One of these machines is the router for the rest of the network and has a startup script that configures the bridges required by the others.  Similar to what is described in the wiki, I have a systemd unit file that can be enabled for each machine (it uses a generic unit file that can be extrapolated for each machine, i.e. my-network@.service).  Since the router configures the network, I want that machine to start first, but they are not starting in the correct order.  Is there a way to order the startup of these services?  Copied below is the unit file I am using:

[Unit]
Description=%i
After=srv.mount

[Service]
Environment="haltcmd=kill -INT $MAINPID"
ExecStart=/usr/bin/bash /srv/%i/launch.sh
ExecStop=/usr/bin/bash -c ${haltcmd}

[Install]
WantedBy=multi-user.target

Last edited by eom.dev (2022-04-08 12:58:19)

Offline

#2 2022-04-08 11:33:15

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 16,187

Re: [SOLVED] ordering systemd units for qemu machines

Make a dedicated service file (e.g. router.service) explicitly for the machine that needs to go up first and add an After=router and Wants=router to this template.

Last edited by V1del (2022-04-08 11:41:25)

Online

#3 2022-04-08 11:42:26

eom.dev
Member
Registered: 2022-02-11
Posts: 25

Re: [SOLVED] ordering systemd units for qemu machines

V1del wrote:

Make a dedicated unit file explicitly for the machine that needs to go up first and add an After=router and Wants=router to this template.

That is unfortunately what I was thinking I would have to do.  I was hoping there might be a way to specify

if [[ %i == $ROUTER ]]
then
After=router
Wants=router

but it doesn't look like that is available

Offline

#4 2022-04-08 11:58:45

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 16,187

Re: [SOLVED] ordering systemd units for qemu machines

You can technically specify templated specifications for After and Wants but not sure how that would behave since logically speaking it would have to wait for itself. But maybe stuff is smart enough to work anyway, maybe just try?

i.e.

After=base@router
Wants=base@router

Online

#5 2022-04-08 12:00:40

Raynman
Member
Registered: 2011-10-22
Posts: 1,417

Re: [SOLVED] ordering systemd units for qemu machines

The example looks weird: router depends on itself?

But you can do something like that with an override for a specific instance of the template unit, as in /etc/systemd/system/my-network@some-instance.d/after-router.conf. All shared settings remain in the my-network@.service; maybe you want that one to have the After=router and then override the router to not depend on itself?

Last edited by Raynman (2022-04-08 12:03:11)

Offline

#6 2022-04-08 12:58:02

eom.dev
Member
Registered: 2022-02-11
Posts: 25

Re: [SOLVED] ordering systemd units for qemu machines

Raynman wrote:

The example looks weird: router depends on itself?

But you can do something like that with an override for a specific instance of the template unit, as in /etc/systemd/system/my-network@some-instance.d/after-router.conf. All shared settings remain in the my-network@.service; maybe you want that one to have the After=router and then override the router to not depend on itself?

That is an elegant solution, thank you.  In practice, this was really the cleanest way to generate the original "separate unit files" solution.  The individual unit files systemd creates from the base are just soft links to that base, so editing one would affect all of the files.  I added the dependency to the base, but then converted the router unit into a regular file and removed those lines.  Since the link no longer exists, this is technically the same as just creating a separate unit file, but it was a particularly effective workflow for getting there.

Offline

Board footer

Powered by FluxBB