freedomboneeee/src/freedombone-image

229 lines
5.4 KiB
Bash
Executable File

#!/bin/bash
#
# .---. . .
# | | |
# |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
# | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
# ' ' --' --' -' - -' ' ' -' -' -' ' - --'
#
# Freedom in the Cloud
#
# Creates a debian image using vmdebootstrap
#
# License
# =======
#
# Copyright (C) 2015 Bob Mottram <bob@robotics.uk.to>
#
# 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 <http://www.gnu.org/licenses/>.
PROJECT_NAME='freedombone'
IMAGE_TYPE='beaglebone'
CURR_DIR=$(pwd)
BUILD_DIR=~/.tmp_${PROJECT_NAME}_build
VMDEBOOTSTRAP_REPO=git://git.liw.fi/vmdebootstrap
VMDEBOOTSTRAP_VERSION=0.8
MAKEFILE=${PROJECT_NAME}-image-makefile
IMAGE_SIZE=4G
USERNAME=$(echo $USER)
PASSWORD=
# 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"
# Minimum number of characters in a password
MINIMUM_PASSWORD_LENGTH=10
# Optional ssh public key to allow
SSH_PUBKEY="no"
# interactive mode
INTERACTIVE="no"
while [[ $# > 1 ]]
do
key="$1"
case $key in
-h|--help)
show_help
;;
-c|--config)
shift
CONFIG_FILENAME="$1"
if [ ! -f $CONFIG_FILENAME ]; then
echo "Config file $CONFIG_FILENAME not found"
exit 3
fi
DEFAULT_DOMAIN_NAME=$(cat $CONFIG_FILENAME | grep 'DEFAULT_DOMAIN_NAME' | awk -F '=' '{print $2}')
;;
-t|--target|--board)
shift
IMAGE_TYPE="$1"
;;
-u|--user|--username)
shift
USERNAME="$1"
;;
-p|--password)
shift
PASSWORD="$1"
if [ ${#PASSWORD} -lt $MINIMUM_PASSWORD_LENGTH ]; then
echo "Your password chould contain at least ${MINIMUM_PASSWORD_LENGTH} characters"
exit 3628
fi
;;
--sshkey|--sshpubkey|--pubkey)
shift
SSH_PUBKEY="$1"
;;
-s|--size)
shift
IMAGE_SIZE="$1"
;;
# 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"
;;
-i|--interactive)
shift
INTERACTIVE="$1"
;;
*)
# unknown option
;;
esac
shift
done
if [[ $INTERACTIVE == "yes" || $INTERACTIVE == "y" || $INTERACTIVE == "Yes" ]]; then
freedombone-config
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 [ ! $PASSWORD ]; then
# generate a random password
PASSWORD="$(openssl rand -base64 10 | cut -c1-8)"
fi
rm $CURR_DIR/*.img.bz2
rm $CURR_DIR/*.img
rm $CURR_DIR/*.sig
if [ -d $BUILD_DIR ]; then
rm -rf $BUILD_DIR
fi
mkdir -p $BUILD_DIR
if [ -f /usr/local/bin/$MAKEFILE ]; then
cp /usr/local/bin/$MAKEFILE $BUILD_DIR/Makefile
else
cp /usr/bin/$MAKEFILE $BUILD_DIR/Makefile
fi
cp -r /etc/${PROJECT_NAME}/* $BUILD_DIR
cd $BUILD_DIR
rm -rf vendor
mkdir vendor
if [ -d vendor/vmdebootstrap ] ; then
(cd vendor/vmdebootstrap; git checkout .; git pull)
else
git clone $VMDEBOOTSTRAP_REPO vendor/vmdebootstrap
fi
cd vendor/vmdebootstrap
git checkout tags/vmdebootstrap-${VMDEBOOTSTRAP_VERSION}
git checkout -b tags/vmdebootstrap-${VMDEBOOTSTRAP_VERSION}
for f in ../../vendor-patches/vmdebootstrap/* ; do
echo applying $(basename $f)
patch -p1 < $f
done
cd $BUILD_DIR
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" \
CONFIG_FILENAME="$CONFIG_FILENAME" \
IMAGE_SIZE="$IMAGE_SIZE" \
SSH_PUBKEY="$SSH_PUBKEY"
shopt -s nullglob
imgfiles=(build/${PROJECT_NAME}*.img)
if [ ${#imgfiles[@]} -eq 0 ]; then
echo 'Image was not created'
rm -rf $BUILD_DIR
exit 1
fi
mv build/${PROJECT_NAME}*.bz2 ${CURR_DIR}
mv build/${PROJECT_NAME}*.img ${CURR_DIR}
mv build/${PROJECT_NAME}*.sig ${CURR_DIR}
#rm -rf ${BUILD_DIR}
clear
echo "
Image was created.
You will be able to log into it with:
ssh $USERNAME@$DEFAULT_DOMAIN_NAME -p 2222
Password: $PASSWORD
"
cd ${CURR_DIR}
ls -lh ${PROJECT_NAME}*.img ${PROJECT_NAME}*.sig ${PROJECT_NAME}*.bz2
# record the default login credentials for later use
echo "Username: $USERNAME
Password: $PASSWORD" > ${CURR_DIR}/${PROJECT_NAME}_login_credentials.txt
chmod 600 ${CURR_DIR}/${PROJECT_NAME}_login_credentials.txt
exit 0