|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# This command only works in privileged container |
| 4 | +tmp_mount='/tmp/_balena' |
| 5 | +mkdir -p "$tmp_mount" |
| 6 | +if mount -t devtmpfs none "$tmp_mount" &> /dev/null; then |
| 7 | + PRIVILEGED=true |
| 8 | + umount "$tmp_mount" |
| 9 | +else |
| 10 | + PRIVILEGED=false |
| 11 | +fi |
| 12 | +rm -rf "$tmp_mount" |
| 13 | + |
| 14 | +function mount_dev() |
| 15 | +{ |
| 16 | + tmp_dir='/tmp/tmpmount' |
| 17 | + mkdir -p "$tmp_dir" |
| 18 | + mount -t devtmpfs none "$tmp_dir" |
| 19 | + mkdir -p "$tmp_dir/shm" |
| 20 | + mount --move /dev/shm "$tmp_dir/shm" |
| 21 | + mkdir -p "$tmp_dir/mqueue" |
| 22 | + mount --move /dev/mqueue "$tmp_dir/mqueue" |
| 23 | + mkdir -p "$tmp_dir/pts" |
| 24 | + mount --move /dev/pts "$tmp_dir/pts" |
| 25 | + touch "$tmp_dir/console" |
| 26 | + mount --move /dev/console "$tmp_dir/console" |
| 27 | + umount /dev || true |
| 28 | + mount --move "$tmp_dir" /dev |
| 29 | + |
| 30 | + # Since the devpts is mounted with -o newinstance by Docker, we need to make |
| 31 | + # /dev/ptmx point to its ptmx. |
| 32 | + # ref: https://www.kernel.org/doc/Documentation/filesystems/devpts.txt |
| 33 | + ln -sf /dev/pts/ptmx /dev/ptmx |
| 34 | + |
| 35 | + # When using io.balena.features.sysfs the mount point will already exist |
| 36 | + # we need to check the mountpoint first. |
| 37 | + sysfs_dir='/sys/kernel/debug' |
| 38 | + |
| 39 | + if ! mountpoint -q "$sysfs_dir"; then |
| 40 | + mount -t debugfs nodev "$sysfs_dir" |
| 41 | + fi |
| 42 | + |
| 43 | +} |
| 44 | + |
| 45 | +function start_udev() |
| 46 | +{ |
| 47 | + if [ "$UDEV" == "on" ]; then |
| 48 | + if $PRIVILEGED; then |
| 49 | + mount_dev |
| 50 | + if command -v udevd &>/dev/null; then |
| 51 | + unshare --net udevd --daemon &> /dev/null |
| 52 | + else |
| 53 | + unshare --net /lib/systemd/systemd-udevd --daemon &> /dev/null |
| 54 | + fi |
| 55 | + udevadm trigger &> /dev/null |
| 56 | + else |
| 57 | + echo "Unable to start udev, container must be run in privileged mode to start udev!" |
| 58 | + fi |
| 59 | + fi |
| 60 | +} |
| 61 | + |
| 62 | +function init() |
| 63 | +{ |
| 64 | + # echo error message, when executable file is passed but doesn't exist. |
| 65 | + if [ -n "$1" ]; then |
| 66 | + if CMD=$(command -v "$1" 2>/dev/null); then |
| 67 | + shift |
| 68 | + exec "$CMD" "$@" |
| 69 | + else |
| 70 | + echo "Command not found: $1" |
| 71 | + exit 1 |
| 72 | + fi |
| 73 | + fi |
| 74 | +} |
| 75 | + |
| 76 | +UDEV=$(echo "$UDEV" | awk '{print tolower($0)}') |
| 77 | + |
| 78 | +case "$UDEV" in |
| 79 | + '1' | 'true') |
| 80 | + UDEV='on' |
| 81 | + ;; |
| 82 | +esac |
| 83 | + |
| 84 | +start_udev |
| 85 | +init "$@" |
| 86 | +sleep 8 |
| 87 | +startx |
0 commit comments