forked from balena-io-library/resin-rpi-raspbian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entry.sh
executable file
·89 lines (76 loc) · 1.86 KB
/
entry.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
#!/bin/bash
# Send SIGTERM to child processes of PID 1.
function signal_handler()
{
kill $pid
}
function update_hostname()
{
HOSTNAME="$HOSTNAME-${RESIN_DEVICE_UUID:0:7}"
echo $HOSTNAME > /etc/hostname
echo "127.0.1.1 $HOSTNAME" >> /etc/hosts
hostname "$HOSTNAME"
}
function mount_dev()
{
mkdir -p /tmp
mount -t devtmpfs none /tmp
mkdir -p /tmp/shm
mount --move /dev/shm /tmp/shm
mkdir -p /tmp/mqueue
mount --move /dev/mqueue /tmp/mqueue
mkdir -p /tmp/pts
mount --move /dev/pts /tmp/pts
touch /tmp/console
mount --move /dev/console /tmp/console
umount /dev || true
mount --move /tmp /dev
# Since the devpts is mounted with -o newinstance by Docker, we need to make
# /dev/ptmx point to its ptmx.
# ref: https://www.kernel.org/doc/Documentation/filesystems/devpts.txt
ln -sf /dev/pts/ptmx /dev/ptmx
mount -t debugfs nodev /sys/kernel/debug
}
function init_systemd()
{
GREEN='\033[0;32m'
echo -e "${GREEN}Systemd init system enabled."
env > /etc/docker.env
printf '#!/bin/bash\n exec ' > /etc/resinApp.sh
printf '%q ' "$@" >> /etc/resinApp.sh
chmod +x /etc/resinApp.sh
mkdir -p /etc/systemd/system/launch.service.d
cat <<-EOF > /etc/systemd/system/launch.service.d/override.conf
[Service]
WorkingDirectory=$(pwd)
EOF
exec /sbin/init quiet systemd.show_status=0
}
function init_non_systemd()
{
# trap the stop signal then send SIGTERM to user processes
trap signal_handler SIGRTMIN+3 SIGTERM
udevd &
udevadm trigger &> /dev/null
CMD=$(which "$1")
# echo error message, when executable file doesn't exist.
if [ $? == '0' ]; then
shift
"$CMD" "$@" &
pid=$!
wait $pid
else
echo "Command not found: $1"
exit 1
fi
}
if [ ! -z "$RESIN_SUPERVISOR_API_KEY" ] && [ ! -z "$RESIN_DEVICE_UUID" ]; then
# run this on resin device only
update_hostname
mount_dev
fi
if [ "$INITSYSTEM" = "on" ]; then
init_systemd "$@"
else
init_non_systemd "$@"
fi