-
Notifications
You must be signed in to change notification settings - Fork 0
/
inject_to_sdcard.sh
executable file
·147 lines (125 loc) · 4.38 KB
/
inject_to_sdcard.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
#!/usr/bin/env bash
#
# This script will inject settings and files into the sdcard.
#
set -e
cd "$(dirname "$0")"
. common.sh
. mass_storage.sh
. change_settings.sh
MOUNT_ROOT="/media/$USER/"
umount_all() {
echo "Umounting ..."
sudo umount "${MOUNT_ROOT}/boot" "${MOUNT_ROOT}/rootfs" || true
sudo rmdir "${MOUNT_ROOT}/boot" "${MOUNT_ROOT}/rootfs" || true
}
mount_all() {
local device="$1"
echo "Mounting $device ..."
sudo mkdir -p "${MOUNT_ROOT}/boot" "${MOUNT_ROOT}/rootfs"
sudo mount "${device}1" "${MOUNT_ROOT}/boot"
sudo mount "${device}2" "${MOUNT_ROOT}/rootfs"
}
main() {
local img_file="$1"
local device="$2"
# Prompt user if user config file is not generated yet.
if [ ! -f "$SETTINGS_SH" ]; then
msg_fail "'$SETTINGS_SH' is not found. Please run ./change_settings.sh to generate it."
exit 2
fi
# Require the SD card device.
if [ -z "$device" ]; then
msg_fail "Please provide the device (ex /dev/sdz) of the SD card."
echo
flags_help
exit 1
fi
# Warn the user.
msg_warn "This will destroy the $device. Please confirm before we move on!"
if [ ${FLAGS_force} -ne ${FLAGS_TRUE} ]; then
read -r -p "Are you sure? [y/N] " response
case "$response" in
[yY][eE][sS]|[yY])
true
;;
*)
exit 1
;;
esac
fi
echo "- Unmounting existing partitions (if any) ..."
umount_all
# Overwrite the drive!
echo "- Write the Raspberry Pi image into SD card [$device] ..."
sudo dd if="$img_file" of="$device" bs=1M status=progress
echo "- Re-detect the partition ..."
sudo partprobe "$device" # re-detect the partition table.
sleep 3
echo "- Mounting SD card ..."
mount_all "$device"
###########################################################################
#
# Let's do some real works now !!!!!
#
###########################################################################
local PROGRAM_DIR="${MOUNT_ROOT}/rootfs/root/teslacam"
echo "- Copying all scripts into $PROGRAM_DIR ..."
sudo mkdir -p "$PROGRAM_DIR"
sudo cp -v -rf -L * "$PROGRAM_DIR"
# Copy ssh credential for the host to login.
#
echo "- Copy ssh credential ..."
local SSH_ROOT="${MOUNT_ROOT}/rootfs/root/.ssh/"
sudo mkdir -p "$SSH_ROOT"
# For user to login this device.
#
cat ~/.ssh/id_rsa.pub | sudo tee -a "$SSH_ROOT"/authorized_keys
# For this device to upload.
#
local reminder=""
if [ ! -f "${CONF_ID_RSA}" ]; then
echo "- Generating [${CONF_ID_RSA}] ..."
ssh-keygen -t rsa -C root@teslacam -N '' -f "${CONF_ID_RSA}"
reminder="* Please manually copy ${CONF_ID_RSA}.pub file to your SCP server ($SCP_ROOT)."
fi
cat "${CONF_ID_RSA}" | sudo tee -a "$SSH_ROOT"/id_rsa > /dev/null
sudo chmod 600 "$SSH_ROOT"/id_rsa
cat "${CONF_ID_RSA}".pub | sudo tee -a "$SSH_ROOT"/id_rsa.pub
# Copy .bashrc and .tmux files
sudo cp conf/.bashrc conf/.tmux.conf "${MOUNT_ROOT}/rootfs/root/"
# Configure gadget mass storage.
local BOOT_CONFIG_FILE="${MOUNT_ROOT}/boot/config.txt"
echo "- Configure '$BOOT_CONFIG_FILE' ..."
echo "dtoverlay=dwc2" | sudo tee -a "$BOOT_CONFIG_FILE"
echo "hdmi_force_hotplug=1" | sudo tee -a "$BOOT_CONFIG_FILE"
# rc.local
#
local RC_LOCAL_FILE="${MOUNT_ROOT}/rootfs/etc/rc.local"
echo "- Remove the last line of the '$RC_LOCAL_FILE' ..."
sudo sed -i '$d' "$RC_LOCAL_FILE" # "exit 0"
echo "- Install run once program into '$RC_LOCAL_FILE' ..."
echo "/root/teslacam/run_once.sh /root/teslacam/setup_system.sh >> /var/log/setup.log 2>&1" | sudo tee -a "$RC_LOCAL_FILE"
echo "- Install programs into '$RC_LOCAL_FILE' ..."
echo "tmux new-session -d -s teslacam -n context" | sudo tee -a "$RC_LOCAL_FILE"
echo "tmux send-keys -t teslacam:context '/root/teslacam/context.sh' Enter" | sudo tee -a "$RC_LOCAL_FILE"
echo "tmux new-window -t teslacam -n button" | sudo tee -a "$RC_LOCAL_FILE"
echo "tmux send-keys -t teslacam:button '/root/teslacam/button.sh' Enter" | sudo tee -a "$RC_LOCAL_FILE"
# Move back the "exit 0" line.
echo "- Add back the last line into '$RC_LOCAL_FILE' ..."
echo "exit 0" | sudo tee -a "$RC_LOCAL_FILE"
# Tear down ...
sync
sleep 3 # Somehow we need this to avoid the device busy issue.
umount_all
sync
sync
# sudo udisksctl power-off -b "$device"
msg_pass "Done"
echo
echo "$reminder"
}
FLAGS_HELP="USAGE: $0 [flags] image_file /dev/sdcard"
parse_args "$@"
eval set -- "${FLAGS_ARGV}"
main "$@"