Skip to content
Snippets Groups Projects
freedombone-image-make 10.7 KiB
Newer Older
Bob Mottram's avatar
Bob Mottram committed
#  _____               _           _
# |   __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# |   __|  _| -_| -_| . | . |     | . | . |   | -_|
# |__|  |_| |___|___|___|___|_|_|_|___|___|_|_|___|
Bob Mottram's avatar
Bob Mottram committed
#
Bob Mottram's avatar
Bob Mottram committed
#                              Freedom in the Cloud
Bob Mottram's avatar
Bob Mottram committed
#
Bob Mottram's avatar
Bob Mottram committed
# Based upon bin/mk-freedombox-image from freedom-maker
# With non-free stuff removed
#
Bob Mottram's avatar
Bob Mottram committed
# License
# =======
#
Bob Mottram's avatar
Bob Mottram committed
# This program is free software: you can redistribute it and/or modify
Bob Mottram's avatar
Bob Mottram committed
# it under the terms of the GNU Affero General Public License as published by
Bob Mottram's avatar
Bob Mottram committed
# 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
Bob Mottram's avatar
Bob Mottram committed
# GNU Affero General Public License for more details.
Bob Mottram's avatar
Bob Mottram committed
#
Bob Mottram's avatar
Bob Mottram committed
# You should have received a copy of the GNU Affero General Public License
Bob Mottram's avatar
Bob Mottram committed
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

set -e # Exit on first error

Bob Mottram's avatar
Bob Mottram committed
PROJECT_NAME='freedombone'

Bob Mottram's avatar
Bob Mottram committed
export TEXTDOMAIN=${PROJECT_NAME}-image-make
Bob Mottram's avatar
Bob Mottram committed
export TEXTDOMAINDIR="/usr/share/locale"

PROJECT_INSTALL_DIR=/usr/local/bin
Bob Mottram's avatar
Bob Mottram committed
if [ -f "/usr/bin/${PROJECT_NAME}" ]; then
Bob Mottram's avatar
Bob Mottram committed
    PROJECT_INSTALL_DIR=/usr/bin
Bob Mottram's avatar
Bob Mottram committed

Bob Mottram's avatar
Bob Mottram committed
source "/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-setup"
Bob Mottram's avatar
Bob Mottram committed
#set -x # Enable debugging

IMAGE=$1
export ARCHITECTURE
export MACHINE
export SOURCE
export SUITE
Bob Mottram's avatar
Bob Mottram committed
export MYUSERNAME
export MYPASSWORD
Bob Mottram's avatar
Bob Mottram committed
export ROUTER_IP_ADDRESS
export BOX_IP_ADDRESS
export NAMESERVER1
export NAMESERVER2
Bob Mottram's avatar
Bob Mottram committed
export NAMESERVER3
export NAMESERVER4
export NAMESERVER5
export NAMESERVER6
export PROJECT_NAME
Bob Mottram's avatar
Bob Mottram committed
export GENERIC_IMAGE
export SSH_PORT
export PROJECT_REPO
Bob Mottram's avatar
Bob Mottram committed
export DEBIAN_INSTALL_ONLY
export WIFI_INTERFACE
export WIFI_SSID
export WIFI_TYPE
export WIFI_PASSPHRASE
export WIFI_HOTSPOT
export WIFI_NETWORKS_FILE
export VARIANT
export MINIMUM_PASSWORD_LENGTH
Bob Mottram's avatar
Bob Mottram committed
export AMNESIC
export LOCAL_NAME
export EXTERNAL_DRIVE
export CONTINUOUS_INTEGRATION
export BUILD_LOG
Bob Mottram's avatar
Bob Mottram committed

# Locate vmdebootstrap program fetched in Makefile
basedir=`pwd`
vendor_dir="${basedir}/vendor"
vmdebootstrap_dir="${vendor_dir}/vmdebootstrap"

if [ -z "$MIRROR" ] || [ -z "$SUITE" ] ; then
Bob Mottram's avatar
Bob Mottram committed
    echo $"error: Missing MIRROR and SUITE settings inherited from Makefile."
    exit 1
Bob Mottram's avatar
Bob Mottram committed
fi

