mkimage-unittest.sh 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #!/bin/bash
  2. # Generate a very minimal filesystem based on busybox-static,
  3. # and load it into the local docker under the name "docker-ut".
  4. missing_pkg() {
  5. echo "Sorry, I could not locate $1"
  6. echo "Try 'apt-get install ${2:-$1}'?"
  7. exit 1
  8. }
  9. BUSYBOX=$(which busybox)
  10. [ "$BUSYBOX" ] || missing_pkg busybox busybox-static
  11. SOCAT=$(which socat)
  12. [ "$SOCAT" ] || missing_pkg socat
  13. shopt -s extglob
  14. set -ex
  15. ROOTFS=`mktemp -d /tmp/rootfs-busybox.XXXXXXXXXX`
  16. trap "rm -rf $ROOTFS" INT QUIT TERM
  17. cd $ROOTFS
  18. mkdir bin etc dev dev/pts lib proc sys tmp
  19. touch etc/resolv.conf
  20. cp /etc/nsswitch.conf etc/nsswitch.conf
  21. echo root:x:0:0:root:/:/bin/sh > etc/passwd
  22. echo root:x:0: > etc/group
  23. ln -s lib lib64
  24. ln -s bin sbin
  25. cp $BUSYBOX $SOCAT bin
  26. for X in $(busybox --list)
  27. do
  28. ln -s busybox bin/$X
  29. done
  30. rm bin/init
  31. ln bin/busybox bin/init
  32. cp -P /lib/x86_64-linux-gnu/lib{pthread*,c*(-*),dl*(-*),nsl*(-*),nss_*,util*(-*),wrap,z}.so* lib
  33. cp /lib/x86_64-linux-gnu/ld-linux-x86-64.so.2 lib
  34. cp -P /usr/lib/x86_64-linux-gnu/lib{crypto,ssl}.so* lib
  35. for X in console null ptmx random stdin stdout stderr tty urandom zero
  36. do
  37. cp -a /dev/$X dev
  38. done
  39. tar -cf- . | docker import - docker-ut
  40. docker run -i -u root docker-ut /bin/echo Success.
  41. rm -rf $ROOTFS