diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8b4eff3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +__pycache__ +tmp +sticks + diff --git a/bootlocal.sh b/bootlocal.sh new file mode 100644 index 0000000..53fe27e --- /dev/null +++ b/bootlocal.sh @@ -0,0 +1,27 @@ +#!/bin/sh +# /opt/bootlocal.sh - run as root at the end of boot (called by tc-config). +# Everything needed is already inside core.gz: no network, no tce-load. + +# do what tce-load does after it has mounted an extension +ldconfig 2> /dev/null +for f in /usr/local/tce.installed/*; do + [ -x "$f" ] && "$f" +done + +# passwords (delete these two lines if only key login is wanted) +echo "tc:tc" | chpasswd +echo "root:root" | chpasswd + +# ssh files for user tc, staged in /opt/ssh by build_core.sh +if [ -d /opt/ssh ]; then + rm -rf /home/tc/.ssh + cp -a /opt/ssh /home/tc/.ssh + chown -R tc:staff /home/tc/.ssh + chmod 700 /home/tc/.ssh + chmod -R go-rwx /home/tc/.ssh +fi + +/usr/local/etc/init.d/openssh start + +# marker so that you can see on the box that this script ran +echo "bootlocal.sh finished $(date)" > /tmp/bootlocal.log diff --git a/build_core.sh b/build_core.sh new file mode 100644 index 0000000..aa05cae --- /dev/null +++ b/build_core.sh @@ -0,0 +1,116 @@ +#!/bin/bash +# Build a custom core.gz for the BOOT_TINY stick: +# - nano, lz4, openssh (and their dependencies) are built in, so no +# network and no tce-load are needed at boot +# - /opt/bootlocal.sh starts sshd and sets up ~/.ssh +# - the ssh/ directory (authorized_keys, id_rsa, ...) is staged in /opt/ssh +# - the stick gets a hostname and its own, fixed ssh host keys +# +# Needs: root, cpio, gzip, unsquashfs (package squashfs-tools), ssh-keygen +# +# Put these next to this script: +# core.gz.orig the original, unmodified core.gz (keep it, never overwrite) +# tcz/*.tcz extensions fetched on the Tiny Core box with +# "tce-load -w nano lz4 openssh", copied from tce/optional +# ssh/ files for ~/.ssh of user tc (authorized_keys, ...) +# bootlocal.sh +# sticks//hostkeys/ ssh host keys of the stick called . Used if it +# exists, created (and filled with new keys) if it does not. +# Keep it, then a rebuilt stick keeps its identity. +# Result: sticks//core.gz (copy it into boot_tiny/ and commit it) +set -e +cd "$(dirname "$0")" + +ORIG=core.gz.orig + +[ "$EUID" -eq 0 ] || { echo "Please run as root"; exit 1; } +for f in "$ORIG" bootlocal.sh; do + [ -f "$f" ] || { echo "missing $f"; exit 1; } +done +ls tcz/*.tcz > /dev/null 2>&1 || { echo "no tcz/*.tcz found"; exit 1; } +for c in cpio gzip unsquashfs ssh-keygen; do + command -v $c > /dev/null || { echo "need $c"; exit 1; } +done + +file "$ORIG" | grep -q gzip || { echo "$ORIG is not gzip compressed:"; file "$ORIG"; exit 1; } + +if [ -z "$1" ]; then + read -p "stick name [apu-boot]: " stickname + stickname=${stickname:-apu-boot} +else + stickname=$1 +fi +# the name ends up in a directory name and in a script: allow only safe characters +case "$stickname" in + *[!A-Za-z0-9-]*|.*) echo "invalid hostname '$stickname' (use letters, digits and dash only)"; exit 1 ;; +esac + +# ssh host keys: use hostkeys_ if present, create missing keys otherwise +STICKDIR=$PWD/sticks/$stickname +KEYDIR=$STICKDIR/hostkeys +SSHDIR=$STICKDIR/ssh +if [ -d "$KEYDIR" ]; then + echo "using ssh host keys from $KEYDIR" +else + echo "creating $KEYDIR with new ssh host keys" + mkdir -p "$KEYDIR" +fi +for t in dsa rsa ecdsa ed25519; do + if [ ! -f "$KEYDIR/ssh_host_${t}_key" ]; then + echo " generate $t key" + rm -f "$KEYDIR/ssh_host_${t}_key.pub" + ssh-keygen -q -t $t -N '' -f "$KEYDIR/ssh_host_${t}_key" + fi +done + +cat << EOF > $STICKDIR/bootsync.sh +#!/bin/sh +# put other system startup commands here, the boot process will wait until they complete. +# Use bootlocal.sh for system startup commands that can run in the background +# and therefore not slow down the boot process. +/usr/bin/sethostname $stickname +/opt/bootlocal.sh & +EOF + +WORK=$(mktemp -d) +TMP=$(mktemp -d) +trap 'rm -rf "$WORK" "$TMP"' EXIT + +echo "unpack $ORIG" +zcat "$ORIG" | (cd "$WORK" && cpio -idm --quiet) + +for t in tcz/*.tcz; do + echo "add $t" + rm -rf "$TMP/x" + unsquashfs -d "$TMP/x" "$t" > /dev/null + cp -a "$TMP/x/." "$WORK/" +done + +echo "add ssh host keys from $KEYDIR" + +if [ -d "$SSHDIR" ]; then + echo "stage $SSHDIR in /opt/ssh" + mkdir -p "$WORK/opt/ssh" + cp -a "$SSHDIR"/. "$WORK/opt/ssh/" +fi + +mkdir -p "$WORK/usr/local/etc/ssh" +for k in "$KEYDIR"/ssh_host_*_key; do + install -m 600 -o root -g root "$k" "$WORK/usr/local/etc/ssh/" + install -m 644 -o root -g root "$k.pub" "$WORK/usr/local/etc/ssh/" +done + +install -m 755 -o root -g root bootlocal.sh "$WORK/opt/bootlocal.sh" +install -m 755 -o root -g root $STICKDIR/bootsync.sh "$WORK/opt/bootsync.sh" + +OUT=core.gz +echo "pack $STICKDIR/$OUT" +(cd "$WORK" && find . | cpio -o -H newc --quiet) | gzip -9 > "${OUT}.new" + +mv "${OUT}.new" $STICKDIR/$OUT + +# change ownership of generated files to the owner of the current directory +chown -R --reference=. $STICKDIR + +ls -l "$STICKDIR/$OUT" +echo "done." diff --git a/clone b/clone deleted file mode 100644 index 5cd6740..0000000 --- a/clone +++ /dev/null @@ -1,10 +0,0 @@ -sh tiny -HOST=l_samenv@linse-c -DIR=boxes/apuimages -IMGDAT=$(date +'%Y-%m-%d').lz4 -read -p "image name to write to $HOST:$DIR [$IMGDAT]: " IMG -if [ -z "$IMG" ]; then - IMG=$IMGDAT -fi -echo "copy image to $HOST:$DIR/$IMG (takes about 5 min) ..." -dd if=/dev/sda bs=512k | lz4 | ssh $HOST "dd of=$DIR/$IMG bs=512k" diff --git a/core.gz b/core.gz.orig similarity index 100% rename from core.gz rename to core.gz.orig diff --git a/makeusb b/makeusb new file mode 100755 index 0000000..746f81a --- /dev/null +++ b/makeusb @@ -0,0 +1,86 @@ +#!/bin/bash +# create an USB stick with TinyLinux on it +# name of the USB stick to be created +BOOT_TINY_SRC=l_samenv@linse-c:boxes/boot_tiny + +[ "$EUID" -eq 0 ] || { echo "Please run as root"; exit 1; } + +# list removeable disks +DISKS=() +while read LINE +do + ARR=($LINE) + if [[ ${ARR[@]:0:2} == "1 disk" ]]; then + DISKS+=(${ARR[@]:2:1}) + printf "%-7s %7s " ${ARR[@]:2:2} + echo ${ARR[@]:4} + elif [[ ${ARR[@]:0:2} == "1 part" ]]; then + printf " %7s " ${ARR[@]:3:1} + echo ${ARR[@]:4} + fi +done < <(lsblk -l --output RM,TYPE,NAME,SIZE,LABEL,VENDOR,MODEL) + +echo "which device? should be one of: ${DISKS[@]}" +read DEVICE + +if [ -z $DEVICE ] ; then + exit +fi + +if [[ " ${DISKS[@]} " =~ " $DEVICE " ]]; then + if [[ $(cat /sys/block/$DEVICE/removable) != "1" ]]; then + echo "/dev/$DEVICE is not a removable disk" + exit + fi + + if [ -z "$1" ]; then + read -p "name for stick [apu-boot]: " stickname + stickname=${stickname:-apu-boot} + else + stickname=$1 + fi + + # allow only letters and digits + case "$stickname" in + *[!A-Za-z0-9-]*|.*) echo "invalid hostname '$stickname' (use letters, digits and dash only)"; exit 1 ;; + esac + + echo "create TinyLinux stick $stickname" + + if [ ! -d boot_tiny ] ; then + sudo --user l_samenv scp -r l_samenv@linse-c:boxes/boot_tiny ./ + fi + + STICKDIR=sticks/$stickname + if [ ! -d $STICKDIR ]; then + mkdir -p $STICKDIR + fi + if [ ! -f $STICKDIR/core.gz ]; then + bash build_core.sh $stickname + if [ ! -f $STICKDIR/core.gz ]; then + echo "no core.gz created -> build_core.sh failed" + exit + fi + fi + sudo --user l_samenv cp $STICKDIR/core.gz boot_tiny/core.gz + DEVICE=/dev/$DEVICE + + umount ${DEVICE}1 + dd if=/dev/zero of=${DEVICE} count=1 conv=notrunc + echo -e "o\nn\np\n1\n\n\nw" | fdisk ${DEVICE} + mkfs.vfat -n $stickname -I ${DEVICE}1 + + syslinux -i ${DEVICE}1 + dd conv=notrunc bs=440 count=1 if=boot_tiny/mbr.bin of=${DEVICE} + parted ${DEVICE} set 1 boot on + + mkdir -p /mnt/apusb + mount ${DEVICE}1 /mnt/apusb + echo "copy files ..." + cp -r boot_tiny/* /mnt/apusb/ + umount /mnt/apusb + + echo "done." +else + echo "/dev/$DEVICE is not a removeable disk" +fi diff --git a/reg b/reg deleted file mode 100644 index 4dac829..0000000 --- a/reg +++ /dev/null @@ -1,10 +0,0 @@ -echo -n "new hostname: " -read hostname -sudo hostname $hostname -echo "address of leftmost socket (eth0 for control box, recommended also for newer bare apus):" -ifconfig | grep eth0 -echo " " -echo "address of rightmost socket (for legacy bare apu):" -ifconfig | grep eth3 -echo " " -echo "register $hostname to packet fence and connect LAN to corresponding socket" diff --git a/scripts/clone b/scripts/clone new file mode 100644 index 0000000..a34c7e7 --- /dev/null +++ b/scripts/clone @@ -0,0 +1,10 @@ +DESTHOST=l_samenv@linse-c +DESTDIR=boxes/apuimages +IMGDAT=$(date +'%Y-%m-%d').lz4 +read -p "image name to write to $DESTHOST:$DESTDIR [$IMGDAT]: " IMG +if [ -z "$IMG" ]; then + IMG=$IMGDAT +fi +[[ $IMG == *.lz4 ]] || IMG+=.lz4 +echo "copy image to ${DESTHOST}:$DESTDIR/$IMG ..." +dd if=/dev/sda bs=512k | lz4 | ssh l_samenv@linse-c "dd of=$DESTDIR/$IMG bs=512k" diff --git a/syslinux.cfg b/scripts/syslinux.cfg similarity index 100% rename from syslinux.cfg rename to scripts/syslinux.cfg diff --git a/vmlinuz b/scripts/vmlinuz similarity index 100% rename from vmlinuz rename to scripts/vmlinuz diff --git a/write_to_ssd b/scripts/write_to_ssd similarity index 70% rename from write_to_ssd rename to scripts/write_to_ssd index bdf5f38..ba61e58 100644 --- a/write_to_ssd +++ b/scripts/write_to_ssd @@ -1,10 +1,11 @@ -sh tiny BOXIMG=boxes/apuimages echo "" echo "images from l_samenv@linse-c:$BOXIMG:" echo "" ssh l_samenv@linse-c "cd $BOXIMG; ls *.lz4" echo "" +echo "ARE YOU SURE: you will override the SSD of the box where this stick is inserted" read -p "which image? " IMG -echo "write to ssd /dev/sda (takes 3 min)" +echo "write to ssd" ssh l_samenv@linse-c "dd if=$BOXIMG/$IMG bs=512k" | lz4 -d | sudo dd of=/dev/sda bs=512k + diff --git a/tcz/lz4.tcz b/tcz/lz4.tcz new file mode 100644 index 0000000..3715a9d Binary files /dev/null and b/tcz/lz4.tcz differ diff --git a/tcz/lz4.tcz.md5.txt b/tcz/lz4.tcz.md5.txt new file mode 100644 index 0000000..f34e031 --- /dev/null +++ b/tcz/lz4.tcz.md5.txt @@ -0,0 +1 @@ +9a994075cedcc853c448cc9691c3fe38 lz4.tcz diff --git a/tcz/nano.tcz b/tcz/nano.tcz new file mode 100644 index 0000000..bd6186f Binary files /dev/null and b/tcz/nano.tcz differ diff --git a/tcz/nano.tcz.dep b/tcz/nano.tcz.dep new file mode 100644 index 0000000..8ae0b07 --- /dev/null +++ b/tcz/nano.tcz.dep @@ -0,0 +1 @@ +ncurses.tcz diff --git a/tcz/nano.tcz.md5.txt b/tcz/nano.tcz.md5.txt new file mode 100644 index 0000000..595aa3c --- /dev/null +++ b/tcz/nano.tcz.md5.txt @@ -0,0 +1 @@ +ce9cf3b540095da2f62d958d85a15e2e nano.tcz diff --git a/tcz/ncurses.tcz b/tcz/ncurses.tcz new file mode 100644 index 0000000..d72d859 Binary files /dev/null and b/tcz/ncurses.tcz differ diff --git a/tcz/ncurses.tcz.md5.txt b/tcz/ncurses.tcz.md5.txt new file mode 100644 index 0000000..b253652 --- /dev/null +++ b/tcz/ncurses.tcz.md5.txt @@ -0,0 +1 @@ +234b09001894df7f888a5905067c9be3 ncurses.tcz diff --git a/tcz/openssh.tcz b/tcz/openssh.tcz new file mode 100644 index 0000000..82b029d Binary files /dev/null and b/tcz/openssh.tcz differ diff --git a/tcz/openssh.tcz.dep b/tcz/openssh.tcz.dep new file mode 100644 index 0000000..8c212df --- /dev/null +++ b/tcz/openssh.tcz.dep @@ -0,0 +1,2 @@ +openssl.tcz + diff --git a/tcz/openssh.tcz.md5.txt b/tcz/openssh.tcz.md5.txt new file mode 100644 index 0000000..575c779 --- /dev/null +++ b/tcz/openssh.tcz.md5.txt @@ -0,0 +1 @@ +ad59211499d2b35c624b85eaec4ce53a openssh.tcz diff --git a/tcz/openssl.tcz b/tcz/openssl.tcz new file mode 100644 index 0000000..500ed70 Binary files /dev/null and b/tcz/openssl.tcz differ diff --git a/tcz/openssl.tcz.md5.txt b/tcz/openssl.tcz.md5.txt new file mode 100644 index 0000000..9abc2dc --- /dev/null +++ b/tcz/openssl.tcz.md5.txt @@ -0,0 +1 @@ +e7bfb084e3fbab7eae0a2a0f2cdef0ff openssl.tcz diff --git a/tiny b/tiny deleted file mode 100644 index f1eec49..0000000 --- a/tiny +++ /dev/null @@ -1,11 +0,0 @@ -tce-load -wi nano -tce-load -wi lz4 -tce-load -wi openssh -sudo /usr/local/etc/init.d/openssh start -sudo chpasswd << EOF -tc:tc -root:root -EOF -cp -rf ssh ~/.ssh -sudo chmod go-r ~/.ssh/id_rsa -ifconfig | grep 'inet addr'