# Packages to install in all Freedombone environments
base_pkgs="apt base-files ifupdown initramfs-tools \
Bob Mottram's avatar
Bob Mottram committed
logrotate kmod netbase rsyslog udev debian-archive-keyring"
Bob Mottram's avatar
Bob Mottram committed

# Packages needed on the beaglebone
beaglebone_pkgs="linux-image-armmp u-boot-tools u-boot"

# Packages needed on rockchip boards (maybe in future debian versions)
rockchip_pkgs="linux-image-arm64 u-boot-tools u-boot u-boot-rockchip"
Bob Mottram's avatar
Bob Mottram committed
# Packages needed on the Allwinner A20 devices:
Bob Mottram's avatar
Bob Mottram committed
a20_pkgs="linux-image-armmp-lpae u-boot-tools u-boot u-boot-sunxi"
Bob Mottram's avatar
Bob Mottram committed

Bob Mottram's avatar
Bob Mottram committed
# Packages needed for self-hosted development
dev_pkgs="build-essential devscripts make man-db emacs org-mode git mercurial"

Bob Mottram's avatar
Bob Mottram committed
echo Building "$MACHINE" "$PROJECT_NAME" for "$ARCHITECTURE" "$EXTERNAL_DRIVE"
Bob Mottram's avatar
Bob Mottram committed

case "$MACHINE" in
Bob Mottram's avatar
Bob Mottram committed
        extra_pkgs="$beaglebone_pkgs"
        extra_opts="\
Bob Mottram's avatar
Bob Mottram committed
 --variant minbase \
 --bootoffset=2mib \
 --bootsize 128M \
 --boottype ext2 \
 --no-kernel \
 --no-extlinux \
 --foreign /usr/bin/qemu-arm-static \
 --roottype ext4 \
"
        ;;
    beaglebonegreen)
        extra_pkgs="$beaglebone_pkgs"
        extra_opts="\
 --variant minbase \
 --bootoffset=2mib \
 --bootsize 128M \
 --boottype ext2 \
 --no-kernel \
 --no-extlinux \
 --foreign /usr/bin/qemu-arm-static \
 --roottype ext4 \
"
        ;;
    beaglex15)
        extra_pkgs="$beaglebone_pkgs"
        extra_opts="\
 --variant minbase \
 --bootoffset=2mib \
 --bootsize 128M \
 --boottype ext2 \
 --no-kernel \
 --no-extlinux \
 --foreign /usr/bin/qemu-arm-static \
 --roottype ext4 \
Bob Mottram's avatar
Bob Mottram committed
"
Bob Mottram's avatar
Bob Mottram committed
        ;;
    cubietruck | a20-olinuxino-lime | a20-olinuxino-lime2 | a20-olinuxino-micro | cubieboard2 | pcduino3 | banana-pro)
Bob Mottram's avatar
Bob Mottram committed
        extra_pkgs="$a20_pkgs"
        extra_opts="\
 --variant minbase \
 --bootoffset=1mib \
 --bootsize 128M \
 --no-kernel \
 --no-extlinux \
 --foreign /usr/bin/qemu-arm-static \
 --roottype ext4 \
"
        ;;
    rock64)
        extra_pkgs="$rockchip_pkgs"
        extra_opts="\
 --variant minbase \
 --bootoffset=1mib \
 --bootsize 128M \
 --boottype vfat \
 --no-kernel \
 --no-extlinux \
 --foreign /usr/bin/qemu-arm-static \
 --roottype ext4 \
Bob Mottram's avatar
Bob Mottram committed
        ;;
    qemu)
        extra_opts="\
Bob Mottram's avatar
Bob Mottram committed
 --grub \
 --roottype ext4 \
Bob Mottram's avatar
Bob Mottram committed
    usb)
        extra_opts="\
 --roottype ext4 \
Bob Mottram's avatar
Bob Mottram committed
"   ;;
Bob Mottram's avatar
Bob Mottram committed
    all)
        extra_opts="\
Bob Mottram's avatar
Bob Mottram committed
 --grub \
Bob Mottram's avatar
Bob Mottram committed
"   ;;
Bob Mottram's avatar
Bob Mottram committed
esac

# allow for lots of extra fun customization options.
for customization in $CUSTOMIZATIONS
do
Bob Mottram's avatar
Bob Mottram committed
    case "$customization" in
        development)
            extra_pkgs="$extra_pkgs $dev_pkgs"
            ;;
    esac
Bob Mottram's avatar
Bob Mottram committed
done

for p in $base_pkgs $extra_pkgs; do
Bob Mottram's avatar
Bob Mottram committed
    pkgopts="$pkgopts --package $p"
Bob Mottram's avatar
Bob Mottram committed
done

# Make sure file is owned by current user, not root
Bob Mottram's avatar
Bob Mottram committed
touch "$(dirname "$IMAGE")/${PROJECT_NAME}.log"
Bob Mottram's avatar
Bob Mottram committed

if [ -x vendor/vmdebootstrap/vmdebootstrap ] ; then
Bob Mottram's avatar
Bob Mottram committed
    VMDEBOOTSTRAP=vendor/vmdebootstrap/vmdebootstrap
Bob Mottram's avatar
Bob Mottram committed
else
Bob Mottram's avatar
Bob Mottram committed
    VMDEBOOTSTRAP=vmdebootstrap
PROJECT_INSTALL_DIR=/usr/local/bin
Bob Mottram's avatar
Bob Mottram committed
if [ -f "/usr/bin/${PROJECT_NAME}" ]; then
Bob Mottram's avatar
Bob Mottram committed
    PROJECT_INSTALL_DIR=/usr/bin
echo $'Making customised customisation script'
Bob Mottram's avatar
Bob Mottram committed
TEMP_CUSTOMISE="/etc/${PROJECT_NAME}/image-customise"
TEMP_CUSTOMISE2="/tmp/${PROJECT_NAME}-image-customise2"
TEMP_CUSTOMISE3="/tmp/${PROJECT_NAME}-image-customise3"
TEMP_CUSTOMISE4="/tmp/${PROJECT_NAME}-image-customise4"

# cat all the things together
combine_all_scripts $TEMP_CUSTOMISE2
if [ ! -f $TEMP_CUSTOMISE2 ]; then
Bob Mottram's avatar
Bob Mottram committed
    echo $'Could not combine scripts'

echo $'Changing values within customised customisation script'
Bob Mottram's avatar
Bob Mottram committed
cp "$PROJECT_INSTALL_DIR/${PROJECT_NAME}-image-customise" "$TEMP_CUSTOMISE3"
if [ "$MYUSERNAME" ]; then
    sed -i "0,/MY_USERNAME=.*/s//MY_USERNAME=${MYUSERNAME}/" "$TEMP_CUSTOMISE3"
Bob Mottram's avatar
Bob Mottram committed
fi
Bob Mottram's avatar
Bob Mottram committed
if [ "$MYPASSWORD" ]; then
    sed -i "s|MY_PASSWORD=.*|MY_PASSWORD=${MYPASSWORD}|g" "$TEMP_CUSTOMISE3"
Bob Mottram's avatar
Bob Mottram committed
fi
Bob Mottram's avatar
Bob Mottram committed
sed -i "s|ROUTER_IP_ADDRESS=.*|ROUTER_IP_ADDRESS=${ROUTER_IP_ADDRESS}|g" "$TEMP_CUSTOMISE3"
sed -i "s|BOX_IP_ADDRESS=.*|BOX_IP_ADDRESS=${BOX_IP_ADDRESS}|g" "$TEMP_CUSTOMISE3"
sed -i "s|NAMESERVER1=.*|NAMESERVER1=${NAMESERVER1}|g" "$TEMP_CUSTOMISE3"
sed -i "s|NAMESERVER2=.*|NAMESERVER2=${NAMESERVER2}|g" "$TEMP_CUSTOMISE3"
sed -i "s|NAMESERVER3=.*|NAMESERVER3=${NAMESERVER3}|g" "$TEMP_CUSTOMISE3"
sed -i "s|NAMESERVER4=.*|NAMESERVER4=${NAMESERVER4}|g" "$TEMP_CUSTOMISE3"
sed -i "s|NAMESERVER5=.*|NAMESERVER5=${NAMESERVER5}|g" "$TEMP_CUSTOMISE3"
sed -i "s|NAMESERVER6=.*|NAMESERVER6=${NAMESERVER6}|g" "$TEMP_CUSTOMISE3"
sed -i "s|PROJECT_NAME=.*|PROJECT_NAME=${PROJECT_NAME}|g" "$TEMP_CUSTOMISE3"
sed -i "s|CONFIG_FILENAME=.*|CONFIG_FILENAME=${CONFIG_FILENAME}|g" "$TEMP_CUSTOMISE3"
sed -i "s|SSH_PUBKEY=.*|SSH_PUBKEY=${SSH_PUBKEY}|g" "$TEMP_CUSTOMISE3"
sed -i "s|GENERIC_IMAGE=.*|GENERIC_IMAGE=${GENERIC_IMAGE}|g" "$TEMP_CUSTOMISE3"
sed -i "s|MINIMAL_INSTALL=.*|MINIMAL_INSTALL=\"${MINIMAL_INSTALL}\"|g" "$TEMP_CUSTOMISE3"
sed -i "0,/SSH_PORT=.*/s//SSH_PORT=\"${SSH_PORT}\"/" "$TEMP_CUSTOMISE3"
Bob Mottram's avatar
Bob Mottram committed
sed -i "s|ONION_ONLY=.*|ONION_ONLY=\"${ONION_ONLY}\"|g" "$TEMP_CUSTOMISE3"
sed -i "s|PROJECT_REPO=.*|PROJECT_REPO=\"${PROJECT_REPO}\"|g" "$TEMP_CUSTOMISE3"
sed -i "s|DEBIAN_INSTALL_ONLY=.*|DEBIAN_INSTALL_ONLY=\"${DEBIAN_INSTALL_ONLY}\"|g" "$TEMP_CUSTOMISE3"
sed -i "s|WIFI_INTERFACE=.*|WIFI_INTERFACE=\"${WIFI_INTERFACE}\"|g" "$TEMP_CUSTOMISE3"
sed -i "s|WIFI_SSID=.*|WIFI_SSID=\"${WIFI_SSID}\"|g" "$TEMP_CUSTOMISE3"
sed -i "s|WIFI_TYPE=.*|WIFI_TYPE=\"${WIFI_TYPE}\"|g" "$TEMP_CUSTOMISE3"
sed -i "s|WIFI_PASSPHRASE=.*|WIFI_PASSPHRASE=\"${WIFI_PASSPHRASE}\"|g" "$TEMP_CUSTOMISE3"
sed -i "s|WIFI_HOTSPOT=.*|WIFI_HOTSPOT=\"${WIFI_HOTSPOT}\"|g" "$TEMP_CUSTOMISE3"
sed -i "s|WIFI_NETWORKS_FILE=.*|WIFI_NETWORKS_FILE=\"${WIFI_NETWORKS_FILE}\"|g" "$TEMP_CUSTOMISE3"
sed -i "s|VARIANT=.*|VARIANT=\"${VARIANT}\"|g" "$TEMP_CUSTOMISE3"
sed -i "s|MINIMUM_PASSWORD_LENGTH=.*|MINIMUM_PASSWORD_LENGTH=\"${MINIMUM_PASSWORD_LENGTH}\"|g" "$TEMP_CUSTOMISE3"
sed -i "s|INSECURE=.*|INSECURE=\"${INSECURE}\"|g" "$TEMP_CUSTOMISE3"
sed -i "s|AMNESIC=.*|AMNESIC=\"${AMNESIC}\"|g" "$TEMP_CUSTOMISE3"
sed -i "s|LOCAL_NAME=.*|LOCAL_NAME=\"${LOCAL_NAME}\"|g" "$TEMP_CUSTOMISE3"
sed -i "s|EXTERNAL_DRIVE=.*|EXTERNAL_DRIVE=\"${EXTERNAL_DRIVE}\"|g" "$TEMP_CUSTOMISE3"
Bob Mottram's avatar
Bob Mottram committed
sed -i 's|#!/bin/bash||g' "$TEMP_CUSTOMISE3"
sed -i "s|CONTINUOUS_INTEGRATION=.*|CONTINUOUS_INTEGRATION=${CONTINUOUS_INTEGRATION}|g" "$TEMP_CUSTOMISE3"
cat $TEMP_CUSTOMISE2 $TEMP_CUSTOMISE3 > $TEMP_CUSTOMISE4
if [ -f $TEMP_CUSTOMISE ]; then
Bob Mottram's avatar
Bob Mottram committed
    sudo rm $TEMP_CUSTOMISE
fi
sudo mv $TEMP_CUSTOMISE4 $TEMP_CUSTOMISE
rm $TEMP_CUSTOMISE2 $TEMP_CUSTOMISE3
if [ ! -f $TEMP_CUSTOMISE ]; then
Bob Mottram's avatar
Bob Mottram committed
    echo $'Customised customisation script could not be created'
Bob Mottram's avatar
Bob Mottram committed
fi
sudo chmod +x $TEMP_CUSTOMISE
echo $'Customised customisation script created'

if [ ! "$BUILD_LOG" ]; then
    BUILD_LOG="$(dirname "$IMAGE")/${PROJECT_NAME}.log"
fi
# append continuous integration option
Bob Mottram's avatar
Bob Mottram committed
if [[ "$CONTINUOUS_INTEGRATION" == 'y'* ]]; then
    extra_opts="$extra_opts\
 --continuous-integration"
fi

Bob Mottram's avatar
Bob Mottram committed
echo "sudo -H \
     SUITE=\"$SUITE\" \
     MIRROR=\"$MIRROR\" \
     BUILD_MIRROR=\"$BUILD_MIRROR\"\
     MACHINE=\"$MACHINE\" \
     ARCHITECTURE=\"$ARCHITECTURE\" \
     SOURCE=\"$SOURCE\" \
     CUSTOM_SETUP=\"$CUSTOM_SETUP\" \
     EXTERNAL_DRIVE=\"$EXTERNAL_DRIVE\" \
     CONTINUOUS_INTEGRATION=\"$CONTINUOUS_INTEGRATION\" \
     $VMDEBOOTSTRAP \
     --log \"$BUILD_LOG\" \
     --log-level debug \
     --size \"$IMAGE_SIZE\" \
     --image \"$IMAGE.img\" \
     --hostname ${PROJECT_NAME} \
     --verbose \
     --mirror \"$BUILD_MIRROR\" \
     --customize \"$TEMP_CUSTOMISE\" \
     --lock-root-password \
     --arch \"$ARCHITECTURE\" \
     --distribution \"$SUITE\" \
     $extra_opts \
     $pkgopts"

echo $"starting $VMDEBOOTSTRAP"
Bob Mottram's avatar
Bob Mottram committed

Bob Mottram's avatar
Bob Mottram committed
# Run vmdebootstrap script to create image
Bob Mottram's avatar
Bob Mottram committed
vmdebootstrap_failed=
Bob Mottram's avatar
Bob Mottram committed
# shellcheck disable=SC2086
Bob Mottram's avatar
Bob Mottram committed
sudo -H \
Bob Mottram's avatar
Bob Mottram committed
     SUITE="$SUITE" \
     MIRROR="$MIRROR" \
     BUILD_MIRROR="$BUILD_MIRROR"\
     MACHINE="$MACHINE" \
     ARCHITECTURE="$ARCHITECTURE" \
     SOURCE="$SOURCE" \
     CUSTOM_SETUP="$CUSTOM_SETUP" \
Bob Mottram's avatar
Bob Mottram committed
     EXTERNAL_DRIVE="$EXTERNAL_DRIVE" \
     CONTINUOUS_INTEGRATION="$CONTINUOUS_INTEGRATION" \
Bob Mottram's avatar
Bob Mottram committed
     $VMDEBOOTSTRAP \
     --log "$BUILD_LOG" \
Bob Mottram's avatar
Bob Mottram committed
     --log-level debug \
Bob Mottram's avatar
Bob Mottram committed
     --size "$IMAGE_SIZE" \
     --image "$IMAGE.img" \
Bob Mottram's avatar
Bob Mottram committed
     --hostname ${PROJECT_NAME} \
     --verbose \
Bob Mottram's avatar
Bob Mottram committed
     --mirror "$BUILD_MIRROR" \
Bob Mottram's avatar
Bob Mottram committed
     --customize "$TEMP_CUSTOMISE" \
     --lock-root-password \
Bob Mottram's avatar
Bob Mottram committed
     --arch "$ARCHITECTURE" \
     --distribution "$SUITE" \
Bob Mottram's avatar
Bob Mottram committed
     $extra_opts \
     $pkgopts
echo $'Removing customised customisation script'
Bob Mottram's avatar
Bob Mottram committed
sudo rm $TEMP_CUSTOMISE