Skip to content

Commit

Permalink
python: sd_nspawn: '--expand-environment' only exists with systemd 254
Browse files Browse the repository at this point in the history
Signed-off-by: Nathan Chancellor <nathan@kernel.org>
  • Loading branch information
nathanchance committed Jan 2, 2025
1 parent 1bd879a commit 30c14e0
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions python/scripts/sd_nspawn.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ def __init__(self, name):
# Set machine path based on the name
self.machine_dir = Path('/var/lib/machines', self.name)

# Set systemd version. If this fails, it means we do not have nspawn
# installed or the output has changed, both of which need to be dealt
# with.
self.systemd_version = int(
lib.utils.chronic(['systemd-nspawn', '--version']).stdout.splitlines()[0].split()[1])

def _add_dynamic_mounts(self):
rw_mounts = {
'/dev/kvm',
Expand Down Expand Up @@ -197,13 +203,11 @@ def _gen_eph_cmd(self):
return nspawn_cmd

def _gen_run_cmd(self, cmd):
return [
args = [
SYSTEMD_RUN_M,
self.name,
# We do not need our units to remain in memory
'--collect',
# The shell will expand our environment
'--expand-environment=no',
# Allowing interacting with stdin and seeing stdout/stderr
'--pty',
# Show no machinectl output
Expand All @@ -212,11 +216,17 @@ def _gen_run_cmd(self, cmd):
f"--uid={USER}",
# Get return code of the process
'--wait',
]
# The shell will expand our environment
if self.systemd_version >= 254:
args.append('--expand-environment=no')
args += [
# Use a qualified path
'/usr/bin/fish',
'-c',
cmd,
]
return args

def install_files(self):
lib.utils.print_green('Requesting sudo permissions for file creation...')
Expand Down Expand Up @@ -370,7 +380,7 @@ def parse_arguments():
return parser.parse_args()


if __name__ == '__main__':
def main():
args = parse_arguments()

if os.geteuid() == 0:
Expand All @@ -392,3 +402,7 @@ def parse_arguments():
config.reset(args.reset)
if args.run:
config.run_mach_cmd(args.run)


if __name__ == '__main__':
main()

0 comments on commit 30c14e0

Please sign in to comment.