Skip to content

systemctl enable on linked service: 'Failed to execute operation: Too many levels of symbolic links' #3010

Closed

Description

@paitonic

Submission type

  • Bug report
  • Request for enhancement (RFE)

NOTE: Do not submit anything other than bug reports or RFEs via the issue tracker!

systemd version the issue has been seen with

229
+PAM +AUDIT +SELINUX +IMA +APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ -LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD -IDN

NOTE: Do not submit bug reports about anything but the two most recently released systemd versions upstream!

Used distribution

Ubuntu 16.04, 4.4.0-15-generic

In case of bug report: Expected behaviour you didn't see

After linking service via systemctl link from /home/developer directory and enabling it via systemctl enable I was expecting that the service should start on boot.

In case of bug report: Unexpected behaviour you saw

Enabling linked service causes to: 'Failed to execute operation: Too many levels of symbolic links'

In case of bug report: Steps to reproduce the problem

cat.service
[Unit]
Description=Service for populating desktop with cats.

[Service]
ExecStart=/bin/cat

[Install]
WantedBy=multi-user.target
$ sudo systemctl link /home/developer/cat.service
Created symlink from /etc/systemd/system/cat.service to /home/developer/cat.service.
$ sudo systemctl status cat.service
● cat.service - Service for populating desktop with cats.
   Loaded: loaded (/home/developer/cat.service; linked
   Active: inactive (dead)
$ sudo systemctl start cat.service
$ sudo systemctl is-enabled cat.service
linked
$ sudo systemctl enable cat.service
Failed to execute operation: Too many levels of symbolic links
$ ls -l /etc/systemd/system | grep -i 'cat'
lrwxrwxrwx 1 root root   53 Apr 11 07:44 cat.service -> /home/developer/cat.service
$ ls -l /etc/systemd/system/multi-user.target.wants/ | grep -i 'cat'
(empty)

Looks like systemctl link places symlink in /etc/systemd/system, however if I place cat.service into /lib/systemd/system and then systemctl enable it, symlink will be created in /etc/systemd/system/multi-user.target.wants/.

Activity

  1. added
    bug 🐛
    regression ⚠️
    on Apr 11, 2016
  2. martinpitt commented on Apr 11, 2016

    @martinpitt
    Contributor

    Related to this: If you directly enable a unit file with full path, it actually creates the symlinks, but then spits out a wrong error message:

    # systemctl enable /tmp/foo.service 
    Created symlink from /etc/systemd/system/multi-user.target.wants/foo.service to /tmp/foo.service.
    Created symlink from /etc/systemd/system/foo.service to /tmp/foo.service.
    The unit files have no [Install] section. They are not meant to be enabled
    using systemctl.
    Possible reasons for having this kind of units are:
    1) A unit may be statically enabled by being symlinked from another unit's
       .wants/ or .requires/ directory.
    2) A unit's purpose may be to act as a helper for some other unit which has
       a requirement dependency on it.
    3) A unit may be started when needed via activation (socket, path, timer,
       D-Bus, udev, scripted systemctl call, ...).
    
  3. martinpitt commented on Apr 11, 2016

    @martinpitt
    Contributor

    I tried with @poettering 's large patch in PR #2973; it largely behaves the same way with systemctl enable /tmp/foo.service, and it's even worse when using --global:

    # systemctl --global enable /tmp/foo.service 
    Assertion 'a' failed at src/basic/path-util.c:389, function path_compare(). Aborting.
    Aborted (core dumped)
    

    (I'm using --global as that makes it much easier to test modified systemctls without having to patch/install/reexec pid 1)

  4. martinpitt commented on Apr 11, 2016

    @martinpitt
    Contributor

    The "too many levels of symbolic links" error in the original report just seems to be a missing flag:

    --- a/src/shared/install.c
    +++ b/src/shared/install.c
    @@ -1811,7 +1811,7 @@ int unit_file_enable(
                     return r;
    
             STRV_FOREACH(f, files) {
    -                r = install_info_discover(scope, &c, root_dir, &paths, *f, SEARCH_LOAD, &i);
    +                r = install_info_discover(scope, &c, root_dir, &paths, *f, SEARCH_LOAD|SEARCH_FOLLOW_CONFIG_SYMLINKS, &i);
                     if (r < 0)
                             return r;
                     if (i->type == UNIT_FILE_TYPE_MASKED)
    

    With that, systemctl enable on a symlinked unit now actually creates the correct symlinks, but later fails with the "The unit files have no [Install] section" bogus error like in my first comment. I. e. this is now exactly equivalent to not doing systemctl link first and doing systemctl enable /some/path/myunit.service directly.

    I'm trying to understand the expectations of the code in src/shared/install.c: I think install_info_symlink_wants() is supposed to return the number of units it actually enabled, i. e. 1 if we created one enablement symlink. But it currently returns 0 even after success, which causes the "0" return to propagate back through install_info_apply() → install_context_apply() → unit_file_enable(), and in the latter carries_install_info == 0 and thus systemctl enable prints that error message. On the other end, create_symlink() always just returns 0 on success (or negative on error), while it seems to me that all callers of create_symlink() expect that to return > 0 if it actually did something.

    So apparently, fixing create_symlink() to return 1 if it did something, and continue to return 0 if the link already exists (with the same target) seems to fix it:

     --- a/src/shared/install.c
    +++ b/src/shared/install.c
    @@ -294,7 +294,7 @@ static int create_symlink(
    
             if (symlink(old_path, new_path) >= 0) {
                     unit_file_changes_add(changes, n_changes, UNIT_FILE_SYMLINK, new_path, old_path);
    -                return 0;
    +                return 1;
             }
    
             if (errno != EEXIST)
    @@ -317,7 +317,7 @@ static int create_symlink(
             unit_file_changes_add(changes, n_changes, UNIT_FILE_UNLINK, new_path, NULL);
             unit_file_changes_add(changes, n_changes, UNIT_FILE_SYMLINK, new_path, old_path);
    
    -        return 0;
    +        return 1;
     }
    
     static int mark_symlink_for_removal(
    

    Am I on the right track here?

  5. martinpitt commented on Apr 11, 2016

    @martinpitt
    Contributor

    I refined the "unit-config" tests to test "link+disable" and "link+enable+disable" separately: https://gist.github.com/martinpitt/dfb97e938f45ce1d079193cd414fb97b.

    With the above two changes everything passes now. This breaks one test in ./test-install-root, so I'll look into that and propose a pull request for discussing this further, which is easier with by-line comments.

  6. self-assigned this
    on Apr 11, 2016
  7. martinpitt commented on Apr 11, 2016

    @martinpitt
    Contributor

    That test-install-root test case is covering exactly this issue:

             * c) a unit file in /opt, that is linked into
             * /etc/systemd/system, and where "enable" should result in
             * -ELOOP, since using information from /etc to generate
             * information in /etc should not be allowed.
    

    I think this is a typo and was meaning to say "using information from /opt to generate information in /etc", and it does

           assert_se(unit_file_enable(UNIT_FILE_SYSTEM, false, root, STRV_MAKE("linked3.service"), false, &changes, &n_changes) == -ELOOP);
    

    where /etc/systemd/system/linked3.service → /opt/linked3.service.

    So it seems that this is intentionally forbidden (even though the error message there is terrible). If we don't want to support unit symlinks from /etc/ to somewhere else (/opt in test-install-root, /tmp/ in my unit-config test, or /var/ in the bug report that I got), why do we offer systemctl link in the first place?

    Depending on the answer, I'll either adjust the test case or add a comprehensible error message. Thanks!

  8. martinpitt commented on Apr 11, 2016

    @martinpitt
    Contributor

    Discussed with @poettering:

    • systemctl enable foo.service should not be allowed if foo.service is a symlink in /etc/. This currently causes the "Too many levels of symbolic links" error message which ought to be improved.
    • systemctl enable or link on a full path should both be allowed.
  9. martinpitt commented on Apr 11, 2016

    @martinpitt
    Contributor

    I fixed the bogus "No [Install] section" warning when enabling a unit with full path in PR #3016 (that also has a link to the now committed unit-test test case).

    Let's keep this open for the part that systemctl enable foo.service prints out that bad "too many levels of symbolic links" error; @poettering wanted to fix that in his mega- PR #2973, to avoid touching the same code in two PRs.

  10. removed
    regression ⚠️
    on Apr 11, 2016
  11. added a commit that references this issue on Apr 12, 2016
    8e20adc
  12. 13 remaining items

  13. Celadora commented on Jan 15, 2017

    @Celadora

    I was able to work around this by using "systemctl daemon-reload" after placing the symbolic link in /etc/systemd/system

    The only problem is that when you disable the service (systemctl disable [service]) the symbolic link is removed.

    Hope this helps!

  14. dlford commented on Feb 25, 2017

    @dlford

    I was here earlier looking for answers and figured it out, thought I'd post here to help anyone else that ends up in my shoes, and although I may have missed it, I haven't seen it anywhere spelled out in simple terms.

    systemd works fine with symlinks, I was just using it wrong, and that's the reason I was getting the "too many levels of symlinks" error.

    This works:

    systemctl enable /var/git/foo/foo.service
    systemctl enable /var/git/foo/foo.timer
    systemctl start foo.timer
    

    The above example creates all the symlinks needed:

    /etc/systemd/system/foo.service -> /var/git/foo/foo.service
    /etc/systemd/system/foo.timer -> /var/git/foo/foo.timer
    /etc/systemd/system/timers.target.wants/foo.timer -> /var/git/foo/foo.timer
    

    Just remember that if you ever disable the service or timer, all relative symlinks are removed (as @Celadora mentioned above), so you will need to re-enable with the full path as you did in the example above.

    If you create the symlinks in /etc/systemd/system manually as in the example below, and then try to enable the timer, systemd tries to make a symlink in /etc/systemd/system/timers.target.wants/ to the timer file in /etc/systemd/system, and fails becuase it's symlinking to a symlink.

    This does not work:

    ln -s /var/git/foo/foo.service /etc/systemd/system/
    ln -s /var/git/foo/foo.timer /etc/systemd/system/
    systemctl enable foo.timer
    
  15. keszybz commented on Feb 27, 2017

    @keszybz
    Member

    https://bugzilla.redhat.com/show_bug.cgi?id=1399991
    Any chance the two are related?

    That's unrelated. Like @msekletar wrote, it's a known issue (though I cannot find an upstream bug report).

  16. bbkane commented on Jan 4, 2018

    @bbkane

    I can't link / enable template services:

    $ systemctl link /home/vagrant/Git/hello_timers/systemd/[email protected]
    Failed to execute operation: Unit name [email protected] is missing the instance name.
    $ systemctl enable /home/vagrant/Git/hello_timers/systemd/[email protected]
    Failed to execute operation: No such file or directory
    

    Is this a bug? I want to use the @ parameter to inject the user for my services (since systemd --user is disabled in RHEL7 )

  17. markstos commented on Jan 4, 2018

    @markstos

    @bbkane this issue was about the error "Failed to execute operation: Too many levels of symbolic links", which is not what you are experiencing. According to your errors, you are trying to work with files that don't exist. Your support questions about systemd are better suited to the Unix & Linux StackExchange site. If you tag your question with [systemd], I follow that tag there. But you should check and provide evidence that the referenced files actually exist!

  18. bbkane commented on Jan 4, 2018

    @bbkane

    @markstos This is a tangent- apologies for that. I posted here because the method used by @dlford here didn't work for template files. I'll create a minimal example, put it on StackExchange, and edit this post with the link. Thanks!

  19. bbkane commented on Jan 5, 2018

    @bbkane

    @markstos so I reproduced the issue, created a question and GitHub repo with code to do it, and, finally, right before posting it, found #661 which says it's fixed in systemd 231. Unfortunately, Centos7 is stuck on systemd 219... I'll just have to cp the files. Thanks for the help.

  20. deleted a comment from Keensyst on Jan 5, 2018
  21. markstos commented on Jan 5, 2018

    @markstos
  22. bbkane commented on Jan 5, 2018

    @bbkane

    cp the [email protected]/timer files into /etc/systemd/system, then enabling them (as opposed to enabling them from my home directory), not systemd libraries or anything. Thanks for the help.

  23. aktau commented on Jun 24, 2018

    @aktau

    Just in case anyone else reads @bbkane's comment that the bug with templated files is fixed since systemd 231. I'm on Debian Stretch with systemd 232 and it still seems to exist:

    [email protected] is a templated file, which is supposed to be supplied with a username (like aktau) in my case.

    Full path enabling doesn't work (because the template instance isn't a path).

    # systemctl enable $HOME/homesrv/systemd/[email protected]
    Failed to enable unit: File /home/aktau/homesrv/systemd/[email protected]: No such file or directory
    

    Without the parameter "works", but of course doesn't provide anything that runs.

    # systemctl enable $HOME/homesrv/systemd/[email protected]
    Created symlink /etc/systemd/system/[email protected] → /home/aktau/homesrv/systemd/[email protected].
    The unit files have no installation config (WantedBy, RequiredBy, Also, Alias
    settings in the [Install] section, and DefaultInstance for template units).
    This means they are not meant to be enabled using systemctl.
    Possible reasons for having this kind of units are:
    1) A unit may be statically enabled by being symlinked from another unit's
       .wants/ or .requires/ directory.
    2) A unit's purpose may be to act as a helper for some other unit which has
       a requirement dependency on it.
    3) A unit may be started when needed via activation (socket, path, timer,
       D-Bus, udev, scripted systemctl call, ...).
    4) In case of template units, the unit is meant to be enabled with some
       instance name specified.
    

    Perhaps if I try a regular relative enable afterwards?

    # systemctl enable [email protected]
    Failed to enable unit: Refusing to operate on linked unit file [email protected]
    

    No dice.

    The only working alternative I've found is to cp the [email protected] timer file into /etc/systemd/system as @bbkane says, even on the systemd version where this is supposed to work.

    Can anyone tell me if I'm holding this patch wrong?

  24. bryanpedini commented on Jan 29, 2021

    @bryanpedini

    Sorry to dig up an old thread, but...
    I'm on Debian 10 stable Linux <hostname> 4.19.0-13-amd64 #1 SMP Debian 4.19.160-2 (2020-11-28) x86_64 GNU/Linux

    systemd 241 (241)
    +PAM +AUDIT +SELINUX +IMA +APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL
    +XZ +LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD -IDN2 +IDN -PCRE2 default-hierarchy=hybrid
    

    I wanted to block sshd on a server of mine, so I masked it via systemctl, afterwards I needed to re-enable the service, so I proceeded to unmask it via normal procedure, however no systemd file were found.
    A search with find / -name "sshd.service" revealed that there was only one file in /var/lib/systemd/deb-systemd-helper-enabled/sshd.service, and of course systemctl status and the other commands failed.

    I then proceeded to apt install --reinstall openssh-server to no avail, followed by one final apt remove --purge openssh-server and apt install openssh-server again (backing up the config of course).
    That worked, sort of - now I get this issue's title error.

    Current situation:

    file /etc/systemd/system/sshd.service
    /etc/systemd/system/sshd.service: symbolic link to /lib/systemd/system/ssh.service
    file /lib/systemd/system/ssh.service
    /lib/systemd/system/ssh.service: ASCII text
    
    systemctl status sshd.service
      ssh.service - OpenBSD Secure Shell server
       Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
       Active: active (running) since [...]
    
    file /etc/systemd/system/multi-user.target.wants/ssh.service
    /etc/systemd/system/multi-user.target.wants/ssh.service: symbolic link to /lib/systemd/system/ssh.service
    
    systemctl enable sshd.service
    Failed to enable unit: Too many levels of symbolic links
    
    systemctl disable sshd.service
    Removed /etc/systemd/system/multi-user.target.wants/ssh.service.
    Removed /etc/systemd/system/sshd.service.
    
    systemctl enable sshd.service
    Failed to enable unit: Unit file sshd.service does not exist.
    
    ln -s /lib/systemd/system/ssh.service /etc/systemd/system/sshd.service
    ln -s /lib/systemd/system/ssh.service /etc/systemd/system/multi-user.target.wants/ssh.service
    systemctl daemon-reload
    
    systemctl status sshd.service
      ssh.service - OpenBSD Secure Shell server
       Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
       Active: active (running) since [...]
    

    At this point I don't even know if this is an error related to openssh-server and/or if this issue is the right one to post this about.
    If not, excuse me in advance, and please let me know where to post this issue in.
    Thanks!

  25. keszybz commented on Jan 29, 2021

    @keszybz
    Member

    @bryanpedini this is more suitable for some debian forum. Version 241 is old, we only support the two latest versions upstream. Debian has special compilation settings to support split-usr and also that code has been changed a lot since 241.

  26. bryanpedini commented on Jan 29, 2021

    @bryanpedini

    this is more suitable for some debian forum.

    I see, thank you for the suggestion.

    Version 241 is old, we only support the two latest versions upstream.

    I see, completely understand, thank you anyway!

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

Metadata

Metadata

Assignees

Labels

bug 🐛systemctl

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions