tools/create-gce-image.sh: Generate ppc64le pseries disk images

Add support for generating ppc64le pseries disk images. This will create a
disk image with a PowerPC PReP boot partition at the start, and install
the IEEE1275/OpenFirmware version of GRUB.

If we ever support the powernv platform in the future we're going to have
to do something different here.

Add a command line argument to specify architecture, defaulting to amd64.

Signed-off-by: Andrew Donnellan <andrew.donnellan@au1.ibm.com>
diff --git a/pkg/build/linux_generated.go b/pkg/build/linux_generated.go
index c2cce94..06fb122 100644
--- a/pkg/build/linux_generated.go
+++ b/pkg/build/linux_generated.go
@@ -10,13 +10,24 @@
 CLEANUP=""
 trap 'eval " $CLEANUP"' EXIT
 
+IMG_ARCH="${3:-amd64}"
+
 if [ ! -e $1/sbin/init ]; then
-	echo "usage: create-gce-image.sh /dir/with/user/space/system /path/to/bzImage"
+	echo "usage: create-gce-image.sh /dir/with/user/space/system /path/to/bzImage [arch]"
 	exit 1
 fi
 
-if [ "$(basename $2)" != "bzImage" ]; then
-	echo "usage: create-gce-image.sh /dir/with/user/space/system /path/to/bzImage"
+case "$IMG_ARCH" in
+	386|amd64)
+		KERNEL_IMAGE_BASENAME=bzImage
+		;;
+	ppc64le)
+		KERNEL_IMAGE_BASENAME=zImage.pseries
+		;;
+esac
+
+if [ "$(basename $2)" != "$KERNEL_IMAGE_BASENAME" ]; then
+	echo "usage: create-gce-image.sh /dir/with/user/space/system /path/to/bzImage [arch]"
 	exit 1
 fi
 
@@ -53,8 +64,18 @@
 	sudo qemu-nbd -c $DISKDEV --format=raw disk.raw
 	CLEANUP="sudo qemu-nbd -d $DISKDEV; $CLEANUP"
 fi
-echo -en "o\nn\np\n1\n\n\na\nw\n" | sudo fdisk $DISKDEV
-PARTDEV=$DISKDEV"p1"
+
+case "$IMG_ARCH" in
+	386|amd64)
+		echo -en "o\nn\np\n1\n\n\na\nw\n" | sudo fdisk $DISKDEV
+		PARTDEV=$DISKDEV"p1"
+		;;
+	ppc64le)
+		echo -en "g\nn\n1\n2048\n16383\nt\n7\nn\n2\n\n\nw\n" | sudo fdisk $DISKDEV
+		PARTDEV=$DISKDEV"p2"
+		;;
+esac
+
 until [ -e $PARTDEV ]; do sleep 1; done
 sudo -E mkfs.ext4 -O ^resize_inode,^has_journal,ext_attr,extents,huge_file,flex_bg,dir_nlink,sparse_super $PARTDEV
 mkdir -p disk.mnt
@@ -99,7 +120,9 @@
 	CMDLINE=$(awk '{printf("%s ", $0)}' $SYZ_CMDLINE_FILE)
 fi
 
-cat << EOF | sudo tee disk.mnt/boot/grub/grub.cfg
+case "$IMG_ARCH" in
+386|amd64)
+	cat << EOF | sudo tee disk.mnt/boot/grub/grub.cfg
 terminal_input console
 terminal_output console
 set timeout=0
@@ -115,5 +138,22 @@
 	linux /vmlinuz root=/dev/sda1 console=ttyS0 earlyprintk=serial vsyscall=native rodata=n oops=panic panic_on_warn=1 nmi_watchdog=panic panic=86400 $CMDLINE
 }
 EOF
-sudo grub-install --target=i386-pc --boot-directory=disk.mnt/boot --no-floppy $DISKDEV
+	sudo grub-install --target=i386-pc --boot-directory=disk.mnt/boot --no-floppy $DISKDEV
+	;;
+ppc64le)
+	cat << EOF | sudo tee disk.mnt/boot/grub/grub.cfg
+terminal_input console
+terminal_output console
+set timeout=0
+menuentry 'linux' --class gnu-linux --class gnu --class os {
+	insmod gzio
+	insmod part_gpt
+	insmod ext2
+	set root='(ieee1275/disk,gpt2)'
+	linux /vmlinuz root=/dev/sda2 console=ttyS0 earlyprintk=serial rodata=n oops=panic panic_on_warn=1 nmi_watchdog=panic panic=86400 $CMDLINE
+}
+EOF
+	sudo grub-install --target=powerpc-ieee1275 --boot-directory=disk.mnt/boot $DISKDEV"p1"
+	;;
+esac
 `
diff --git a/tools/create-gce-image.sh b/tools/create-gce-image.sh
index 58c91c0..bcb11d0 100755
--- a/tools/create-gce-image.sh
+++ b/tools/create-gce-image.sh
@@ -14,7 +14,7 @@
 #   sudo apt-get install grub-efi
 #
 # Usage:
-#   ./create-gce-image.sh /dir/with/user/space/system /path/to/bzImage
+#   ./create-gce-image.sh /dir/with/user/space/system /path/to/{zImage,bzImage} [arch]
 #
 # SYZ_VM_TYPE env var controls type of target test machine. Supported values:
 # - qemu (default)
@@ -50,13 +50,24 @@
 CLEANUP=""
 trap 'eval " $CLEANUP"' EXIT
 
+IMG_ARCH="${3:-amd64}"
+
 if [ ! -e $1/sbin/init ]; then
-	echo "usage: create-gce-image.sh /dir/with/user/space/system /path/to/bzImage"
+	echo "usage: create-gce-image.sh /dir/with/user/space/system /path/to/bzImage [arch]"
 	exit 1
 fi
 
-if [ "$(basename $2)" != "bzImage" ]; then
-	echo "usage: create-gce-image.sh /dir/with/user/space/system /path/to/bzImage"
+case "$IMG_ARCH" in
+	386|amd64)
+		KERNEL_IMAGE_BASENAME=bzImage
+		;;
+	ppc64le)
+		KERNEL_IMAGE_BASENAME=zImage.pseries
+		;;
+esac
+
+if [ "$(basename $2)" != "$KERNEL_IMAGE_BASENAME" ]; then
+	echo "usage: create-gce-image.sh /dir/with/user/space/system /path/to/bzImage [arch]"
 	exit 1
 fi
 
@@ -101,8 +112,19 @@
 	sudo qemu-nbd -c $DISKDEV --format=raw disk.raw
 	CLEANUP="sudo qemu-nbd -d $DISKDEV; $CLEANUP"
 fi
-echo -en "o\nn\np\n1\n\n\na\nw\n" | sudo fdisk $DISKDEV
-PARTDEV=$DISKDEV"p1"
+
+case "$IMG_ARCH" in
+	386|amd64)
+		echo -en "o\nn\np\n1\n\n\na\nw\n" | sudo fdisk $DISKDEV
+		PARTDEV=$DISKDEV"p1"
+		;;
+	ppc64le)
+		# Create a small PowerPC PReP boot partition, and a Linux partition for the rest
+		echo -en "g\nn\n1\n2048\n16383\nt\n7\nn\n2\n\n\nw\n" | sudo fdisk $DISKDEV
+		PARTDEV=$DISKDEV"p2"
+		;;
+esac
+
 until [ -e $PARTDEV ]; do sleep 1; done
 sudo -E mkfs.ext4 -O ^resize_inode,^has_journal,ext_attr,extents,huge_file,flex_bg,dir_nlink,sparse_super $PARTDEV
 mkdir -p disk.mnt
@@ -148,7 +170,9 @@
 	CMDLINE=$(awk '{printf("%s ", $0)}' $SYZ_CMDLINE_FILE)
 fi
 
-cat << EOF | sudo tee disk.mnt/boot/grub/grub.cfg
+case "$IMG_ARCH" in
+386|amd64)
+	cat << EOF | sudo tee disk.mnt/boot/grub/grub.cfg
 terminal_input console
 terminal_output console
 set timeout=0
@@ -169,4 +193,24 @@
 	linux /vmlinuz root=/dev/sda1 console=ttyS0 earlyprintk=serial vsyscall=native rodata=n oops=panic panic_on_warn=1 nmi_watchdog=panic panic=86400 $CMDLINE
 }
 EOF
-sudo grub-install --target=i386-pc --boot-directory=disk.mnt/boot --no-floppy $DISKDEV
+	sudo grub-install --target=i386-pc --boot-directory=disk.mnt/boot --no-floppy $DISKDEV
+	;;
+ppc64le)
+	cat << EOF | sudo tee disk.mnt/boot/grub/grub.cfg
+terminal_input console
+terminal_output console
+set timeout=0
+# rodata=n: mark_rodata_ro becomes very slow with KASAN (lots of PGDs)
+# panic=86400: prevents kernel from rebooting so that we don't get reboot output in all crash reports
+# debug is not set as it produces too much output
+menuentry 'linux' --class gnu-linux --class gnu --class os {
+	insmod gzio
+	insmod part_gpt
+	insmod ext2
+	set root='(ieee1275/disk,gpt2)'
+	linux /vmlinuz root=/dev/sda2 console=ttyS0 earlyprintk=serial rodata=n oops=panic panic_on_warn=1 nmi_watchdog=panic panic=86400 $CMDLINE
+}
+EOF
+	sudo grub-install --target=powerpc-ieee1275 --boot-directory=disk.mnt/boot $DISKDEV"p1"
+	;;
+esac