Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # File: mkEspressoBIN-image
- # Create SD-card ubuntu 16.04.3 image for booting EspressoBIN.
- # Example usage:
- # mkEspressoBIN-image [tarball [image [hostname [shortname [homes]]]]]
- #
- # Defaults:
- # tarball : rootfs.tar.bz2
- # image : EspressoBIN.img
- # hostname : ebin.<currentdomain>
- # shorthame: `whoami` or $shortname if exported
- # homes : <same your current account>
- #
- # Note: The tarball contains both the /boot directory and the rootfs; you can write it all to a single partition.
- # For simplicity, just run this script from the same folder as the tarball, then it'll create the image.
- [ "$EUID" -eq 0 ] && { echo "Please do not run this script as root"; exit 1; }
- domain=`hostname -f`; domain=${domain#*.}
- export tarball="${1:-rootfs.tar.bz2}"
- export image="${2:-EspressoBIN.img}"
- export hostname="${3-ebin.$domain}"
- export shortname="${4:-${shortname:-`whoami`}}"
- export homes="${5:-${HOME%/$USER}}"
- export homedir="${shortname:+${homes:-home}/$shortname}"
- [ "$homedir" ] && {
- export mkuser="adduser --home $homedir --shell /bin/bash $shortname; adduser $shortname sudo"
- }
- export networkconf=`cat <<EOF
- auto lo
- iface lo inet loopback
- auto eth0
- iface eth0 inet manual
- auto wan
- iface wan inet dhcp
- pre-up /sbin/ifconfig eth0 up
- EOF`
- export firstrun=`cat <<EOF
- #!/bin/bash
- apt-get update
- apt-get upgrade
- apt-get install nano openssh-server
- $mkuser
- sudo hostname -b $hostname
- EOF`
- echo -en "\033[1;32m"
- cat << EOF
- tarball : $tarball
- image : $image
- hostname : $hostname
- shortname: $shortname
- homes : $homes
- homedir : $homedir
- domain : $domain
- firstrun :
- $firstrun
- networkconf:
- $networkconf
- EOF
- echo -e "\033[m"
- [ -f "$shortname.pub" ] && { [ -f "authorized_keys" ] || cat "$shortname.pub" >> "authorized_keys"; }
- # A 512MB image woiuld probably need 451072 blocks, perhaps less
- # quickly create an 1GB empty disk image using no space until it's written to:
- dd if=/dev/zero of="$image" bs=1024 count=0 seek=975360
- # The following is executed as privileged user (root):
- sudo -E bash -c '
- createImage(){
- # create one primary Linux partition:
- (echo n; echo p; echo 1; echo ""; echo ""; echo w) | fdisk "$image"
- # attach the image as a loop-back device:
- dev="$(losetup --show -f -P "$image")"
- # get a list of partitions (eg. /dev/loop0p1 /dev/loop0p2):
- partitions=("$dev"?*)
- # format partition:
- mkfs.ext4 "${partitions[0]}"
- # create mounting directory and mount partition:
- mkdir /mnt/p0; mount "${partitions[0]}" /mnt/p0
- # extract the files into the partition:
- echo -e "\033[1;34mExtracting files, please wait...\033[m"
- tar -xjf "$tarball" -C /mnt/p0
- # create a simple network configuration, which will allow you to use SSH:
- echo "$networkconf" >>/mnt/p0/etc/network/interfaces.d/all
- # create a "firstrun" executable for convenient setup:
- echo "$firstrun" >>/mnt/p0/usr/bin/firstrun
- chmod a+x /mnt/p0/usr/bin/firstrun
- [ "$homedir" ] && [ -f "authorized_keys" ] && {
- mkdir -p "/mnt/p0/$homedir/.ssh" && cat "authorized_keys" >>"/mnt/p0/$homedir/.ssh/authorized_keys"
- }
- # sync, unmount and remove mount directory:
- sync; umount /mnt/p0; rmdir /mnt/p0
- # detach the loopback device:
- losetup -d "$dev"
- }
- createImage'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement