Skip to content

Commit

Permalink
Merge pull request #2 from gunet/non-interactive
Browse files Browse the repository at this point in the history
update: support non-interactive setup
  • Loading branch information
konkourgr authored Dec 12, 2023
2 parents f768d0f + 0078758 commit fc64eb5
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 8 deletions.
28 changes: 27 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ ARG DEBIAN_REPO=https://cdimage.debian.org/mirror/cdimage/archive/${DEBIAN_VERSI
ARG DEBIAN_ISO=debian-${DEBIAN_VERSION}-amd64-netinst.iso

RUN curl -L ${DEBIAN_REPO}/${DEBIAN_ISO} > ${JEOS_DIR}/debian/${DEBIAN_ISO}
COPY mkiso.sh ${JEOS_DIR}/
COPY mkiso.sh helper_functions.sh ${JEOS_DIR}/
COPY gunet/ ${JEOS_DIR}/gunet/

RUN chmod 0755 ${JEOS_DIR}/mkiso.sh && \
Expand All @@ -35,6 +35,32 @@ WORKDIR ${JEOS_DIR}
ENV TZ=Europe/Athens
ENV DEBIAN_ISO=${DEBIAN_ISO}

# Network settings which can be passed along in the command-line if DHCP does not return something
# NET_IP: CIDR format for the IP
# NET_GATEWAY: The gateway to setup
# NET_NAMESERVERS: a list of nameservers (separated by space)
# NET_HOSTNAME: The hostname
# NET_DOMAIN: The domain
#
# Actual working example:
# ENV NET_IP="195.134.100.24/24"
# ENV NET_GATEWAY="195.134.100.1"
# ENV NET_NAMESERVERS="8.8.8.8 4.4.4.4"
# ENV NET_HOSTNAME="sso.gunet.gr"
# ENV NET_DOMAIN="gunet.gr"

ENV NET_STATIC="no"
ENV NET_IP="notset"
ENV NET_GATEWAY="notset"
ENV NET_NAMESERVERS="notset"
ENV NET_HOSTNAME="notset"
ENV NET_DOMAIN="notset"

# We can accept the root password from the command-line as an environment
# variable

ENV ROOT_PASSWORD="notset"

ENTRYPOINT [ "/var/jeos/mkiso.sh" ]

CMD ["${DEBIAN_ISO}"]
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ the Docker container has finished.
The recommened way to run the container is:
`docker run --rm -v ${PWD}/final:/var/jeos/final --privileged ghcr.io/gunet/jeos-builder:<version>`

### Environment variables
The following environment variables are available. For network configuration, the general path is to use DHCP provided ones and *only* if these are not available, then use the ones in environment variables (if they are provided):
* `NET_IP`: The static IP in CIDR form (ie `192.168.2.1/24`).
* `NET_GATEWAY`: The gateway IP. Only if IP has already been passed.
* `NET_NAMESERVERS`: Nameserver IPs to use, separated by space (ie `8.8.8.8 4.4.4.4`)
* `NET_HOSTNAME`: The hostname to set (ie `sso.gunet.gr`)
* `NET_DOMAIN`: The domain to set (ie `gunet.gr`)
* `NET_STATIC`: If set to `yes` then we only perform static network configuration and **all** the above variables **must** be set
* `ROOT_PASSWORD`: The (plaintext) root password

### Available versions
* `latest`: `11.8.0`

Expand All @@ -22,10 +32,6 @@ In order to produce a Just Enough Operating System iso image, we need to run the
The `<debian_image.iso>` file is a Debian ISO file from the Debian project. An archive of ISO images for previous
Debian versions can be found [here](https://cdimage.debian.org/mirror/cdimage/archive/)

***Notes***:
* The script will produce the ***JeOS iso file*** _gunet-jeos.iso_ into the ***Working Directory***.
* We must run the script with ***sudo*** privileges.

### Configuration
The produced .iso file installs a Debian OS, by requesting only the root password and the network configuration paramenters, in case DHCP fails, during the installation. All the configuration must be located into _gunet/_ folder. In the current configuration, _gunet/_ folder contains the follwing:
* <ins>_preseed.cfg_</ins>: This file contains all the configuration of d-i installer that automates the installation procedure. The parameters are set to produce an as minimal as possible installation. During the _late_command_ step, we add further configuration and run scripts that we want to include in the installation procedure.
Expand Down
26 changes: 25 additions & 1 deletion gunet/preseed.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,38 @@ d-i keyboard-configuration/xkb-keymap select us

d-i netcfg/choose_interface select auto

## Network setup
#
# Use DHCP, otherwise manual setup
#
# Unless we have been configured to only make static network config
#STATIC#d-i netcfg/disable_autoconfig boolean true

d-i netcfg/dhcp_failed note
d-i netcfg/dhcp_options select Configure network manually

# But if we are passed the IP/gateway configuration then we set it up statically, *only* if DHCP does not work

#NET#d-i netcfg/get_ipaddress string __IP__
#NET#d-i netcfg/get_netmask string __NETMASK__
#NET#d-i netcfg/get_gateway string __GATEWAY__
#NET#d-i netcfg/get_nameservers string __NAMESERVERS__
#NET#d-i netcfg/confirm_static boolean true

# The same about hostname and domain (if DHCP returns values use them, otherwise use static configuration)
#HOST#d-i netcfg/get_hostname string __HOSTNAME__
#HOST#d-i netcfg/get_domain string __DOMAIN__
#STATIC#d-i netcfg/hostname string __HOSTNAME__

d-i apt-setup/use_mirror boolean false

## Account setup
d-i passwd/make-user boolean false

## Root password if it is passed in the command-line
#ROOT#d-i passwd/root-password password __ROOT_PASSWORD__
#ROOT#d-i passwd/root-password-again password __ROOT_PASSWORD__

d-i clock-setup/utc boolean false

d-i clock-setup/ntp boolean true
Expand Down Expand Up @@ -47,7 +71,7 @@ popularity-contest popularity-contest/participate boolean false

tasksel tasksel/first multiselect minimal

d-i grub-installer/bootdev string /dev/sda
d-i grub-installer/bootdev string default

d-i finish-install/reboot_in_progress note

Expand Down
67 changes: 67 additions & 0 deletions helper_functions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/bin/bash

# converts IPv4 as "A.B.C.D" to integer
# ip4_to_int 192.168.0.1
# => 3232235521
ip4_to_int() {
IFS=. read -r i j k l <<EOF
$1
EOF
echo $(( (i << 24) + (j << 16) + (k << 8) + l ))
}

# converts interger to IPv4 as "A.B.C.D"
#
# int_to_ip4 3232235521
# => 192.168.0.1
int_to_ip4() {
echo "$(( ($1 >> 24) % 256 )).$(( ($1 >> 16) % 256 )).$(( ($1 >> 8) % 256 )).$(( $1 % 256 ))"
}

# returns the ip part of an CIDR
#
# cidr_ip "172.16.0.10/22"
# => 172.16.0.10
cidr_ip() {
IFS=/ read -r ip _ <<EOF
$1
EOF
echo $ip
}

# returns the prefix part of an CIDR
#
# cidr_prefix "172.16.0.10/22"
# => 22
cidr_prefix() {
IFS=/ read -r _ prefix <<EOF
$1
EOF
echo $prefix
}

# returns net mask in numeric format from prefix size
#
# netmask_of_prefix 8
# => 4278190080
int_netmask_of_prefix() {
netmask_int=$((4294967295 ^ (1 << (32 - $1)) - 1))
echo $netmask_int
}

# returns net mask in IPv4 format from prefix size
#
# netmask_of_prefix 24
# => 255.255.255.0
netmask_of_prefix() {
netmask_int=$(int_netmask_of_prefix $1)
netmask=$(int_to_ip4 $netmask_int)
echo $netmask
}

# IP=$1
# echo "IP/net is ${IP}"
# echo "IP is $(cidr_ip ${IP})"
# prefix=$(cidr_prefix ${IP})
# echo "Prefix is ${prefix}"
# echo "Netmask is $(netmask_of_prefix ${prefix})"
63 changes: 61 additions & 2 deletions mkiso.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#!/bin/bash
# Include the helper functions
source $(dirname "$0")/helper_functions.sh

set -e
set -u

Expand All @@ -22,6 +25,61 @@ ISODIR=${PROJECT_DIR}/isofiles
ISODIR_WRITE=${ISODIR}-rw
PRESEED_DIR=${PROJECT_DIR}/gunet

# check for environment variables
if [[ ${NET_STATIC} == "yes" ]]; then
if [[ ${NET_IP} == "notset" || ${NET_GATEWAY} == "notset" || \
${NET_NAMESERVERS} == "notset" || ${NET_HOSTNAME} == "notset" || \
${NET_DOMAIN} == "notset" ]]; then
echo "Environment variable NET_STATIC is yes but some NET_* variables are not set!"
exit 1
fi
sed -i'' -e "s/^#STATIC#//g" ${PRESEED_DIR}/preseed.cfg
fi
if [[ ${NET_IP} != "notset" ]]; then
if [[ ${NET_GATEWAY} == "notset" || ${NET_NAMESERVERS} == "notset" ]]; then
echo "Environment variable NET_IP is set but NET_GATEWAY or NET_NAMESERVERS are not!"
exit 1
fi
NET_IP_PLAIN=$(cidr_ip ${NET_IP})
NET_PREFIX=$(cidr_prefix ${NET_IP})
if [[ ${NET_PREFIX} == "" ]]; then
echo "NET_IP should be of CIDR form"
exit 1
fi
NET_NETMASK=$(netmask_of_prefix ${NET_PREFIX})

echo "Network configuration:"
echo "IP (CIDR): ${NET_IP}"
echo "IP (plain): ${NET_IP_PLAIN}"
echo "IP Prefix: ${NET_PREFIX}"
echo "Netmask: ${NET_NETMASK}"
echo "IP gateway: ${NET_GATEWAY}"
echo "Nameservers: ${NET_NAMESERVERS}"
echo "-------------------------------"

sed -i'' -e "s/^#NET#//g" -e "s/__IP__/${NET_IP_PLAIN}/" -e "s/__NETMASK__/${NET_NETMASK}/" \
-e "s/__GATEWAY__/${NET_GATEWAY}/" -e "s/__NAMESERVERS__/${NET_NAMESERVERS}/" ${PRESEED_DIR}/preseed.cfg
fi

if [[ ${NET_HOSTNAME} != "notset" ]]; then
if [[ ${NET_DOMAIN} == "notset" ]]; then
echo "Environment variable NET_HOSTNAME is set but NET_DOMAIN is not!"
exit 1
fi
echo "Hostname configuration:"
echo "Hostname: ${NET_HOSTNAME}"
echo "Domain: ${NET_DOMAIN}"
echo "----------------------------"

sed -i'' -e "s/^#HOST#//g" \
-e "s/__HOSTNAME__/${NET_HOSTNAME}/" -e "s/__DOMAIN__/${NET_DOMAIN}/" ${PRESEED_DIR}/preseed.cfg
fi

if [[ ${ROOT_PASSWORD} != "notset" ]]; then
echo "Root passwd: ${ROOT_PASSWORD}"
sed -i'' -e "s/^#ROOT#//g" -e "s/__ROOT_PASSWORD__/${ROOT_PASSWORD}/" ${PRESEED_DIR}/preseed.cfg
fi

sed -i "s/^M//" $PRESEED_DIR/custom_script.sh

echo 'mounting ISO9660 filesystem...'
Expand All @@ -32,7 +90,7 @@ mount -o loop $ISOFILE $ISODIR
echo 'copying to writable dir...'
rm -rf $ISODIR_WRITE || true
[ -d $ISODIR_WRITE ] || mkdir -p $ISODIR_WRITE
rsync -a -H --exclude=TRANS.TBL $ISODIR/ $ISODIR_WRITE
rsync --info=progress2 -a -H --exclude=TRANS.TBL $ISODIR/ $ISODIR_WRITE
echo 'unmount iso dir'
umount $ISODIR

Expand All @@ -45,6 +103,7 @@ cp -r $PRESEED_DIR/ $ISODIR_WRITE/
echo 'edit isolinux/txt.cfg...'
sed 's/initrd.gz/initrd.gz file=\/cdrom\/gunet\/preseed.cfg/' -i $ISODIR_WRITE/isolinux/txt.cfg

echo 'creating initrd.gz..'
mkdir -p irmod
cd irmod
gzip -d < $ISODIR_WRITE/install.amd/initrd.gz | \
Expand All @@ -66,7 +125,7 @@ popd
echo 'making ISO...'
genisoimage -o $ISOFILE_FINAL \
-r -J -no-emul-boot -boot-load-size 4 \
-boot-info-table \
-boot-info-table -quiet \
-b isolinux/isolinux.bin \
-c isolinux/boot.cat $ISODIR_WRITE

Expand Down

0 comments on commit fc64eb5

Please sign in to comment.