From ae84077ca69fe508440f142ce2939ecf77efef81 Mon Sep 17 00:00:00 2001
From: "Lukas F. Hartmann" <lukas@mntre.com>
Date: Thu, 4 Mar 2021 15:08:17 +0100
Subject: [PATCH] create 2 images: rescue image and normal image

---
 reform2-imx8mq/mkimage.sh               | 30 ++++++++++++++++-----
 reform2-imx8mq/mkuserland.sh            | 21 +++++----------
 reform2-imx8mq/mkuserland3.sh           | 35 +++++++++++++++++++++++++
 reform2-imx8mq/template-etc/motd-rescue | 15 +++++++++++
 4 files changed, 81 insertions(+), 20 deletions(-)
 create mode 100755 reform2-imx8mq/mkuserland3.sh
 create mode 100644 reform2-imx8mq/template-etc/motd-rescue

diff --git a/reform2-imx8mq/mkimage.sh b/reform2-imx8mq/mkimage.sh
index a022f73..d106053 100755
--- a/reform2-imx8mq/mkimage.sh
+++ b/reform2-imx8mq/mkimage.sh
@@ -17,18 +17,36 @@ sudo ./mkuserland.sh
 # chroot into the userland and build custom packages (mesa, xserver...)
 sudo ./mkuserland2.sh
 
-SIZE=9000M
-#SIZE=2048M
+# Rescue System ---------------------------------------------------------
 
-# create ext4 partition from target root directory directly at 4MiB offset, 8192MiB (8GiB) image size
-sudo /sbin/mke2fs -v -L 'MNTREFORM' -N 0 -O 64bit -E offset=4194304 -d target-userland -m 5 -r 1 -t ext4 reform-system.img $SIZE
+SIZE=2048M
+# create ext4 partition from target root directory directly at 4MiB offset
+sudo /sbin/mke2fs -v -L 'MNTRESCUE' -N 0 -O 64bit -E offset=4194304 -d target-userland -m 5 -r 1 -t ext4 reform-system.img $SIZE
 
 sudo /sbin/parted -s reform-system.img "mklabel msdos"
 sudo /sbin/parted -s reform-system.img "mkpart primary ext4 4MiB -1s"
 sudo /sbin/parted -s reform-system.img print
 
 # mkuboot.sh needs to run before. this creates flash.bin.
-# install u-boot for i.MX8M
+# install u-boot for i.MX8MQ
+sudo dd if=./u-boot/flash.bin of=reform-system.img conv=notrunc bs=1k seek=33
+
+echo Reform Rescue System Image created: reform-rescue-system.img
+
+# Full System -----------------------------------------------------------
+
+# chroot into the userland and add extra applications
+sudo ./mkuserland3.sh
+
+SIZE=9000M
+# create ext4 partition from target root directory directly at 4MiB offset
+sudo /sbin/mke2fs -v -L 'MNTREFORM' -N 0 -O 64bit -E offset=4194304 -d target-userland -m 5 -r 1 -t ext4 reform-system.img $SIZE
+
+sudo /sbin/parted -s reform-system.img "mklabel msdos"
+sudo /sbin/parted -s reform-system.img "mkpart primary ext4 4MiB -1s"
+sudo /sbin/parted -s reform-system.img print
+
+# install u-boot for i.MX8MQ
 sudo dd if=./u-boot/flash.bin of=reform-system.img conv=notrunc bs=1k seek=33
 
-echo Reform system image created: reform-system.img
+echo Reform Full System Image created: reform-system.img
diff --git a/reform2-imx8mq/mkuserland.sh b/reform2-imx8mq/mkuserland.sh
index 4046b8e..fd0f179 100755
--- a/reform2-imx8mq/mkuserland.sh
+++ b/reform2-imx8mq/mkuserland.sh
@@ -14,6 +14,11 @@ cp linux/arch/arm64/boot/dts/freescale/imx8mq-mnt-reform2.dtb target-userland/im
 cp linux/arch/arm64/boot/dts/freescale/imx8mq-mnt-reform2-hdmi.dtb target-userland/imx8mq-mnt-reform2-dual-display.dtb
 # default to single display (less flickery)
 cp linux/arch/arm64/boot/dts/freescale/imx8mq-mnt-reform2.dtb target-userland/imx8mq-mnt-reform2.dtb
+# provide a copy of u-boot for (re)flashing
+mkdir -p target-userland/boot
+cp ./u-boot/flash.bin target-userland/boot/
+cp ./u-boot/flash-rescue.bin target-userland/boot/
+cp ./u-boot/flash-rescue.sh target-userland/boot/
 
 # default audio settings, or PCM will be muted
 mkdir -p target-userland/var/lib/alsa
@@ -34,7 +39,7 @@ cp $ETC/ld.so.conf target-userland/etc # or our libraries won't be picked up
 cp $ETC/resolv.conf target-userland/etc
 mkdir -p target-userland/etc/dhcp
 cp $ETC/dhclient.conf target-userland/etc/dhcp
-cp $ETC/motd target-userland/etc
+cp $ETC/motd-rescue target-userland/etc # we start with the rescue system
 cp $ETC/hostname target-userland/etc
 cp $ETC/hosts target-userland/etc
 cp $ETC/reform-* target-userland/etc
@@ -54,6 +59,7 @@ export LANGUAGE=C
 export LANG=C
 
 echo "root:root" | chpasswd
+passwd -d root # remove root password
 
 # mount /proc if it doesn't exist
 if [[ ! -e /proc/version ]]; then
@@ -90,19 +96,6 @@ mv /default.conf /usr/share/pulseaudio/alsa-mixer/profile-sets/
 mv /etc/reform-hw-setup.service /etc/systemd/system/
 systemctl enable reform-hw-setup.service
 
-# install applications
-
-apt install -y libreoffice libreoffice-gtk3 inkscape firefox-esr emacs gimp wmaker
-apt install -y chromium evolution freecad ardour sxiv neverball scummvm dosbox wf-recorder wev linphone-desktop
-
-# install patched software from mntre
-
-apt install -y kicad minetest libjsoncpp1 blender=2.79b+mntreform
-
-# install a minimal gnome3
-
-apt install -y --no-install-recommends gnome-control-center gnome-session
-
 # install build dependencies
 apt install -y zlib1g-dev patch expat bison flex libunwind-dev libwayland-dev wayland-protocols libwayland-egl-backend-dev libx11-dev libx11-xcb-dev libxdamage-dev libxfixes-dev libxcb-dri3-dev libxcb-xfixes0-dev libxcb-sync-dev libxrandr-dev libxext-dev libxcb-glx0-dev libxcb-present-dev libxcb-dri2-0-dev gettext autopoint libpixman-1-dev libbsd-dev libxkbfile-dev libxcb-composite0-dev libxcb-xinput-dev libxcb-icccm4-dev libxfont-dev nettle-dev libdbus-1-dev libsystemd-dev libpciaccess-dev llvm-dev libudev-dev libmtdev-dev libevdev-dev libxshmfence-dev xutils-dev libdrm-dev libxxf86vm-dev meson gcc g++ python3-setuptools python3-mako xfonts-utils libepoxy-dev libjson-c-dev libpcre3-dev libpango1.0-dev libxkbcommon-dev libinput-dev autoconf make libtool intltool libxml2-dev libxfce4ui-2-dev libgarcon-1-dev xfce4-dev-tools libdbusmenu-gtk3-dev libfmt-dev libgirepository1.0-dev libgtkmm-3.0-dev libjsoncpp-dev libmpdclient-dev libnl-3-dev libnl-genl-3-dev libpulse-dev  libsigc++-2.0-dev libspdlog-dev  libsdl2-gfx-dev libsdl2-mixer-dev libsdl2-net-dev libsdl2-ttf-dev libsdl2-dev libsdl2-image-dev libflac-dev libmpg123-dev libpng-dev libmpeg2-4-dev
 
diff --git a/reform2-imx8mq/mkuserland3.sh b/reform2-imx8mq/mkuserland3.sh
new file mode 100755
index 0000000..12f2815
--- /dev/null
+++ b/reform2-imx8mq/mkuserland3.sh
@@ -0,0 +1,35 @@
+#!/bin/bash
+#
+# This script installs extra applications that are not included in the minimal rescue image.
+#
+
+set -e
+
+ETC=./template-etc
+SKEL=./template-skel
+
+# populate /etc
+cp $ETC/motd target-userland/etc
+
+chroot target-userland /bin/bash <<EOF
+export DEBIAN_FRONTEND=noninteractive
+export DEBCONF_NONINTERACTIVE_SEEN=true
+export LC_ALL=C
+export LANGUAGE=C
+export LANG=C
+
+# install applications
+
+apt install -y libreoffice libreoffice-gtk3 inkscape firefox-esr emacs gimp wmaker
+apt install -y chromium evolution freecad ardour sxiv neverball scummvm dosbox wf-recorder wev linphone-desktop
+
+# install patched software from mntre
+
+apt install -y kicad minetest libjsoncpp1 blender=2.79b+mntreform
+
+# install a minimal gnome3
+
+apt install -y --no-install-recommends gnome-control-center gnome-session
+
+EOF
+
diff --git a/reform2-imx8mq/template-etc/motd-rescue b/reform2-imx8mq/template-etc/motd-rescue
new file mode 100644
index 0000000..5f0022a
--- /dev/null
+++ b/reform2-imx8mq/template-etc/motd-rescue
@@ -0,0 +1,15 @@
+
+                     [?25h[?1;5;2004lWelcome to Debian GNU/Linux
+                     on MNT Reform
+                     Rescue System Image: 2021-03-04
+                     
+                     https://mntre.com/reform
+                     https://www.debian.org
+
+                     This is the Rescue System on eMMC flash (mmcblk0).
+                     It is intended for repairs on your main system
+                     on SD card (mmcblk1) or NVMe SSD.
+
+                     If you don't need a Rescue System, you can
+                     overwrite it with an OS of your choice.
+
-- 
GitLab