Python script as systemd service

Hi.

I’m being unable to have a python script be used as an ExecStart of a systemd service. It fails with a file not found error, after running. I must have struck this issue before, with another script of mine that ended up earning a nix-shell shebang…

The specifics: I’m using NixOS as a iSCSI target for virtual machines, and, after not finding a reasonable way to set up the shares at bootup, I hacked together the following service:

    systemd.services.iscsi-target = {
      description = "Restore LIO kernel target configuration";
      after = [ "sys-kernel-config.mount" "network.target" "local-fs.target" ];
      requires = [ "sys-kernel-config.mount" ];
      serviceConfig = {
        Type = "oneshot";
        RemainAfterExit = "yes";
        ExecStart = "${pkgs.pythonPackages.rtslib}/bin/targetctl restore /etc/nixos/etc/iscsi-harvest.json";
        ExecStop = "${pkgs.pythonPackages.rtslib}/bin/targetctl clear";
        SyslogIdentifier = "target";
      };
      wantedBy = [ "multi-user.target" ];
    };

(Based on the equivalent service from the AUR…)

The journal output:

Apr 03 13:17:37 HighCharity systemd[1]: Starting Restore LIO kernel target configuration...
Apr 03 13:17:37 HighCharity target[14409]: Traceback (most recent call last):
Apr 03 13:17:37 HighCharity target[14409]:   File "/nix/store/bxx5ahvd99f32dlf6dsdj6ll1hkzydn8-python2.7-rtslib-2.1.fb69/bin/.targetctl-wrapped", line 83, in <module>
Apr 03 13:17:37 HighCharity target[14409]:     main()
Apr 03 13:17:37 HighCharity target[14409]:   File "/nix/store/bxx5ahvd99f32dlf6dsdj6ll1hkzydn8-python2.7-rtslib-2.1.fb69/bin/.targetctl-wrapped", line 80, in main
Apr 03 13:17:37 HighCharity target[14409]:     funcs[sys.argv[1]](savefile)
Apr 03 13:17:37 HighCharity target[14409]:   File "/nix/store/bxx5ahvd99f32dlf6dsdj6ll1hkzydn8-python2.7-rtslib-2.1.fb69/bin/.targetctl-wrapped", line 48, in restore
Apr 03 13:17:37 HighCharity target[14409]:     errors = RTSRoot().restore_from_file(restore_file=from_file)
Apr 03 13:17:37 HighCharity target[14409]:   File "/nix/store/bxx5ahvd99f32dlf6dsdj6ll1hkzydn8-python2.7-rtslib-2.1.fb69/lib/python2.7/site-packages/rtslib_fb/root.py", line 76, in __init__
Apr 03 13:17:37 HighCharity target[14409]:     mount_configfs()
Apr 03 13:17:37 HighCharity target[14409]:   File "/nix/store/bxx5ahvd99f32dlf6dsdj6ll1hkzydn8-python2.7-rtslib-2.1.fb69/lib/python2.7/site-packages/rtslib_fb/utils.py", line 438, in mount_configfs
Apr 03 13:17:37 HighCharity target[14409]:     stderr=subprocess.PIPE)
Apr 03 13:17:37 HighCharity target[14409]:   File "/nix/store/ih03q5vi6kj278l4jk4kh41lqyagzzdi-python-2.7.15/lib/python2.7/subprocess.py", line 394, in __init__
Apr 03 13:17:37 HighCharity target[14409]:     errread, errwrite)
Apr 03 13:17:37 HighCharity target[14409]:   File "/nix/store/ih03q5vi6kj278l4jk4kh41lqyagzzdi-python-2.7.15/lib/python2.7/subprocess.py", line 1047, in _execute_child
Apr 03 13:17:37 HighCharity target[14409]:     raise child_exception
Apr 03 13:17:37 HighCharity target[14409]: OSError: [Errno 2] No such file or directory
Apr 03 13:17:37 HighCharity systemd[1]: iscsi-target.service: Main process exited, code=exited, status=1/FAILURE
Apr 03 13:17:37 HighCharity systemd[1]: iscsi-target.service: Failed with result 'exit-code'.
Apr 03 13:17:37 HighCharity systemd[1]: Failed to start Restore LIO kernel target configuration.

nix run nixpkgs.pythonPackages.rtslib -c sudo targetctl restore works just fine.

I can’t think of any python-based server software to compare in nixpkgs. Is there any magic voodoo useful to this situation?

1 Like

Just by looking at the stacktrace (without actually knowing anything about rtslib-fb), it seems it tries to call the mount command at lib/python3.7/site-packages/rtslib_fb/utils.py:435, which isn’t available without specifying explicitly (while it might be available in your shell).

rtslib-fb also calls modprobe in other places.

You could try adding with pkgs; [utillinux kmod]; to systemd.services.iscsi-target.path, so it’s in the services $PATH, too.

1 Like

Indeed, that makes it work. I hadn’t thought of looking at the actual code, because I was convinced this was python being weird in nixpkgs.

Thank you!