From d70a3f5e86f37abe657c13d16ccc081832356d40 Mon Sep 17 00:00:00 2001
From: Kim Hagen <kim@sentrium.io>
Date: Mon, 2 Aug 2021 09:15:21 -0500
Subject: add items for grub setup and enable persistence

---
 functions/configuration.sh       |  42 ++++++++++++++++
 scripts/build/binary_dm-verity   |   4 +-
 scripts/build/binary_grub-legacy |   4 +-
 scripts/build/binary_grub_cfg    |  41 ++++++++++++++-
 scripts/build/binary_hdd         |  11 +++++
 scripts/build/binary_linux-image |   2 +-
 scripts/build/binary_manifest    |   2 +-
 scripts/build/binary_memtest     |   2 +-
 scripts/build/binary_rootfs      |   2 +-
 scripts/build/config             | 104 ++++++++++++++++++++++++++++++++++++++-
 10 files changed, 204 insertions(+), 10 deletions(-)

diff --git a/functions/configuration.sh b/functions/configuration.sh
index 7750a5c54..1e5ff7d12 100755
--- a/functions/configuration.sh
+++ b/functions/configuration.sh
@@ -388,6 +388,15 @@ Prepare_config ()
 	case "${LB_INITRAMFS}" in
 		live-boot)
 			LB_BOOTAPPEND_LIVE="${LB_BOOTAPPEND_LIVE:-boot=live components quiet splash}"
+			LB_BOOTAPPEND_VYOS_CONSOLE_1="${LB_BOOTAPPEND_VYOS_CONSOLE_1:-none}"
+			LB_BOOTAPPEND_VYOS_CONSOLE_2="${LB_BOOTAPPEND_VYOS_CONSOLE_2:-none}"
+			LB_BOOTAPPEND_VYOS_CONSOLE_3="${LB_BOOTAPPEND_VYOS_CONSOLE_3:-none}"
+			LB_BOOTAPPEND_VYOS_PW_CONSOLE_1="${LB_BOOTAPPEND_VYOS_PW_CONSOLE_1:-none}"
+			LB_BOOTAPPEND_VYOS_PW_CONSOLE_2="${LB_BOOTAPPEND_VYOS_PW_CONSOLE_2:-none}"
+			LB_BOOTAPPEND_VYOS_PW_CONSOLE_3="${LB_BOOTAPPEND_VYOS_PW_CONSOLE_3:-none}"
+			LB_VYOS_CONSOLE_1="${LB_VYOS_CONSOLE_1:-none}"
+			LB_VYOS_CONSOLE_2="${LB_VYOS_CONSOLE_2:-none}"
+			LB_VYOS_CONSOLE_3="${LB_VYOS_CONSOLE_3:-none}"
 			LB_BOOTAPPEND_LIVE_FAILSAFE="${LB_BOOTAPPEND_LIVE_FAILSAFE:-boot=live components memtest noapic noapm nodma nomce nolapic nomodeset nosmp nosplash vga=788}"
 			;;
 
@@ -501,6 +510,9 @@ Prepare_config ()
 	LB_BOOTSTRAP_QEMU_ARCHITECTURE="${LB_BOOTSTRAP_QEMU_ARCHITECTURE:-}"
 	LB_BOOTSTRAP_QEMU_EXCLUDE="${LB_BOOTSTRAP_QEMU_EXCLUDE:-}"
 	LB_BOOTSTRAP_QEMU_STATIC="${LB_BOOTSTRAP_QEMU_STATIC:-}"
+
+        LB_VYOS_VERSION="${LB_VYOS_VERSION:-live}"
+        LB_VYOS_PERSISTENCE="${LB_VYOS_PERSISTENCE:-false}"
 }
 
 Validate_config ()
@@ -710,6 +722,36 @@ Validate_config_permitted_values ()
 		exit 1
 	fi
 
+        if [ "${LB_BOOTAPPEND_VYOS_CONSOLE_1}" != "none" ] && [ "${LB_VYOS_CONSOLE_1}" = "none" ]; then
+                Echo_error "(--vyos-console-1) is not set."
+                exit 1
+        fi
+
+        if [ "${LB_VYOS_CONSOLE_1}" != "none" ] && [ "${LB_BOOTAPPEND_VYOS_CONSOLE_1}" = "none" ]; then
+                Echo_error "(--bootappend-vyos-console-1) is not set."
+                exit 1
+        fi
+
+        if [ "${LB_BOOTAPPEND_VYOS_CONSOLE_2}" != "none" ] && [ "${LB_VYOS_CONSOLE_2}" = "none" ]; then
+                Echo_error "(--vyos-console-2) is not set."
+                exit 1
+        fi
+
+        if [ "${LB_VYOS_CONSOLE_2}" != "none" ] && [ "${LB_BOOTAPPEND_VYOS_CONSOLE_2}" = "none" ]; then
+                Echo_error "(--bootappend-vyos-console-2) is not set."
+                exit 1
+        fi
+
+        if [ "${LB_BOOTAPPEND_VYOS_CONSOLE_3}" != "none" ] && [ "${LB_VYOS_CONSOLE_3}" = "none" ]; then
+                Echo_error "(--vyos-console-3) is not set."
+                exit 1
+        fi
+
+        if [ "${LB_VYOS_CONSOLE_3}" != "none" ] && [ "${LB_BOOTAPPEND_VYOS_CONSOLE_3}" = "none" ]; then
+                Echo_error "(--bootappend-vyos-console-3) is not set."
+                exit 1
+        fi
+
 	if ! In_list "${LB_INITRAMFS_COMPRESSION}" bzip2 gzip lzma; then
 		Echo_error "You have specified an invalid value for LB_INITRAMFS_COMPRESSION (--initramfs-compression)."
 		exit 1
diff --git a/scripts/build/binary_dm-verity b/scripts/build/binary_dm-verity
index c9d899c42..066f527b9 100755
--- a/scripts/build/binary_dm-verity
+++ b/scripts/build/binary_dm-verity
@@ -40,7 +40,7 @@ esac
 
 case "${LB_INITRAMFS}" in
 	live-boot)
-		INITFS="live"
+		INITFS="${LB_VYOS_VERSION}"
 		;;
 
 	*)
@@ -117,4 +117,4 @@ fi
 
 
 # Creating stage file
-Create_stagefile
\ No newline at end of file
+Create_stagefile
diff --git a/scripts/build/binary_grub-legacy b/scripts/build/binary_grub-legacy
index 1b5f3ebb6..55550e5ee 100755
--- a/scripts/build/binary_grub-legacy
+++ b/scripts/build/binary_grub-legacy
@@ -100,7 +100,7 @@ fi
 
 case "${LB_INITRAMFS}" in
 	live-boot)
-		INITFS="live"
+		INITFS="${LB_VYOS_VERSION}"
 		;;
 
 	*)
@@ -111,7 +111,7 @@ esac
 # Setting destination directory
 case "${LB_INITRAMFS}" in
 	live-boot)
-		DESTDIR_LIVE="binary/live"
+		DESTDIR_LIVE="binary/${LB_VYOS_VERSION}"
 		;;
 
 	*)
diff --git a/scripts/build/binary_grub_cfg b/scripts/build/binary_grub_cfg
index 811410f36..91447acc7 100755
--- a/scripts/build/binary_grub_cfg
+++ b/scripts/build/binary_grub_cfg
@@ -99,7 +99,7 @@ fi
 
 case "${LB_INITRAMFS}" in
 	live-boot)
-		INITFS="live"
+		INITFS="${LB_VYOS_VERSION}"
 		;;
 
 	*)
@@ -156,6 +156,45 @@ if [ "${_AMD64_686_NUMBER}" -ge 2 ] ; then
 			"/${INITFS}/${_686_INITRD}" \
 			"${LB_BOOTAPPEND_LIVE_FAILSAFE}"
 	fi
+elif [ "${LB_VYOS_VERSION}" != "live" ]; then
+	if [ "${LB_BOOTAPPEND_VYOS_CONSOLE_1}" != "none" ]; then
+                Grub_live_menu_entry "VyOS $(basename ${LB_VYOS_VERSION}) linux (${LB_VYOS_CONSOLE_1} console)" \
+                        "/${INITFS}/${DEFAULT_KERNEL}" \
+                        "/${INITFS}/${DEFAULT_INITRD}" \
+                        "${LB_BOOTAPPEND_VYOS_CONSOLE_1}" \
+                        "l"
+        fi
+        if [ "${LB_BOOTAPPEND_VYOS_CONSOLE_2}" != "none" ]; then
+                Grub_live_menu_entry "VyOS $(basename ${LB_VYOS_VERSION}) linux (${LB_VYOS_CONSOLE_2} console)" \
+                        "/${INITFS}/${DEFAULT_KERNEL}" \
+                        "/${INITFS}/${DEFAULT_INITRD}" \
+                        "${LB_BOOTAPPEND_VYOS_CONSOLE_2}"
+        fi
+        if [ "${LB_BOOTAPPEND_VYOS_CONSOLE_3}" != "none" ]; then
+                Grub_live_menu_entry "VyOS $(basename ${LB_VYOS_VERSION}) linux (${LB_VYOS_CONSOLE_3} console)" \
+                        "/${INITFS}/${DEFAULT_KERNEL}" \
+                        "/${INITFS}/${DEFAULT_INITRD}" \
+                        "${LB_BOOTAPPEND_VYOS_CONSOLE_3}"
+        fi
+        if [ "${LB_BOOTAPPEND_VYOS_PW_CONSOLE_1}" != "none" ]; then
+                Grub_live_menu_entry "Lost password change $(basename ${LB_VYOS_VERSION}) linux (${LB_VYOS_CONSOLE_1} console)" \
+                        "/${INITFS}/${DEFAULT_KERNEL}" \
+                        "/${INITFS}/${DEFAULT_INITRD}" \
+                        "${LB_BOOTAPPEND_VYOS_PW_CONSOLE_1}"
+        fi
+        if [ "${LB_BOOTAPPEND_VYOS_PW_CONSOLE_2}" != "none" ]; then
+                Grub_live_menu_entry "Lost password change $(basename ${LB_VYOS_VERSION}) linux (${LB_VYOS_CONSOLE_2} console)" \
+                        "/${INITFS}/${DEFAULT_KERNEL}" \
+                        "/${INITFS}/${DEFAULT_INITRD}" \
+                        "${LB_BOOTAPPEND_VYOS_PW_CONSOLE_2}"
+        fi
+        if [ "${LB_BOOTAPPEND_VYOS_PW_CONSOLE_3}" != "none" ]; then
+                Grub_live_menu_entry "Lost password change $(basename ${LB_VYOS_VERSION}) linux (${LB_VYOS_CONSOLE_3} console)" \
+                        "/${INITFS}/${DEFAULT_KERNEL}" \
+                        "/${INITFS}/${DEFAULT_INITRD}" \
+                        "${LB_BOOTAPPEND_VYOS_PW_CONSOLE_3}"
+        fi
+
 else
 	Grub_live_menu_entry "Live system" \
 		"/${INITFS}/${DEFAULT_KERNEL}" \
diff --git a/scripts/build/binary_hdd b/scripts/build/binary_hdd
index 83e068c23..42800df0c 100755
--- a/scripts/build/binary_hdd
+++ b/scripts/build/binary_hdd
@@ -260,6 +260,17 @@ else
 fi
 cp -T ${CP_OPTIONS} binary/ chroot/binary.tmp
 
+if [ "${LB_VYOS_PERSISTENCE}" = "true" ]
+then
+        mkdir -p chroot/binary.tmp/${LB_VYOS_VERSION}/rw
+        mkdir -p chroot/binary.tmp/${LB_VYOS_VERSION}/work
+        mv chroot/binary.tmp/${LB_VYOS_VERSION}/filesystem.squashfs \
+        chroot/binary.tmp/${LB_VYOS_VERSION}/$(basename ${LB_VYOS_VERSION}).squashfs
+        cat > chroot/binary.tmp/persistence.conf << EOF
+/ union
+EOF
+fi
+
 if [ "${LB_BOOTLOADER_BIOS}" = "grub-pc" ];
 then
         case "${LB_BUILD_WITH_CHROOT}" in
diff --git a/scripts/build/binary_linux-image b/scripts/build/binary_linux-image
index 26626cfe8..6afe21ca0 100755
--- a/scripts/build/binary_linux-image
+++ b/scripts/build/binary_linux-image
@@ -39,7 +39,7 @@ Acquire_lockfile
 
 case "${LB_INITRAMFS}" in
 	live-boot)
-		DESTDIR="binary/live"
+		DESTDIR="binary/${LB_VYOS_VERSION}"
 		;;
 
 	*)
diff --git a/scripts/build/binary_manifest b/scripts/build/binary_manifest
index 382305611..606e55448 100755
--- a/scripts/build/binary_manifest
+++ b/scripts/build/binary_manifest
@@ -39,7 +39,7 @@ Acquire_lockfile
 
 case "${LB_INITRAMFS}" in
 	live-boot)
-		INITFS="live"
+		INITFS="${LB_VYOS_VERSION}"
 		SUFFIX="packages"
 		;;
 
diff --git a/scripts/build/binary_memtest b/scripts/build/binary_memtest
index acfdfcbce..bb0f22175 100755
--- a/scripts/build/binary_memtest
+++ b/scripts/build/binary_memtest
@@ -75,7 +75,7 @@ Install_packages
 # Setting destination directory
 case "${LB_INITRAMFS}" in
 	live-boot)
-		DESTDIR="binary/live"
+		DESTDIR="binary/${LB_VYOS_VERSION}"
 		;;
 
 	*)
diff --git a/scripts/build/binary_rootfs b/scripts/build/binary_rootfs
index 135a2dbc0..24177c225 100755
--- a/scripts/build/binary_rootfs
+++ b/scripts/build/binary_rootfs
@@ -44,7 +44,7 @@ esac
 
 case "${LB_INITRAMFS}" in
 	live-boot)
-		INITFS="live"
+		INITFS="${LB_VYOS_VERSION}"
 		;;
 
 	*)
diff --git a/scripts/build/config b/scripts/build/config
index 14cff154b..4b1b679f5 100755
--- a/scripts/build/config
+++ b/scripts/build/config
@@ -33,7 +33,17 @@ USAGE="${PROGRAM} [--apt apt|apt-get|aptitude]\n\
 \t    [-b|--binary-image iso|iso-hybrid|netboot|tar|hdd]\n\
 \t    [--bootappend-install PARAMETER|\"PARAMETERS\"]\n\
 \t    [--bootappend-live PARAMETER|\"PARAMETERS\"]\n\
+\t    [--bootappend-vyos-console-1 PARAMETER|\"PARAMETERS\"]\n\
+\t    [--bootappend-vyos-console-2 PARAMETER|\"PARAMETERS\"]\n\
+\t    [--bootappend-vyos-console-3 PARAMETER|\"PARAMETERS\"]\n\
+\t    [--bootappend-vyos-pw-console-1 PARAMETER|\"PARAMETERS\"]\n\
+\t    [--bootappend-vyos-pw-console-2 PARAMETER|\"PARAMETERS\"]\n\
+\t    [--bootappend-vyos-pw-console-3 PARAMETER|\"PARAMETERS\"]\n\
 \t    [--bootappend-live-failsafe PARAMETER|\"PARAMETERS\"]\n\
+\t    [--vyos-console-1 KVM|Serial|USB]\n\
+\t    [--vyos-console-2 KVM|Serial|USB]\n\
+\t    [--vyos-console-3 KVM|Serial|USB]\n\
+\t    [--vyos-persistence true|false]\n\
 \t    [--bootloaders grub-legacy|grub-pc|syslinux|grub-efi|\"BOOTLOADERS\"]\n\
 \t    [--bootstrap-qemu-arch ARCH]\n\
 \t    [--bootstrap-qemu-exclude PACKAGE|\"PACKAGES\"]\n\
@@ -127,6 +137,7 @@ USAGE="${PROGRAM} [--apt apt|apt-get|aptitude]\n\
 \t    [--utc-time true|false]\n\
 \t    [--validate]\n\
 \t    [--verbose]\n\
+\t    [--vyos-version]\n\
 \t    [--win32-loader true|false]\n\
 \t    [--zsync true|false]"
 
@@ -140,6 +151,9 @@ Local_arguments ()
 		apt-source-archives:,architecture:,architectures:,archive-areas:,
 		backports:,binary-filesystem:,binary-image:,binary-images:,
 		bootappend-install:,bootappend-live:,bootappend-live-failsafe:,
+                bootappend-vyos-console-1:,bootappend-vyos-console-2:,bootappend-vyos-console-3:,
+                bootappend-vyos-pw-console-1:,bootappend-vyos-pw-console-2:,bootappend-vyos-pw-console-3:,
+                vyos-console-1:,vyos-console-2:,vyos-console-3:,vyos-persistence:,
 		bootloader:,bootloaders:,bootstrap-qemu-arch:,bootstrap-qemu-exclude:,
 		bootstrap-qemu-static:,breakpoints,build-with-chroot:,
 		cache:,cache-indices:,cache-packages:,cache-stages:,checksums:,
@@ -175,7 +189,7 @@ Local_arguments ()
 		security:,source:,source-images:,swap-file-path:,swap-file-size:,system:,
 		tasksel:,
 		uefi-secure-boot:,updates:,utc-time:,usage,
-		validate,verbose,version,
+		validate,verbose,version,vyos-version:,
 		win32-loader:,
 		zsync:"
 	# Remove spaces added by indentation
@@ -365,11 +379,61 @@ Local_arguments ()
 				shift 2
 				;;
 
+			--bootappend-vyos-console-1)
+				LB_BOOTAPPEND_VYOS_CONSOLE_1="${2}"
+				shift 2
+				;;
+
+			--bootappend-vyos-console-2)
+				LB_BOOTAPPEND_VYOS_CONSOLE_2="${2}"
+				shift 2
+				;;
+
+			--bootappend-vyos-console-3)
+				LB_BOOTAPPEND_VYOS_CONSOLE_3="${2}"
+				shift 2
+				;;
+
+			--bootappend-vyos-pw-console-1)
+				LB_BOOTAPPEND_VYOS_PW_CONSOLE_1="${2}"
+				shift 2
+				;;
+
+			--bootappend-vyos-pw-console-2)
+				LB_BOOTAPPEND_VYOS_PW_CONSOLE_2="${2}"
+				shift 2
+				;;
+
+			--bootappend-vyos-pw-console-3)
+				LB_BOOTAPPEND_VYOS_PW_CONSOLE_3="${2}"
+				shift 2
+				;;
+
 			--bootappend-live-failsafe)
 				LB_BOOTAPPEND_LIVE_FAILSAFE="${2}"
 				shift 2
 				;;
 
+			--vyos-console-1)
+				LB_VYOS_CONSOLE_1="${2}"
+				shift 2
+				;;
+
+			--vyos-console-2)
+				LB_VYOS_CONSOLE_2="${2}"
+				shift 2
+				;;
+
+			--vyos-console-3)
+				LB_VYOS_CONSOLE_3="${2}"
+				shift 2
+				;;
+
+			--vyos-persistence)
+				LB_VYOS_PERSISTENCE="${2}"
+				shift 2
+				;;
+
 			--bootloaders|--bootloader)
 				# This will be split up into BIOS and EFI selections by `Prepare_config()`
 				LB_BOOTLOADERS="${2}"
@@ -831,6 +895,11 @@ Local_arguments ()
 				shift 2
 				;;
 
+                        --vyos-version)
+                                LB_VYOS_VERSION="boot/${2}"
+                                shift 2
+                                ;;
+
 			--win32-loader)
 				LB_WIN32_LOADER="${2}"
 				shift 2
@@ -1215,6 +1284,36 @@ LB_APT_INDICES="${LB_APT_INDICES}"
 # Set boot parameters
 LB_BOOTAPPEND_LIVE="${LB_BOOTAPPEND_LIVE}"
 
+# Set boot parameters
+LB_BOOTAPPEND_VYOS_CONSOLE_1="${LB_BOOTAPPEND_VYOS_CONSOLE_1}"
+
+# Set boot parameters
+LB_BOOTAPPEND_VYOS_CONSOLE_2="${LB_BOOTAPPEND_VYOS_CONSOLE_2}"
+
+# Set boot parameters
+LB_BOOTAPPEND_VYOS_CONSOLE_3="${LB_BOOTAPPEND_VYOS_CONSOLE_3}"
+
+# Set boot parameters
+LB_BOOTAPPEND_VYOS_PW_CONSOLE_1="${LB_BOOTAPPEND_VYOS_PW_CONSOLE_1}"
+
+# Set boot parameters
+LB_BOOTAPPEND_VYOS_PW_CONSOLE_2="${LB_BOOTAPPEND_VYOS_PW_CONSOLE_2}"
+
+# Set boot parameters
+LB_BOOTAPPEND_VYOS_PW_CONSOLE_3="${LB_BOOTAPPEND_VYOS_PW_CONSOLE_3}"
+
+# Set console
+LB_VYOS_CONSOLE_1="${LB_VYOS_CONSOLE_1}"
+
+# Set console
+LB_VYOS_CONSOLE_2="${LB_VYOS_CONSOLE_2}"
+
+# Set console
+LB_VYOS_CONSOLE_3="${LB_VYOS_CONSOLE_3}"
+
+# Set VyOS presistence
+LB_VYOS_PERSISTENCE="${LB_VYOS_PERSISTENCE}"
+
 # Set boot parameters
 LB_BOOTAPPEND_INSTALL="${LB_BOOTAPPEND_INSTALL}"
 
@@ -1291,6 +1390,9 @@ LB_MEMTEST="${LB_MEMTEST}"
 # Set loadlin
 LB_LOADLIN="${LB_LOADLIN}"
 
+# Set VyOS version
+LB_VYOS_VERSION="${LB_VYOS_VERSION}"
+
 # Set win32-loader
 LB_WIN32_LOADER="${LB_WIN32_LOADER}"
 
-- 
cgit v1.2.3