Skip to content

Commit

Permalink
feat!: new init system using init and entrypoint from device tree
Browse files Browse the repository at this point in the history
  • Loading branch information
edubart committed Oct 6, 2023
1 parent 063d6d3 commit 37e3f43
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 92 deletions.
13 changes: 0 additions & 13 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,6 @@ RUN mkdir -p ${BUILD_BASE}linux-sources && \
make -C ${BUILD_BASE}linux-sources headers_install INSTALL_HDR_PATH=/usr && \
rm ${BUILD_BASE}${LINUX_SOURCES_FILEPATH}

# copy & extract rndaddentropy
# ------------------------------------------------------------------------------
COPY ${RNDADDENTROPY_FILEPATH} ${BUILD_BASE}${RNDADDENTROPY_FILEPATH}
RUN mkdir -p ${BUILD_BASE}/twuewand && \
tar xf ${BUILD_BASE}${RNDADDENTROPY_FILEPATH} \
--strip-components=1 -C ${BUILD_BASE}twuewand && \
rm ${BUILD_BASE}${RNDADDENTROPY_FILEPATH}

RUN cd ${BUILD_BASE}twuewand/rndaddentropy/ && \
make all CFLAGS="-O2 -Wno-error" && \
strip rndaddentropy

# copy tools
COPY linux/ ${BUILD_BASE}tools/linux/

Expand Down Expand Up @@ -87,7 +75,6 @@ ARG MACHINE_EMULATOR_TOOLS_TAR_GZ=machine-emulator-tools.tar.gz

COPY skel/ ${STAGING_BASE}
RUN mkdir -p ${STAGING_BIN} && \
cp ${BUILD_BASE}twuewand/rndaddentropy/rndaddentropy ${STAGING_BIN} && \
cp ${BUILD_BASE}tools/linux/xhalt/xhalt ${STAGING_BIN} && \
cp ${BUILD_BASE}tools/linux/htif/yield ${STAGING_BIN} && \
cp ${BUILD_BASE}tools/linux/rollup/ioctl-echo-loop/ioctl-echo-loop ${STAGING_BIN} && \
Expand Down
4 changes: 2 additions & 2 deletions linux/utils/flashdrive
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
# limitations under the License.
#

for t in /dev/mtdblock*; do
name=$(busybox cat /sys/block/$(busybox basename $t)/device/name)
for t in /dev/pmem*; do
name=$(busybox cat /run/drive-label/$(busybox basename $t))
if [ "$name" = $1 ]; then
echo $t
exit 0
Expand Down
127 changes: 61 additions & 66 deletions skel/opt/cartesi/bin/init
Original file line number Diff line number Diff line change
@@ -1,85 +1,80 @@
#!/bin/busybox sh

# Copyright Cartesi and individual authors (see AUTHORS)
# SPDX-License-Identifier: Apache-2.0
#
# 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
#
# 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.
#

export PATH=/opt/cartesi/bin:"$PATH"

[ ! -f /opt/cartesi/etc/motd -o "$splash" == "no" ] || busybox cat /opt/cartesi/etc/motd

# mount
busybox mkdir -p /dev/pts /dev/shm
busybox mount -t proc proc /proc -o nosuid,nodev,noexec
busybox mount -t sysfs sys /sys -o nosuid,nodev,noexec
busybox mount -t devpts devpts /dev/pts -o nosuid,noexec,gid=5,mode=620
busybox mount -t tmpfs tmpfs /dev/shm -o nosuid,nodev,mode=1777
#busybox mdev -s
busybox mount -o nosuid,nodev,noexec -t proc proc /proc
busybox mount -o nosuid,noexec,mode=620,gid=5 -t devpts devpts /dev/pts
busybox mount -o nosuid,nodev,mode=1777 -t tmpfs tmpfs /dev/shm
busybox mount -o nosuid,nodev,noexec -t sysfs sys /sys
[ -d /tmp ] && busybox mount -o nosuid,nodev,mode=1777 -t tmpfs tmpfs /tmp
[ -d /run ] && busybox mount -o nosuid,nodev,mode=0755 -t tmpfs tmpfs /run

# rand
if [ -f /opt/cartesi/var/run/random-seed ]; then
rndaddentropy < /opt/cartesi/var/run/random-seed
busybox chmod 600 /opt/cartesi/var/run/random-seed
fi

# disk
if [ -d /mnt ]; then
(cd /sys/block && for DEV in *; do
[ ! "$DEV" = "mtdblock0" ] && \
NAME=$(busybox cat /sys/block/"$DEV"/device/name) && \
busybox mkdir "/mnt/$NAME" && \
busybox mount "/dev/$DEV" "/mnt/$NAME"
done)
fi
# system config
[ -f /etc/sysctl.conf ] && busybox sysctl -pq

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

# cmdline application
if [ -n "$*" ]; then
[ -f /etc/environment ] && \
source /etc/environment && \
export PATH=/opt/cartesi/bin:"$PATH" # put it back on PATH
# source environment
[ -f /etc/environment ] && . /etc/environment
export PATH="$PATH:/opt/cartesi/bin"

# 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
fi
# execute init from device tree when available
[ -f /proc/device-tree/cartesi-machine/init ] && . /proc/device-tree/cartesi-machine/init

# execute cmdline
cd $HOME &&
# use entrypoint from device tree when available, otherwise from command line
if [ -s /proc/device-tree/cartesi-machine/entrypoint ]; then
ENTRYPOINT=$(busybox cat /proc/device-tree/cartesi-machine/entrypoint)
elif [ -n "$*" ]; then
ENTRYPOINT="$*"
fi

# is entrypoint not empty?
if [ -n "$ENTRYPOINT" ]; then
USER=${USER:-root}
HOME=$(eval echo ~$USER)
WORKDIR=${WORKDIR:-"$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

# execute entrypoint
(cd $WORKDIR &&
USER=$USER LOGNAME=$USER HOME="$HOME" \
busybox setsid \
busybox cttyhack \
busybox su -p $USER -c "$*"
RC=$?
busybox su -p $USER -c "$ENTRYPOINT")
else
echo "Nothing to do."
RC=0
fi
RC=$?

# shutdown
busybox mount -o ro,remount /
busybox umount -af
[ $RC == 0 ] && busybox poweroff -f || xhalt "$RC"
if [ $RC != 0 ] && [ -x /opt/cartesi/bin/xhalt ]; then
/opt/cartesi/bin/xhalt "$RC"
fi
busybox poweroff -f

# Copyright Cartesi and individual authors (see AUTHORS)
# SPDX-License-Identifier: Apache-2.0
#
# 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
#
# 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.
#
11 changes: 0 additions & 11 deletions skel/opt/cartesi/etc/motd

This file was deleted.

Binary file removed skel/opt/cartesi/var/run/random-seed
Binary file not shown.

0 comments on commit 37e3f43

Please sign in to comment.