freedombone/src/freedombone-image

388 lines
9.6 KiB
Plaintext
Raw Normal View History

#!/bin/bash
#
# .---. . .
# | | |
# |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
# | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
# ' ' --' --' -' - -' ' ' -' -' -' ' - --'
#
# Freedom in the Cloud
#
# Creates a debian image using vmdebootstrap
#
# License
# =======
#
2016-01-02 22:58:27 +01:00
# Copyright (C) 2015-2016 Bob Mottram <bob@robotics.uk.to>
#
# This program is free software: you can redistribute it and/or modify
2016-02-13 23:09:27 +01:00
# it under the terms of the GNU Affero 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
2016-02-13 23:09:27 +01:00
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
2016-02-13 23:09:27 +01:00
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
PROJECT_NAME='freedombone'
2015-11-27 12:42:16 +01:00
2015-11-27 17:52:23 +01:00
export TEXTDOMAIN=${PROJECT_NAME}-image
2015-11-27 12:42:16 +01:00
export TEXTDOMAINDIR="/usr/share/locale"
PROJECT_REPO="https://github.com/bashrc/${PROJECT_NAME}"
2016-01-05 15:17:49 +01:00
# recommended RAM for virtual machines
VM_MEMORY='1G'
2015-12-24 20:04:17 +01:00
VARIANT='full'
# fixed username and password when the --generic option is used
GENERIC_IMAGE_USERNAME='fbone'
GENERIC_IMAGE_PASSWORD='freedombone'
2015-11-20 22:43:03 +01:00
IMAGE_TYPE='beaglebone'
CURR_DIR=$(pwd)
2015-11-22 00:27:17 +01:00
CURR_USER=$(echo $USER)
BUILD_DIR=~/.tmp_${PROJECT_NAME}_build
2015-11-20 22:43:03 +01:00
VMDEBOOTSTRAP_REPO=git://git.liw.fi/vmdebootstrap
VMDEBOOTSTRAP_VERSION=0.8
MAKEFILE=${PROJECT_NAME}-image-makefile
2015-11-21 14:16:49 +01:00
IMAGE_SIZE=4G
2016-01-05 22:19:02 +01:00
IMAGE_NAME='full'
2015-11-20 22:43:03 +01:00
USERNAME=$(echo $USER)
PASSWORD=
2015-11-20 22:43:03 +01:00
# IP address of the router (gateway)
ROUTER_IP_ADDRESS="192.168.1.254"
# The fixed IP address of the Beaglebone Black (or other SBC) on your local network
BOX_IP_ADDRESS="192.168.1.55"
# DNS
NAMESERVER1='213.73.91.35'
NAMESERVER2='85.214.20.141'
# An optional freedombone configuration file
CONFIG_FILENAME=
DEFAULT_DOMAIN_NAME="${PROJECT_NAME}.local"
2015-11-21 15:11:34 +01:00
# Minimum number of characters in a password
MINIMUM_PASSWORD_LENGTH=10
# Optional ssh public key to allow
SSH_PUBKEY="no"
2015-11-21 16:44:59 +01:00
# interactive mode
INTERACTIVE="no"
2015-11-23 11:33:45 +01:00
# Whether this is a generic image for mass redistribution on the interwebs
GENERIC_IMAGE="yes"
2015-11-23 11:33:45 +01:00
# Whether to reduce the number of decisions during interactive install
MINIMAL_INSTALL="yes"
# default SSH port
SSH_PORT=2222
# Whether sites are accessible only within a Tor browser
ONION_ONLY="no"
# Where to fetch packages
MIRROR='http://httpredir.debian.org/debian'
2015-12-24 20:04:17 +01:00
mesh_router_setup_script() {
# create a setup script for a mesh router
mesh_script_filename=$1
echo "MY_USERNAME=${USERNAME}" > $mesh_script_filename
echo "DEFAULT_DOMAIN_NAME=${USERNAME}" >> $mesh_script_filename
echo 'SYSTEM_TYPE=mesh' >> $mesh_script_filename
echo 'INSTALLING_ON_BBB=no' >> $mesh_script_filename
2016-01-11 18:18:41 +01:00
echo 'USB_DRIVE=/dev/sda1' >> $mesh_script_filename
2015-12-24 20:04:17 +01:00
echo 'DDNS_PROVIDER=' >> $mesh_script_filename
echo 'DDNS_USERNAME=' >> $mesh_script_filename
echo 'DDNS_PASSWORD=' >> $mesh_script_filename
echo 'DEFAULT_LANGUAGE=en_GB.UTF-8' >> $mesh_script_filename
echo 'MY_EMAIL_ADDRESS=' >> $mesh_script_filename
echo 'ENABLE_CJDNS=no' >> $mesh_script_filename
echo 'ENABLE_BATMAN=yes' >> $mesh_script_filename
echo 'ENABLE_BABEL=no' >> $mesh_script_filename
echo 'DEBIAN_REPO=' >> $mesh_script_filename
echo 'NAMESERVER1=' >> $mesh_script_filename
echo 'NAMESERVER2=' >> $mesh_script_filename
echo 'BATMAN_CELLID=any' >> $mesh_script_filename
echo 'WIFI_CHANNEL=9' >> $mesh_script_filename
}
2015-11-20 22:43:03 +01:00
while [[ $# > 1 ]]
do
key="$1"
case $key in
-h|--help)
show_help
;;
-c|--config)
shift
CONFIG_FILENAME="$1"
if [ ! -f $CONFIG_FILENAME ]; then
2015-11-27 16:29:43 +01:00
echo $"Config file $CONFIG_FILENAME not found"
exit 3
fi
DEFAULT_DOMAIN_NAME=$(cat $CONFIG_FILENAME | grep 'DEFAULT_DOMAIN_NAME' | awk -F '=' '{print $2}')
;;
2015-11-20 22:43:03 +01:00
-t|--target|--board)
shift
IMAGE_TYPE="$1"
;;
-u|--user|--username)
shift
USERNAME="$1"
;;
-p|--password)
shift
PASSWORD="$1"
2015-11-21 15:11:34 +01:00
if [ ${#PASSWORD} -lt $MINIMUM_PASSWORD_LENGTH ]; then
2015-11-27 16:29:43 +01:00
echo $"Your password chould contain at least ${MINIMUM_PASSWORD_LENGTH} characters"
2015-11-21 15:11:34 +01:00
exit 3628
fi
2015-11-20 22:43:03 +01:00
;;
--sshkey|--sshpubkey|--pubkey)
shift
SSH_PUBKEY="$1"
;;
2015-11-21 14:16:49 +01:00
-s|--size)
shift
IMAGE_SIZE="$1"
;;
2015-11-20 22:43:03 +01:00
# Box static IP address on the LAN
--ip)
shift
BOX_IP_ADDRESS="$1"
;;
# Router IP address on the LAN
--iprouter)
shift
ROUTER_IP_ADDRESS="$1"
;;
# nameserver 1
--ns1|--nameserver1)
shift
NAMESERVER1="$1"
;;
# nameserver 2
--ns2|--nameserver2)
shift
NAMESERVER2="$1"
;;
2015-11-21 16:44:59 +01:00
-i|--interactive)
shift
INTERACTIVE="$1"
;;
2015-11-23 11:33:45 +01:00
-g|--generic)
shift
GENERIC_IMAGE="$1"
;;
--minimal)
shift
MINIMAL_INSTALL="$1"
;;
--ssh|--sshport)
shift
SSH_PORT="$1"
;;
2015-12-24 20:04:17 +01:00
-v|--variant)
shift
VARIANT="$1"
;;
-o|--onion)
shift
ONION_ONLY="$1"
;;
-r|--repo|--repository)
shift
PROJECT_REPO="$1"
;;
-m|--mirror)
shift
MIRROR="$1"
;;
2015-11-20 22:43:03 +01:00
*)
# unknown option
;;
esac
shift
done
2015-11-21 16:44:59 +01:00
if [[ $INTERACTIVE == "yes" || $INTERACTIVE == "y" || $INTERACTIVE == "Yes" ]]; then
2015-12-08 17:22:48 +01:00
${PROJECT_NAME}-config --minimal "$MINIMAL_INSTALL"
2015-11-21 16:44:59 +01:00
if [ -f freedombone.cfg ]; then
CONFIG_FILENAME=freedombone.cfg
DEFAULT_DOMAIN_NAME=$(cat $CONFIG_FILENAME | grep 'DEFAULT_DOMAIN_NAME' | awk -F '=' '{print $2}')
fi
fi
if [[ $GENERIC_IMAGE == "yes" ]]; then
USERNAME=$GENERIC_IMAGE_USERNAME
PASSWORD=$GENERIC_IMAGE_PASSWORD
fi
2015-12-24 20:04:17 +01:00
# If this is a mesh variant then create an appropriate script
2016-01-14 18:45:02 +01:00
#if [[ $VARIANT == "mesh"* ]]; then
#CONFIG_FILENAME=/tmp/${PROJECT_NAME}_mesh.cfg
#mesh_router_setup_script $CONFIG_FILENAME
#DEFAULT_DOMAIN_NAME=$(cat $CONFIG_FILENAME | grep 'DEFAULT_DOMAIN_NAME' | awk -F '=' '{print $2}')
#fi
2015-12-24 20:04:17 +01:00
if [ ! $PASSWORD ]; then
# generate a random password
PASSWORD="$(openssl rand -base64 10 | cut -c1-8)"
fi
2015-11-20 22:43:03 +01:00
2016-01-14 12:07:40 +01:00
image_types=( xz img sig vdi qcow2 )
2015-12-01 15:13:23 +01:00
for im in "${image_types[@]}"
do
no_of_files=$(ls -afq ${CURR_DIR}/${PROJECT_NAME}*.${im} | wc -l)
if (( no_of_files > 0 )); then
rm ${CURR_DIR}/${PROJECT_NAME}*.${im}
fi
done
2015-11-24 10:17:05 +01:00
if [ -f ${CURR_DIR}/${PROJECT_NAME}_login_credentials.txt ]; then
rm ${CURR_DIR}/${PROJECT_NAME}_login_credentials.txt
fi
if [ -d $BUILD_DIR ]; then
2015-11-20 22:43:03 +01:00
rm -rf $BUILD_DIR
fi
mkdir -p $BUILD_DIR
2015-11-20 22:43:03 +01:00
if [ -f /usr/local/bin/$MAKEFILE ]; then
cp /usr/local/bin/$MAKEFILE $BUILD_DIR/Makefile
else
2015-11-20 22:43:03 +01:00
cp /usr/bin/$MAKEFILE $BUILD_DIR/Makefile
fi
cp -r /etc/${PROJECT_NAME}/* $BUILD_DIR
2015-11-22 23:52:59 +01:00
rm -rf $BUILD_DIR/vendor
2015-11-22 00:27:17 +01:00
chown -R $CURR_USER:$CURR_USER $BUILD_DIR
cd $BUILD_DIR
2016-01-05 15:54:32 +01:00
if [[ $MINIMAL_INSTALL == "yes" ]]; then
2016-01-05 22:19:02 +01:00
IMAGE_NAME='min'
2016-01-05 15:54:32 +01:00
fi
if [[ $ONION_ONLY != "no" ]]; then
2016-01-05 22:19:02 +01:00
IMAGE_NAME='onion'
2016-01-05 15:54:32 +01:00
fi
cd $BUILD_DIR
2015-11-20 22:43:03 +01:00
make $IMAGE_TYPE \
USERNAME="$USERNAME" \
PASSWORD="$PASSWORD" \
ROUTER_IP_ADDRESS="$ROUTER_IP_ADDRESS" \
BOX_IP_ADDRESS="$BOX_IP_ADDRESS" \
NAMESERVER1="$NAMESERVER1" \
NAMESERVER2="$NAMESERVER2" \
PROJECT_NAME="$PROJECT_NAME" \
2015-11-21 14:16:49 +01:00
CONFIG_FILENAME="$CONFIG_FILENAME" \
IMAGE_SIZE="$IMAGE_SIZE" \
2015-11-23 11:33:45 +01:00
SSH_PUBKEY="$SSH_PUBKEY" \
GENERIC_IMAGE="$GENERIC_IMAGE" \
MINIMAL_INSTALL="$MINIMAL_INSTALL" \
SSH_PORT="$SSH_PORT" \
2016-01-05 15:54:32 +01:00
ONION_ONLY="$ONION_ONLY" \
IMAGE_NAME="$IMAGE_NAME" \
PROJECT_REPO="$PROJECT_REPO" \
MIRROR="$MIRROR" \
BUILD_MIRROR="$MIRROR"
2015-11-20 22:43:03 +01:00
2015-11-24 10:37:53 +01:00
if [ ! "$?" = "0" ]; then
2015-11-27 16:29:43 +01:00
echo $'Build failed'
2015-11-24 10:37:53 +01:00
rm -rf $BUILD_DIR
exit 1
fi
2016-01-17 23:08:54 +01:00
EXPECTED_EXTENSION='xz'
2015-11-24 10:17:05 +01:00
if [[ $IMAGE_TYPE == "qemu"* ]]; then
EXPECTED_EXTENSION='qcow2'
fi
if [[ $IMAGE_TYPE == "virtualbox"* ]]; then
EXPECTED_EXTENSION='vdi'
fi
2015-11-21 11:29:10 +01:00
shopt -s nullglob
2015-11-24 10:17:05 +01:00
imgfiles=(build/${PROJECT_NAME}*.${EXPECTED_EXTENSION})
2015-11-21 11:29:10 +01:00
if [ ${#imgfiles[@]} -eq 0 ]; then
2015-11-27 16:29:43 +01:00
echo $'Image was not created'
rm -rf $BUILD_DIR
2015-11-24 10:37:53 +01:00
exit 2
2015-11-21 11:29:10 +01:00
fi
2015-12-01 15:13:23 +01:00
# Move images to the current directory
for im in "${image_types[@]}"
do
no_of_files=$(ls -afq build/${PROJECT_NAME}*.${im} | wc -l)
if (( no_of_files > 0 )); then
2015-12-01 22:51:20 +01:00
mv build/${PROJECT_NAME}*.${im} ${CURR_DIR}/
2015-12-01 15:13:23 +01:00
fi
done
2015-12-01 13:31:49 +01:00
2015-11-21 21:41:58 +01:00
rm -rf ${BUILD_DIR}
cd ${CURR_DIR}
2015-11-21 11:29:10 +01:00
clear
2015-11-27 16:29:43 +01:00
echo $"
Image was created.
You will be able to log into it with:
"
if [[ $IMAGE_TYPE != "virtualbox"* && $IMAGE_TYPE != "qemu"* ]]; then
echo $" ssh $USERNAME@$DEFAULT_DOMAIN_NAME -p $SSH_PORT
Password: $PASSWORD
"
else
if [[ $IMAGE_TYPE != "qemu"* ]]; then
2015-11-27 16:29:43 +01:00
echo $" Username: $USERNAME
Password: $PASSWORD
"
else
if [[ $IMAGE_TYPE != "qemu-x86_64"* && $IMAGE_TYPE != "qemu-amd64"* ]]; then
2016-01-05 15:17:49 +01:00
echo "qemu-system-i386 -m ${VM_MEMORY} $(ls ${PROJECT_NAME}*.qcow2)"
else
2016-01-05 15:17:49 +01:00
echo "qemu-system-x86_64 -m ${VM_MEMORY} $(ls ${PROJECT_NAME}*.qcow2)"
fi
2015-11-27 16:29:43 +01:00
echo $"
Username: $USERNAME
Password: $PASSWORD
"
fi
fi
2016-01-14 12:07:40 +01:00
ls -lh ${PROJECT_NAME}*.img ${PROJECT_NAME}*.sig ${PROJECT_NAME}*.xz ${PROJECT_NAME}*.vdi ${PROJECT_NAME}*.qcow2
2015-11-21 11:29:10 +01:00
2015-12-24 20:04:17 +01:00
# Remove the mesh script after use
if [[ $VARIANT == "mesh"* ]]; then
rm -f $CONFIG_FILENAME
fi
# record the default login credentials for later use
2015-11-27 16:29:43 +01:00
echo $"Username: $USERNAME
2015-11-21 18:47:47 +01:00
Password: $PASSWORD" > ${CURR_DIR}/${PROJECT_NAME}_login_credentials.txt
chmod 600 ${CURR_DIR}/${PROJECT_NAME}_login_credentials.txt
2015-11-25 12:43:57 +01:00
if [[ $IMAGE_TYPE != "virtualbox"* && $IMAGE_TYPE != "qemu"* ]]; then
echo ''
2015-11-27 16:29:43 +01:00
echo $'You can copy the image to a microSD card with:'
2015-11-25 12:43:57 +01:00
echo ''
echo " sudo dd bs=1M if=$(ls ${PROJECT_NAME}*.img) of=/dev/sdX conv=fdatasync"
echo ''
fi
exit 0