Skip to content

Instantly share code, notes, and snippets.

@tierpod
Created June 6, 2019 05:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tierpod/a924004c0838822a678d1790fddc9b3c to your computer and use it in GitHub Desktop.
Save tierpod/a924004c0838822a678d1790fddc9b3c to your computer and use it in GitHub Desktop.
Example: add systemd-nspawn machine, install packages, add user
#!/bin/bash
# Add systemd-nspawn machine, install base packages, create builder user
set -eu
if [ $# -ne 1 ]; then
echo "usage: $0 MACHINE"
exit 1
fi
MACHINE=$1
if [ -d "/var/lib/machines/$MACHINE" ]; then
echo "machine '$MACHINE' already exists"
exit 2
fi
BUILDER_GID=1000
BUILDER_UID=1000
echo "--> install packages"
yum -y --releasever=7 --installroot=/var/lib/machines/$MACHINE install \
systemd systemd-networkd passwd yum vim-enhanced bash bash-completion \
less psmisc iputils iproute net-tools mc vim sudo wget curl redhat-lsb-core \
epel-release rpmdevtools make yum-utils '@Development Tools'
echo "--> configure container"
systemd-nspawn --machine=$MACHINE -- /bin/bash << EOF
rm -f /etc/securetty
echo export TERM=screen > /etc/profile.d/profile.sh
echo export LANG=en_US.utf-8 >> /etc/profile.d/profile.sh
EOF
echo "--> configure users"
systemd-nspawn --machine=$MACHINE -- /bin/bash << EOF
echo "root:root" | chpasswd
groupadd -g $BUILDER_GID builder
useradd -u $BUILDER_UID -g builder -G wheel builder
echo "builder:builder" | chpasswd
sudo -u builder mkdir -p /home/builder/{bin,tmp}
sudo -u builder rpmdev-setuptree
EOF
echo "--> add systemd-nspawn@$MACHINE.service.d/override.conf file"
mkdir -p /etc/systemd/system/systemd-nspawn@$MACHINE.service.d
cat << EOF > /etc/systemd/system/systemd-nspawn@$MACHINE.service.d/override.conf
[Service]
ExecStart=
ExecStart=/usr/bin/systemd-nspawn --keep-unit --boot --link-journal=try-guest --machine=%I \\
--bind=/storage/%I/rpmbuild:/home/builder/rpmbuild \\
--bind=/storage/%I/bin:/home/builder/bin
EOF
systemctl daemon-reload
echo "--> create shared directories"
install -d -g $BUILDER_GID -m 0775 /storage/$MACHINE
install -d -g $BUILDER_GID -m 0775 /storage/$MACHINE/{rpmbuild,bin}
echo "--> start 'systemd-nspawn@$MACHINE' container"
systemctl start systemd-nspawn@$MACHINE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment