Advertisement
ryzhov_al

Chroot'ed Alpine on Keenetic router

Nov 2nd, 2023
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.86 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. set -e
  4.  
  5. VER=3.17
  6. REL=2
  7.  
  8. echo 'Downloading sources…'
  9. SRC_URL="http://dl-cdn.alpinelinux.org/alpine/v${VER}/releases/aarch64/alpine-minirootfs-${VER}.${REL}-aarch64.tar.gz"
  10. SRC=$(echo $SRC_URL | grep -o '[^/]*$')
  11. [ -f $SRC ] || wget $SRC_URL
  12.  
  13. echo 'Unpacking…'
  14. BASE='chroot_alpine'
  15.  
  16. # Safe removing previous root folder
  17. if [ -d  $BASE ]; then
  18.     [ -d $BASE/dev/pts ] && umount $BASE/dev
  19.     [ -d $BASE/proc/1 ] && umount $BASE/proc
  20.     [ -d $BASE/sys/bus ] && umount $BASE/sys
  21.     rm -fr $BASE
  22. fi
  23. mkdir -p $BASE
  24. tar -xzf $SRC -C $BASE
  25.  
  26. echo 'Preparing chroot environment…'
  27. # Adding folders for hooks
  28. for i in button fs ifcreated ifdestroyed ifipchanged ifstatechanged neighbour netfilter schedule sms time usb user wan; do
  29.     mkdir -p $BASE/etc/ndm/$i.d
  30. done
  31.  
  32. # Adding necessary packages
  33. mount -o bind /dev $BASE/dev
  34. mount -o bind /proc $BASE/proc
  35. mount -o bind /sys $BASE/sys
  36. PATH=/usr/sbin:/usr/bin:/sbin:/bin SHELL=/bin/sh HOME=/root TERM=xterm \
  37.     /opt/sbin/chroot $BASE /bin/sh -x <<'EOF'
  38. apk add --no-cache openrc
  39. OPENRC_VER=`apk --quiet list openrc | grep installed | cut -d'-' -f2,3 | cut -d' ' -f1`
  40. apk add --no-cache --quiet openrc=$OPENRC_VER
  41. apk add --no-cache alpine-base dropbear
  42. rc-update add dropbear
  43. echo 'root:alpine' | chpasswd
  44. EOF
  45. umount $BASE/dev
  46. umount $BASE/proc
  47. umount $BASE/sys
  48.  
  49. # Few fixes for OpenRC init system
  50. echo 'nameserver 127.0.0.1' > $BASE/etc/resolv.conf
  51. echo > $BASE/etc/fstab
  52. sed -i -e 's|^DROPBEAR_OPTS.*|DROPBEAR_OPTS="-p 2222"|' $BASE/etc/conf.d/dropbear
  53. rm $BASE/lib/sysctl.d/*
  54. for i in dev devfs firstboot fsck killprocs localmount logger loopback mdev \
  55.     modloop mount-ro mtab networking procfs root sysfs sysfsconf syslog \
  56.     termencoding udhcpd urandom watchdog; do
  57.     cat <<'EOF' > $BASE/etc/init.d/$i
  58. #!/sbin/openrc-run
  59.  
  60. start()
  61. {
  62.     return 0
  63. }
  64. EOF
  65.     chmod +x $BASE/etc/init.d/$i
  66. done
  67.  
  68. # Make start script
  69. cat <<'EOF' > $BASE/etc/ndm/initrc
  70. #!/bin/sh
  71.  
  72. . /etc/profile
  73.  
  74. case "$1" in
  75.     start)
  76.     openrc default
  77.     ;;
  78.     stop)
  79.     openrc single
  80.     ;;
  81.     *)
  82.     echo "Usage: $0 (start|stop)"
  83.     exit 1
  84.     ;;
  85. esac
  86.  
  87. exit 0
  88. EOF
  89.  
  90. # Fix some NDM-specific env vars
  91. cat <<'EOF' > $BASE/etc/profile.d/ndm.sh
  92.  
  93. #!/bin/sh
  94.  
  95. unset LD_BIND_NOW
  96. unset LD_LIBRARY_PATH
  97.  
  98. EOF
  99.  
  100. # Copy this script to target archive
  101. cp $0 $BASE/etc/ndm
  102.  
  103. echo 'Making target archive…'
  104. TGT=install-$SRC
  105. [ -f $TGT ] && rm -f $TGT
  106. tar -czf $TGT -C $BASE .
  107.  
  108. echo 'Done! Now make clean Ext2/3/4 volume,'
  109. echo "put $TGT to 'install' folder on it,"
  110. cat <<'EOF'
  111. configure Keenetic from CLI:
  112.  
  113. opkg chroot
  114. opkg initrc /opt/etc/ndm/initrc
  115. opkg opkg disk <volume>
  116.  
  117. You may connect to Alpine Linux via SSH root:alpine, TCP2222 port.
  118. EOF
  119.  
  120. exit 0
  121. ### Cut here! This is just for tests.
  122. VOL=/tmp/mnt/STORAGE
  123. if [ -d $VOL ]; then
  124.     rm -fr $VOL/*
  125.     mkdir $VOL/install
  126.     cp $TGT $VOL/install
  127. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement