Linux – Start systemd-nspawn and execute commands inside

bashcommand linelinuxsystemd

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.

Best Answer

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.

Related Question