-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpids
executable file
·224 lines (188 loc) · 4.64 KB
/
pids
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
#!/bin/bash
# pids
# (c) 2019 Andy Smith <[email protected]>
# This is licensed under the MIT License, a copy of which can
# be found at https://opensource.org/licenses/MIT
# Paths
FIM="/usr/bin/fim"
SHUTDOWN="/sbin/shutdown"
SYSTEMCTL="/bin/systemctl"
PASSWD="/usr/bin/passwd"
MOUNT="/bin/mount"
UMOUNT="/bin/umount"
RSYNC="/usr/bin/rsync"
DF="/bin/df"
WGET="/usr/bin/wget"
USB_DEV="/dev/sda1"
USB_DIR="/mnt/usb"
PERSISTENT_DIR="/opt/pids"
PIDS_IMAGE_DIR="/usr/local/share/pids"
CONFIG_FILE="pids.conf"
# Default options
INTERVAL=5
RANDOM=1
QUIET=1
DISABLED=0
PERSISTENT=0
source_config() {
CONFIG_FILE="$1"
echo "Sourcing ${CONFIG_FILE}"
source ${CONFIG_FILE}
}
create_persistent_dir() {
echo "Creating persistent directory"
mkdir -p /opt/pids
chown pi:pi /opt/pids
}
delete_persistent_dir() {
echo "Deleting persistent directory"
rm -rf /opt/pids
}
recreate_persistent_dir() {
delete_persistent_dir
create_persistent_dir
}
copy_to_persistent_dir() {
echo "Copying data to persistent directory"
${RSYNC} ${USB_DIR}/* ${PERSISTENT_DIR}
}
run_fim() {
IMAGE_PATH="$1"
echo "Running FIM in ${IMAGE_PATH}"
${FIM} -a ${RANDOM_OPT} ${QUIET_OPT} -R ${IMAGE_PATH} -c "while(1){display;sleep \"${INTERVAL}\";next;}"
}
set_pi_passwd() {
echo "Setting password for pi user"
echo -e "pids\npids" | ${PASSWD} pi
}
delete_pi_passwd() {
echo "Deleting password for pi user"
${PASSWD} -d pi
}
ensure_usb_dir() {
echo "Ensuring ${USB_DIR} exists"
if [[ ! -d ${USB_DIR} ]]; then
mkdir -p ${USB_DIR}
echo "Created ${USB_DIR}"
fi
}
is_usb_storage_mounted() {
${DF} ${USB_DIR} >/dev/null 2>&1
return $?
}
mount_usb_storage() {
echo "Mounting USB storage"
${MOUNT} ${USB_DEV} ${USB_DIR}
}
unmount_usb_storage() {
echo "Unmounting USB storage"
${UMOUNT} ${USB_DIR}
}
download_image_list() {
echo "Downloading image list"
if [[ -f ${PERSISTENT_DIR}/${CONFIG_FILE} ]]; then
mv ${PERSISTENT_DIR}/${CONFIG_FILE} /tmp
recreate_persistent_dir
mv /tmp/${CONFIG_FILE} ${PERSISTENT_DIR}/
else
cp ${USB_DIR}/${CONFIG_FILE} ${PERSISTENT_DIR}/
recreate_persistent_dir
fi
${WGET} ${URL} -O ${PERSISTENT_DIR}/remote-images.txt
if [[ $? -ne 0 ]]; then
echo "Failed to download images - restarting"
${SYSTEMCTL} restart pids
fi
}
download_remote_images() {
echo "Downloading images"
cd ${PERSISTENT_DIR}
${WGET} -i ${PERSISTENT_DIR}/remote-images.txt
}
# Ensure the local pi user has no password, to prevent SSH logins with a password
delete_pi_passwd
# Check to see if the USB storage device exists
if [[ -b ${USB_DEV} ]]; then
ensure_usb_dir
if [[ is_usb_storage_mounted -eq 0 ]]; then
unmount_usb_storage
fi
mount_usb_storage
if [[ -f ${USB_DIR}/${CONFIG_FILE} ]]; then
echo "Sourcing ${USB_DIR}/${CONFIG_FILE}"
source ${USB_DIR}/${CONFIG_FILE}
fi
if [[ ${PERSISTENT} -eq 1 ]]; then
recreate_persistent_dir
copy_to_persistent_dir
IMAGE_DIR="${PERSISTENT_DIR}"
else
echo "Persistence is not enabled"
IMAGE_DIR="${USB_DIR}"
fi
elif [[ -d ${PERSISTENT_DIR} ]]; then
if [[ -f ${PERSISTENT_DIR}/${CONFIG_FILE} ]]; then
echo "Sourcing ${PERSISTENT_DIR}/${CONFIG_FILE}"
source ${PERSISTENT_DIR}/${CONFIG_FILE}
fi
IMAGE_DIR="${PERSISTENT_DIR}"
else
# Set a temporary password
set_pi_passwd
# Disable pids
DISABLED=1
# Display the 'no USB device detected` splash screen, and reboot in 2 minutes
${FIM} -a -q ${PIDS_IMAGE_DIR} pids-nousb.png -c 'quit'
${SHUTDOWN} -r 2
fi
# Check to see if we're supposed to be disabled
if [[ ${DISABLED} -eq 1 ]]; then
echo "Disabling pids"
${SYSTEMCTL} stop pids
exit
fi
# Check to see if we should be showing images in a random order (default: yes)
if [[ ${RANDOM} -eq 1 ]]; then
echo "Enabling randomiser"
RANDOM_OPT="-u"
else
RANDOM_OPT=""
fi
# Check to see if we should be in quiet mode (default: yes)
if [[ ${QUIET} -eq 1 ]]; then
echo "Enabling quiet mode"
QUIET_OPT="-q"
else
QUIET_OPT=""
fi
# Check to see if there's an SSH key configured
if [[ ! -z ${SSH_KEY} ]]; then
echo "SSH key found"
mkdir -p /home/pi/.ssh
echo "${SSH_KEY}" >/home/pi/.ssh/authorized_keys
chown -R pi:pi /home/pi/.ssh
chmod 700 /home/pi/.ssh
chmod 644 /home/pi/.ssh/authorized_keys
fi
# Allow system to settle after boot
sleep 3
# If URL is specified, download the images
if [[ ! -z ${URL} ]]; then
if [[ ! -z ${REFRESH} ]]; then
# Refresh
while true; do
download_image_list
download_remote_images
run_fim ${IMAGE_DIR} &
sleep ${REFRESH}
killall fim
done
else
# One shot
download_image_list
download_remote_images
run_fim ${IMAGE_DIR}
fi
else
run_fim ${IMAGE_DIR}
fi