Advertisement
Rockford

Create EspressoBIN ubuntu image

Mar 9th, 2018
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.38 KB | None | 0 0
  1. #!/bin/bash                                                                                                            
  2. # File: mkEspressoBIN-image
  3.  
  4. # Create SD-card ubuntu 16.04.3 image for booting EspressoBIN.
  5. # Example usage:
  6. # mkEspressoBIN-image [tarball [image [hostname [shortname [homes]]]]]
  7. #
  8. # Defaults:
  9. # tarball  : rootfs.tar.bz2
  10. # image    : EspressoBIN.img
  11. # hostname : ebin.<currentdomain>
  12. # shorthame: `whoami` or $shortname if exported
  13. # homes    : <same your current account>
  14. #
  15. # Note: The tarball contains both the /boot directory and the rootfs; you can write it all to a single partition.
  16. # For simplicity, just run this script from the same folder as the tarball, then it'll create the image.
  17.  
  18. [ "$EUID" -eq 0 ] && { echo "Please do not run this script as root"; exit 1; }
  19.  
  20. domain=`hostname -f`; domain=${domain#*.}
  21.  
  22. export tarball="${1:-rootfs.tar.bz2}"
  23. export image="${2:-EspressoBIN.img}"
  24. export hostname="${3-ebin.$domain}"
  25. export shortname="${4:-${shortname:-`whoami`}}"
  26. export homes="${5:-${HOME%/$USER}}"
  27.  
  28. export homedir="${shortname:+${homes:-home}/$shortname}"
  29.  
  30. [ "$homedir" ] && {
  31.   export mkuser="adduser --home $homedir --shell /bin/bash $shortname; adduser $shortname sudo"
  32. }
  33.  
  34. export networkconf=`cat <<EOF
  35. auto lo
  36. iface lo inet loopback
  37.  
  38. auto eth0
  39. iface eth0 inet manual
  40.  
  41. auto wan
  42. iface wan inet dhcp
  43. pre-up /sbin/ifconfig eth0 up
  44. EOF`
  45.  
  46. export firstrun=`cat <<EOF
  47. #!/bin/bash
  48. apt-get update
  49. apt-get upgrade
  50. apt-get install nano openssh-server
  51.  
  52. $mkuser
  53. sudo hostname -b $hostname
  54. EOF`
  55.  
  56. echo -en "\033[1;32m"
  57. cat << EOF
  58.   tarball  : $tarball
  59.   image    : $image
  60.   hostname : $hostname
  61.   shortname: $shortname
  62.   homes    : $homes
  63.   homedir  : $homedir
  64.   domain   : $domain
  65.  
  66.   firstrun :
  67. $firstrun
  68.  
  69.   networkconf:
  70. $networkconf
  71.  
  72. EOF
  73. echo -e "\033[m"
  74.  
  75. [ -f "$shortname.pub" ] && { [ -f "authorized_keys" ] || cat "$shortname.pub" >> "authorized_keys"; }
  76.  
  77. # A 512MB image woiuld probably need 451072 blocks, perhaps less
  78. # quickly create an 1GB empty disk image using no space until it's written to:
  79. dd if=/dev/zero of="$image" bs=1024 count=0 seek=975360
  80.  
  81. # The following is executed as privileged user (root):
  82. sudo -E bash -c '
  83. createImage(){
  84.  # create one primary Linux partition:
  85.  (echo n; echo p; echo 1; echo ""; echo ""; echo w) | fdisk "$image"
  86.  
  87.  # attach the image as a loop-back device:
  88.  dev="$(losetup --show -f -P "$image")"
  89.  
  90.  # get a list of partitions (eg. /dev/loop0p1 /dev/loop0p2):
  91.  partitions=("$dev"?*)
  92.  
  93.  # format partition:
  94.  mkfs.ext4 "${partitions[0]}"
  95.  
  96.  # create mounting directory and mount partition:
  97.  mkdir /mnt/p0; mount "${partitions[0]}" /mnt/p0
  98.  
  99.  # extract the files into the partition:
  100.  echo -e "\033[1;34mExtracting files, please wait...\033[m"
  101.  tar -xjf "$tarball" -C /mnt/p0
  102.  
  103.  # create a simple network configuration, which will allow you to use SSH:
  104.  echo "$networkconf" >>/mnt/p0/etc/network/interfaces.d/all
  105.  
  106.  # create a "firstrun" executable for convenient setup:
  107.  echo "$firstrun" >>/mnt/p0/usr/bin/firstrun
  108.  chmod a+x /mnt/p0/usr/bin/firstrun
  109.  
  110.  [ "$homedir" ] && [ -f "authorized_keys" ] && {
  111.    mkdir -p "/mnt/p0/$homedir/.ssh" && cat "authorized_keys" >>"/mnt/p0/$homedir/.ssh/authorized_keys"
  112.  }
  113.  
  114.  # sync, unmount and remove mount directory:
  115.  sync; umount /mnt/p0; rmdir /mnt/p0
  116.  
  117.  # detach the loopback device:
  118.  losetup -d "$dev"
  119. }
  120. createImage'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement