5

Is it possible to use a variable in the what= field of a systemd mount configuration file instead of an absolute path string?

For Example:

[Unit]
Before=local-fs.target

[Mount]
What=/dev/disk/by-partlabel/${part_variable}
Where=/mnt/optional/config/

I am trying to find a way to use a custom kernel command-line bootparam, and mount a partition provided by that bootparam.

3 Answers 3

6

For the record:

Is if possible to use a variable in the What= field of a systemd mount configuration file instead of an absolute path string?

Yes, it is possible, e.g. (just tested with systemd v.238):

mnt-data.mount content:

[Unit]
Description=Mount System Backups Directory

[Mount]
What=${SOMEPATH}
Where=/mnt/data
Type=ext4
Options=defaults

if you run

systemctl set-environment SOMEPATH=/dev/disk/by-label/DATA

and then

systemctl start mnt-date.mount

it will mount /dev/disk/by-label/DATA to /mnt/data

or, via a env file:

mnt-data.mount content:

[Unit]
Description=Mount System Backups Directory

[Mount]
EnvironmentFile=/path/to/env_file
What=${SOMEPATH}
Where=/mnt/data
Type=ext4
Options=defaults

/path/to/env_file content:

SOMEPATH=/dev/disk/by-uuid/1b5a38ca-9f0d-4f1a-8fc1-7c418e79bf07

This may not help you to

find a way to use a custom kernel command-line bootparam, and mount a partition provided by that bootparam.

but it's definitely possible to use What=${SOMEVAR}.

1

According to man systemd.mount, no variable substitution is supported there.

I believe it's still possible to write a systemd service which runs early in the boot process, which writes out a a dynamically generated .mount file.

0

No.

What you describe is exactly what generators are for, see man systemd.generators. Compare man systemd-fstab-generator which generates the mount units from /etc/fstab, it also reads e.g. root= from the kernel command line. That said, if you only support a kernel command line option, and you don't have a config file, you may be just as happy writing an early-boot (DefaultDependencies=no) service unit for this.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .