Skip to content

Instantly share code, notes, and snippets.

Created June 24, 2017 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/8b0c3c48267a03c1659bd53eae19200d to your computer and use it in GitHub Desktop.
Save anonymous/8b0c3c48267a03c1659bd53eae19200d to your computer and use it in GitHub Desktop.
#!/bin/bash
set -m
function start_udev()
{
udevd &
}
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
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
tini -sg -- "$CMD" "$@" &
pid=$!
wait "$pid"
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment