From ddc7f1a7bbb0dc69d1acd06d16bceb4d43ddad66 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Tue, 26 Apr 2016 15:41:35 +0100 Subject: [PATCH] Can configure wifi from image builder --- src/freedombone-image | 204 +++++++++++++++++++------------- src/freedombone-image-customise | 23 ++++ src/freedombone-image-make | 10 ++ 3 files changed, 152 insertions(+), 85 deletions(-) diff --git a/src/freedombone-image b/src/freedombone-image index 7b0f543b..1781deb6 100755 --- a/src/freedombone-image +++ b/src/freedombone-image @@ -100,6 +100,13 @@ MIRROR='http://ftp.de.debian.org/debian' # Whether to only install debian but nothing else DEBIAN_INSTALL_ONLY='no' +# wifi settings +WIFI_INTERFACE='wlan0' +WIFI_SSID= +WIFI_TYPE='wpa-psk' +WIFI_PASSPHRASE= +WIFI_HOTSPOT='no' + mesh_router_setup_script() { # create a setup script for a mesh router mesh_script_filename=$1 @@ -220,24 +227,46 @@ do shift DEBIAN_INSTALL_ONLY="$1" ;; - *) - # unknown option - ;; - esac - shift + --interface|--if) + shift + WIFI_INTERFACE="$1" + ;; + --ssid|--essid) + shift + WIFI_SSID="$1" + ;; + --wifitype) + shift + WIFI_TYPE="$1" + ;; + --wifipass|--passphrase) + shift + WIFI_PASSPHRASE="$1" + ;; + --hotspot) + shift + if [[ $"$1" == $'yes' || $"$1" == $'y' ]]; then + WIFI_HOTSPOT='yes' + fi + ;; + *) + # unknown option + ;; + esac + shift done if [[ $INTERACTIVE == "yes" || $INTERACTIVE == "y" || $INTERACTIVE == "Yes" ]]; then - ${PROJECT_NAME}-config --minimal "$MINIMAL_INSTALL" - if [ -f freedombone.cfg ]; then - CONFIG_FILENAME=freedombone.cfg - DEFAULT_DOMAIN_NAME=$(cat $CONFIG_FILENAME | grep 'DEFAULT_DOMAIN_NAME' | awk -F '=' '{print $2}') - fi + ${PROJECT_NAME}-config --minimal "$MINIMAL_INSTALL" + 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 + USERNAME=$GENERIC_IMAGE_USERNAME + PASSWORD=$GENERIC_IMAGE_PASSWORD fi # If this is a mesh variant then create an appropriate script @@ -248,45 +277,45 @@ fi #fi if [ ! $PASSWORD ]; then - # generate a random password - PASSWORD="$(openssl rand -base64 10 | cut -c1-8)" + # generate a random password + PASSWORD="$(openssl rand -base64 10 | cut -c1-8)" fi # Move any existing images into a build subdirectory image_types=( xz img sig vdi qcow2 ) for im in "${image_types[@]}" do - no_of_files=$(ls -afq ${CURR_DIR}/${PROJECT_NAME}*.${im} | wc -l) - if (( no_of_files > 0 )); then - if [ ! -d ${CURR_DIR}/build ]; then - mkdir ${CURR_DIR}/build - fi - mv -f ${CURR_DIR}/${PROJECT_NAME}*.${im} ${CURR_DIR}/build - fi + no_of_files=$(ls -afq ${CURR_DIR}/${PROJECT_NAME}*.${im} | wc -l) + if (( no_of_files > 0 )); then + if [ ! -d ${CURR_DIR}/build ]; then + mkdir ${CURR_DIR}/build + fi + mv -f ${CURR_DIR}/${PROJECT_NAME}*.${im} ${CURR_DIR}/build + fi done # Delete anything which didn't move 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 -f ${CURR_DIR}/${PROJECT_NAME}*.${im} - fi + no_of_files=$(ls -afq ${CURR_DIR}/${PROJECT_NAME}*.${im} | wc -l) + if (( no_of_files > 0 )); then + rm -f ${CURR_DIR}/${PROJECT_NAME}*.${im} + fi done # Remove any existing login credentials file if [ -f ${CURR_DIR}/${PROJECT_NAME}_login_credentials.txt ]; then - rm ${CURR_DIR}/${PROJECT_NAME}_login_credentials.txt + rm ${CURR_DIR}/${PROJECT_NAME}_login_credentials.txt fi if [ -d $TEMPBUILD_DIR ]; then - rm -rf $TEMPBUILD_DIR + rm -rf $TEMPBUILD_DIR fi mkdir -p $TEMPBUILD_DIR if [ -f /usr/local/bin/$MAKEFILE ]; then - cp /usr/local/bin/$MAKEFILE $TEMPBUILD_DIR/Makefile + cp /usr/local/bin/$MAKEFILE $TEMPBUILD_DIR/Makefile else - cp /usr/bin/$MAKEFILE $TEMPBUILD_DIR/Makefile + cp /usr/bin/$MAKEFILE $TEMPBUILD_DIR/Makefile fi cp -r /etc/${PROJECT_NAME}/* $TEMPBUILD_DIR @@ -295,64 +324,69 @@ chown -R $CURR_USER:$CURR_USER $TEMPBUILD_DIR cd $TEMPBUILD_DIR if [[ $MINIMAL_INSTALL == "yes" ]]; then - IMAGE_NAME='min' + IMAGE_NAME='min' fi if [[ $ONION_ONLY != "no" ]]; then - IMAGE_NAME='onion' + IMAGE_NAME='onion' fi cd $TEMPBUILD_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" \ - GENERIC_IMAGE="$GENERIC_IMAGE" \ - MINIMAL_INSTALL="$MINIMAL_INSTALL" \ - SSH_PORT="$SSH_PORT" \ - ONION_ONLY="$ONION_ONLY" \ - IMAGE_NAME="$IMAGE_NAME" \ - PROJECT_REPO="$PROJECT_REPO" \ - MIRROR="$MIRROR" \ - BUILD_MIRROR="$MIRROR" \ - DEBIAN_INSTALL_ONLY="$DEBIAN_INSTALL_ONLY" - + 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" \ + GENERIC_IMAGE="$GENERIC_IMAGE" \ + MINIMAL_INSTALL="$MINIMAL_INSTALL" \ + SSH_PORT="$SSH_PORT" \ + ONION_ONLY="$ONION_ONLY" \ + IMAGE_NAME="$IMAGE_NAME" \ + PROJECT_REPO="$PROJECT_REPO" \ + MIRROR="$MIRROR" \ + BUILD_MIRROR="$MIRROR" \ + DEBIAN_INSTALL_ONLY="$DEBIAN_INSTALL_ONLY" \ + WIFI_INTERFACE="$WIFI_INTERFACE" \ + WIFI_SSID="$WIFI_SSID" \ + WIFI_TYPE="$WIFI_TYPE" \ + WIFI_PASSPHRASE="$WIFI_PASSPHRASE" \ + WIFI_HOTSPOT="$WIFI_HOTSPOT" + if [ ! "$?" = "0" ]; then - echo $'Build failed' - rm -rf $TEMPBUILD_DIR - exit 1 + echo $'Build failed' + rm -rf $TEMPBUILD_DIR + exit 1 fi EXPECTED_EXTENSION='xz' if [[ $IMAGE_TYPE == "qemu"* ]]; then - EXPECTED_EXTENSION='qcow2' + EXPECTED_EXTENSION='qcow2' fi if [[ $IMAGE_TYPE == "virtualbox"* ]]; then - EXPECTED_EXTENSION='vdi' + EXPECTED_EXTENSION='vdi' fi shopt -s nullglob imgfiles=(build/${PROJECT_NAME}*.${EXPECTED_EXTENSION}) if [ ${#imgfiles[@]} -eq 0 ]; then - echo $'Image was not created' - rm -rf $TEMPBUILD_DIR - exit 2 + echo $'Image was not created' + rm -rf $TEMPBUILD_DIR + exit 2 fi # Move images from temporary directory 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 - mv build/${PROJECT_NAME}*.${im} ${CURR_DIR}/ - fi + no_of_files=$(ls -afq build/${PROJECT_NAME}*.${im} | wc -l) + if (( no_of_files > 0 )); then + mv build/${PROJECT_NAME}*.${im} ${CURR_DIR}/ + fi done # Remove the temporary directory @@ -367,32 +401,32 @@ 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 + echo $" ssh $USERNAME@$DEFAULT_DOMAIN_NAME -p $SSH_PORT - Password: $PASSWORD +Password: $PASSWORD " else - if [[ $IMAGE_TYPE != "qemu"* ]]; then - echo $" Username: $USERNAME - Password: $PASSWORD + if [[ $IMAGE_TYPE != "qemu"* ]]; then + echo $" Username: $USERNAME +Password: $PASSWORD " - else - if [[ $IMAGE_TYPE != "qemu-x86_64"* && $IMAGE_TYPE != "qemu-amd64"* ]]; then - echo "qemu-system-i386 -m ${VM_MEMORY} $(ls ${PROJECT_NAME}*.qcow2)" - else - echo "qemu-system-x86_64 -m ${VM_MEMORY} $(ls ${PROJECT_NAME}*.qcow2)" - fi - echo $" - Username: $USERNAME - Password: $PASSWORD + else + if [[ $IMAGE_TYPE != "qemu-x86_64"* && $IMAGE_TYPE != "qemu-amd64"* ]]; then + echo "qemu-system-i386 -m ${VM_MEMORY} $(ls ${PROJECT_NAME}*.qcow2)" + else + echo "qemu-system-x86_64 -m ${VM_MEMORY} $(ls ${PROJECT_NAME}*.qcow2)" + fi + echo $" +Username: $USERNAME +Password: $PASSWORD " - fi + fi fi ls -lh ${PROJECT_NAME}*.img ${PROJECT_NAME}*.sig ${PROJECT_NAME}*.xz ${PROJECT_NAME}*.vdi ${PROJECT_NAME}*.qcow2 # Remove the mesh script after use if [[ $VARIANT == "mesh"* ]]; then - rm -f $CONFIG_FILENAME + rm -f $CONFIG_FILENAME fi # record the default login credentials for later use @@ -401,12 +435,12 @@ Password: $PASSWORD" > ${CURR_DIR}/${PROJECT_NAME}_login_credentials.txt chmod 600 ${CURR_DIR}/${PROJECT_NAME}_login_credentials.txt if [[ $IMAGE_TYPE != "virtualbox"* && $IMAGE_TYPE != "qemu"* ]]; then - echo '' - echo $'You can copy the image to a microSD card with:' - echo '' - echo " unxz -k ${PROJECT_NAME}*.img.xz" - echo " sudo dd bs=1M if=${PROJECT_NAME}*.img of=/dev/sdX conv=fdatasync" - echo '' + echo '' + echo $'You can copy the image to a microSD card with:' + echo '' + echo " unxz -k ${PROJECT_NAME}*.img.xz" + echo " sudo dd bs=1M if=${PROJECT_NAME}*.img of=/dev/sdX conv=fdatasync" + echo '' fi exit 0 diff --git a/src/freedombone-image-customise b/src/freedombone-image-customise index 89e00819..62aa6a75 100755 --- a/src/freedombone-image-customise +++ b/src/freedombone-image-customise @@ -78,7 +78,12 @@ ONION_ONLY="no" # Whether to only install debian and nothing else DEBIAN_INSTALL_ONLY="no" +# wifi settings WIFI_INTERFACE='wlan0' +WIFI_SSID= +WIFI_TYPE='wpa-psk' +WIFI_PASSPHRASE= +WIFI_HOTSPOT='no' enable_eatmydata_override() { chroot $rootdir apt-get install --no-install-recommends -y eatmydata @@ -421,6 +426,23 @@ initialise_mesh() { chroot "$rootdir" systemctl enable $MESH_SERVICE } +configure_wifi() { + if [[ $VARIANT == "mesh" ]]; then + return + fi + if [[ $WIFI_TYPE != 'none' ]]; then + if [ ! $WIFI_PASSPHRASE ]; then + return + fi + if [ ${#WIFI_PASSPHRASE} -lt 2 ]; then + return + fi + chroot "$rootdir" ${PROJECT_NAME}-wifi -i $WIFI_INTERFACE -s $WIFI_SSID -t $WIFI_TYPE -p $WIFI_PASSPHRASE --hotspot $HOTSPOT + else + chroot "$rootdir" ${PROJECT_NAME}-wifi -i $WIFI_INTERFACE -s $WIFI_SSID -t $WIFI_TYPE --hotspot $HOTSPOT + fi +} + # Set to true/false to control if eatmydata is used during build use_eatmydata=true @@ -540,6 +562,7 @@ create_generic_image atheros_wifi continue_installation initialise_mesh +configure_wifi cd / echo $"info: killing leftover processes in chroot" diff --git a/src/freedombone-image-make b/src/freedombone-image-make index 7f437215..c584c3ae 100755 --- a/src/freedombone-image-make +++ b/src/freedombone-image-make @@ -56,6 +56,11 @@ export SSH_PORT export ONION_ONLY export PROJECT_REPO export DEBIAN_INSTALL_ONLY +export WIFI_INTERFACE +export WIFI_SSID +export WIFI_TYPE +export WIFI_PASSPHRASE +export WIFI_HOTSPOT # Locate vmdebootstrap program fetched in Makefile basedir=`pwd` @@ -171,6 +176,11 @@ sudo sed -i "s|SSH_PORT=.*|SSH_PORT=\"${SSH_PORT}\"|g" $TEMP_CUSTOMISE sudo sed -i "s|ONION_ONLY=.*|ONION_ONLY=\"${ONION_ONLY}\"|g" $TEMP_CUSTOMISE sudo sed -i "s|PROJECT_REPO=.*|PROJECT_REPO=\"${PROJECT_REPO}\"|g" $TEMP_CUSTOMISE sudo sed -i "s|DEBIAN_INSTALL_ONLY=.*|DEBIAN_INSTALL_ONLY=\"${DEBIAN_INSTALL_ONLY}\"|g" $TEMP_CUSTOMISE +sudo sed -i "s|WIFI_INTERFACE=.*|WIFI_INTERFACE=\"${WIFI_INTERFACE}\"|g" $TEMP_CUSTOMISE +sudo sed -i "s|WIFI_SSID=.*|WIFI_SSID=\"${WIFI_SSID}\"|g" $TEMP_CUSTOMISE +sudo sed -i "s|WIFI_TYPE=.*|WIFI_TYPE=\"${WIFI_TYPE}\"|g" $TEMP_CUSTOMISE +sudo sed -i "s|WIFI_PASSPHRASE=.*|WIFI_PASSPHRASE=\"${WIFI_PASSPHRASE}\"|g" $TEMP_CUSTOMISE +sudo sed -i "s|WIFI_HOTSPOT=.*|WIFI_HOTSPOT=\"${WIFI_HOTSPOT}\"|g" $TEMP_CUSTOMISE echo $"starting $VMDEBOOTSTRAP" # Run vmdebootstrap script to create image