diff --git a/Dockerfile b/Dockerfile index 40fa5e34..c50b9a2e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -66,6 +66,8 @@ RUN zypper -n install \ cp -vr container-scripts/label-* \ container-scripts/wwctl \ container-scripts/warewulf.service \ + container-scripts/warewulf-container-manage.sh \ + container-scripts/config-warewul \ /container &&\ mkdir -p /usr/share/bash_completion/completions/ &&\ cp /etc/warewulf/bash_completion.d/warewulf /usr/share/bash_completion/completions/wwctl &&\ diff --git a/Makefile b/Makefile index 55165968..993c032e 100644 --- a/Makefile +++ b/Makefile @@ -254,7 +254,7 @@ warewulfconf: config_defaults ./config_defaults dist: vendor config - rm -rf .dist/$(WAREWULF)-$(VERSION) + rm -rf .dist/$(WAREWULF)-$(VERSION) $(WAREWULF)-$(VERSION).tar.gz mkdir -p .dist/$(WAREWULF)-$(VERSION) cp -rap * .dist/$(WAREWULF)-$(VERSION)/ find .dist/$(WAREWULF)-$(VERSION)/ -type f -name '*~' -delete diff --git a/container-scripts/config-warewulf b/container-scripts/config-warewulf new file mode 100755 index 00000000..c7074527 --- /dev/null +++ b/container-scripts/config-warewulf @@ -0,0 +1,73 @@ +#!/bin/sh +# Helper functions +# Get the mask form prefix +cdr2mask() +{ + # Number of args to shift, 255..255, first non-255 byte, zeroes + set -- $(( 5 - ($1 / 8) )) 255 255 255 255 $(( (255 << (8 - ($1 % 8))) & 255 )) 0 0 0 + [ $1 -gt 1 ] && shift $1 || shift + echo ${1-0}.${2-0}.${3-0}.${4-0} +} +# Get the ip4 address of the netork +network_address() { + declare address prefix_length + IFS=/ read address prefix_length <<< "$1" + + declare -a octets + IFS=. read -a octets <<< "$address" + + declare mask + mask=$( printf "%08x" $(( (1 << 32) - (1 << (32 - prefix_length)) )) ) + + declare -i i + for i in {0..3}; do octets[$i]=$(( octets[i] & 16#${mask:2*i:2} )); done + + echo $( IFS=.; echo "${octets[*]}" ) +} +echo "-- WW4 CONFIGURAION $* --" + +# Make sure that a ip address was defined for out network so that +# we can configure dhcpd correctly +IP4CIDR=`ip addr | awk '/scope global/ {print $2;exit}'` +IP4=${IP4CIDR%/*} +IP4PREFIX=${IP4CIDR#*/} +IP4MASK=$(cdr2mask $IP4PREFIX) +IP4NET=$(network_address "$IP4/$IP4PREFIX") + +if [ "$IP4PREFIX" -gt 25 ] ; then + echo "ERROR: warewulf does at least a /25 network for dynamic addresses" + exit 1 +fi + +DYNSIZE=100 +DYNSTART=${IP4#*.*.*.} +DYNPRE=${IP4%.*} +DYNEND=$(( $DYNSTART + $DYNSIZE )) +if [ $DYNEND -gt 254 ] ; then + DYNEND=$(( $IPNET + 2 + $DYNSIZE )) + DYNSTART=$(( $IPNET + 2 )) +fi +DYNSTART="${DYNPRE}.${DYNSTART}" +DYNEND="${DYNPRE}.${DYNEND}" + + +WW4CONF=/etc/warewulf/warewulf.conf +if [ -e $WW4CONF ] ; then + test -n $IP4 && sed -i 's/^ipaddr: EMPTY/ipaddr: '$IP4'/' $WW4CONF + test -n $IP4MASK && sed -i 's/^netmask: EMPTY/netmask: '$IP4MASK'/' $WW4CONF + test -n $IP4NET && sed -i 's/^network: EMPTY/network: '$IP4NET'/' $WW4CONF + test -n $DYNSTART && sed -i 's/^ range start: EMPTY/ range start: '$DYNSTART'/' $WW4CONF + test -n $DYNEND && sed -i 's/^ range end: EMPTY/ range end: '$DYNEND'/' $WW4CONF + cat << EOF +ipaddr: $IP4 +netmask: $IP4MASK +network: $IP4NET + range start: $DYNSTART + range end: $DYNEND +EOF +fi + +# configure the services of the host by building the host overlay +echo "-- Running wwctl --" +wwctl overlay build +wwctl configure tftp diff --git a/container-scripts/label-install b/container-scripts/label-install old mode 100644 new mode 100755 index 8f54ab42..063c00a0 --- a/container-scripts/label-install +++ b/container-scripts/label-install @@ -12,7 +12,8 @@ OVERLAYDIR=/var/lib/warewulf/overlays CHROOTDIR=/var/lib/warewulf/chroots CONTAINERDIR=/var/lib/warewulf/container WAREWULFCONF=/etc/warewulf -BASHCOMPLETION=/usr/share/bash-completion/completions/wwctl +BASHCOMPLETION=/usr/share/bash_completion/completions/wwctl +CONFSCRIPT=/container/config-warewulf echo "LABEL INSTALL" # ensure all scripts will be present on the host @@ -23,6 +24,7 @@ mkdir -p $BASEDIR if [ ! -e /host/usr/local/bin/${SCRIPT} ]; then echo "copy /container/${SCRIPT} in /host/usr/local/bin/" rsync -u /container/${SCRIPT} /host/usr/local/bin/${SCRIPT} + chmod 755 /host/usr/local/bin/${SCRIPT} else echo "/host/usr/local/bin/${SCRIPT} already exist, will not update it" fi @@ -73,3 +75,6 @@ fi # containerdir mkdir -p /host/$CONTAINERDIR + +# now config the container +$CONFSCRIPT diff --git a/container-scripts/label-purge b/container-scripts/label-purge old mode 100644 new mode 100755 diff --git a/container-scripts/label-uninstall b/container-scripts/label-uninstall old mode 100644 new mode 100755 index 16d9b411..915e65f1 --- a/container-scripts/label-uninstall +++ b/container-scripts/label-uninstall @@ -17,4 +17,4 @@ echo "LABEL UNINSTALL: Removing all files" rm -vf /host/usr/local/bin/wwctl rm -vf /host/usr/local/bin/warewulf-container-manage.sh rm -vf /host/etc/systemd/system/${CONTAINER}.service -rm -vf /host/usr/share/bash-completion/completions/wwctl +rm -vf /host/usr/share/bash_completion/completions/wwctl diff --git a/container-scripts/warewulf-container-manage.sh b/container-scripts/warewulf-container-manage.sh new file mode 100755 index 00000000..abb94001 --- /dev/null +++ b/container-scripts/warewulf-container-manage.sh @@ -0,0 +1,108 @@ +#!/bin/bash +# quick script to manage the container +CONTAINER_NAME=warewulf + +create_container() { +podman create \ + --name ${CONTAINER_NAME} \ + --tls-verify=false \ + --network host \ + ${IMAGE} +} + +run_container() { +podman run \ + --name ${CONTAINER_NAME} \ + --rm -ti \ + --network host \ + --entrypoint bash \ + ${IMAGE} +} + +if [ -z "$1" ]; then +echo " +First ARG is mandatory: +$0 [create|start|stop|rm|rmcache|run|bash|logs|install|uninstall] + +CONTAINER_NAME: '${CONTAINER_NAME}' + +DEPLOYMENT: +create + Pull the image and create the container automatically + +install + install needed files on the host to manage '${CONTAINER_NAME}' container + (in /usr/local/bin and /etc) + +start + Start the container '${CONTAINER_NAME}' + +REMOVAL: +uninstall + uninstall all needed files on the host to manage '${CONTAINER_NAME}' container + +stop + stop the container '${CONTAINER_NAME}' + +rm + delete the container '${CONTAINER_NAME}' + +rmcache + remove the container image in cache ${IMAGE} + +DEBUG: +run + podman run container '${CONTAINER_NAME}' + +bash + go with /bin/bash command inside '${CONTAINER_NAME}' + +logs + see log of container '${CONTAINER_NAME}' + + " + exit 1 +fi + +########### +# MAIN +########### +set -euxo pipefail + +case $1 in + start) + podman start ${CONTAINER_NAME} + podman ps | grep ${CONTAINER_NAME} + ;; + stop) + podman stop ${CONTAINER_NAME} + podman ps | grep ${CONTAINER_NAME} + ;; + rm) + set +e + podman stop ${CONTAINER_NAME} + podman rm ${CONTAINER_NAME} + ;; + create) + create_container + ;; + run) + run_container + ;; + rmcache) + podman rmi ${IMAGE} + ;; + logs) + podman logs ${CONTAINER_NAME} + ;; + bash) + set +e + podman exec -ti ${CONTAINER_NAME} $@ + ;; + install) + podman run --env IMAGE=${IMAGE} --rm --privileged -v /:/host ${IMAGE} /bin/bash /container/label-install + ;; + uninstall) + podman run --env IMAGE=${IMAGE} --rm --privileged -v /:/host ${IMAGE} /bin/bash /container/label-uninstall + ;; +esac diff --git a/container-scripts/wwctl b/container-scripts/wwctl old mode 100644 new mode 100755 index 42fbf644..1eab34b7 --- a/container-scripts/wwctl +++ b/container-scripts/wwctl @@ -1,7 +1,5 @@ #!/bin/bash -source /etc/warewulf-container.conf - set -euxo pipefail # Run the domain