[systemd-devel] Using path name in instantiated units

Lennart Poettering lennart at poettering.net
Wed Aug 20 05:55:36 PDT 2014


On Wed, 20.08.14 06:21, Andrei Borzenkov (arvidjaar at gmail.com) wrote:

> 
> В Tue, 19 Aug 2014 22:54:04 +0200
> Lennart Poettering <lennart at poettering.net> пишет:
> 
> > On Tue, 19.08.14 19:34, Florian Lindner (mailinglists at xgm.de) wrote:
> > 
> > > Hello,
> > > I have a systemd unit that should do a commit on a specific path. Since 
> > > there are more then one path / repos, I want to use an instantiated unit for 
> > > that (the stuff with @ in the unit name):
> > > 
> > > 
> > > ~/.config/systemd/user % cat git-commit at .service
> > > [Unit]
> > > Description=Auto commit for instance.
> > > 
> > > [Service]
> > > Type = oneshot
> > > Nice = 10
> > > 
> > > # git returns 1 if there is nothing to commit
> > > SuccessExitStatus=1
> > > 
> > > WorkingDirectory = %I  # also tried %i
> > > ExecStart = /usr/bin/sh -c 'echo %i > wd'
> > > ExecStart = /usr/bin/sh -c 'echo %i >> wd'
> > > ExecStart = /usr/bin/sh -c 'echo `pwd` >> wd'
> > > ExecStart = /usr/bin/git add --all .
> > > ExecStart = /usr/bin/git commit -a -m "Automatic commit."
> > 
> > Use "%f" for unescaping paths.
> 
> Actually this will give incorrect result. If you call it
> foo@/path/to/file, instance gets escaped into -path-to-file;
> prepending "/" to unescaped instance results in //path/to/file. Not
> fatal, but some programs may misinterpret initial "//".
> 
> %I works just fine here.

Well, we slightly modified the rules how paths are escaped in
systemd. For example, the mount unit for /tmp is called "tmp.mount", not
"-tmp.mount". 

Of course, if you define your own template, it's completely up to you to
how you escape the instance string, however, it's of course nicer to
just follow the usual schemes.

You should never reference a unit as "foo@/bar.service" as that is an
invalid unit name. To be nice however, on the systemctl command line we
will escape the minimum necessary to the string useful as a unit
name. But that's not reversible actually!

If you build a unit name, use systemd-escape, that's the way to do it
cleanly:

    $ systemd-escape -p --template=foo at .service /home/lennart/projects/systemd
    foo at home-lennart-projects-systemd.service

The "-p" here tells the tool to follow the slightly altered escaping
rules for paths here. 

Lennart

-- 
Lennart Poettering, Red Hat


More information about the systemd-devel mailing list