-
Notifications
You must be signed in to change notification settings - Fork 1
/
make_image.sh
executable file
·121 lines (105 loc) · 3.3 KB
/
make_image.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env bash
# Install/dd a raspberry pi os image to SD card
# copy ovdr files in sync
# optionally create /boot/ssh and wpa_supplicatn files
#
# You need to download a raspberry pi os image first
# sudo call integrity check: only root should be able to change script
l=($(ls -l `readlink -f $0`))
[ ${l[0]:2:1} != "-" ] && [ "${l[2]}" != "root" ] ||
[ ${l[0]:5:1} != "-" ] && [ "${l[3]}" != "root" ] ||
[ ${l[0]:8:1} != "-" ] && { echo -e "only root should be able to modify\n${l[@]}"; exit 1;}
OS=$(uname)
if [ $# -lt 2 ]; then
echo "attached drives:"
[ $OS == "Darwin" ] \
&& diskutil list \
|| (mount | grep -e sda -e mmcblk && ls /dev/sd* /dev/mmcblk* | sort)
echo
echo "downloaded images:"
ls -t *.zip *.xz 2>/dev/null
echo
echo "usage: $0 <image.zip|xz> <rdiskpath>"
exit
fi
zip="$1"
rdisk="$2"
sudo -v
read -p "dd $1 contents to $2 drive? ([y]/n) " YN
####
if [ "$YN" != "n" ]; then
test -s "$zip" || exit
#test -b "$rdisk" || exit
test -e "$rdisk" || exit
if [ $OS == "Darwin" ]; then
diskutil unmountDisk "$rdisk"
else
sudo umount $rdisk
fi
unzip $1 || xz -k -d $1 || exit
image="$(ls -t *.img | head -1)"
test -s "$image" || exit
set +x
read -p "dd over $image? (ctrl+c for no)"
if [ $OS == "Darwin" ]; then
BS="4m"
else
BS="4M"
fi
echo "send 'killall -USR1 dd' for progress"
set -x
sudo dd if="$image" of="$2" bs=$BS conv=fsync status=progress || exit
rm -rf "$image"
set +x
echo "done dd'ing image"
fi
#####
rdiskpartroot=$(sudo fdisk -l $rdisk | tail -1 | awk '{print $1}' )
rdiskpartboot=$(sudo fdisk -l $rdisk | tail -2 | head -1 | awk '{print $1}' )
if [ $OS == "Linux" ]; then
read -p "Prepare ${rdiskpartroot} with ~pi copy, our keys,etc? ([y]/n) " YN
if [ "$YN" != "n" ]; then
mkdir -p /mnt/root
sudo mount ${rdiskpartroot} /mnt/root || exit
cd sync
test -f home/pi/.ssh/authorized_keys || (echo "didn't find authorized_keys in sync" && exit )
/usr/bin/find .
sudo rsync -rva ./ /mnt/root/
sudo chown -R 1000:1000 /mnt/root/home/pi/.ssh
sudo chmod 700 /mnt/root/home/pi/
sudo chmod 700 /mnt/root/home/pi/.ssh
sudo chmod 600 /mnt/root/home/pi/.ssh/authorized_keys
sudo umount ${rdiskpartroot}
echo "to double check:"
echo "sudo mount ${rdiskpartroot} /mnt/root && ls -a /mnt/root/home/pi"
fi
echo "Prepare ${rdiskpartboot} with wpa_supplicant.conf,ssh start? (y/[n]) "
read -p "Note: if you plan to use startup.sh from pitheater you should skip this " YNN
if [ "$YNN" = "y" ]; then
mkdir -p /mnt/boot
read -p "Touch /boot/ssh on '$rdiskpartboot'? (y/[n]) " SSH
if [ "$SSH" = "y" ]; then
sudo mount ${rdiskpartboot} /mnt/boot \
&& sudo touch /mnt/boot/ssh && sudo umount /mnt/boot
fi
read -p "Setup wifi client at /boot/wpa_supplicant.conf on '$rdiskpartboot'? (y/[n]) " WIFI
if [ "$WIFI" = "y" ]; then
read -p "Wifi ssid: " SSID
read -p "Wifi password: " PASSWORD
echo "Genrating config for SSID '$SSID' PASSWORD '$PASSWORD'"
sudo mount ${rdiskpartboot} /mnt/boot
sudo tee /mnt/boot/wpa_supplicant.conf <<EOM
country=BE
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
ssid="$SSID"
psk="$PASSWORD"
}
EOM
sudo umount /mnt/boot
fi
echo "to double check:"
echo "sudo mount ${rdiskpartboot} /mnt/boot && ls /mnt/boot"
fi
fi