Skip to content

systemctl enable with an absolute path as argument cannot be used to create template instances #661

Closed

Description

@vishvananda

It is not possible to properly enable a service file that has been symlinked in via systemctl enable.

Take the following example:

cat > test.unit <<EOF
[Unit]
Description=Test

[Service]
ExecStart=/bin/sleep infinity

[Install]
WantedBy=multi-user.target
EOF
sudo systemctl link $PWD/test.unit
sudo systemctl enable test.unit # Failed to execute operation: No such file or directory

Note that you can enable it by specifying the full path

sudo systemctl enable $PWD/test.unit # symlinks from multi-user.target.wants to $PWD/test.unit

This links directly to the local file. This strategy doesn't work for templated files:

cat > [email protected] <<EOF
[Unit]
Description=Test %i

[Service]
ExecStart=/bin/sleep infinity

[Install]
WantedBy=multi-user.target
EOF
sudo systemctl link $PWD/test.unit
sudo systemctl enable $PWD/[email protected] # Failed to execute operation: No such file or directory

It seems that it should be able to enable in both cases once it has been linked in without specifying the full path. The enable code could either follow the symlink and link directly to the linked file, or it could just symlink to the symlink and systemd could follow them during startup.

Activity

  1. poettering commented on Jul 22, 2015

    @poettering
    Member

    The idea was that "link" only makes a unit available in the search paths and "enable" also hooks it in. i.e. "enable" is kind of a superset of "link", as it does the same work if you pass a path, plus some more...

    But yeah the template issue you point out is a real issue that this logic cannot cover. Yuck.

  2. changed the title [-]systemctl link followed by systemctl enable fails for templated services[/-] [+]systemctl enable with an absolute path as argument cannot be used to create template instances[/+] on Jul 22, 2015
  3. emamirazavi commented on Jul 22, 2015

    @emamirazavi

    Why are you putting that unit file there? This is not standard. Add your
    favorite unit files to systemd directory located usually in
    /etc/systemd/system path(redhat default path). I think it's not a good idea
    to manage symlinked path. After all I think systemd can support symlink if
    you use it in the systemd directory planned to store unit files.
    On Jul 22, 2015 10:45 PM, "Vish Ishaya" [email protected] wrote:

    It is not possible to properly enable a service file that has been
    symlinked in via systemctl enable.

    Take the following example file

    cat > test.unit <<EOF[Unit]Description=Test[Service]ExecStart=/bin/sleep infinity[Install]WantedBy=multi-user.targetEOF
    sudo systemctl link $PWD/test.unit
    sudo systemctl enable test.unit # Failed to execute operation: No such file or directory

    Note that you can enable it by specifying the full path

    sudo systemctl enable $PWD/test.unit #symlinks directly to from multi-user.target.wants to $PWD

    This links directly to the local file and this strategy doesn't work for
    templated files:

    cat > [email protected] <<EOF
    [Unit]
    Description=Test %i

    [Service]
    ExecStart=/bin/sleep infinity

    [Install]
    WantedBy=multi-user.target
    EOF
    sudo systemctl link $PWD/test.unit
    sudo systemctl enable $PWD/[email protected] # Failed to execute operation: No such file or directory

    It seems that it should be able to enable in both cases once it has been
    linked in without specifying the full path. The enable code could either
    follow the symlink and link directly to the linked file, or it could just
    symlink to the symlink and systemd could follow them during startup.

    —
    Reply to this email directly or view it on GitHub
    #661.

  4. poettering commented on Jul 22, 2015

    @poettering
    Member
  5. emamirazavi commented on Jul 22, 2015

    @emamirazavi

    Therefore it can be labeled as a feature to implement. Can not it?

    On Wed, Jul 22, 2015 at 11:34 PM, Lennart Poettering <
    [email protected]> wrote:

    On Wed, 22.07.15 11:49, S.Mohammad Emami Razavi ([email protected])
    wrote:

    Why are you putting that unit file there? This is not standard. Add your
    favorite unit files to systemd directory located usually in
    /etc/systemd/system path(redhat default path). I think it's not a good
    idea
    to manage symlinked path. After all I think systemd can support symlink
    if
    you use it in the systemd directory planned to store unit files.

    In general I think we really should ensure that we support in systemd
    a scheme nicely where you install a 3rd party unit file somewhere in
    /opt or so, and then make it available to systemd with just "systemctl
    link" or "systemctl enable", so that 3rd party stuff doesn't litter
    /usr (which is private property of the OS vendor), and doesn't add
    anything but symlinks to the 3rd party unit files to /etc.

    Or in other words: I think what @vishvananda is trying to do is
    something we explicitly should support to cover nicely for 3rd party
    software, that is neither written by the owner of the machine nor
    supplied by the original vendor of the OS but by some 3rd party ISV.

    —
    Reply to this email directly or view it on GitHub
    #661 (comment).

  6. poettering commented on Jul 22, 2015

    @poettering
    Member

    Well, what @vishvananda pointed out is more than an RFE, since the current logic is not delivering what it was supposed to deliver.

  7. keszybz commented on Feb 18, 2017

    @keszybz
    Member

    This now works correctly with recent git. There were a bunch of fixes, mostly before v232, so I won't link to specific commits.

    Tested:

     sudo systemctl link $PWD/test.service
     sudo systemctl enable $PWD/test.service
     sudo systemctl disable $PWD/test.service
     sudo systemctl link $PWD/[email protected]
     sudo systemctl enable [email protected]
     sudo systemctl enable $PWD/[email protected]
     sudo systemctl disable $PWD/[email protected]
     sudo systemctl disable [email protected]
    

    All work as expected.

  8. markstos commented on Feb 22, 2017

    @markstos

    Thank you for this feature!

  9. aktau commented on Jun 24, 2018

    @aktau

    How far did you test the templated versions?

     sudo systemctl link $PWD/[email protected]
     sudo systemctl enable [email protected]
     sudo systemctl enable $PWD/[email protected]
     sudo systemctl disable $PWD/[email protected]
     sudo systemctl disable [email protected]
    

    I'm asking in relation to #3010 (comment), where it seems that for me this does not work (when I have to supply a parameter).

  10. keszybz commented on Jun 25, 2018

    @keszybz
    Member

    It seems to work with --root=/ (i.e. on the client side), but not without (i.e. through pid1).

  11. elgow commented on Jan 14, 2019

    @elgow

    Related to this issue, template instances can't be created using aliases to work around this bug. For example

    ~/tryit$ cat serviceB.service
    [Unit]
    Description=Unit %n
    
    [Service]
    Type=oneshot
    ExecStart=/bin/echo ExecStart %i go: $${STARTED} 
    
    [Install]
    [email protected]
    [email protected]
    Alias=serviceBalias.service
    
    ~/tryit$ systemctl --user enable $(pwd)/serviceB.service
    Created symlink from /home/me/.config/systemd/user/[email protected] to /home/me/tryit/serviceB.service.
    Created symlink from /home/me/.config/systemd/user/[email protected] to /home/me/tryit/serviceB.service.
    Created symlink from /home/me/.config/systemd/user/serviceBalias.service to /home/me/tryit/serviceB.service.
    Created symlink from /home/me/.config/systemd/user/serviceB.service to /home/me/tryit/serviceB.service.
    
    ~/tryit$ systemctl --user list-unit-files
    UNIT FILE                    STATE
    serviceB.path                linked
    LIS_import_failure.service   linked
    serviceB.service             enabled
    [email protected]         bad
    [email protected]         bad
    serviceBalias.service        enabled
    

    So the template instance aliases serviceB@... do not get enabled while an alias w/o the @ does. If instance aliases would function then this issue could be worked around. In order to allow general use of template instance units, which would be much better than having the feature work in some cases and not others, this issue needs to be fixed.

  12. keszybz commented on Mar 3, 2022

    @keszybz
    Member

    I tested this again, and things seem fine. (With #22649. W/o it at least systemctl link ./some.service will be broken.)

    The original report:

    sudo systemctl link $PWD/test.unit
    sudo systemctl enable $PWD/[email protected]

    Those two can never work, because we don't know what the type of the unit is. Systemd logic requires that the name of a service unit must end in .service. If a symlink was created manually with ln -s --relative test.unit /etc/systemd/system/test.service, then systemd would know that it's a service and not care about the target file name.

    But if the unit is renamed to test4.service, those all work as expected:

    $ sudo systemctl link $PWD/test4.service
    $ sudo systemctl link ./test4.service
    $ sudo systemctl link --root=/ $PWD/test4.service
    $ sudo systemctl link --root=/ ./test4.service

    (and disable removes the symlink again.)

    $ sudo systemctl link --root=/ test4.service   
    Non-absolute paths are not allowed when --root is used: test4.service
    $ sudo systemctl link test4.service       
    Link argument does contain at least one directory separator: test4.service

    Those should not work, so this is good.

    serviceB.service from the previous comment:

    $ sudo systemctl enable ./serviceB.service
    Created symlink /etc/systemd/system/serviceBalias.service → /home/zbyszek/src/systemd/serviceB.service.
    Created symlink /etc/systemd/system/serviceB.service → /home/zbyszek/src/systemd/serviceB.service.

    This is correct. [email protected] and [email protected] are not valid aliases of serviceB.service because a unit cannot simultaneously have and not have an instance. Thus serviceBalias.service is the only alias that should be created.

    $ sudo systemctl disable ./serviceB.service
    Warning: Can't execute disable on the unit file path. Proceeding with the unit name.
    Removed "/etc/systemd/system/serviceBalias.service".
    Removed "/etc/systemd/system/serviceB.service".
    $ sudo systemctl enable --root=/ ./serviceB.service
    [email protected]: symlink target name type "serviceB.service" does not match source, rejecting.
    [email protected]: symlink target name type "serviceB.service" does not match source, rejecting.
    Created symlink /etc/systemd/system/serviceBalias.service → /home/zbyszek/src/systemd/serviceB.service.
    Created symlink /etc/systemd/system/serviceB.service → /home/zbyszek/src/systemd/serviceB.service.
    $ sudo systemctl disable serviceB.service          
    Removed "/etc/systemd/system/serviceBalias.service".
    Removed "/etc/systemd/system/serviceB.service".

    So all the examples listed seem to behave fine with git tip. I'll close this again.

    --

    There is a minor issue: the symlinks that are created often link to the resolved symlink target:

    $ sudo systemctl enable --root=/ ./serviceB.service
    ...
    Created symlink /etc/systemd/system/serviceBalias.service → /home/zbyszek/src/systemd/serviceB.service.
    
    $ sudo systemctl link --root=/ ./[email protected]      
    Created symlink /etc/systemd/system/[email protected] → /home/zbyszek/src/systemd/[email protected].
    $ sudo systemctl enable --root=/ [email protected] 
    Created symlink /etc/systemd/system/multi-user.target.wants/[email protected] → /home/zbyszek/src/systemd/[email protected].
    Created symlink /etc/systemd/system/[email protected] → /home/zbyszek/src/systemd/[email protected].
    

    It'd be nicer to link to the symlink, i.e. have /etc/systemd/system/serviceBalias.service → serviceB.service, and /etc/systemd/system/multi-user.target.wants/[email protected] → [email protected], and /etc/systemd/system/[email protected] → … not even created at all. But this is a separate issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions