dotfiles/scripts/create_container

86 lines
2.3 KiB
Bash
Executable file

#!/bin/bash
# Debian containers only
# ca-certificates needed in order to clone from github
PACKAGES="ca-certificates vim git dbus less locales man-db "
REPO=https://github.com/SonnyBA/dotfiles.git
if [ $EUID != 0 ]; then
echo "This script needs sudo priveleges (for systemd-nspawn)"
echo "Exiting..."
exit 1
fi
run() {
systemd-nspawn -D $CONTAINER_PATH $@
}
config_ssh() {
sed -i 's/^#Port .*/Port '"$PORT"'/' $CONTAINER_PATH/etc/ssh/sshd_config
sed -i 's/^#PermitRootLogin .*/PermitRootLogin yes/' $CONTAINER_PATH/etc/ssh/sshd_config
sed -i 's/^#PubkeyAuthentication .*/PubkeyAuthentication yes/' $CONTAINER_PATH/etc/ssh/sshd_config
cat $SSHKEY >> $CONTAINER_PATH/root/.ssh/authorized_keys
}
dotfiles() {
run git clone $REPO /root/dotfiles
run /root/dotfiles/initialize_script
}
read -e -p "Enter the path of the container (including container root): " CONTAINER_PATH
read -p "Enter debian release channel: " CHANNEL
read -e -p "Enter (private) ssh key for git (absolute path): " KEY
read -p "Start a ssh server? [y/n]: " SSH_SERVER
read -p "Hostname? : " HOSTNAME
read -p "Add dotfiles? [y/n]: " DOTFILES
read -p "Enter additional packages (space separated) to install or leave blank: " EXTRA
read -p "Append container to ssh user config? [y/n]: " USER_CONFIG
if [ "$SSH_SERVER" == "y" ];then
read -p "Enter ssh listening port: " PORT
PACKAGES+=" ssh "
fi
PACKAGES+=$EXTRA
# Seperate packages with a comma for debootstrap's include flag
PACKAGES=$(echo $PACKAGES | sed 's/\ /,/g;s/,$//')
debootstrap --include=$PACKAGES $CHANNEL $CONTAINER_PATH
mkdir $CONTAINER_PATH/root/{.bin,.ssh}
cp $KEY $KEY.pub $CONTAINER_PATH/root/.ssh/
if [ -n "$HOSTNAME" ] ;then
echo $HOSTNAME > $CONTAINER_PATH/etc/hostname
fi
if [ "$SSH_SERVER" == "y" ] ;then
read -e -p "Enter ssh key for container access: " SSHKEY
config_ssh
fi
if [ "$DOTFILES" == "y" ] ;then
dotfiles
fi
if [ "$USER_CONFIG" == "y" ]; then
cat <<- EOF >> /home/$SUDO_USER/.ssh/config
Host $HOSTNAME
User root
Hostname 127.0.0.1
Port $PORT
EOF
# Remove leading whitespace
sed -i 's/^ *//g' /home/$SUDO_USER/.ssh/config
fi
echo "Updating default pager to less"
update-alternatives --set pager /bin/less
echo "Enter root pass"
run passwd
CONTAINER_NAME=$(basename $CONTAINER_PATH)
echo "Container finished setting up, use ssh $HOSTNAME after starting the machine" \
"with machinectl start $CONTAINER_NAME"