Skip to content

Commit

Permalink
feat: add support for custom user and workdir in init
Browse files Browse the repository at this point in the history
  • Loading branch information
edubart committed Jul 26, 2023
1 parent 8bf90a4 commit 0b46d07
Showing 1 changed file with 26 additions and 23 deletions.
49 changes: 26 additions & 23 deletions skel/sbin/init
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,42 @@ busybox mount -t tmpfs tmpfs /dev/shm -o nosuid,nodev,mode=1777
[ -f /etc/hostname ] && busybox hostname -F /etc/hostname
busybox ifconfig lo up

# custom init
if [ -f /proc/device-tree/cartesi/init ]; then
. /proc/device-tree/cartesi/init
fi
# set environment
[ -f /etc/environment ] && . /etc/environment
export PATH="$PATH:/opt/cartesi/bin"

# execute init from device tree
[ -f /proc/device-tree/cartesi/init ] && . /proc/device-tree/cartesi/init

# use entrypoint from device tree when available
# set entrypoint from device tree when available
if [ -s /proc/device-tree/cartesi/entrypoint ]; then
ENTRYPOINT=". /proc/device-tree/cartesi/entrypoint"
else
elif [ -n "$*" ]; then
ENTRYPOINT="$*"
fi

# execute entrypoint
if [ -n "$ENTRYPOINT" ]; then
[ -f /etc/environment ] && . /etc/environment
export PATH="/opt/cartesi/bin:$PATH"

# can login as dapp user?
if [ ! "$single" == "yes" ] && busybox id dapp &> /dev/null; then
if [ -c /dev/rollup ]; then
busybox chown root:dapp /dev/rollup
busybox chmod 660 /dev/rollup
fi
if [ -c /dev/yield ]; then
busybox chown root:dapp /dev/yield
busybox chmod 660 /dev/yield
fi
export HOME=~dapp USER=dapp LOGNAME=dapp
else
export HOME=~root USER=root LOGNAME=root
# set user
if [ -z "$USER" ]; then
busybox id dapp &> /dev/null && USER=dapp || USER=root
fi
export USER LOGNAME=$USER HOME="$(eval echo ~$USER)"

# set working directory
[ -z "$WORKDIR" ] && WORKDIR="$HOME"
[ ! -d "$WORKDIR" ] && WORKDIR=/

# give user group access to rollup and yield devices
[ -c /dev/rollup ] && \
busybox chown :$(busybox id -g $USER) /dev/rollup && \
busybox chmod g+rw /dev/rollup
[ -c /dev/yield ] &&
busybox chown :$(busybox id -g $USER) /dev/yield && \
busybox chmod g+rw /dev/yield

cd $HOME &&
# finally execute
cd "$WORKDIR" &&
busybox setsid \
busybox cttyhack \
busybox su -p $USER -c "$ENTRYPOINT"
Expand Down

0 comments on commit 0b46d07

Please sign in to comment.