Start systemd-nspawn and execute commands inside

3

1

I didn't find a question like this. I have a bash script (running in Debian) that start a systemd-nspawn session. After that I would like to continue my script as I am in the spawned machine:

#!/bin/bash
systemd-nspawn -q --bind /usr/bin/qemu-arm-static -D /mnt/project /bin/bash

apt-get update
apt-get -y upgrade

After executing the systemd-nspawn command, the script is suspended and the prompt of the console is inside the spawned system. When I exit the script resumes but in the LOCAL machine.

Mark

Posted 2017-08-21T13:00:37.933

Reputation: 327

Answers

4

A couple alternatives how you can approach your problem:

  1. Make a script you execute inside systemd-nspawn container and place the commands you wish to run there (systemd-nspwan -D .. /your-script). If you want to get interactive shell after running your commands, place something like exec bash -i at the end of script .

  2. If you want to keep your current script, you could provide your commands as input to bash:

    #!/bin/bash
    systemd-nspawn -q --bind /usr/bin/qemu-arm-static -D /mnt/project /bin/bash << EOF
    apt-get update
    apt-get -y upgrade
    EOF
    

    This works fine if you don't require an interactive shell afterwards.

sebasth

Posted 2017-08-21T13:00:37.933

Reputation: 670

1In 2019, I think you have to pass systemd-nspawn --pipe for this to work. – 425nesp – 2019-10-23T04:56:23.703