Skip to content

Instantly share code, notes, and snippets.

@countingpine
Last active February 26, 2022 16:34
Show Gist options
  • Select an option

  • Save countingpine/474bdfae5dac47545c4f9bc3ba21722b to your computer and use it in GitHub Desktop.

Select an option

Save countingpine/474bdfae5dac47545c4f9bc3ba21722b to your computer and use it in GitHub Desktop.
tc-terminal-server
#!/bin/busybox ash
# Woven together by Curaga (2008)
# Changes made remove user interaction and allow serial console and TFTP package loading by Matthew Fearnley (2018)
. /etc/init.d/tc-functions
useBusybox
set -x
set -u
ROOTFS=core
# Set the variables
IFACE=eth0 # `ifconfig | sed -n '1 s/ .*//p'`
MYIP=192.168.0.1
STARTIP=192.168.0.30
ENDIP=192.168.0.60
ROUTER=$MYIP
SUBNET=255.255.255.0
DNS=1.1.1.1
SERIAL="" # 9600
CPSHADOW=1
TFTPLIST=tftpboot.lst
TCEDIR=/tmp/tce # /mnt/sda1/tce/
fail(){
[ -d /netboot ] && rm -rf /netboot
echo "$1"
exit 1
}
servers(){
echo Please leave this terminal open
udhcpd /netboot/udhcpd.conf &
udpsvd -E 0 69 tftpd /netboot/
}
checkroot
ifconfig $IFACE $MYIP netmask $SUBNET
KERNELOPTIONS='blacklist=pcspkr kmap=qwerty/uk'
#Boot device
BOOTFROM=
# Check for the host's IP here
MYIP=`ifconfig $IFACE | grep inet | cut -d":" -f2 | cut -d" " -f1`
[ -n "$MYIP" ] || fail "$IFACE doesn't have an IP set"
# Checks & copying to ram
case `cat /proc/mounts` in
*$BOOTFROM* )
true ;;
* )
mount $BOOTFROM || fail "Failed to mount $BOOTFROM" ;;
esac
PREPATH="$(find /mnt/${BOOTFROM##*/} -name "$ROOTFS".gz)"
AFTERMATH="${PREPATH%/"$ROOTFS".gz}"
[ -n "$PREPATH" ] || fail "Can't find kernel! Aborting!"
# Cleanup / create folders
rm -rf /netboot
mkdir -p /netboot/pxelinux.cfg/
# Copy kernel, initrd, pxelinux.0 to /netboot/
cp $AFTERMATH/vmlinuz $AFTERMATH/"$ROOTFS".gz /usr/share/syslinux/pxelinux.0 /netboot/
# Should we rebuild the rootfs?
if [ -n ${SERIAL:+x} ] || [ -n ${CPSHADOW:+x} ] ; then
# Extract rootfs
rm -rf /tmp/"$ROOTFS"
mkdir -p /tmp/"$ROOTFS"
cd /tmp/"$ROOTFS"
gzip -dc /netboot/"$ROOTFS".gz | cpio -H newc -i
if [ -n ${CPSHADOW:+x} ]; then
# Copy /etc/shadow, containing user passwords
cp /etc/shadow ./etc/
fi
if [ -n ${SERIAL:+x} ]; then
# Add kernel options to echo to serial, and update inittab to open a serial terminal
KERNELOPTIONS="$KERNELOPTIONS text console=ttyS0,$SERIAL"
echo "ttyS0::respawn:/sbin/getty $SERIAL ttyS0" >> ./etc/inittab
fi
# Rebuild rootfs
find | cpio -H newc -o | gzip -9 > /netboot/"$ROOTFS.gz"
fi
# Function to build package list with dependencies (for TFTPLIST)
addpackage(){
if ! grep "^$1$" /netboot/$TFTPLIST; then
for i in $(cat "$TCEDIR"/optional/$1.dep); do
addpackage $i
done
echo $1 >> /netboot/$TFTPLIST
cp "$TCEDIR"/optional/$1 /netboot/
fi
}
#if TFTPLIST set, compile list of packages from onboot.lst, update kernel options
if [ -n ${TFTPLIST+x} ]; then
for i in $(cat "$TCEDIR"/onboot.lst); do
addpackage $i
done
KERNELOPTIONS="$KERNELOPTIONS TFTPLIST=$MYIP:/$TFTPLIST"
fi
## Write udhcpd.conf:
echo "# The start and end of the IP lease block
start $STARTIP
end $ENDIP
# The interface that udhcpd will use
interface $IFACE
boot_file /pxelinux.0
opt dns $DNS
option subnet $SUBNET
opt router $ROUTER
option lease 864000 # 10 days of seconds
option tftp $MYIP
siaddr $MYIP
" > /netboot/udhcpd.conf
## Write pxelinux.conf
echo "DEFAULT $ROOTFS
${SERIAL+SERIAL 0 $SERIAL}
TIMEOUT 300
LABEL $ROOTFS
KERNEL vmlinuz
APPEND $KERNELOPTIONS initrd=$ROOTFS.gz
" > /netboot/pxelinux.cfg/default
# A workaround for buggy busybox udhcpd - make link of pxelinux.0 with 0xff appended to filename
ln -s /netboot/pxelinux.0 $'/netboot/pxelinux.0\xff'
# If everything's OK, let's roll!
servers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment