package: Add in the runonce script

This creates the runonce service we can use for running initial
OEM-style OS setup scripts. Currently this only includes the e2fsresize
and service enablement stuff.

Change-Id: I2dd9e4b846e95a828f810dfc7a776d8d84bff5bf
diff --git a/debian/aiy-board-tweaks.install b/debian/aiy-board-tweaks.install
new file mode 100644
index 0000000..4e66675
--- /dev/null
+++ b/debian/aiy-board-tweaks.install
@@ -0,0 +1,2 @@
+usr /
+etc /
diff --git a/debian/changelog b/debian/changelog
new file mode 100644
index 0000000..7b8b957
--- /dev/null
+++ b/debian/changelog
@@ -0,0 +1,5 @@
+aiy-board-tweaks (0.1) UNRELEASED; urgency=medium
+
+  * Initial release.
+
+ -- AIY Projects <support-aiyprojects@google.com>  Wed, 12 Sep 2018 16:14:00 -0800
diff --git a/debian/compat b/debian/compat
new file mode 100644
index 0000000..f599e28
--- /dev/null
+++ b/debian/compat
@@ -0,0 +1 @@
+10
diff --git a/debian/control b/debian/control
new file mode 100644
index 0000000..a6c4a94
--- /dev/null
+++ b/debian/control
@@ -0,0 +1,21 @@
+Source: aiy-board-tweaks
+Maintainer: AIY Projects <support-aiyprojects@google.com>
+Build-Depends: debhelper
+Section: misc
+Priority: optional
+
+Package: aiy-board-tweaks
+Section: misc
+Priority: required
+Architecture: all
+Depends: e2fsprogs(>=1.43), adduser, sudo, openssh-server, locales, avahi-daemon,
+  bluez, network-manager, passwd, ${misc:Depends}
+Description: Performs initial system setup work
+ This package contains the initial "run once" systemd service that performs
+ initial startup work such as resizing the root filesystem to match the emmc
+ size, adding in known users, enabling services that users will likely want by
+ default, and other housekeeping behaviors (such as forcing a regeneration of
+ ssh host keys).
+ . 
+ This package is mostly empty, save for the runonce systemd service, so
+ removing it should have little effect on a running system.
diff --git a/debian/copyright b/debian/copyright
new file mode 100644
index 0000000..c1b02eb
--- /dev/null
+++ b/debian/copyright
@@ -0,0 +1,7 @@
+Format: http://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
+Upstream-Name: aiy-board-resize2fs
+Source: https://aiyprojects.googlesource.com/aiy-board-resize2fs
+
+Files: *
+Copyright: Copyright 2018 Google, LLC <support-aiyprojects@google.com>
+License: Apache-2.0
diff --git a/debian/dirs b/debian/dirs
new file mode 100644
index 0000000..f4cccf4
--- /dev/null
+++ b/debian/dirs
@@ -0,0 +1 @@
+var/cache/runonce
diff --git a/debian/postinst b/debian/postinst
new file mode 100755
index 0000000..8784ce2
--- /dev/null
+++ b/debian/postinst
@@ -0,0 +1,31 @@
+#!/bin/sh -e
+
+# Remove pre-generated ssh host keys and any leftover bootstrap log files. 
+rm -f /etc/ssh/ssh_host_*
+rm -f /var/log/bootstrap.log
+
+# Fix up our hostname and make sure it can be resolved to something sane
+echo aiy >/etc/hostname
+echo 127.0.0.1 aiy >>/etc/hosts
+
+# We need spidev stuff loaded so that we get SPI access
+# XXX: Should this live here? Seems specific to a particular board.
+echo spidev >> /etc/modules
+
+# Choose a locale and regenerate it to eliminate LC warnings.
+echo en_US.UTF-8 UTF-8 >/etc/locale.gen
+locale-gen
+
+# Add the aiy user and give them all the access they need.
+adduser aiy --home /home/aiy --shell /bin/bash --disabled-password --gecos ""
+mkdir -p /home/aiy
+chown aiy:aiy /home/aiy
+echo 'aiy:aiy' |chpasswd
+
+GROUPS="adm audio bluetooth games input plugdev staff sudo users video netdev systemd-journal"
+
+for group in $GROUPS; do \
+	adduser aiy $group; \
+done
+
+echo 'aiy ALL=(ALL) ALL' >>/etc/sudoers
diff --git a/debian/rules b/debian/rules
new file mode 100755
index 0000000..522626d
--- /dev/null
+++ b/debian/rules
@@ -0,0 +1,11 @@
+#!/usr/bin/make -f
+# -*- makefile -*-
+
+%:
+	dh $@ --with systemd
+
+override_dh_systemd_enable:
+	dh_systemd_enable --name=runonce
+
+override_dh_systemd_start:
+	dh_systemd_start --no-start
diff --git a/debian/runonce.service b/debian/runonce.service
new file mode 100644
index 0000000..399e0dd
--- /dev/null
+++ b/debian/runonce.service
@@ -0,0 +1,13 @@
+[Unit]
+Description=Scripts that should be run only once
+Before=basic.target
+After=sysinit.target local-fs.target
+DefaultDependencies=no
+
+[Service]
+Type=oneshot
+RemainAfterExit=yes
+ExecStart=/usr/sbin/runonce
+
+[Install]
+WantedBy=basic.target
diff --git a/etc/runonce.d/00-e2fsresize b/etc/runonce.d/00-e2fsresize
new file mode 100755
index 0000000..ca135ec
--- /dev/null
+++ b/etc/runonce.d/00-e2fsresize
@@ -0,0 +1,3 @@
+#!/bin/bash
+
+/usr/sbin/resize2fs $(findmnt -n -o SOURCE /)
diff --git a/etc/runonce.d/01-enable-services b/etc/runonce.d/01-enable-services
new file mode 100755
index 0000000..69e6d20
--- /dev/null
+++ b/etc/runonce.d/01-enable-services
@@ -0,0 +1,6 @@
+#!/bin/bash
+
+systemctl enable ssh
+systemctl enable bluetooth
+systemctl enable avahi-daemon
+systemctl enable NetworkManager
diff --git a/usr/sbin/runonce b/usr/sbin/runonce
new file mode 100755
index 0000000..dfe173b
--- /dev/null
+++ b/usr/sbin/runonce
@@ -0,0 +1,7 @@
+#!/bin/bash
+
+for script in /etc/runonce.d/*; do
+    fname=$(basename $script)
+    [[ -f /var/cache/runonce/$fname ]] && continue
+    $script && touch /var/cache/runonce/$fname
+done