From c0060c31fdf1c2af1454c5877c311b6680618084 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Thu, 19 Aug 2021 09:50:51 +0200 Subject: Refactor debootstrap create script (FIXME WIP). Signed-off-by: Daniel Baumann --- Makefile | 4 +- share/create-scripts/bootstrap | 179 +++++++++++++++++++++ share/create-scripts/debootstrap | 178 -------------------- .../create-scripts/parts/chroot_clean-apt-archives | 23 +++ .../parts/chroot_config-root-password | 33 ++++ .../parts/chroot_config-system-hostname | 23 +++ 6 files changed, 260 insertions(+), 180 deletions(-) create mode 100755 share/create-scripts/bootstrap delete mode 100755 share/create-scripts/debootstrap create mode 100755 share/create-scripts/parts/chroot_clean-apt-archives create mode 100755 share/create-scripts/parts/chroot_config-root-password create mode 100755 share/create-scripts/parts/chroot_config-system-hostname diff --git a/Makefile b/Makefile index d35156c..f1dc5bf 100644 --- a/Makefile +++ b/Makefile @@ -123,7 +123,7 @@ install: build ln -sf container-build-debconf.1 $(DESTDIR)/usr/share/man/man1/container-build-$${MODE}.1; \ done - for BOOTSTRAP in mmdebstrap; \ + for BOOTSTRAP in debootstrap cdebootstrap mmdebstrap; \ do \ ln -sf bootstrap $(DESTDIR)/usr/share/$(SOFTWARE)/build-scripts/$${BOOTSTRAP}; \ ln -sf container-build-bootstrap.1 $(DESTDIR)/usr/share/man/man1/container-build-$${BOOTSTRAP}.1; \ @@ -174,7 +174,7 @@ uninstall: rm -f $(DESTDIR)/usr/share/man/man1/container-build-$${MODE}.1; \ done - for BOOTSTRAP in mmdebstrap; \ + for BOOTSTRAP in debootstrap cdebootstrap mmdebstrap; \ do \ rm -f $(DESTDIR)/usr/share/man/man1/container-build-$${BOOTSTRAP}.1; \ done diff --git a/share/create-scripts/bootstrap b/share/create-scripts/bootstrap new file mode 100755 index 0000000..a332d4d --- /dev/null +++ b/share/create-scripts/bootstrap @@ -0,0 +1,179 @@ +#!/bin/sh + +# Copyright (C) 2014-2021 Daniel Baumann +# +# SPDX-License-Identifier: GPL-3.0+ +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +set -e + +PROJECT="open-infrastructure" +SOFTWARE="compute-tools" +PROGRAM="container" + +SCRIPT="$(basename ${0})" +DIRECTORY="$(dirname ${0})" + +HOOKS="/etc/${SOFTWARE}/hooks" +MACHINES="/var/lib/machines" + +Parameters () +{ + GETOPT_LONGOPTIONS="name:,architecture:,distribution:,mirror:,password:" + GETOPT_OPTIONS="n:,a:,d:,m:,p:" + + PARAMETERS="$(getopt --longoptions ${GETOPT_LONGOPTIONS} --name=${SCRIPT} --options ${GETOPT_OPTIONS} --shell sh -- ${@})" + + if [ "${?}" != "0" ] + then + echo "'${SCRIPT}': getopt exit" >&2 + exit 1 + fi + + eval set -- "${PARAMETERS}" + + while true + do + case "${1}" in + -n|--name) + NAME="${2}" + shift 2 + ;; + + -a|--architecture) + ARCHITECTURE="${2}" + shift 2 + ;; + + -d|--distribution) + DISTRIBUTION="${2}" + shift 2 + ;; + + -m|--mirror) + MIRROR="${2}" + shift 2 + ;; + + -p|--password) + PASSWORD="${2}" + shift 2 + ;; + + --) + shift 1 + break + ;; + + *) + echo "'${SCRIPT}': getopt error" >&2 + exit 1 + ;; + esac + done +} + +Usage () +{ + echo "Usage: container build -n|--name NAME -s|--script ${SCRIPT} -- [-a|--architecture ARCHITECTURE] [-d|--distribution DISTRIBUTION] [-m|--mirror MIRROR] [-p|--password PASSWORD}" >&2 + exit 1 +} + +Parameters "${@}" + +if [ -z "${NAME}" ] +then + Usage +fi + +if [ -e "${MACHINES}/${NAME}" ] +then + echo "'${NAME}': container already exists" >&2 + exit 1 +fi + +case "${SCRIPT}" in + cdebootstrap) + BOOTSTRAP="/usr/bin/cdebootstrap" + BOOTSTRAP_OPTIONS="" + ;; + + mmdebstrap) + BOOTSTRAP="/usr/bin/mmdebstrap" + BOOTSTRAP_OPTIONS="--mode=root" + ;; + + *) + BOOTSTRAP="/usr/sbin/debootstrap" + BOOTSTRAP_OPTIONS="" + ;; +esac + +if [ ! -x "${BOOTSTRAP}" ] +then + echo "'${NAME}': ${BOOTSTRAP} - no such file." >&2 + exit 1 +fi + +if [ "$(id -u)" -ne 0 ] +then + echo "'${NAME}': need root privileges" >&2 + exit 1 +fi + +ARCHITECTURE="${ARCHITECTURE:-$(dpkg --print-architecture)}" +DISTRIBUTION="${DISTRIBUTION:-bullseye}" +MIRROR="${MIRROR:-https://deb.debian.org/debian}" + +INCLUDE="dbus" + +case "${MIRROR}" in + https*) + INCLUDE="${INCLUDE},apt-transport-https,ca-certificates" + ;; +esac + +# Pre hooks +for FILE in "${HOOKS}/pre-${SCRIPT}".* "${HOOKS}/${NAME}.pre-${SCRIPT}" +do + if [ -x "${FILE}" ] + then + "${FILE}" + fi +done + +# Run +mkdir -p "${MACHINES}" +${BOOTSTRAP} ${BOOTSTRAP_OPTIONS} --arch=${ARCHITECTURE} --include=${INCLUDE} ${DISTRIBUTION} ${MACHINES}/${NAME} ${MIRROR} + +SYSTEM_NAME="${NAME}" +SYSTEM_PATH="${MACHINES}/${NAME}" +SYSTEM_ROOT_PASSWORD="${PASSWORD}" + +export SYSTEM_NAME SYSTEM_PATH SYSTEM_ROOT_PASSWORD + +# Customization +"${DIRECTORY}"/parts/chroot_clean-apt-archives +"${DIRECTORY}"/parts/chroot_config-root-password +"${DIRECTORY}"/parts/chroot_config-system-hostname + +# Post hooks +for FILE in "${HOOKS}/post-${SCRIPT}".* "${HOOKS}/${NAME}.post-${SCRIPT}" +do + if [ -x "${FILE}" ] + then + "${FILE}" + fi +done diff --git a/share/create-scripts/debootstrap b/share/create-scripts/debootstrap deleted file mode 100755 index bef75b1..0000000 --- a/share/create-scripts/debootstrap +++ /dev/null @@ -1,178 +0,0 @@ -#!/bin/sh - -# Copyright (C) 2014-2021 Daniel Baumann -# -# SPDX-License-Identifier: GPL-3.0+ -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - -set -e - -PROJECT="open-infrastructure" -SOFTWARE="compute-tools" -PROGRAM="container" - -SCRIPT="$(basename ${0})" - -HOOKS="/etc/${SOFTWARE}/hooks" -MACHINES="/var/lib/machines" - -Parameters () -{ - GETOPT_LONGOPTIONS="name:,architecture:,distribution:,mirror:,password:" - GETOPT_OPTIONS="n:,a:,d:,m:,p:" - - PARAMETERS="$(getopt --longoptions ${GETOPT_LONGOPTIONS} --name=${SCRIPT} --options ${GETOPT_OPTIONS} --shell sh -- ${@})" - - if [ "${?}" != "0" ] - then - echo "'${SCRIPT}': getopt exit" >&2 - exit 1 - fi - - eval set -- "${PARAMETERS}" - - while true - do - case "${1}" in - -n|--name) - NAME="${2}" - shift 2 - ;; - - -a|--architecture) - ARCHITECTURE="${2}" - shift 2 - ;; - - -d|--distribution) - DISTRIBUTION="${2}" - shift 2 - ;; - - -m|--mirror) - MIRROR="${2}" - shift 2 - ;; - - -p|--password) - PASSWORD="${2}" - shift 2 - ;; - - --) - shift 1 - break - ;; - - *) - echo "'${SCRIPT}': getopt error" >&2 - exit 1 - ;; - esac - done -} - -Usage () -{ - echo "Usage: container build -n|--name NAME -s|--script ${SCRIPT} -- [-a|--architecture ARCHITECTURE] [-d|--distribution DISTRIBUTION] [-m|--mirror MIRROR] [-p|--password PASSWORD}" >&2 - exit 1 -} - -Parameters "${@}" - -if [ -z "${NAME}" ] -then - Usage -fi - -if [ -e "${MACHINES}/${NAME}" ] -then - echo "'${NAME}': container already exists" >&2 - exit 1 -fi - -case "${SCRIPT}" in - cdebootstrap) - BOOTSTRAP="/usr/bin/cdebootstrap" - BOOTSTRAP_OPTIONS="" - ;; - - debootstrap) - BOOTSTRAP="/usr/sbin/debootstrap" - BOOTSTRAP_OPTIONS="" - ;; - - mmdebstrap) - BOOTSTRAP="/usr/bin/mmdebstrap" - BOOTSTRAP_OPTIONS="--mode=root" - ;; -esac - -if [ ! -x "${BOOTSTRAP}" ] -then - echo "'${NAME}': ${BOOTSTRAP} - no such file." >&2 - exit 1 -fi - -if [ "$(id -u)" -ne 0 ] -then - echo "'${NAME}': need root privileges" >&2 - exit 1 -fi - -ARCHITECTURE="${ARCHITECTURE:-$(dpkg --print-architecture)}" -DISTRIBUTION="${DISTRIBUTION:-bullseye}" -MIRROR="${MIRROR:-https://deb.debian.org/debian}" -PASSWORD="${PASSWORD:-$(dd if=/dev/urandom bs=12 count=1 2> /dev/null | base64)}" - -INCLUDE="dbus" - -case "${MIRROR}" in - https*) - INCLUDE="${INCLUDE},apt-transport-https,ca-certificates" - ;; -esac - -# Pre hooks -for FILE in "${HOOKS}/pre-${SCRIPT}".* "${HOOKS}/${NAME}.pre-${SCRIPT}" -do - if [ -x "${FILE}" ] - then - "${FILE}" - fi -done - -# Run -mkdir -p "${MACHINES}" -${BOOTSTRAP} ${BOOTSTRAP_OPTIONS} --arch=${ARCHITECTURE} --include=${INCLUDE} ${DISTRIBUTION} ${MACHINES}/${NAME} ${MIRROR} - -# Cleaning apt cache -chroot "${MACHINES}/${NAME}" apt clean - -# Setting hostname -echo "${NAME}" > "${MACHINES}/${NAME}/etc/hostname" - -# Setting root password -echo root:${PASSWORD} | chroot "${MACHINES}/${NAME}" chpasswd -echo "${NAME}: root password set to '${PASSWORD}'." - -# Post hooks -for FILE in "${HOOKS}/post-${SCRIPT}".* "${HOOKS}/${NAME}.post-${SCRIPT}" -do - if [ -x "${FILE}" ] - then - "${FILE}" - fi -done diff --git a/share/create-scripts/parts/chroot_clean-apt-archives b/share/create-scripts/parts/chroot_clean-apt-archives new file mode 100755 index 0000000..5c86cf0 --- /dev/null +++ b/share/create-scripts/parts/chroot_clean-apt-archives @@ -0,0 +1,23 @@ +#!/bin/sh + +# Copyright (C) 2014-2021 Daniel Baumann +# +# SPDX-License-Identifier: GPL-3.0+ +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +set -e + +echo "${SYSTEM_NAME}: cleaning apt archives" +chroot "${SYSTEM_PATH}" apt-get clean diff --git a/share/create-scripts/parts/chroot_config-root-password b/share/create-scripts/parts/chroot_config-root-password new file mode 100755 index 0000000..4868ccd --- /dev/null +++ b/share/create-scripts/parts/chroot_config-root-password @@ -0,0 +1,33 @@ +#!/bin/sh + +# Copyright (C) 2014-2021 Daniel Baumann +# +# SPDX-License-Identifier: GPL-3.0+ +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +set -e + +if [ -e "${SYSTEM_ROOT_PASSWORD}" ] +then + SYSTEM_ROOT_PASSWORD="$(head -n1 ${SYSTEM_ROOT_PASSWORD})" +fi + +if [ -z "${SYSTEM_ROOT_PASSWORD}" ] +then + SYSTEM_ROOT_PASSWORD="$(dd if=/dev/urandom bs=12 count=1 2> /dev/null | base64)" +fi + +echo "${SYSTEM_NAME}: setting root password to '${SYSTEM_ROOT_PASSWORD}'" +echo root:${SYSTEM_ROOT_PASSWORD} | chroot "${SYSTEM_PATH}" chpasswd diff --git a/share/create-scripts/parts/chroot_config-system-hostname b/share/create-scripts/parts/chroot_config-system-hostname new file mode 100755 index 0000000..7580f45 --- /dev/null +++ b/share/create-scripts/parts/chroot_config-system-hostname @@ -0,0 +1,23 @@ +#!/bin/sh + +# Copyright (C) 2014-2021 Daniel Baumann +# +# SPDX-License-Identifier: GPL-3.0+ +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +set -e + +echo "${SYSTEM_NAME}: setting hostname" +echo "${SYSTEM_NAME}" > "${SYSTEM_PATH}/etc/hostname" -- cgit v1.2.3