-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_config.sh
executable file
·104 lines (91 loc) · 2.97 KB
/
image_config.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/bin/bash
# . . .;
# .' .' .;'
# ;-. .;.::..-. . ,';.,';. ;-. .; .-.
# ; ; .; ; : ;; ;; ;; ; ; :: .;.-'
# .'`::'`-.;' `:::'-''; ;; ';.'`::'`-_;;_.-`:::'
# _; `-'
# https://github.com/tkphd/bramble-config
# Run this script with privileges on a Raspbian
# image [first argument] before writing to the
# SD card to do the following:
# 1. Change the hostname from raspberrypi to the
# name you supply [second argument]
# 2. Set domain to "local"
# 3. Enable ssh by default, and create authorized_keys
# from the file named ssh_key.pub
# 4. Set static IP for eth0 based on hostname
# 5. Write WiFi configuration into wpa_supplicant.conf
# from the file named wifi.txt, which you must format:
#network={
# ssid="your_wifi_network"
# psk="your_wifi_password"
# scan_ssid=1
#}
if [[ ! -f $1 ]]; then
echo "Invalid image specified: $1"
echo "Usage: $0 <raspbian.img> <short_hostname>"
echo "e.g. $0 raspbian.img blueberry"
echo "Cluster: $0 <raspbian.img> <rack> <node>"
echo "e.g. $0 raspbian.img 1 0 "
echo
echo "Failed."
elif [[ -z $2 ]]; then
echo "You must supply a hostname."
echo "Usage: $0 <raspbian.img> <short_hostname>"
echo "e.g. $0 raspbian.img blueberry"
echo "Cluster: $0 <raspbian.img> <rack> <node>"
echo "e.g. $0 raspbian.img 1 0 "
echo
echo "Failed."
elif [[ ! -a ssh.pub ]]; then
echo "Please copy a public key into ssh.pub"
echo
echo "Failed."
else
echo "Customizing raspbian image:"
rack=0
host=1
name="${2}"
if [[ "${2}" == "data" ]]; then
rack=0
host=2
name="data"
elif [[ $# -gt 2 ]]; then
name="r${2}n${3}"
rack="${2}"
host="${3}"
fi
dest=raspbian_${name}.img
cp $1 $dest
bootoff=$((8192*512))
rootoff=$(($(fdisk -l $dest | awk '$7=="Linux"{print $2}')*512))
mkdir tmpmnt
mount -o loop,offset=${rootoff} $dest tmpmnt
echo "${name}.local" > tmpmnt/etc/hostname
sed -i "s/raspberrypi/${name} ${name}.local/g" tmpmnt/etc/hosts
echo "configured hostname..."
echo -e "interface eth0\nstatic ip_address=192.168.3.1${rack}${host}\n" >> tmpmnt/etc/dhcpcd.conf
echo -e "auto eth0\niface eth0 inet manual\n" >> tmpmnt/etc/network/interfaces
echo "configured static ethernet..."
if [[ -f wifi.txt ]]
then
sed -i "s/GB/US/" tmpmnt/etc/wpa_supplicant/wpa_supplicant.conf
cat wifi.txt >> tmpmnt/etc/wpa_supplicant/wpa_supplicant.conf
echo "configured wifi..."
fi
mkdir -p tmpmnt/home/pi/.ssh
cp ssh.pub tmpmnt/home/pi/.ssh/authorized_keys
sync
umount tmpmnt
mount -o loop,offset=${bootoff} $dest tmpmnt
touch tmpmnt/ssh
echo "configured ssh..."
sync
umount tmpmnt
rm -rf tmpmnt
echo "done. Once powered on, your RPi3 should be accessible over"
echo "SSH at ${name}.local with mDNS running on one local host."
echo "To write an SD card, please run dmesg | tail or fdisk -l, then"
echo "# dd bs=4M conv=fsync status=progress if=raspbian_${name}.img of=/dev/sdX"
fi