freedomboneeee/src/freedombone-prep

293 lines
9.0 KiB
Plaintext
Raw Normal View History

2014-11-07 21:09:57 +01:00
#!/bin/bash
2015-04-04 14:07:00 +02:00
#
# .---. . .
# | | |
# |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
# | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
# ' ' --' --' -' - -' ' ' -' -' -' ' - --'
#
# Freedom in the Cloud
#
2014-11-07 21:09:57 +01:00
# This script installs the Debian image to the microSD card, and should
# be run on your laptop/desktop with the microSD card plugged in.
# License
# =======
#
2015-01-04 12:40:46 +01:00
# Copyright (C) 2014-2015 Bob Mottram <bob@robotics.uk.to>
2014-11-07 21:09:57 +01:00
#
# 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/>.
# The number of arguments
NO_OF_ARGS=$#
2014-11-07 21:09:57 +01:00
# Version number of this script
2015-07-09 19:59:43 +02:00
VERSION="1.01"
2014-11-07 21:09:57 +01:00
# typically /dev/sdb or /dev/sdc, depending upon how
# many drives there are on your system
2015-01-16 23:55:18 +01:00
MICROSD_DRIVE=
2014-11-07 21:09:57 +01:00
# IP address of the router (gateway)
ROUTER_IP_ADDRESS="192.168.1.254"
# The fixed IP address of the Beaglebone Black on your local network
2015-01-16 20:46:15 +01:00
BBB_FIXED_IP_ADDRESS="192.168.1.55"
2014-11-07 21:09:57 +01:00
2015-01-31 12:11:07 +01:00
# DNS
NAMESERVER1='213.73.91.35'
NAMESERVER2='85.214.20.141'
2014-11-07 21:09:57 +01:00
MICROSD_MOUNT_POINT="/media/$USER"
DEBIAN_FILE_NAME="bone-debian-8.1-console-armhf-2015-07-12-2gb"
2014-11-07 21:09:57 +01:00
# Downloads for the Debian installer
DOWNLOAD_LINK1="https://rcn-ee.com/rootfs/bb.org/testing/2015-07-12/console/$DEBIAN_FILE_NAME.img.xz"
2014-11-07 21:09:57 +01:00
ROOTFS='bbb'
2015-01-30 22:02:28 +01:00
PARTITION_NUMBER=1
2015-01-16 23:55:18 +01:00
function show_help {
echo ''
2015-01-30 21:32:36 +01:00
echo 'freedombone-prep -d [microSD device] --ip [BBB LAN IP address] --iprouter [Router LAN IP address] --mount [mount directory]'
2015-01-16 23:55:18 +01:00
echo ''
echo 'See the manpage for more details'
echo ''
2015-01-16 23:55:18 +01:00
}
# if no arguments are given
if [[ $NO_OF_ARGS == 0 ]]; then
show_help
exit 0
fi
2015-02-07 11:06:34 +01:00
if [ -d /media ]; then
# different directories for Debian
if [ -d /media/usb1 ]; then
MICROSD_MOUNT_POINT=/media
ROOTFS=usb1
2015-01-30 22:02:28 +01:00
fi
2015-02-07 11:06:34 +01:00
if [ -d /media/usb0 ]; then
MICROSD_MOUNT_POINT=/media
2015-01-30 22:02:28 +01:00
fi
2015-01-30 21:32:36 +01:00
fi
2015-02-07 11:06:34 +01:00
if [ ! -d $MICROSD_MOUNT_POINT ]; then
echo "The mount directory $MICROSD_MOUNT_POINT does not exist."
echo 'Use the --mount option to specify where the microSD gets mounted to.'
exit 67563
fi
echo "MICROSD_MOUNT_POINT=$MICROSD_MOUNT_POINT"
2015-01-30 21:32:36 +01:00
2015-01-16 23:55:18 +01:00
while [[ $# > 1 ]]
do
key="$1"
case $key in
-h|--help)
show_help
;;
# Drive path for the microSD
-d|--drive)
shift
MICROSD_DRIVE="$1"
;;
# BBB static IP address on the LAN
--ip)
shift
BBB_FIXED_IP_ADDRESS="$1"
;;
# Router IP address on the LAN
--iprouter)
shift
ROUTER_IP_ADDRESS="$1"
;;
2015-01-28 22:32:13 +01:00
# mount point
--mount)
shift
MICROSD_MOUNT_POINT="$1"
;;
2015-01-31 12:11:07 +01:00
# nameserver 1
--ns1)
shift
NAMESERVER1="$1"
;;
# nameserver 2
--ns2)
shift
NAMESERVER2="$1"
;;
2015-01-16 23:55:18 +01:00
*)
# unknown option
;;
esac
shift
done
if [ ! $MICROSD_DRIVE ]; then
echo 'You need to specify a drive for the connected microSD.'
echo 'This can most easily be found by removing the microSD, then'
echo 'running:'
echo ''
echo ' ls /dev/sd*'
echo ''
echo 'or'
echo ''
echo ' ls /dev/mmcblk*'
echo ''
2015-01-16 23:55:18 +01:00
echo 'Then plugging the microSD back in and entering the same command again'
exit 1
2014-11-07 21:09:57 +01:00
fi
if [ ! -b ${MICROSD_DRIVE}${PARTITION_NUMBER} ]; then
if [ -b ${MICROSD_DRIVE}p${PARTITION_NUMBER} ]; then
PARTITION_NUMBER=p${PARTITION_NUMBER}
else
echo "The microSD drive could not be found at ${MICROSD_DRIVE}1"
exit 2
fi
2014-11-07 21:09:57 +01:00
fi
SUDO=
if [ -f /usr/bin/sudo ]; then
SUDO='sudo'
fi
$SUDO apt-get install p7zip dd wget
2014-11-07 21:09:57 +01:00
if [ ! -d ~/freedombone ]; then
2015-01-16 23:55:18 +01:00
mkdir ~/freedombone
2014-11-07 21:09:57 +01:00
fi
cd ~/freedombone
if [ ! -f ~/freedombone/$DEBIAN_FILE_NAME.img.xz ]; then
if [ ! -f ~/freedombone/$DEBIAN_FILE_NAME.img ]; then
wget $DOWNLOAD_LINK1
2015-01-16 23:55:18 +01:00
fi
2014-11-07 21:09:57 +01:00
fi
echo 'Extracting image...'
xz -d $DEBIAN_FILE_NAME.img.xz
if [ ! -f ~/freedombone/$DEBIAN_FILE_NAME.img ]; then
echo "Couldn't extract image"
2015-01-16 23:55:18 +01:00
exit 4
2014-11-07 21:09:57 +01:00
fi
cd ~/freedombone
echo 'Flashing image. This may take a while.'
$SUDO dd if=$DEBIAN_FILE_NAME.img of=$MICROSD_DRIVE
sync
sleep 5
2015-07-23 09:11:08 +02:00
if [ -d $MICROSD_MOUNT_POINT/$ROOTFS ]; then
umount $MICROSD_MOUNT_POINT/$ROOTFS
$SUDO rm $MICROSD_MOUNT_POINT/$ROOTFS
fi
2015-07-23 09:11:08 +02:00
$SUDO mkdir -p $MICROSD_MOUNT_POINT/$ROOTFS
$SUDO mount ${MICROSD_DRIVE}${PARTITION_NUMBER} $MICROSD_MOUNT_POINT/$ROOTFS
sync
2014-11-07 21:09:57 +01:00
if [ ! -b ${MICROSD_DRIVE}${PARTITION_NUMBER} ]; then
2015-01-16 23:55:18 +01:00
echo ''
echo "The microSD drive could not be found at ${MICROSD_DRIVE}${PARTITION_NUMBER}"
2015-01-16 23:55:18 +01:00
read -p "Wait for the drive to mount then press any key... " -n1 -s
if [ ! -b ${MICROSD_DRIVE}${PARTITION_NUMBER} ]; then
echo "microSD drive not found at ${MICROSD_DRIVE}${PARTITION_NUMBER}"
2015-01-16 23:55:18 +01:00
exit 5
fi
2014-11-07 21:09:57 +01:00
fi
2015-01-30 22:02:28 +01:00
if [ ! -d $MICROSD_MOUNT_POINT/$ROOTFS ]; then
2015-01-31 11:55:19 +01:00
echo ''
2015-01-30 22:02:28 +01:00
echo "The rootfs partition $MICROSD_MOUNT_POINT/$ROOTFS was not found."
ls $MICROSD_MOUNT_POINT
2015-01-30 21:32:36 +01:00
exit 65688
fi
2015-01-31 11:51:38 +01:00
if [ ! -d $MICROSD_MOUNT_POINT/$ROOTFS/home ]; then
2015-01-31 11:55:19 +01:00
echo ''
2015-01-31 11:51:38 +01:00
echo "The rootfs partition was not written correctly."
ls $MICROSD_MOUNT_POINT/$ROOTFS
exit 65688
fi
2015-01-30 22:02:28 +01:00
$SUDO sed -i 's/iface eth0 inet dhcp/iface eth0 inet static/g' $MICROSD_MOUNT_POINT/$ROOTFS/etc/network/interfaces
2015-02-07 11:06:34 +01:00
$SUDO sed -i "/iface eth0 inet static/a\ dns-nameservers $NAMESERVER1 $NAMESERVER2" $MICROSD_MOUNT_POINT/$ROOTFS/etc/network/interfaces
2015-01-30 22:02:28 +01:00
$SUDO sed -i "/iface eth0 inet static/a\ gateway $ROUTER_IP_ADDRESS" $MICROSD_MOUNT_POINT/$ROOTFS/etc/network/interfaces
$SUDO sed -i '/iface eth0 inet static/a\ netmask 255.255.255.0' $MICROSD_MOUNT_POINT/$ROOTFS/etc/network/interfaces
$SUDO sed -i "/iface eth0 inet static/a\ address $BBB_FIXED_IP_ADDRESS" $MICROSD_MOUNT_POINT/$ROOTFS/etc/network/interfaces
$SUDO sed -i '/iface usb0 inet static/,/ gateway 192.168.7.1/ s/^/#/' $MICROSD_MOUNT_POINT/$ROOTFS/etc/network/interfaces
2015-01-16 20:46:15 +01:00
2015-07-25 15:45:18 +02:00
hexarray=( 1 2 3 4 5 6 7 8 9 0 a b c d e f )
a=${hexarray[$RANDOM%16]}${hexarray[$RANDOM%16]}
b=${hexarray[$RANDOM%16]}${hexarray[$RANDOM%16]}
c=${hexarray[$RANDOM%16]}${hexarray[$RANDOM%16]}
d=${hexarray[$RANDOM%16]}${hexarray[$RANDOM%16]}
2015-07-27 09:29:13 +02:00
e=${hexarray[$RANDOM%16]}${hexarray[$RANDOM%16]}
$SUDO sed -i "s|#hwaddress ether.*|hwaddress ether de:$a:$b:$c:$d:$e|g" $MICROSD_MOUNT_POINT/$ROOTFS/etc/network/interfaces
2015-07-25 15:45:18 +02:00
2015-01-31 12:11:07 +01:00
$SUDO sed -i "s/nameserver.*/nameserver $NAMESERVER1/g" $MICROSD_MOUNT_POINT/$ROOTFS/etc/resolv.conf
$SUDO sed -i "/nameserver $NAMESERVER1/a\nameserver $NAMESERVER2" $MICROSD_MOUNT_POINT/$ROOTFS/etc/resolv.conf
2014-11-07 21:09:57 +01:00
# copy the commands to the card
$SUDO cp -f $(which freedombone)* $MICROSD_MOUNT_POINT/$ROOTFS/usr/local/bin/
2015-08-29 17:26:51 +02:00
$SUDO cp -f $(which zeronetavahi)* $MICROSD_MOUNT_POINT/$ROOTFS/usr/local/bin/
if [ ! -f $MICROSD_MOUNT_POINT/$ROOTFS/usr/local/bin/freedombone ]; then
echo 'There was a problem with writing freedombone commands to the SD card'
exit 8736
fi
# change the motd to show further install instructions
echo '' >> /tmp/freedombone_motd
echo 'Create a user for the system with:' >> /tmp/freedombone_motd
echo '' >> /tmp/freedombone_motd
echo ' adduser [username]' >> /tmp/freedombone_motd
echo '' >> /tmp/freedombone_motd
echo 'Enter the command "exit" a couple of times to get back to your main system' >> /tmp/freedombone_motd
echo 'then log back in as the user you just created with:' >> /tmp/freedombone_motd
echo '' >> /tmp/freedombone_motd
2015-02-14 15:57:46 +01:00
echo " ssh [username]@$BBB_FIXED_IP_ADDRESS" >> /tmp/freedombone_motd
echo '' >> /tmp/freedombone_motd
echo 'and use the "su" command to become the root user again.' >> /tmp/freedombone_motd
echo '' >> /tmp/freedombone_motd
echo 'Finally you can use the freedombone command to install a server configuration:' >> /tmp/freedombone_motd
echo '' >> /tmp/freedombone_motd
echo ' apt-get update' >> /tmp/freedombone_motd
echo ' apt-get -y install git dialog build-essential' >> /tmp/freedombone_motd
echo ' freedombone menuconfig' >> /tmp/freedombone_motd
$SUDO cp -f /tmp/freedombone_motd $MICROSD_MOUNT_POINT/$ROOTFS/etc/motd
2014-11-07 21:09:57 +01:00
clear
echo '*** Initial microSD card setup is complete ***'
echo ''
2015-07-25 15:36:23 +02:00
echo 'To avoid running out of disk space you may first wish to resize the'
echo 'partition to the size of your microSD card, using something like gparted.'
echo ''
2014-11-07 21:09:57 +01:00
echo 'The microSD card can now be removed and inserted into the Beaglebone Black.'
echo 'Once the Beaglebone has booted then you can log in with:'
echo ''
echo " ssh root@$BBB_FIXED_IP_ADDRESS"
2014-11-07 21:09:57 +01:00
echo ''
echo 'The root password should be changed with the command "passwd".'
cat /tmp/freedombone_motd
rm /tmp/freedombone_motd
$SUDO umount $MICROSD_MOUNT_POINT/$ROOTFS
sync
if [ -d $MICROSD_MOUNT_POINT/$ROOTFS ]; then
$SUDO rm -rf $MICROSD_MOUNT_POINT/$ROOTFS
fi
2014-11-07 21:09:57 +01:00
exit 0