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 70b67d3
Showing 1 changed file with 36 additions and 34 deletions.
70 changes: 36 additions & 34 deletions skel/sbin/init
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,43 @@ busybox mount -t tmpfs tmpfs /dev/shm -o nosuid,nodev,mode=1777

# net
[ -f /etc/hostname ] && busybox hostname -F /etc/hostname
busybox ifconfig lo up
[ -z "$noloopback" ] && busybox ifconfig lo up

# custom init
if [ -f /proc/device-tree/cartesi/init ]; then
. /proc/device-tree/cartesi/init
fi
# source 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

cd $HOME &&
# 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

# finally execute
(export USER LOGNAME=$USER HOME="$(eval echo ~$USER)"; \
cd ${WORKDIR:-"$HOME"} && \
busybox setsid \
busybox cttyhack \
busybox su -p $USER -c "$ENTRYPOINT"
busybox su -p $USER -c "$ENTRYPOINT")
else
echo "Nothing to do."
fi
Expand All @@ -55,15 +54,18 @@ busybox mount -o ro,remount /
busybox umount -af
busybox poweroff -f

# Copyright 2023 Cartesi Pte. Ltd.
# Copyright Cartesi and individual authors (see AUTHORS)
# SPDX-License-Identifier: LGPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at http://www.apache.org/licenses/LICENSE-2.0
# This program is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
# You should have received a copy of the GNU General Public License along with
# this program (see COPYING). If not, see <https://www.gnu.org/licenses/>.
#

0 comments on commit 70b67d3

Please sign in to comment.