-
Notifications
You must be signed in to change notification settings - Fork 1
/
entry.sh
executable file
·123 lines (105 loc) · 2.61 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
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
#!/bin/bash
set -m
function start_udev()
{
udevd &
udevadm trigger &> /dev/null
}
function remove_buildtime_env_var()
{
unset QEMU_CPU
}
# Send SIGTERM to child processes of PID 1.
function signal_handler()
{
kill $pid
}
# On ResinOS 2.x devices, the hostname is set by the hostOS.
# For backward compatibility, we only update the hostname for ResinOS 1.x devices.
function update_hostname()
{
if [ ${RESIN_DEVICE_UUID:0:7} != ${HOSTNAME:0:7} ]; then
# For 1.x Devices only.
HOSTNAME="$RESIN_DEVICE_TYPE-${RESIN_DEVICE_UUID:0:7}"
echo $HOSTNAME > /etc/hostname
echo "127.0.1.1 $HOSTNAME" >> /etc/hosts
hostname "$HOSTNAME"
fi
}
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."
for var in $(compgen -e); do
printf '%q="%q"\n' "$var" "${!var}"
done > /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
mount -t tmpfs -o mode=0755 cgroup /sys/fs/cgroup
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
CMD=$(which "$1")
# echo error message, when executable file doesn't exist.
if [ $? == '0' ]; then
shift
if [ ! -z "$RESIN" ] && [ ! -z "$RESIN_DEVICE_UUID" ]; then
# run this on resin device only
start_udev
fi
"$CMD" "$@" &
pid=$!
wait "$pid"
exit_code=$?
fg &> /dev/null || exit "$exit_code"
else
echo "Command not found: $1"
exit 1
fi
}
remove_buildtime_env_var
INITSYSTEM=$(echo "$INITSYSTEM" | awk '{print tolower($0)}')
case "$INITSYSTEM" in
'1' | 'true')
INITSYSTEM='on'
;;
esac
if [ ! -z "$RESIN" ] && [ ! -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