Skip to content

Instantly share code, notes, and snippets.

@floffel
Created June 16, 2018 16:20
Show Gist options
  • Select an option

  • Save floffel/d19f93390073b8e24805e1c98cd733b4 to your computer and use it in GitHub Desktop.

Select an option

Save floffel/d19f93390073b8e24805e1c98cd733b4 to your computer and use it in GitHub Desktop.
[ArchLinux] nspawn contaier creation automisation
#!/usr/bin/bash
############################
# Script zum erstellen von #
# nspawn Containern #
# #
# <[email protected]> #
############################
# basic checks
if [ $UID -ne 0 ]; then
echo "run this script as root" >&2
exit 1
fi
if [ -z "$1" ]; then
echo "Usage: $0 </destination>" >&2
echo " (destination=machinename)"
exit 0
fi
input="$1"
# Colors:
#########
MRED_ON="\033[1m\033[31m"
URED_ON="\033[5m\033[31m"
LRED_ON="\033[0m\033[31m"
MGREEN_ON="\033[1m\033[32m"
LGREEN_ON="\033[0m\033[32m"
MYELLOW_ON="\033[1m\033[33m"
LYELLOW_ON="\033[0m\033[33m"
MBLUE_ON="\033[1m\033[34m"
LBLUE_ON="\033[0m\033[34m"
MMAGENTA_ON="\033[1m\033[35m"
UMAGENTA_ON="\033[5m\033[35m"
LMAGENTA_ON="\033[0m\033[35m"
MCYAN_ON="\033[1m\033[36m"
UCYAN_ON="\033[5m\033[36m"
LCYAN_ON="\033[0m\033[36m"
BOLD_ON="\033[1m"
UNDERLINE_ON="\e[4m"
COL_OFF="\033[m"
###########################
###############UI-Variables
UI_TEXTOUT_INSET=0
# Catch the sigint
sigint_handler() {
# TODO: implement better error-handling...!
echo "No tmp-files where deleted... This is the end...";
# Finally, reset the Terminal
echo -e "$COL_OFF"
exit 1
}
trap 'sigint_handler' INT
# Prints the text to stdout/stderr
# gets the following arguments
# - any subargument in any subargument can be omitted
# $TEXT_OUT $1
# ! // reverse or fatal the command
# ERROR // prints the error to STDERR
# INSET // insets the following code
# {COLOR} // Color is one of {@COLORS}
#
function TEXT_OUT {
#$1
#$@
local reverse="off"
local error="off"
if [ "$1" == "!" ]; then
shift
reverse="on"
fi
if [ "$1" == "ERROR" ]; then
shift
if [ "$reverse" == "on" ]; then
TEXT_OUT "!" "$@" 1>&2
return
else
TEXT_OUT "$@" 1>&2
return
fi
if [ "$reverse" == "on" ]; then
exit 030
fi
fi
if [ "$1" == "INSET" ]; then
if [ "$reverse" == "on" ]; then
((UI_TEXTOUT_INSET-=1))
else
((UI_TEXTOUT_INSET+=1))
fi
shift
TEXT_OUT "$@"
return
fi
# print the INSETS
local inset_num=0
while [ "$inset_num" -lt "$UI_TEXTOUT_INSET" ]; do
printf " "
((inset_num+=1))
done
echo "$@"
}
# 1. Create the directory for the container
# 2. Chown to floffel
# 3. Packstrap container without linux
function createAndPacstrapContainer {
#$1
local dest="$1"
TEXT_OUT "INSET" "Creating Container $dest: "
if test -e "$dest"; then
TEXT_OUT "!" "ERROR" "Destination directory exists: $dest!"
fi
mkdir "/home/container/$dest"
TEXT_OUT "Running pacstrap: "
pacstrap -i -c -d "$dest/" base --ignore linux
TEXT_OUT "Running systemd-nspawn to start the container - remember to halt the machine"
systemd-nspawn -b --network-veth -D "$dest"
TEXT_OUT "Symlinking to the right direction..."
ln -s "/home/container/$dest" "/var/lib/machines/$dest"
TEXT_OUT "!" "INSET" "CREATED CONTAINER in directory: $dest"
}
# 1. create the /etc/systemd/nspawn/$dest file
# 2. insert the nework bridge access granting
function giveHostNetworkAccess {
#$1
dest="$1"
file="/etc/systemd/nspawn/$dest.nspawn"
if test -e "$file"; then
TEXT_OUT "!" "ERROR" "File $file alrdy exists...!"
else
touch "$file"
echo '
[Network]
Bridge=natbr0
[Files]
Bind=/var/cache/pacman/pkg
' > "$file"
fi
TEXT_OUT "CREATED: $file"
}
function giveGuestNetworkAccess {
#$1
dest="$1"
systemctl start "systemd-nspawn@$input"
sleep 10;
machinectl shell "root@$dest" /bin/bash -c "systemctl enable systemd-networkd"
sleep 5;
machinectl shell "root@$dest" /bin/bash -c "systemctl enable systemd-resolved"
sleep 5;
machinectl shell "root@$dest" /bin/bash -c "systemctl start systemd-networkd"
sleep 5;
machinectl shell "root@$dest" /bin/bash -c "systemctl start systemd-resolved"
sleep 5;
machinectl shell "root@$dest" /bin/bash -c "rm /etc/resolv.conf"
sleep 5;
machinectl shell "root@$dest" /bin/bash -c "ln -s /run/systemd/resolve/resolv.conf /etc/resolv.conf"
}
giveHostNetworkAccess "$input"
sleep 5;
createAndPacstrapContainer "$input"
sleep 5;
giveGuestNetworkAccess "$input"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment