forked from PaperCutSoftware/RaspberryPiReleaseStation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runme
executable file
·547 lines (436 loc) · 17.2 KB
/
runme
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
#!/usr/bin/env bash
# Install the PaperCut Release Client on a Raspberry Pi Computer.
# Copyright PaperCut software 2016 under the terms of the GNU Open Source
# License Version 3.
# Some information and techniques taken from
# https://github.com/RPi-Distro/raspi-config
# https://github.com/alecthegeek/CCHS_Raspian_for_IoT/
# This script may be run as often as needed.
_FREESPACEWANTED=500 # MByte of free space required to install
FREESPACEWANTED=$_FREESPACEWANTED
_PART_SIZE='+3.5G' # Used by fdisk, root partition size
UPGRADE="" # Default to full install
SERVER_PORT=9191 # Default secure port
SERVER_IP="Edit Me" # Prompt System Admin after install
SERVER_NAME="Edit Me" # Prompt System Admin after install
DOWNLOAD_URL=http://cdn.papercut.com/files/pcng/current/pcng-setup-linux-x64.sh
PP_USER="papercut" # User account to own and run papecut
ask() {
read -p "$1" -r < /dev/tty
echo "$REPLY"
}
# This code is adapted from https://github.com/RPi-Distro/raspi-config/blob/master/raspi-config
resize_sd() {
ROOT_PART=$(mount | sed -n 's|^/dev/\(.*\) on / .*|\1|p')
PART_NUM=${ROOT_PART#mmcblk0p}
if [ "$PART_NUM" = "$ROOT_PART" ]; then
whiptail --msgbox "$ROOT_PART is not an SD card. Don't know how to expand" 20 60 2
return 0
fi
# NOTE: the NOOBS partition layout confuses parted. For now, let's only
# agree to work with a sufficiently simple partition layout
if [ "$PART_NUM" -ne 2 ]; then
whiptail --msgbox "Your partition layout is not currently supported by this tool. You are probably using NOOBS, in which case your root filesystem is already expanded anyway." 20 60 2
return 0
fi
LAST_PART_NUM=$(parted /dev/mmcblk0 -ms unit s p | tail -n 1 | cut -f 1 -d:)
if [ $LAST_PART_NUM -ne $PART_NUM ]; then
whiptail --msgbox "$ROOT_PART is not the last partition. Don't know how to expand" 20 60 2
return 0
fi
# Get the starting offset of the root partition
PART_START=$(sudo parted /dev/mmcblk0 -ms unit s p | grep "^${PART_NUM}" | cut -f 2 -d: | sed 's/[^0-9]//g')
[ "$PART_START" ] || return 1
# Return value will likely be error for fdisk as it fails to reload the
# partition table because the root fs is mounted
sudo fdisk /dev/mmcblk0 <<EOF
p
d
$PART_NUM
n
p
$PART_NUM
$PART_START
$_PART_SIZE
p
w
EOF
# now set up an init.d script
cat <<EOF | sudo tee > /dev/null /etc/init.d/resize2fs_once &&
#!/bin/sh
### BEGIN INIT INFO
# Provides: resize2fs_once
# Required-Start:
# Required-Stop:
# Default-Start: 3
# Default-Stop:
# Short-Description: Resize the root filesystem to fill partition
# Description:
### END INIT INFO
. /lib/lsb/init-functions
case "\$1" in
start)
log_daemon_msg "Starting resize2fs_once" &&
resize2fs /dev/$ROOT_PART &&
update-rc.d resize2fs_once remove &&
rm /etc/init.d/resize2fs_once &&
log_end_msg \$?
;;
*)
echo "Usage: \$0 start" >&2
exit 3
;;
esac
EOF
sudo chmod +x /etc/init.d/resize2fs_once &&
sudo update-rc.d resize2fs_once defaults
}
usage() {
echo "$0" '-i <server-ip> -n <server-name> [-f <installer-file>] [-u]'
echo '-i PaperCut server IP addresss xxx.xxx.xxx.xxx'
echo '-n PaperCut server Network IP name e.g. papercut.example.com'
echo '-f is the name of the PaperCut installer file, If not present the script will attempt to download an installer'
echo '-u Only upgrade the software'
echo '-s Skip the check of free disk space. Useful with -u'
echo '-h Print this help text'
exit
}
do_wifi_country() { # Adapted from raspi-config script
oIFS="$IFS"
IFS="/"
value=$(tail -n +26 /usr/share/zoneinfo/iso3166.tab | tr '\t' '/' | tr '\n' '/')
COUNTRY=$(whiptail --menu "Select the country in which the Pi is to be used" 20 60 10 ${value} 3>&1 1>&2 2>&3)
if [ $? -eq 0 ];then
if [ -e /etc/wpa_supplicant/wpa_supplicant.conf ]; then
if sudo grep -q "^country=" /etc/wpa_supplicant/wpa_supplicant.conf ; then
sudo sed -i -e "s/^country=.*/country=$COUNTRY/g" /etc/wpa_supplicant/wpa_supplicant.conf
else
sudo sed -i -e "1i country=$COUNTRY" /etc/wpa_supplicant/wpa_supplicant.conf
fi
else
echo "country=$COUNTRY" | sudo tee /etc/wpa_supplicant/wpa_supplicant.conf > /dev/null
fi
whiptail --msgbox "Wi-fi country set to $COUNTRY" 20 60 1
else
echo Error selecting wifi country code
fi
IFS=$oIFS
}
# Main
while getopts :i:n:p:f:uh FLAG ; do
case $FLAG in
h) usage
;;
u) UPGRADE=YES
;;
s) FREESPACEWANTED=""
;;
i) SERVER_IP=$OPTARG
;;
n) SERVER_NAME=$OPTARG
;;
p) SERVER_PORT=$OPTARG
;;
f) INSTALLER_FILE=$(cd $(dirname "$OPTARG") ; pwd)/"$OPTARG" # Need full path to installer file
;;
\?) usage
;;
esac
done
# START HERE
if [[ ! -e /etc/issue ]] || ! grep "Raspbian" /etc/issue > /dev/null ; then
echo Please run this script on Raspian GNU/Linux.
echo You can download Linux from the Raspberry Pi website at https://www.raspberrypi.org/downloads/raspbian/
echo 'The Lite version is recommended to save space, but the full version will also work'
exit
fi
if [[ ! -d "$(eval echo ~$PP_USER)" && -n "$UPGRADE" ]] ; then
echo User $PP_USER does not exist. Upgrade option is being ignored
UPGRADE=""
if [[ -z "$FREESPACEWANTED" ]] ; then
FREESPACEWANTED=$_FREESPACEWANTED
echo also checking for $FREESPACEWANTED free disk space.
fi
fi
if [[ -n "$FREESPACEWANTED" && $(df -m --output=avail / | tail -1) -lt $FREESPACEWANTED ]] ; then
echo Insufficuent disk space -- Please make sure there is at least $FREESPACEWANTED Mb and run run again
if [[ $(df -m --output=size / | tail -1) -lt 2800 ]] ; then
echo N.B. If you have NOT already autosized your image to fit all the space on the SD card
echo 'I can resize the image for you to 3G which will be enough if you are using Jessie "lite"'
if [[ $(ask "Do you want to resize the SD card image to 3G? [N/y]") =~ [Yy] ]] ; then
resize_sd
echo The Pi will now reboot emmdialty to complete the resize. Please rerun $0 again
ask "Press any key to continue"
sudo init 6
fi
echo Note you can manualy resize the SD card from by running the command \"sudo raspi-config\"
fi
exit -1
fi
if [[ -z "$INSTALLER_FILE" ]] ; then
echo About to download the latest PaperCut installer file. This will take some time
echo
INSTALLER_FILE=/tmp/${DOWNLOAD_URL##*/}
wget $DOWNLOAD_URL -O $INSTALLER_FILE --progress=dot:giga
fi
if [[ ! -e "$INSTALLER_FILE" ]] ; then
echo "Can\'t find installer file $INSTALLER_FILE"
exit
fi
chmod aog+x "$INSTALLER_FILE"
echo About to upgrade Raspian to the latest version -- this may take some time
echo
sudo apt-get update && sudo apt-get upgrade -y && sudo apt-get dist-upgrade -y
echo About to install X Windows and the Java runtime if required -- this may take some time
sudo apt-get install -y xorg xinit openjdk-8-jre pwgen matchbox-keyboard
sudo apt-get autoremove
sudo apt-get clean
echo
sudo sed -i -e '/\(trap.*2\)\|\(set.*\+m\)/d' /etc/profile
sudo sed -i -e $'3i\
trap \'\' 2 # Disable ctrl-c control\
set +m # Disable job control\
' /etc/profile
if [[ -z "$UPGRADE" ]] ; then
echo About to run various setup programs. Please configure your locale, keyboard and timezone.
echo 'NB You will need a physical keyboard attached to the Pi (even if using a remote ssh connection)'
ask "Press any key to continue"
echo
sudo dpkg-reconfigure keyboard-configuration
sudo dpkg-reconfigure locales
sudo dpkg-reconfigure tzdata
do_wifi_country
PP_PW=$(pwgen -s 18 1) # Give the new user a secure password that we won't know
if [[ -d "$(eval echo ~$PP_USER)" ]] ; then
# User already exists
sudo killall -u $PP_USER
sudo userdel -fr $PP_USER
fi
PP_HOME=/usr/local/$PP_USER # Papercut software is installed in home dir of PaperCut user
(echo $PP_PW; echo $PP_PW ) | sudo adduser --gecos "PaperCut Admin User" --home $PP_HOME $PP_USER
sudo usermod -aG video $PP_USER
echo
echo PaperCut application user created with secure password. For security resons you cannot log in as papercut
echo
else
sudo killall -u $PP_USER
PP_HOME="$(eval echo ~$PP_USER)"
sudo rm -rf "$PP_HOME"/release
fi
echo About to unpack the PaperCut installer. This may take some time
echo
sudo -u $PP_USER "$INSTALLER_FILE" -e -- -C "$PP_HOME/.." --checkpoint=.1000 --exclude papercut/release/runtime papercut/release
if [[ ! -d "$PP_HOME/release" ]] ; then
echo Failed to unpack the installer. Please download again
exit -1
fi
find $PP_HOME -name \*.jar.pack -exec sh -c "echo Unpacking \$1 && sudo -u $PP_USER unpack200 \$1 \${1%%.pack}" sh {} \;
sudo -u $PP_USER chmod ugo+x $PP_HOME/release/*.sh
[[ -e $PP_HOME/release-station.log ]] &&
sudo rm $PP_HOME/release-station.log
sudo touch /var/log/release-station.log
sudo ln -s /var/log/release-station.log $PP_HOME/release-station.log
sudo chown $PP_USER:$PP_USER /var/log/release-station.log
sudo chown $PP_USER:$PP_USER $PP_HOME/release-station.log
if [[ -z "$UPGRADE" ]] ; then
sudo tee /boot/pc-connection.properties <<EOF
server-ip=$SERVER_IP
server-port=$SERVER_PORT
server-name=$SERVER_NAME
EOF
sudo mv $PP_HOME/release/config.properties /boot/pc-config.properties
# sudo mv $PP_HOME/release/client-machine-aliases.properties /boot/client-machine-aliases.properties
fi
for i in connection.properties config.properties ; do
[[ -e $PP_HOME/release/$i ]] && sudo rm $PP_HOME/release/$i
sudo ln -s /boot/pc-$i $PP_HOME/release/$i
sudo chown $PP_USER:$PP_USER $PP_HOME/release/$i
done
# Make Pi boot into user papercut text mode
if command -v systemctl > /dev/null && systemctl | grep -q '^ *\-\.mount' ; then
sudo systemctl set-default multi-user.target
sudo cp /etc/systemd/system/[email protected] /etc/systemd/system/[email protected]
sudo sed -i -e "s/--autologin \+pi/--autologin $PP_USER/" /etc/systemd/system/[email protected]
[[ -e /etc/systemd/system/getty.target.wants/[email protected] ]] &&
sudo rm /etc/systemd/system/getty.target.wants/[email protected]
sudo ln -fs /etc/systemd/system/[email protected] /etc/systemd/system/getty.target.wants/[email protected]
else
echo Upgrade to current version of Debian failed. Can\'t use systemd
exit -1
fi
# Remove old calls to release station
sudo sed -i -e '/pc-release-linux.sh/d' $PP_HOME/.profile
# Force locale etc setting on 1st boot
sudo touch /boot/prompt4Locale
if ! grep "prompt4Locale" $PP_HOME/.profile ; then
cat <<EOF | sudo tee -a $PP_HOME/.profile
echo -n "Network hostname is "
hostname
echo -n "Network IP addresses are "
hostname -I
read -p "Press any key to continue" -n 1 < /dev/tty
if [[ -e /boot/prompt4Locale ]] ; then
echo
echo "WARNING:"
echo "Please set up:"
echo "locale,"
echo " keyboard,"
echo " timezone and"
echo " WiFi region"
echo
echo "You will need the login credentials for the local admin account"
echo "and connect via ssh to \"\$(hostname)\". You can then run the command"
echo "\"sudo raspi-config\" and select the \"Internationalisation Options\" menu."
echo "After completing the configuration process please delete the file"
echo "/boot/prompt4Locale"
read -p "Press any key to continue" -n 1 < /dev/tty
fi
EOF
fi
echo 'xinit ~/release/pc-release-linux.sh -- vt$XDG_VTNR ;exit'|sudo tee -a "$PP_HOME/.profile" > /dev/null
# Finshed set up for Papercut. Now going to add some system mnagement goodness
# Make the pi run file system check/repair on every boot (need to clear out old settings 1st)
sudo sed -i -e 's/ fsck\.\(repair\|mode\)=[^ ]*//g;s/$/ fsck.mode=force fsck.repair=yes/' /boot/cmdline.txt
if [[ $(ask "Do you wish to flip the display 180˚, useful for displays that require specific orientation because of cables etc.?") =~ [Yy] ]] ; then
sudo sed -i -e '/lcd_rotate=/d;$a\
# Added by PaperCut
lcd_rotate=2' /boot/config.txt
fi
#Disable VT switching
cat <<EOF | sudo tee /usr/share/X11/xorg.conf.d/20-disable-vt-switching.conf > /dev/null
Section "ServerFlags"
Option "DontVTSwitch" "true"
EndSection
EOF
echo We can put some host setttings in the /boot/machine.local and you can edit them on your laptop as needed
echo
echo Host name management
echo ====================
echo
echo There are three options:
echo "Keep the current hostname \"$(hostname)\""
echo "Change to a fixed hostname of your choosing"
echo "Have the Pi dynamically allocate a a hostname based on the CPU serial number at boot time"
echo
echo
dynhostname=$'rpi$(sed -nre \'/^Serial\t\t: ........(........)$/s//\\1/p\' /proc/cpuinfo)'
select selection in "Keep the current hostname \"$(hostname)\"" \
"Provide a new hostname" \
"CPU serial number based hostname" ; do
case "$REPLY" in
1) newhostname=$(hostname) ; break ;;
2) newhostname=$(ask "Please provide new hostname ") ; break ;;
3) newhostname=$'rpi$(sed -nre \'/^Serial\t\t: ........(........)$/s//\\1/p\' /proc/cpuinfo)' ; break;;
*) echo "$selection is invalid. Please select from list" ;;
esac
done
sudo tee /boot/machine.local <<EOF
# Edit this file and reboot the Pi.
# NB Insert the SD card into a workstation and you can edit the file as well
newhostname=$newhostname # Host for /etc/host
#newhostname=<fixed hostname> # Or use a different hostname instead
#newhostname=$dynhostname
#
# To enable a wifi network add the following content to a file /boot/wpa_supplicant.conf
# and reboot the Raspiberry Pi
# TODO -- add example here.
#
EOF
cat <<'EOF' | sudo tee /usr/local/sbin/config-hostname > /dev/null
#!/bin/sh
if [ -f /boot/machine.local ] ; then
. /boot/machine.local # dash does not support `source` keyword
oldhostname=$(hostname)
if [ -n "$newhostname" -a "$newhostname" != "$oldhostname" ] ; then
sudo hostname $newhostname
echo $newhostname | sudo tee /etc/hostname > /dev/null
sudo sed -i -re 's/^(..*)'$oldhostname'(.*)$/\1'$newhostname'\2/' /etc/hosts
sudo invoke-rc.d hostname.sh start
sudo invoke-rc.d networking force-reload
sudo invoke-rc.d avahi-daemon force-reload
sudo /etc/init.d/ssh restart
logger "Changed hostname to $(hostname) on $interface"
logger "About to reboot"
sudo init 6
fi
fi
EOF
sudo tee /usr/local/sbin/config-wifi <<'EOFx' > /dev/null
#!/bin/sh
# Can we even manage the wifi?
if ! wpa_cli status 2> /dev/null ; then
echo "Cannot configure wifi -- no wifi hardware?"
exit 0
fi
# Do we have a file with setings?
[ -f /boot/machine.local ] || (echo "No /boot/machine.local settings file found" && exit 0)
. /boot/machine.local # dash does not support `source` keyword
# Do we at least have some wifi settings
[ -z "$ssid" ] && exit 0
[ -z "$id_str" ] && id_str=$ssid
network_id=$(wpa_cli list_networks 2> /dev/null| tail -n +3 | grep "$ssid" |cut -f 1)
if [ -z "$network_id" ] ; then
maxNetworkId=$(wpa_cli list_networks 2> /dev/null | tail -n +3 | tail -n 1 | cut -f 1)]
if [ -z $maxNetworkId ] ; then
network_id=0
else
network_id=$(($maxNetworkID + 1 ))
fi
else
wpa_cli remove_network "$network_id" # Don't care if it fails
fi
if [ -n "$interface" ] && ! grep $interface /etc/network/interfaces ; then
sudo tee -a /etc/network/interfaces <<EOF
allow-hotplug $interface
iface $interface inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp
EOF
fi
wpa_cli <<EOF
add_network $network_id
set_network $network_id ssid "$ssid"
set_network $network_id psk "$psk"
set_network $network_id id_str "$id_str"
enable_network $network_id
save_config
quit
EOF
EOFx
sudo chmod +x /usr/local/sbin/config-hostname
sudo chmod +x /usr/local/sbin/config-wifi
sudo sed -i -re '/config-(hostname|wifi)/d;/exit 0/i\
[ -x /usr/local/sbin/config-hostname ] && /usr/local/sbin/config-hostname\
[ -x /usr/local/sbin/config-wifi ] && /usr/local/sbin/config-wifi
' /etc/rc.local
# shellcheck disable=2154
if [[ "pi" -eq "$USER" ]] ; then
echo "The default account is still named \"$USER\". This should be changed for security reasons"
if [[ $(ask "Do you wish to rename the $USER user account?[y/N] ") =~ ^[Yy]$ ]] ; then
read -rp "Please enter the new user name " < /dev/tty
# Make all these changes under a single sudo session -- must be all done at once
cat <<'EOF' | sudo bash -s $USER $REPLY $HOME
for i in /etc/group /etc/passwd /etc/sudoers /etc/shadow ; do
sed -ri -e '/\b'$1'\b/s/\b'$1'\b/'$2'/g' $i
done
if [[ -f /etc/sudoers.d/010_${1}-nopasswd ]] ; then
# Password setting introduced in 20161018+1
mv /etc/sudoers.d/010_${1}-nopasswd /etc/sudoers.d/010_${2}-nopasswd
sed -ri -e '/\b'$1'\b/s/\b'$1'\b/'$2'/g' /etc/sudoers.d/010_$2-nopasswd
fi
# Rename home directory
mv $3 /home/$2
EOF
USER=$REPLY
echo "User name changed to $USER"
echo please change the password as well
sudo passwd "$USER" # Don't prompt the user for their own password by using sudo
else
echo
fi
fi
# Enable ssh server (Requried for Raspian after November 2016)
sudo touch /boot/ssh
echo 'Setup complete. Please change the password for '"$USER"' if not already done'
ask "Please press any key to reboot the Raspberry Pi"
sudo init 6