From 0f91280e285e38411a87319fd370182db6afe0b1 Mon Sep 17 00:00:00 2001
From: Bob Mottram <bob@robotics.uk.to>
Date: Fri, 20 Nov 2015 16:09:21 +0000
Subject: [PATCH] Make a debian image

Based on freedom-maker with proprietary
stuff removed
---
 Makefile                                      |   7 +
 .../03-roottype-btrfs-bts-741223.patch        | 147 +++++++++++++++
 image_build/vendor/vmdebootstrap              |   1 +
 man/freedombone-image.1.gz                    | Bin 0 -> 692 bytes
 src/freedombone-image                         |  72 ++++++++
 src/freedombone-image-customise               | 158 ++++++++++++++++
 src/freedombone-image-hardware-setup          | 170 ++++++++++++++++++
 src/freedombone-image-make                    | 129 +++++++++++++
 src/freedombone-image-makefile                | 147 +++++++++++++++
 9 files changed, 831 insertions(+)
 create mode 100644 image_build/vendor-patches/vmdebootstrap/03-roottype-btrfs-bts-741223.patch
 create mode 160000 image_build/vendor/vmdebootstrap
 create mode 100644 man/freedombone-image.1.gz
 create mode 100755 src/freedombone-image
 create mode 100755 src/freedombone-image-customise
 create mode 100755 src/freedombone-image-hardware-setup
 create mode 100755 src/freedombone-image-make
 create mode 100755 src/freedombone-image-makefile

diff --git a/Makefile b/Makefile
index 16f24ffa7..20f09dff1 100644
--- a/Makefile
+++ b/Makefile
@@ -10,6 +10,8 @@ source:
 	gzip -f9n ../${APP}_${VERSION}.orig.tar
 install:
 	mkdir -p ${DESTDIR}${PREFIX}/bin
+	mkdir -p ${DESTDIR}/etc/freedombone
+	cp -r image_build/* ${DESTDIR}/etc/freedombone
 	install -m 755 src/${APP} ${DESTDIR}${PREFIX}/bin
 	install -m 755 src/zeronetavahi ${DESTDIR}${PREFIX}/bin
 	install -m 755 src/${APP}-keydrive ${DESTDIR}${PREFIX}/bin
@@ -45,6 +47,7 @@ install:
 	install -m 755 src/${APP}-rmsipuser ${DESTDIR}${PREFIX}/bin
 	install -m 755 src/${APP}-sipfreeext ${DESTDIR}${PREFIX}/bin
 	install -m 755 src/${APP}-format ${DESTDIR}${PREFIX}/bin
+	install -m 755 src/${APP}-image* ${DESTDIR}${PREFIX}/bin
 	mkdir -m 755 -p ${DESTDIR}${PREFIX}/share/man/man1
 	install -m 644 man/${APP}.1.gz ${DESTDIR}${PREFIX}/share/man/man1
 	install -m 644 man/${APP}-keydrive.1.gz ${DESTDIR}${PREFIX}/share/man/man1
@@ -75,6 +78,7 @@ install:
 	install -m 644 man/${APP}-addsipuser.1.gz ${DESTDIR}${PREFIX}/share/man/man1
 	install -m 644 man/${APP}-rmsipuser.1.gz ${DESTDIR}${PREFIX}/share/man/man1
 	install -m 644 man/${APP}-format.1.gz ${DESTDIR}${PREFIX}/share/man/man1
+	install -m 644 man/${APP}-image.1.gz ${DESTDIR}${PREFIX}/share/man/man1
 uninstall:
 	rm -f ${PREFIX}/share/man/man1/${APP}.1.gz
 	rm -f ${PREFIX}/share/man/man1/${APP}-keydrive.1.gz
@@ -105,6 +109,7 @@ uninstall:
 	rm -f ${PREFIX}/share/man/man1/${APP}-addsipuser.1.gz
 	rm -f ${PREFIX}/share/man/man1/${APP}-rmsipuser.1.gz
 	rm -f ${PREFIX}/share/man/man1/${APP}-format.1.gz
+	rm -f ${PREFIX}/share/man/man1/${APP}-image.1.gz
 	rm -rf ${PREFIX}/share/${APP}
 	rm -f ${PREFIX}/bin/${APP}
 	rm -f ${PREFIX}/bin/zeronetavahi
@@ -139,7 +144,9 @@ uninstall:
 	rm -f ${PREFIX}/bin/${APP}-rmsipuser
 	rm -f ${PREFIX}/bin/${APP}-sipfreeext
 	rm -f ${PREFIX}/bin/${APP}-format
+	rm -f ${PREFIX}/bin/${APP}-image*
 clean:
 	rm -f \#* \.#* debian/*.substvars debian/*.log
 	rm -fr deb.* debian/${APP}
 	rm -f ../${APP}*.deb ../${APP}*.changes ../${APP}*.asc ../${APP}*.dsc
+	rm -rf /etc/freedombone
diff --git a/image_build/vendor-patches/vmdebootstrap/03-roottype-btrfs-bts-741223.patch b/image_build/vendor-patches/vmdebootstrap/03-roottype-btrfs-bts-741223.patch
new file mode 100644
index 000000000..2698738b9
--- /dev/null
+++ b/image_build/vendor-patches/vmdebootstrap/03-roottype-btrfs-bts-741223.patch
@@ -0,0 +1,147 @@
+diff --git a/vmdebootstrap b/vmdebootstrap
+index 4895147..c46be43 100755
+--- a/vmdebootstrap
++++ b/vmdebootstrap
+@@ -218,6 +218,25 @@ class VmDebootstrap(cliapp.Application):  # pylint: disable=too-many-public-meth
+                     self.runcmd(['mkswap', swapdev])
+                 self.mkfs(rootdev, fstype=roottype)
+                 rootdir = self.mount(rootdev)
++                rootfsdir = rootdir
++                if 'btrfs' == roottype:
++                    # Put root in a subvolume, to ease snapshots and volume management
++                    self.message("Creating root file system as btrfs subvolume @")
++                    self.runcmd(['btrfs', 'subvolume', 'create', "%s/@" % rootdir])
++
++                    # Make sure the subvolume mount point show in in
++                    # /proc/mounts for grub-update to figure out the
++                    # device for the root file system.
++                    newrootdir = "%s/build" % rootdir
++                    os.mkdir(newrootdir)
++                    self.mount(rootdev, newrootdir, ['-o','subvol=@'])
++#                    self.runcmd(['btrfs', 'subvolume', 'set-default', '@', rootdir])
++
++                    # Make the btrfs root file system available in the chroot.
++                    os.mkdir("%s/btrfs" % newrootdir)
++                    self.mount(rootdev, "%s/btrfs" % newrootdir)
++
++                    rootdir = newrootdir
+                 if bootdev:
+                     if self.settings['boottype']:
+                         boottype = self.settings['boottype']
+@@ -245,9 +264,9 @@ class VmDebootstrap(cliapp.Application):  # pylint: disable=too-many-public-meth
+ 
+             if self.settings['image']:
+                 if self.settings['grub']:
+-                    self.install_grub2(rootdev, rootdir)
++                    self.install_grub2(rootdev, rootdir, rootfsdir)
+                 elif self.settings['extlinux']:
+-                    self.install_extlinux(rootdev, rootdir)
++                    self.install_extlinux(rootdev, rootdir, rootfsdir)
+                 self.append_serial_console(rootdir)
+                 self.optimize_image(rootdir)
+                 if self.settings['squash']:
+@@ -300,13 +319,19 @@ class VmDebootstrap(cliapp.Application):  # pylint: disable=too-many-public-meth
+         logging.debug('mkdir %s', dirname)
+         return dirname
+ 
+-    def mount(self, device, path=None):
++    def mount(self, device, path=None, opts=None):
+         if not path:
+             mount_point = self.mkdtemp()
+         else:
+             mount_point = path
+         self.message('Mounting %s on %s' % (device, mount_point))
+-        self.runcmd(['mount', device, mount_point])
++        cmd = ['mount']
++        if opts is not None:
++            for opt in opts:
++                cmd.append(opt)
++        cmd.append(device)
++        cmd.append(mount_point)
++        self.runcmd(cmd)
+         self.mount_points.append(mount_point)
+         logging.debug('mounted %s on %s', device, mount_point)
+         return mount_point
+@@ -458,6 +483,9 @@ class VmDebootstrap(cliapp.Application):  # pylint: disable=too-many-public-meth
+         if self.settings['grub']:
+             include.append('grub-pc')
+ 
++        if 'btrfs' == self.settings['roottype']:
++            include.append('btrfs-tools')
++
+         if not self.settings['no-kernel']:
+             if self.settings['kernel-package']:
+                 kernel_image = self.settings['kernel-package']
+@@ -546,7 +574,12 @@ class VmDebootstrap(cliapp.Application):  # pylint: disable=too-many-public-meth
+         fstab = os.path.join(rootdir, 'etc', 'fstab')
+         with open(fstab, 'w') as f:
+             f.write('proc /proc proc defaults 0 0\n')
+-            f.write('%s / %s errors=remount-ro 0 1\n' % (rootdevstr, roottype))
++            if 'btrfs' == roottype:
++#                f.write('%s / %s defaults 0 1\n' % (rootdevstr, roottype))
++                f.write('%s / %s subvol=@ 0 1\n' % (rootdevstr, roottype))
++                f.write('%s /btrfs %s defaults 1\n' % (rootdevstr, roottype))
++            else:
++                f.write('%s / %s errors=remount-ro 0 1\n' % (rootdevstr, roottype))
+             if bootdevstr:
+                 f.write('%s /boot %s errors=remount-ro 0 2\n' % (bootdevstr, boottype))
+                 if self.settings['swap'] > 0:
+@@ -661,7 +694,8 @@ class VmDebootstrap(cliapp.Application):  # pylint: disable=too-many-public-meth
+             cfg.write("%s\n" % terminal)
+             cfg.write("%s\n" % command)
+ 
+-    def install_grub2(self, rootdev, rootdir):
++    def install_grub2(self, rootdev, rootdir, rootfsdir):
++        # FIXME use rootfsdir
+         self.message("Configuring grub2")
+         # rely on kpartx using consistent naming to map loop0p1 to loop0
+         install_dev = os.path.join('/dev', os.path.basename(rootdev)[:-2])
+@@ -679,12 +713,12 @@ class VmDebootstrap(cliapp.Application):  # pylint: disable=too-many-public-meth
+             self.runcmd(['chroot', rootdir, 'grub-install', install_dev])
+         except cliapp.AppException:
+             self.message("Failed. Is grub2-common installed? Using extlinux.")
+-            self.install_extlinux(rootdev, rootdir)
++            self.install_extlinux(rootdev, rootdir, rootfsdir)
+         self.runcmd(['umount', os.path.join(rootdir, 'sys')])
+         self.runcmd(['umount', os.path.join(rootdir, 'proc')])
+         self.runcmd(['umount', os.path.join(rootdir, 'dev')])
+ 
+-    def install_extlinux(self, rootdev, rootdir):
++    def install_extlinux(self, rootdev, rootdir, rootfsdir):
+         if not os.path.exists("/usr/bin/extlinux"):
+             self.message("extlinux not installed, skipping.")
+             return
+@@ -711,7 +745,7 @@ class VmDebootstrap(cliapp.Application):  # pylint: disable=too-many-public-meth
+                            '-s', 'UUID', rootdev])
+         uuid = out.splitlines()[0].strip()
+ 
+-        conf = os.path.join(rootdir, 'extlinux.conf')
++        conf = os.path.join(rootfsdir, 'extlinux.conf')
+         logging.debug('configure extlinux %s', conf)
+         kserial = 'console=ttyS0,115200' if self.settings['serial-console'] else ''
+         extserial = 'serial 0 115200' if self.settings['serial-console'] else ''
+@@ -721,13 +755,14 @@ timeout 1
+ 
+ label linux
+ kernel %(kernel)s
+-append initrd=%(initrd)s root=UUID=%(uuid)s ro %(kserial)s
++append initrd=%(initrd)s root=UUID=%(uuid)s ro %(kserial)s %(rootflags)s
+ %(extserial)s
+ ''' % {
+             'kernel': kernel_image,  # pylint: disable=bad-continuation
+             'initrd': initrd_image,  # pylint: disable=bad-continuation
+             'uuid': uuid,  # pylint: disable=bad-continuation
+             'kserial': kserial,  # pylint: disable=bad-continuation
++            'rootflags': 'rootfsflags=subvol=@' if 'btrfs' == self.settings['roottype'] else '',  # pylint: disable=bad-continuation
+             'extserial': extserial,  # pylint: disable=bad-continuation
+         }  # pylint: disable=bad-continuation
+         logging.debug("extlinux config:\n%s", msg)
+@@ -738,7 +773,7 @@ append initrd=%(initrd)s root=UUID=%(uuid)s ro %(kserial)s
+         f = open(conf, 'w')
+         f.write(msg)
+ 
+-        self.runcmd(['extlinux', '--install', rootdir])
++        self.runcmd(['extlinux', '--install', rootfsdir])
+         self.runcmd(['sync'])
+         time.sleep(2)
+ 
diff --git a/image_build/vendor/vmdebootstrap b/image_build/vendor/vmdebootstrap
new file mode 160000
index 000000000..390e88369
--- /dev/null
+++ b/image_build/vendor/vmdebootstrap
@@ -0,0 +1 @@
+Subproject commit 390e883698f85e34433ef79aeabfde77c06b1c22
diff --git a/man/freedombone-image.1.gz b/man/freedombone-image.1.gz
new file mode 100644
index 0000000000000000000000000000000000000000..254f3bfbb08261b86c9e23f1d7a0e1a2bdca5701
GIT binary patch
literal 692
zcmV;l0!#fLiwFQXA5T^Q1C3MLZqq;zea2rg$_pUX4lP202qBU-q!DQv)d>~1hh2LT
zuj1V?yR*IgJ(Ei*m4k$3S=P+#nKN_FX7h7cdc=~fCWoo9YK3qNos(BEr5iYTb^N9S
z9eF=JR>2~me;(jMxv1(+l4SWgbf*1_VJB&W{Yc@|qhbu8phRtygIy*>!%Ei(OY&e`
zU{%+~t$-X8g!{l~!yY{UI=!6b<NQ#A@2va^`RBg$Y?z;3jc4=m<+PK`D-)pjkM_6T
z@TRwPR$xuxDL;ck`7+zXj;Hy&KbiD*xM$d)E7283a0`Ld_%H#MaBH~gB?-tAVkw9X
zr@|1@`-dXX(bQ#%A)sTUYFHVD=AJ8ZX-P$7QwOkXChw&HZmcjT;=o=tP*PY1CXy1(
zei_o;Ki;vdFW&vZF;Y&caZ3WLJ^Vr&(<suVLV-;IT2hwN5SxZPLrga<c3}IV7kPgq
zqCRUcU87pnxCtc5m}O#_iPc)RN*005=0p3<16R1)`4K2r!aYR@6~(%gQ6wo6k(0I8
zvL|Xl)F?5mH@{XDl@Y_eFd1gxh__IWy}@w6dinA}q`_h&7P-RhJUXsKb561WRLo82
zb-S|VD(b8t+tn&mzUclnS=WRR@sHz%L+DTP%O{R|as2Y=$$e2}S)k`uyr_-nb}%JJ
z&?-|OO4K6Rq4=F`nt$|Yh0V&5Z!;~(EzPPG_GpRQ)#5t5O%L+-_Aq^K53=|6Ao+kr
zD;C{9xNi{BMupJIG#)qDd(HZb`T6BlCmE223u3t}7T#;pA3bRj$A>JgGo}worhPIS
z&AOx6s0YLTySFF9*KfDcfnh7RQoP!O!9{vfT*t}|ZoZUDe_l=K+q?|T>H0I8Ir#;K
ae6l@tjk>ornRx7JujDsbuj@5p1pojTE?PGL

literal 0
HcmV?d00001

diff --git a/src/freedombone-image b/src/freedombone-image
new file mode 100755
index 000000000..739f2fb90
--- /dev/null
+++ b/src/freedombone-image
@@ -0,0 +1,72 @@
+#!/bin/bash
+#
+# .---.                  .              .
+# |                      |              |
+# |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
+# |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
+# '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
+#
+#                    Freedom in the Cloud
+#
+# Creates a debian image using vmdebootstrap
+#
+# License
+# =======
+#
+# Copyright (C) 2015 Bob Mottram <bob@robotics.uk.to>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+IMAGE_TYPE=$1
+CURR_DIR=$(pwd)
+BUILD_DIR=~/.tmp_freedombone_build
+
+rm $CURR_DIR/*.img
+
+if [ -d $BUILD_DIR ]; then
+	rm -rf $BUILD_DIR
+fi
+mkdir -p $BUILD_DIR
+if [ -f /usr/local/bin/freedombone-image-makefile ]; then
+	cp /usr/local/bin/freedombone-image-makefile $BUILD_DIR/Makefile
+else
+	cp /usr/bin/freedombone-image-makefile $BUILD_DIR/Makefile
+fi
+
+cp -r /etc/freedombone/* $BUILD_DIR
+cd $BUILD_DIR
+
+rm -rf vendor
+mkdir vendor
+if [ -d vendor/vmdebootstrap ] ; then
+    (cd vendor/vmdebootstrap; git checkout .; git pull)
+else
+    git clone git://git.liw.fi/vmdebootstrap vendor/vmdebootstrap
+fi
+cd vendor/vmdebootstrap
+git checkout tags/vmdebootstrap-0.8
+git checkout -b tags/vmdebootstrap-0.8
+for f in ../../vendor-patches/vmdebootstrap/* ; do
+    echo applying $(basename $f)
+    patch -p1 < $f
+done
+
+cd $BUILD_DIR
+make $IMAGE_TYPE
+mv build/*.bz2 $CURR_DIR
+mv build/*.img $CURR_DIR
+mv build/*.sig $CURR_DIR
+rm -rf $BUILD_DIR
+
+exit 0
diff --git a/src/freedombone-image-customise b/src/freedombone-image-customise
new file mode 100755
index 000000000..9b369ea24
--- /dev/null
+++ b/src/freedombone-image-customise
@@ -0,0 +1,158 @@
+#!/bin/sh
+#
+# Based on bin/freedombox-customize from freedom-maker
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+set -e
+set -x
+
+enable_eatmydata_override() {
+    chroot $rootdir apt-get install --no-install-recommends -y eatmydata
+    if [ -x $rootdir/usr/bin/eatmydata ] && \
+        [ ! -f $rootdir/etc/apt/apt.conf.d/95debian-edu-install-dpkg-eatmydata ]; then
+        echo "info: Adding apt config to call dpkg via eatmydata"
+        printf "#!/bin/sh\nexec eatmydata dpkg \"\$@\"\n" \
+            > $rootdir/var/tmp/dpkg-eatmydata
+        chmod 755 $rootdir/var/tmp/dpkg-eatmydata
+        cat > $rootdir/etc/apt/apt.conf.d/95debian-edu-install-dpkg-eatmydata <<EOF
+Dir::Bin::dpkg "/var/tmp/dpkg-eatmydata";
+EOF
+    else
+        echo "error: unable to find /usr/bin/eatmydata after installing the eatmydata package"
+    fi
+}
+
+disable_eatmydata_override() {
+    for override in \
+        /etc/apt/apt.conf.d/95debian-edu-install-dpkg-eatmydata \
+        /var/tmp/dpkg-eatmydata ; do
+        echo "info: Removing apt config to call dpkg via eatmydata"
+        if [ -f $rootdir$override ] ; then
+            rm -f $rootdir$override
+        else
+            echo "warning: missing $rootdir$override"
+        fi
+    done
+    sync # Flush file buffers before continuing
+}
+
+set_apt_sources() {
+    NEW_MIRROR="$1"
+    COMPONENTS="main"
+
+    cat <<EOF > etc/apt/sources.list
+deb $NEW_MIRROR $SUITE $COMPONENTS
+deb-src $NEW_MIRROR $SUITE $COMPONENTS
+
+#deb http://security.debian.org/ $SUITE/updates main
+#deb-src http://security.debian.org/ $SUITE/updates main
+EOF
+}
+
+# Set to true/false to control if eatmydata is used during build
+use_eatmydata=true
+
+rootdir="$1"
+fmdir="$(pwd)"
+image="$fmdir"/"$2"
+cd "$rootdir"
+
+echo info: building $MACHINE for $ARCHITECTURE
+
+export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true
+export LC_ALL=C LANGUAGE=C LANG=C
+
+# Override libpam-tmpdir setting during build, as the directories
+# are not created yet.
+export TMP=/tmp/ TMPDIR=/tmp/
+
+username=freedom
+echo "warning: creating initial user $username with well known password!"
+password=bone
+chroot $rootdir adduser --gecos $username --disabled-password $username
+echo $username:$password | chroot $rootdir /usr/sbin/chpasswd
+chroot $rootdir adduser $username sudo
+
+case "$MACHINE" in
+    virtualbox)
+        # hide irrelevant console keyboard messages.
+        echo "echo \"4 4 1 7\" > /proc/sys/kernel/printk" \
+            >> /etc/init.d/rc.local
+        ;;
+esac
+
+set_apt_sources $BUILD_MIRROR
+chroot $rootdir apt-get update
+
+cat > $rootdir/usr/sbin/policy-rc.d <<EOF
+#!/bin/sh
+exit 101
+EOF
+chmod a+rx $rootdir/usr/sbin/policy-rc.d
+
+if $use_eatmydata ; then
+    enable_eatmydata_override
+fi
+
+if [ -n "$CUSTOM_SETUP" ]; then
+    cp "$CUSTOM_SETUP" "$rootdir"/tmp
+    chroot "$rootdir" apt-get install -y gdebi-core
+    chroot "$rootdir" gdebi -n /tmp/"$(basename $CUSTOM_SETUP)"
+fi
+
+chroot "$rootdir" apt-get install -y git dialog
+chroot "$rootdir" git clone https://github.com/bashrc/freedombone /root/freedombone
+chroot "$rootdir" cd /root/freedombone && make install
+
+chroot $rootdir freedombone-image-hardware-setup 2>&1 | \
+    tee $rootdir/var/log/freedombone-image-hardware-setup.log
+
+rm $rootdir/usr/sbin/policy-rc.d
+
+chroot $rootdir /usr/lib/freedombone/setup 2>&1 | \
+    tee $rootdir/var/log/freedombone-setup.log
+
+# Remove SSH keys from the image
+rm $rootdir/etc/ssh/ssh_host_* || true
+
+# copy u-boot to beginning of image
+case "$MACHINE" in
+    beaglebone)
+    chroot $rootdir apt-get -y install rng-tools
+    sed -i 's|#HRNGDEVICE=/dev/hwrng|HRNGDEVICE=/dev/hwrng|g' $rootdir/etc/default/rng-tools
+
+	dd if=$rootdir/usr/lib/u-boot/am335x_boneblack/MLO of="$image" \
+	   count=1 seek=1 conv=notrunc bs=128k
+	dd if=$rootdir/usr/lib/u-boot/am335x_boneblack/u-boot.img of="$image" \
+	   count=2 seek=1 conv=notrunc bs=384k
+	;;
+    cubieboard2)
+        dd if=$rootdir/usr/lib/u-boot/Cubieboard2/u-boot-sunxi-with-spl.bin of="$image" \
+           seek=8 conv=notrunc bs=1k
+        ;;
+esac
+
+if $use_eatmydata ; then
+    disable_eatmydata_override
+fi
+
+set_apt_sources $MIRROR
+chroot $rootdir apt-get update
+
+cd /
+echo "info: killing leftover processes in chroot"
+# 2014-11-04 this killed /usr/lib/erlang/erts-6.2/bin/epmd, see
+# <URL: https://www.ejabberd.im/epmd?q=epmd > to learn more.
+fuser -mvk $rootdir/. || true
diff --git a/src/freedombone-image-hardware-setup b/src/freedombone-image-hardware-setup
new file mode 100755
index 000000000..c0d147eb8
--- /dev/null
+++ b/src/freedombone-image-hardware-setup
@@ -0,0 +1,170 @@
+#!/bin/sh
+
+enable_serial_console() {
+    # By default, spawn a console on the serial port
+    device="$1"
+    echo "Adding a getty on the serial port"
+    echo "T0:12345:respawn:/sbin/getty -L $device 115200 vt100" >> /etc/inittab
+}
+
+beaglebone_setup_boot() {
+    # Setup uEnv.txt
+    if grep -q btrfs /etc/fstab ; then
+        fstype=btrfs
+    else
+        fstype=ext4
+    fi
+    kernelVersion=$(ls /usr/lib/*/am335x-boneblack.dtb | head -1 | cut -d/ -f4)
+    version=$(echo $kernelVersion | sed 's/linux-image-\(.*\)/\1/')
+    initRd=initrd.img-$version
+    vmlinuz=vmlinuz-$version
+
+    # uEnv.txt for Beaglebone
+    # based on https://github.com/beagleboard/image-builder/blob/master/target/boot/beagleboard.org.txt
+    cat >> /boot/uEnv.txt <<EOF
+mmcroot=/dev/mmcblk0p2 ro
+mmcrootfstype=$fstype rootwait fixrtc
+mmcrootflags=subvol=@
+
+console=ttyO0,115200n8
+
+kernel_file=$vmlinuz
+initrd_file=$initRd
+
+loadaddr=0x80200000
+initrd_addr=0x81000000
+fdtaddr=0x80F80000
+
+initrd_high=0xffffffff
+fdt_high=0xffffffff
+
+loadkernel=load mmc \${mmcdev}:\${mmcpart} \${loadaddr} \${kernel_file}
+loadinitrd=load mmc \${mmcdev}:\${mmcpart} \${initrd_addr} \${initrd_file}; setenv initrd_size \${filesize}
+loadfdt=load mmc \${mmcdev}:\${mmcpart} \${fdtaddr} /dtbs/\${fdtfile}
+
+loadfiles=run loadkernel; run loadinitrd; run loadfdt
+mmcargs=setenv bootargs console=tty0 console=\${console} root=\${mmcroot} rootfstype=\${mmcrootfstype} rootflags=\${mmcrootflags}
+
+uenvcmd=run loadfiles; run mmcargs; bootz \${loadaddr} \${initrd_addr}:\${initrd_size} \${fdtaddr}
+EOF
+
+    mkdir -p /boot/dtbs
+    cp /usr/lib/linux-image-*-armmp/* /boot/dtbs
+}
+
+beaglebone_flash() {
+    # allow flash-kernel to work without valid /proc contents
+    # ** this doesn't *really* work, since there are too many checks
+    #    that fail in an emulated environment!  We'll have to do it by
+    #    hand below anyway...
+    export FK_MACHINE="TI AM335x BeagleBone"
+    apt-get install -y flash-kernel
+}
+
+beaglebone_repack_kernel() {
+# process installed kernel to create uImage, uInitrd, dtb
+# using flash-kernel would be a good approach, except it fails in the
+# cross build environment due to too many environment checks...
+#FK_MACHINE="TI AM335x BeagleBone" flash-kernel
+#  so, let's do it manually...
+
+# flash-kernel's hook-functions provided to mkinitramfs have the
+# unfortunate side-effect of creating /conf/param.conf in the initrd
+# when run from our emulated chroot environment, which means our root=
+# on the kernel command line is completely ignored!  repack the initrd
+# to remove this evil...
+
+    echo "info: repacking beaglebone kernel and initrd"
+
+    kernelVersion=$(ls /usr/lib/*/am335x-boneblack.dtb | head -1 | cut -d/ -f4)
+    version=$(echo $kernelVersion | sed 's/linux-image-\(.*\)/\1/')
+    initRd=initrd.img-$version
+    vmlinuz=vmlinuz-$version
+
+    mkdir /tmp/initrd-repack
+
+    (cd /tmp/initrd-repack ; \
+    zcat /boot/$initRd | cpio -i ; \
+    rm -f conf/param.conf ; \
+    find . | cpio --quiet -o -H newc | \
+    gzip -9 > /boot/$initRd )
+
+    rm -rf /tmp/initrd-repack
+
+    (cd /boot ; \
+    cp /usr/lib/$kernelVersion/am335x-boneblack.dtb dtb ; \
+    cat $vmlinuz dtb >> temp-kernel ; \
+    mkimage -A arm -O linux -T kernel -n "Debian kernel ${version}" \
+    -C none -a 0x80200000 -e 0x80200000 -d temp-kernel uImage ; \
+    rm -f temp-kernel ; \
+    mkimage -A arm -O linux -T ramdisk -C gzip -a 0x81000000 -e 0x81000000 \
+    -n "Debian ramdisk ${version}" \
+    -d $initRd uInitrd )
+}
+
+cubieboard2_setup_boot() {
+    # Setup boot.cmd
+    if grep -q btrfs /etc/fstab ; then
+        fstype=btrfs
+    else
+        fstype=ext4
+    fi
+    kernelVersion=$(ls /usr/lib/*/sun7i-a20-cubieboard2.dtb | head -1 | cut -d/ -f4)
+    version=$(echo $kernelVersion | sed 's/linux-image-\(.*\)/\1/')
+    initRd=initrd.img-$version
+    vmlinuz=vmlinuz-$version
+
+    # boot.cmd for CubieTruck
+    cat >> /boot/boot.cmd <<EOF
+setenv mmcdev 0
+setenv mmcpart 1
+
+setenv mmcroot /dev/mmcblk0p2 ro
+setenv mmcrootfstype $fstype rootwait fixrtc
+setenv mmcrootflags subvol=@
+
+setenv console ttyS0,115200n8
+
+setenv kernel_file $vmlinuz
+setenv initrd_file $initRd
+
+setenv loadaddr 0x46000000
+setenv initrd_addr 0x48000000
+setenv fdtaddr 0x47000000
+
+setenv initrd_high 0xffffffff
+setenv fdt_high 0xffffffff
+
+setenv loadkernel load mmc \${mmcdev}:\${mmcpart} \${loadaddr} \${kernel_file}
+setenv loadinitrd load mmc \${mmcdev}:\${mmcpart} \${initrd_addr} \${initrd_file}\\; setenv initrd_size \\\${filesize}
+setenv loadfdt load mmc \${mmcdev}:\${mmcpart} \${fdtaddr} /dtbs/\${fdtfile}
+
+setenv loadfiles run loadkernel\\; run loadinitrd\\; run loadfdt
+setenv mmcargs setenv bootargs console=\${console} root=\${mmcroot} rootfstype=\${mmcrootfstype} rootflags=\${mmcrootflags}
+
+run loadfiles; run mmcargs; bootz \${loadaddr} \${initrd_addr}:\${initrd_size} \${fdtaddr}
+EOF
+
+    # boot.scr for CubieTruck
+    mkimage -C none -A arm -T script -d /boot/boot.cmd /boot/boot.scr
+
+    # DTBs for CubieTruck
+    mkdir -p /boot/dtbs
+    cp /usr/lib/$kernelVersion/* /boot/dtbs
+
+    # extra boot modules
+    echo "rtc_sunxi" >> /etc/initramfs-tools/modules
+}
+
+case "$MACHINE" in
+    beaglebone)
+    beaglebone_setup_boot
+    beaglebone_flash
+    beaglebone_repack_kernel
+    enable_serial_console ttyO0
+    ;;
+    cubieboard2)
+    cubieboard2_setup_boot
+    enable_serial_console ttyS0
+    ;;
+esac
diff --git a/src/freedombone-image-make b/src/freedombone-image-make
new file mode 100755
index 000000000..907cc1b50
--- /dev/null
+++ b/src/freedombone-image-make
@@ -0,0 +1,129 @@
+#!/bin/sh
+#
+# Based upon bin/mk-freedombox-image from freedom-maker
+# With non-free stuff removed
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+set -e # Exit on first error
+
+#set -x # Enable debugging
+
+IMAGE=$1
+export ARCHITECTURE
+export MACHINE
+export SOURCE
+export SUITE
+
+# Locate vmdebootstrap program fetched in Makefile
+basedir=`pwd`
+vendor_dir="${basedir}/vendor"
+vmdebootstrap_dir="${vendor_dir}/vmdebootstrap"
+
+if [ -z "$MIRROR" ] || [ -z "$SUITE" ] ; then
+    echo error: Missing MIRROR and SUITE settings inherited from Makefile.
+    exit 1
+fi
+
+# Packages to install in all Freedombone environments
+base_pkgs="apt base-files ifupdown initramfs-tools \
+logrotate module-init-tools netbase rsyslog udev debian-archive-keyring"
+
+# Packages needed on the beaglebone
+beaglebone_pkgs="linux-image-armmp u-boot-tools u-boot"
+
+# Packages needed for self-hosted development
+dev_pkgs="build-essential devscripts make man-db emacs org-mode git mercurial"
+
+# Packages needed for XFCE desktop environment
+xfce_pkgs="task-xfce-desktop iceweasel ca-certificates"
+
+echo Building $MACHINE Freedombone for $ARCHITECTURE.
+
+case "$MACHINE" in
+    beaglebone)
+	extra_pkgs="$beaglebone_pkgs"
+	extra_opts="\
+ --variant minbase \
+ --bootoffset=2mib \
+ --bootsize 128M \
+ --boottype ext2 \
+ --no-kernel \
+ --no-extlinux \
+ --foreign /usr/bin/qemu-arm-static \
+ --roottype btrfs \
+"
+	;;
+    virtualbox)
+	extra_opts="\
+ --grub \
+ --roottype btrfs \
+"	;;
+    all)
+	extra_opts="\
+ --grub \
+ --roottype btrfs \
+"	;;
+esac
+
+# allow for lots of extra fun customization options.
+for customization in $CUSTOMIZATIONS
+do
+    case "$customization" in
+        development)
+            extra_pkgs="$extra_pkgs $dev_pkgs"
+            ;;
+	xfce)
+	    extra_pkgs="$extra_pkgs $xfce_pkgs"
+	    ;;
+    esac
+done
+
+for p in $base_pkgs $extra_pkgs; do
+    pkgopts="$pkgopts --package $p"
+done
+
+# Make sure file is owned by current user, not root
+touch $(dirname $IMAGE)/freedombone.log
+
+if [ -x vendor/vmdebootstrap/vmdebootstrap ] ; then
+    VMDEBOOTSTRAP=vendor/vmdebootstrap/vmdebootstrap
+else
+    VMDEBOOTSTRAP=vmdebootstrap
+fi
+
+echo "starting $VMDEBOOTSTRAP"
+# Run vmdebootstrap script to create image
+sudo -H \
+    SUITE="$SUITE" \
+    MIRROR="$MIRROR" \
+    BUILD_MIRROR="$BUILD_MIRROR"\
+    MACHINE="$MACHINE" \
+    ARCHITECTURE="$ARCHITECTURE" \
+    SOURCE="$SOURCE" \
+    CUSTOM_SETUP="$CUSTOM_SETUP" \
+    $VMDEBOOTSTRAP \
+    --log $(dirname $IMAGE)/freedombone.log \
+    --log-level debug \
+    --size $IMAGE_SIZE \
+    --image $IMAGE.img \
+    --hostname freedombone \
+    --verbose \
+    --mirror $BUILD_MIRROR \
+    --customize "freedombone-image-customise" \
+    --lock-root-password \
+    --arch $ARCHITECTURE \
+    --distribution $SUITE \
+    $extra_opts \
+    $pkgopts
diff --git a/src/freedombone-image-makefile b/src/freedombone-image-makefile
new file mode 100755
index 000000000..323fa185b
--- /dev/null
+++ b/src/freedombone-image-makefile
@@ -0,0 +1,147 @@
+#!/usr/bin/make
+#
+# A debian image builder, based upon freedom-maker
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# Where to fetch packages
+MIRROR ?= http://httpredir.debian.org/debian
+BUILD_MIRROR ?= http://httpredir.debian.org/debian
+IMAGE_SIZE ?= 8G
+SUITE ?= jessie
+# include source packages in image?
+SOURCE ?= false
+
+# yes no
+BUILD = $(MACHINE)-$(ARCHITECTURE)
+TODAY := $(shell date +%Y-%m-%d)
+NAME = build/freedombone-$(TODAY)_$(BUILD)
+IMAGE = $(NAME).img
+ARCHIVE = $(NAME).tar.bz2
+SIGNATURE = $(ARCHIVE).sig
+OWNER = 1000
+TAR = tar --checkpoint=1000 --checkpoint-action=dot -cjvf
+SIGN = -gpg --output $(SIGNATURE) --detach-sig $(ARCHIVE)
+
+# settings for `make test`
+TEST_SSH_PORT = 2222
+TEST_FIRSTRUN_WAIT_TIME = 120 # seconds
+
+# Using taskset to pin build process to single core. This is a
+# workaround for a qemu-user-static issue that causes builds to
+# hang. (See Debian bug #769983 for details.)
+MAKE_IMAGE = ARCHITECTURE=$(ARCHITECTURE) MACHINE=$(MACHINE) SOURCE=$(SOURCE) \
+    MIRROR=$(MIRROR) SUITE=$(SUITE) OWNER=$(OWNER) \
+    BUILD_MIRROR=$(BUILD_MIRROR) \
+    CUSTOM_PLINTH=$(CUSTOM_PLINTH) CUSTOM_SETUP=$(CUSTOM_SETUP) \
+    IMAGE_SIZE=$(IMAGE_SIZE) taskset 0x01 freedombone-image-make $(NAME)
+
+# build Beaglebone SD card image
+beaglebone: prep
+	$(eval ARCHITECTURE = armhf)
+	$(eval MACHINE = beaglebone)
+	$(MAKE_IMAGE)
+	$(TAR) $(ARCHIVE) $(IMAGE)
+	@echo ""
+	$(SIGN)
+	@echo "Build complete."
+
+# build Cubieboard2 SD card image
+cubieboard2: prep
+	$(eval ARCHITECTURE = armhf)
+	$(eval MACHINE = cubieboard2)
+	$(MAKE_IMAGE)
+	$(TAR) $(ARCHIVE) $(IMAGE)
+	@echo ""
+	$(SIGN)
+	@echo "Build complete."
+
+# build an i386 image
+i386: prep
+	$(eval ARCHITECTURE = i386)
+	$(eval MACHINE = all)
+	$(MAKE_IMAGE)
+	$(TAR) $(ARCHIVE) $(IMAGE)
+	@echo ""
+	$(SIGN)
+	@echo "Build complete."
+
+# build an amd64 image
+amd64: prep
+	$(eval ARCHITECTURE = amd64)
+	$(eval MACHINE = all)
+	$(MAKE_IMAGE)
+	$(TAR) $(ARCHIVE) $(IMAGE)
+	@echo ""
+	$(SIGN)
+	@echo "Build complete."
+
+# build a virtualbox image
+virtualbox: virtualbox-i386
+
+virtualbox-i386: prep
+	$(eval ARCHITECTURE = i386)
+	$(eval MACHINE = virtualbox)
+	$(MAKE_IMAGE)
+	# Convert image to vdi hard drive
+	VBoxManage convertdd $(NAME).img $(NAME).vdi
+	$(TAR) $(ARCHIVE) $(NAME).vdi
+	@echo ""
+	$(SIGN)
+	@echo "Build complete."
+
+virtualbox-amd64: prep
+	$(eval ARCHITECTURE = amd64)
+	$(eval MACHINE = virtualbox)
+	$(MAKE_IMAGE)
+	# Convert image to vdi hard drive
+	VBoxManage convertdd $(NAME).img $(NAME).vdi
+	$(TAR) $(ARCHIVE) $(NAME).vdi
+	@echo ""
+	$(SIGN)
+	@echo "Build complete."
+
+test: test-virtualbox
+
+test-virtualbox: virtualbox
+	$(eval VM_NAME = freedom-maker-test)
+	VBoxManage createvm --name $(VM_NAME) --ostype "Debian" --register
+	VBoxManage storagectl $(VM_NAME) --name "SATA Controller" --add sata \
+		 --controller IntelAHCI
+	VBoxManage storageattach $(VM_NAME) --storagectl "SATA Controller" \
+		--port 0 --device 0 --type hdd --medium $(NAME).vdi
+	VBoxManage modifyvm $(VM_NAME) --pae on --memory 1024 --vram 128 \
+		--nic1 nat --natpf1 ,tcp,,$(TEST_SSH_PORT),,22
+	VBoxManage startvm $(VM_NAME) --type headless
+	sleep $(TEST_FIRSTRUN_WAIT_TIME) # wait for first-run to complete
+	echo frdm |sshpass -p frdm ssh -o UserKnownHostsFile=/dev/null \
+		-o StrictHostKeyChecking=no -t -t \
+		-p $(TEST_SSH_PORT) fbx@127.0.0.1 \
+		"sudo plinth --diagnose" \
+		|tee build/$(VM_NAME)-results_$(TODAY).log
+	VBoxManage controlvm $(VM_NAME) poweroff
+	VBoxManage modifyvm $(VM_NAME) --hda none
+	VBoxManage unregistervm $(VM_NAME) --delete
+
+vendor/vmdebootstrap/vmdebootstrap: vendor-patches/vmdebootstrap/*.patch
+	bin/fetch-new-vmdebootstrap
+
+prep: vendor/vmdebootstrap/vmdebootstrap
+	mkdir -p build
+
+clean:
+	-rm -f build/freedombone.log
+
+distclean: clean
+	sudo rm -rf build
-- 
GitLab