Skip to content

Commit

Permalink
Minor changes. Mostly code/file reorganization.
Browse files Browse the repository at this point in the history
  • Loading branch information
parke committed Oct 30, 2021
1 parent 8946392 commit 53ae190
Show file tree
Hide file tree
Showing 8 changed files with 1,477 additions and 1,060 deletions.
72 changes: 45 additions & 27 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,29 +1,38 @@


# version 20210624
# version 20211020


bin ?= bin
demo ?= /tmp/lxroot-demo
unit ?= /tmp/lxroot-unit


help:
@ echo
@ echo 'usage:'
@ echo ' make build # build bin/lxroot'
@ echo ' make unit # run the unit tests'
@ echo ' make unit-clean # delete the unit test environment'
@ echo ' make demo # run the interactive demo'
@ echo ' make demo-clean # delete the demo environment'
@ echo

bin ?= bin
demo ?= /tmp/lxroot-demo
static ?= /tmp/lxroot-static
unit ?= /tmp/lxroot-unit

Wextra ?= -Wextra -Wno-unused-parameter
gpp_opts ?= -fmax-errors=2 -Wall -Werror $(Wextra)


$(bin)/lxroot: Makefile lxroot.cpp $(bin)
help:
@ echo "\
\n\
Welcome to the Lxroot Makefile. \n\
\n\
Usage: \n\
\n\
make build # build bin/lxroot \n\
make unit # run the unit tests \n\
make demo # run the interactive Alpine demo \n\
make demo3 # run the Arch Linux + Chromium demo \n\
make static # build a statically linked lxroot \n\
\n\
make clean # delete bin/lxroot \n\
make unit-clean # delete the unit test environment \n\
make demo-clean # delete the demo environment \n\
make demo3-clean # delete the demo3 environment \n\
make static-clean # delete the static build environment "


$(bin)/lxroot: Makefile lxroot.cpp help.cpp str.cpp $(bin)
g++ -g $(gpp_opts) lxroot.cpp -o $@


Expand All @@ -41,9 +50,9 @@ clean:
unit: $(bin)/lxroot $(unit)
cp $(bin)/lxroot $(unit)/lxr
@ echo
bash demo.sh demo1_extract $(demo) $(unit)/nr 2>&1
bash aux/demo.sh demo1_extract $(demo) $(unit)/nr 2>&1
@ echo
bash unit.sh $(unit) 2>&1
bash aux/unit.sh $(unit) 2>&1


unit-clean: clean
Expand All @@ -53,7 +62,7 @@ unit-clean: clean

demo: $(bin)/lxroot $(demo)
cp $(bin)/lxroot $(demo)/lxroot
bash demo.sh demo1 $(demo)
bash aux/demo.sh demo1 $(demo)


demo-clean: clean
Expand All @@ -62,6 +71,13 @@ demo-clean: clean
rm -rf /tmp/lxroot-demo/demo1


static:
bash aux/static.sh $(bin)/lxroot $(static)


# demo 3 ----------------------------------------------------------- demo 3


demo3_iso=$(demo)/dist/archlinux-2021.06.01-x86_64.iso
demo3_url=https://mirror.rackspace.com/archlinux/iso/2021.06.01/archlinux-2021.06.01-x86_64.iso

Expand All @@ -75,25 +91,25 @@ demo3-base: demo3-iso-soft $(bin)/lxroot

@ echo
@ echo 'demo3 create userland1'
bash demo.sh demo1_extract $(demo) $(demo)/demo3
bash aux/demo.sh demo1_extract $(demo) $(demo)/demo3
cp /etc/resolv.conf $(demo)/demo3/etc/
mkdir -p $(demo)/demo3/dist
ln -f $(demo3_iso) $(demo)/demo3/dist

@ echo
cp demo.sh $(demo)/demo3/root/
cp aux/demo.sh $(demo)/demo3/root/
$(bin)/lxroot -nw $(demo)/demo3 \
/bin/ash /root/demo.sh demo3_u1_create_u2
-- /bin/ash /root/demo.sh demo3_u1_create_u2

@ echo
cp demo.sh $(demo)/demo3/userland2/root/
cp aux/demo.sh $(demo)/demo3/userland2/root/
$(bin)/lxroot -nr $(demo)/demo3/userland2 \
/bin/bash /root/demo.sh demo3_u2_create_u3
-- /bin/bash /root/demo.sh demo3_u2_create_u3

@ echo
cp demo.sh $(demo)/demo3/userland2/userland3/root/
cp aux/demo.sh $(demo)/demo3/userland2/userland3/root/
$(bin)/lxroot -nr $(demo)/demo3/userland2/userland3 \
/bin/bash /root/demo.sh demo3_u3_finish
-- /bin/bash /root/demo.sh demo3_u3_finish

@ echo
mkdir -p $(demo)/demo3/userland2/userland3/$(HOME)
Expand Down Expand Up @@ -127,3 +143,5 @@ demo3-clean:
mkdir -p /tmp/lxroot-demo/demo3
chmod -R u+w /tmp/lxroot-demo/demo3
rm -rf /tmp/lxroot-demo/demo3


181 changes: 181 additions & 0 deletions aux/static.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
#! /bin/bash


# static.sh version 20211028


# this script will:
# Download an x86 (32 bit) Alpine userland.
# Build a statically compiled lxroot binary in that userland.


tgz_url='https://dl-cdn.alpinelinux.org/alpine/v3.14/releases/x86/alpine-minirootfs-3.14.2-x86.tar.gz'
tgz_sum='874f6e60047535c08ffcc8f430afec21d323b367269391279f74ff8f87420d83'


set -o errexit


usage () { # ---------------------------------------------------- usage
echo
echo 'usage: sh static.sh /path/to/lxroot /path/to/build_dir' ; }


die () { echo "die $*" ; exit 1 ; } # ------------------- die

found () { printf 'found %-8s %s\n' "$1" "$2" ; } # ----- found

trace () { echo "+ $@" ; "$@" ; } # ---------------------- trace


lxr () { # -------------------------------------------------------- lxr
"$lxroot" -rn "$build"/newroot -- "$@" ; }


init () { # ------------------------------------------------------ init
[ "$build" ] || { usage ; exit 1 ; }
if [ -d "$build" ]
then found init "$build"
else trace mkdir -p "$build" ; fi
cd "$build" ; }


fetch () ( # ---------------------------------------------------- fetch
[ -d 'dist' ] || trace mkdir dist
[ -f dist/"$tgz" ] && { found fetch dist/"$tgz" ; return ; }
cd dist
trace wget -nc "$tgz_url" )


checksum () { # ---------------------------------------------- checksum
local actual="$( sha256sum dist/"$tgz" )"
actual="${actual%% dist/*}"
[ "$actual" = "$tgz_sum" ] && { found checksum valid ; return ; }
echo
echo 'checksum error'
echo " expect $tgz_sum"
echo " actual $actual"
exit 1 ; }


extract () { # ------------------------------------------------ extract
[ -d newroot ] && { found extract newroot ; return ; }
trace mkdir newroot
trace tar xzf dist/"$tgz" -C newroot ; }


dns () { # -------------------------------------------------------- dns
local path='newroot/etc/resolv.conf'
[ -f "$path" ] && { found dns "$path" ; return ; }
trace cp /etc/resolv.conf newroot/etc/resolv.conf ; }


apk_update () { # ------------------------------------------ apk_update
local path="newroot/root/touch_apk_update"
[ -f "$path" ] && { found update "$path" ; return ; }
trace lxr apk update
trace touch "$path" ; }


apk_add () { # ------------------------------------------------ apk_add
local path="newroot/usr/bin/g++"
[ -f "$path" ] && { found add "$path" ; return ; }
trace lxr apk add build-base ; }


build () { # ---------------------------------------------------- build

# see http://ptspts.blogspot.com/2013/12/how-to-make-smaller-c-and-c-binaries.html

local options=(

-s
-Os
-fomit-frame-pointer
-fno-stack-protector
-ffunction-sections
-fdata-sections
-Wl,--gc-sections
-fno-unroll-loops
-fmerge-all-constants
-fno-ident
-Wl,-z,norelro
-Wl,--build-id=none

-fno-exceptions
-fno-rtti

# 20211028 -fvtable-gc is no longer supported
# -fvtable-gc

)

echo
trace rm -f 'newroot/root/lxroot-plain'
trace rm -f 'newroot/root/lxroot-small'
trace rm -f 'newroot/root/lxroot-static'
trace cp "$src"/lxroot.cpp newroot/root/
trace cp "$src"/help.cpp newroot/root/
trace cp "$src"/str.cpp newroot/root/

echo
trace lxr time g++ lxroot.cpp -o lxroot-plain

echo
trace lxr time g++ lxroot.cpp -o lxroot-small \
"${options[@]}"

echo
trace lxr time g++ lxroot.cpp -o lxroot-static \
"${options[@]}" -static



return ; }


main () { # ------------------------------------------------------ main

local src="$PWD" lxroot="$1" build="$2"
local tgz="${tgz_url##*/}"

lxroot=` realpath "$lxroot" `

echo
echo 'static.sh'
echo " src $src"
echo " lxroot $lxroot"
echo " bulid $build"

echo
init
fetch
checksum
extract
dns
apk_update
apk_add

build

trace cd newroot/root

echo
echo
echo '==== size of elf sections'
size lxroot-small lxroot-plain lxroot-static

echo
echo
echo '==== file size in bytes'
ls -lSr lxroot-*

echo
echo
echo '==== file size in kilobytes'
ls -lSrh lxroot-*

return ; }


main "$@"
37 changes: 30 additions & 7 deletions aux/unit.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#! /bin/bash


# unit.sh version 20210624
# unit.sh version 20210803


set -o errexit
Expand All @@ -20,7 +20,7 @@ run () { # -------------------------------------------------------- run
# echo "command ${command[@]}"
local stdout status actual pretty
set +o errexit
stdout=` "${command[@]}" 2>&1 ` ; status=$? pretty="$status"
stdout=` "${command[@]}" 2>&1 ` ; status="$?" ; pretty="$status"
set -o errexit


Expand Down Expand Up @@ -224,8 +224,7 @@ test_no_home () { # -------------------------------------- test_no_home
lxr1=()

run1 'err 0-' ./lxr
run1 'err 1 lxroot error execve bad No such file or directory' \
./lxr bad -- true
run1 'err 1-' ./lxr bad -- true

return ; }

Expand Down Expand Up @@ -336,7 +335,7 @@ prepare_full_overlay () { # ---------------------- prepare_full_overlay

test_full_overlay () { # ---------------------------- test_full_overlay

echo ; echo "- test_full_overlay"
echo ; echo '- test_full_overlay'

prepare_full_overlay
env1=( env - ) lxr1=() cmd1=()
Expand Down Expand Up @@ -590,10 +589,33 @@ test_env () { # ---------------------------------------------- test_env

echo ; echo "- test_env"

env1=( env - 'FOO=BAR2' ) lxr1=() cmd1=()
TZ='America/Los_Angeles'
env1=( env - 'FOO=BAR2' "TZ=$TZ" ) lxr1=() cmd1=()

run1 'hello' ./lxr -e -- /bin/sh -c 'echo hello'
run1 'hello' ./lxr -- /bin/sh -c 'echo hello'
run1 '' ./lxr -- /bin/sh -c 'echo "$FOO"'
run1 'BAR2' ./lxr -e -- /bin/sh -c 'echo "$FOO"'
run1 "$TZ" ./lxr -- /bin/sh -c 'echo "$TZ"'
run1 "$TZ" ./lxr -e -- /bin/sh -c 'echo "$TZ"'

return ; }


test_dev_shm () { # -------------------------------------- test_dev_shm

# 20211002 On my development system, /dev/shm exists. Moreover,
# /dev/shm is mounted with the nosuid and nodev flags.
# Therefore, I can use /dev/shm to test read-only
# remounting of bind mounts with nosuid and nodev.

[ ! -d /dev/shm ] && return

env1=( env - ) lxr1=() cmd1=()

run1 'foo' ./lxr nr bind rw /mnt /dev/shm -- echo foo
run1 'foo' ./lxr nr bind ra /mnt /dev/shm -- echo foo
run1 'foo' ./lxr nr bind ro /mnt /dev/shm -- echo foo
run1 'foo' ./lxr nr bind /mnt /dev/shm -- echo foo

return ; }

Expand All @@ -612,6 +634,7 @@ main () { # ------------------------------------------------------ main

cd "$unit"

test_dev_shm
test_no_newroot
test_env

Expand Down
Loading

0 comments on commit 53ae190

Please sign in to comment.