resources: Make the boot-exit disk image generic

Change the boot-exit script to use m5 readfile to run any script at boot
instead of just m5 exit. If no script to run is passed, then exit as
before. The previous exit.sh script remains for any prior configs based
on this image that set the init.

Rename boot-exit to x86-ubuntu to reflect this more generic behavior.

Jira Issue: https://gem5.atlassian.net/browse/GEM5-1082

Change-Id: I7ab22c03cbe50e879019919482cba93659efebeb
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5-resources/+/50307
Reviewed-by: Bobby R. Bruce <bbruce@ucdavis.edu>
Maintainer: Bobby R. Bruce <bbruce@ucdavis.edu>
Tested-by: Bobby R. Bruce <bbruce@ucdavis.edu>
diff --git a/README.md b/README.md
index 88d8abc..e5ded43 100644
--- a/README.md
+++ b/README.md
@@ -703,11 +703,11 @@
 More information on Linux boot tests can be found [here](https://www.gem5.org/project/2020/03/09/boot-tests.html).
 
 The boot-tests resources consist of three main components:
-- boot-tests disk image
+- x86-ubuntu disk image
 - gem5 run scripts to execute boot tests
 - linux kernel configuration files
 
-The instructions to build the boot-tests disk image (`boot-exit`), the Linux binaries, and how to use gem5 run scripts to run boot-tests are available in this [README](src/boot-tests/README.md) file.
+The instructions to build the x86-ubuntu disk image, the Linux binaries, and how to use gem5 run scripts to run boot-tests are available in this [README](src/x86-ubuntu/README.md) file.
 
 ## Resource: RISCV Full System
 
@@ -819,9 +819,9 @@
 [`src/npb`](
 https://gem5.googlesource.com/public/gem5-resources/+/refs/heads/stable/src/npb).
 The NAS Parallel Benchmarks utilize a permissive BSD-style license.
-* **boot-exit**: Consult individual copyright notices of source files in
-[`src/boot-exit`](
-https://gem5.googlesource.com/public/gem5-resources/+/refs/heads/stable/src/boot-exit).
+* **x86-ubuntu**: Consult individual copyright notices of source files in
+[`src/x86-ubuntu`](
+https://gem5.googlesource.com/public/gem5-resources/+/refs/heads/stable/src/x86-ubuntu).
 * **insttest**: Consult individual copyright notices of source files in
 [`src/insttest`](
 https://gem5.googlesource.com/public/gem5-resources/+/refs/heads/stable/src/insttest).
diff --git a/src/boot-exit/.gitignore b/src/x86-ubuntu/.gitignore
similarity index 100%
rename from src/boot-exit/.gitignore
rename to src/x86-ubuntu/.gitignore
diff --git a/src/boot-exit/README.md b/src/x86-ubuntu/README.md
similarity index 65%
rename from src/boot-exit/README.md
rename to src/x86-ubuntu/README.md
index a5d2160..7265038 100644
--- a/src/boot-exit/README.md
+++ b/src/x86-ubuntu/README.md
@@ -1,30 +1,31 @@
 ---
-title: Linux boot-exit image
+title: Linux x86-ubuntu image
 tags:
     - x86
     - fullsystem
 layout: default
-permalink: resources/boot-exit
+permalink: resources/x86-ubuntu
 shortdoc: >
-    Resources to build a disk image and run "boot-exit" test.
+    Resources to build a generic x86-ubuntu disk image and run a "boot-exit" test.
 author: ["Ayaz Akram"]
 ---
 
-This document provides instructions to create the "boot-exit" image, the Linux kernel binaries, and also points to the gem5 configuration files needed to run the boot.
-The boot-exit disk image is based on Ubuntu 18.04 and has its `.bashrc` file modified in such a way that the guest OS terminates the simulation (using the `m5 exit` instruction) as soon as the system boots.
+This document provides instructions to create the "x86-ubuntu" image, the Linux kernel binaries, and also points to the gem5 configuration files needed to run the boot.
+The x86-ubuntu disk image is based on Ubuntu 18.04 and has its `.bashrc` file modified in such a way that it executes a script passed from the gem5 configuration files (using the `m5 readfile` instruction).
+The boot-exit test passes a script that causes the guest OS to terminate the simulation (using the `m5 exit` instruction) as soon as the system boots.
 
 We assume the following directory structure while following the instructions in this README file:
 
 ```
-boot-exit/
+x86-ubuntu/
   |___ gem5/                                   # gem5 source code (to be cloned here)
   |
   |___ disk-image/
   |      |___ shared/                          # Auxiliary files needed for disk creation
-  |      |___ boot-exit/
-  |            |___ boot-exit-image/           # Will be created once the disk is generated
-  |            |      |___ boot-exit           # The generated disk image
-  |            |___ boot-exit.json             # The Packer script
+  |      |___ x86-ubuntu/
+  |            |___ x86-ubuntu-image/           # Will be created once the disk is generated
+  |            |      |___ x86-ubuntu           # The generated disk image
+  |            |___ x86-ubuntu.json             # The Packer script
   |            |___ exit.sh                    # Exits the simulated guest upon booting
   |            |___ post-installation.sh       # Moves exit.sh to guest's .bashrc
   |
@@ -40,7 +41,7 @@
 
 ## Disk Image
 
-Assuming that you are in the `src/boot-exit/` directory (the directory containing this README), first build `m5` (which is needed to create the disk image):
+Assuming that you are in the `src/x86-ubuntu/` directory (the directory containing this README), first build `m5` (which is needed to create the disk image):
 
 ```sh
 git clone https://gem5.googlesource.com/public/gem5
@@ -48,7 +49,7 @@
 scons build/x86/out/m5
 ```
 
-Next (within the `src/boot-exit/` directory),
+Next (within the `src/x86-ubuntu/` directory),
 
 ```sh
 cd disk-image
@@ -57,12 +58,12 @@
 unzip packer_1.6.0_linux_amd64.zip
 
 # validate the packer script
-./packer validate boot-exit/boot-exit.json
+./packer validate x86-ubuntu/x86-ubuntu.json
 # build the disk image
-./packer build boot-exit/boot-exit.json
+./packer build x86-ubuntu/x86-ubuntu.json
 ```
 
-If you see errors or warnings from `packer validate` you can modify the file `disk-image/boot-exit/boot-exit.json` to update the file.
+If you see errors or warnings from `packer validate` you can modify the file `disk-image/x86-ubuntu/x86-ubuntu.json` to update the file.
 Specifically, you may see the following error.
 
 ```
@@ -75,7 +76,7 @@
 ```
 
 In this case, the `gem5` directory is in a different location than when this script was written.
-You can update the following line in the `boot-exit.json` file.
+You can update the following line in the `x86-ubuntu.json` file.
 
 ```
          "destination": "/home/gem5/",
@@ -84,8 +85,8 @@
          "type": "file"
 ```
 
-Once this process succeeds, the disk image can be found on `boot-exit/boot-exit-image/boot-exit`.
-A disk image already created following the above instructions can be found, gzipped, [here](http://dist.gem5.org/dist/v21-1/images/x86/ubuntu-18-04/boot-exit.img.gz).
+Once this process succeeds, the disk image can be found on `x86-ubuntu/x86-ubuntu-image/x86-ubuntu`.
+A disk image already created following the above instructions can be found, gzipped, [here](http://dist.gem5.org/dist/v21-1/images/x86/ubuntu-18-04/x86-ubuntu.img.gz).
 
 
 ## gem5 Run Scripts
diff --git a/src/boot-exit/configs/run_exit.py b/src/x86-ubuntu/configs/run_exit.py
similarity index 99%
rename from src/boot-exit/configs/run_exit.py
rename to src/x86-ubuntu/configs/run_exit.py
index 0eadcde..2bacc67 100755
--- a/src/boot-exit/configs/run_exit.py
+++ b/src/x86-ubuntu/configs/run_exit.py
@@ -27,8 +27,9 @@
 """
 """
 
-import time
 import argparse
+import os
+import time
 
 import m5
 import m5.ticks
diff --git a/src/boot-exit/configs/system/MESI_Two_Level.py b/src/x86-ubuntu/configs/system/MESI_Two_Level.py
similarity index 100%
rename from src/boot-exit/configs/system/MESI_Two_Level.py
rename to src/x86-ubuntu/configs/system/MESI_Two_Level.py
diff --git a/src/boot-exit/configs/system/MI_example_caches.py b/src/x86-ubuntu/configs/system/MI_example_caches.py
similarity index 100%
rename from src/boot-exit/configs/system/MI_example_caches.py
rename to src/x86-ubuntu/configs/system/MI_example_caches.py
diff --git a/src/boot-exit/configs/system/MOESI_CMP_directory.py b/src/x86-ubuntu/configs/system/MOESI_CMP_directory.py
similarity index 100%
rename from src/boot-exit/configs/system/MOESI_CMP_directory.py
rename to src/x86-ubuntu/configs/system/MOESI_CMP_directory.py
diff --git a/src/boot-exit/configs/system/__init__.py b/src/x86-ubuntu/configs/system/__init__.py
similarity index 100%
rename from src/boot-exit/configs/system/__init__.py
rename to src/x86-ubuntu/configs/system/__init__.py
diff --git a/src/boot-exit/configs/system/caches.py b/src/x86-ubuntu/configs/system/caches.py
similarity index 100%
rename from src/boot-exit/configs/system/caches.py
rename to src/x86-ubuntu/configs/system/caches.py
diff --git a/src/boot-exit/configs/system/fs_tools.py b/src/x86-ubuntu/configs/system/fs_tools.py
similarity index 100%
rename from src/boot-exit/configs/system/fs_tools.py
rename to src/x86-ubuntu/configs/system/fs_tools.py
diff --git a/src/boot-exit/configs/system/ruby_system.py b/src/x86-ubuntu/configs/system/ruby_system.py
similarity index 100%
rename from src/boot-exit/configs/system/ruby_system.py
rename to src/x86-ubuntu/configs/system/ruby_system.py
diff --git a/src/boot-exit/configs/system/system.py b/src/x86-ubuntu/configs/system/system.py
similarity index 100%
rename from src/boot-exit/configs/system/system.py
rename to src/x86-ubuntu/configs/system/system.py
diff --git a/src/boot-exit/disk-image/shared/preseed.cfg b/src/x86-ubuntu/disk-image/shared/preseed.cfg
similarity index 100%
rename from src/boot-exit/disk-image/shared/preseed.cfg
rename to src/x86-ubuntu/disk-image/shared/preseed.cfg
diff --git a/src/boot-exit/disk-image/shared/serial-getty@.service b/src/x86-ubuntu/disk-image/shared/serial-getty@.service
similarity index 100%
rename from src/boot-exit/disk-image/shared/serial-getty@.service
rename to src/x86-ubuntu/disk-image/shared/serial-getty@.service
diff --git a/src/boot-exit/disk-image/boot-exit/exit.sh b/src/x86-ubuntu/disk-image/x86-ubuntu/exit.sh
similarity index 100%
rename from src/boot-exit/disk-image/boot-exit/exit.sh
rename to src/x86-ubuntu/disk-image/x86-ubuntu/exit.sh
diff --git a/src/x86-ubuntu/disk-image/x86-ubuntu/gem5_init.sh b/src/x86-ubuntu/disk-image/x86-ubuntu/gem5_init.sh
new file mode 100755
index 0000000..bbdbbfb
--- /dev/null
+++ b/src/x86-ubuntu/disk-image/x86-ubuntu/gem5_init.sh
@@ -0,0 +1,22 @@
+#!/bin/bash
+
+# Copyright (c) 2021 The University of Texas at Austin.
+# SPDX-License-Identifier: BSD 3-Clause
+
+echo "Starting gem5 init... reading run script file."
+if ! m5 readfile > /tmp/script; then
+    echo "Failed to run m5 readfile, exiting!"
+    rm -f /tmp/script
+    if ! m5 exit; then
+        # Useful for booting the disk image in (e.g.,) qemu for debugging
+        echo "m5 exit failed, dropping to shell."
+        /bin/sh
+    fi
+else
+    echo "Running m5 script from /tmp/script"
+    chmod 755 /tmp/script
+    /tmp/script
+    echo "Done running script, exiting."
+    rm -f /tmp/script
+    m5 exit
+fi
diff --git a/src/boot-exit/disk-image/boot-exit/post-installation.sh b/src/x86-ubuntu/disk-image/x86-ubuntu/post-installation.sh
similarity index 75%
rename from src/boot-exit/disk-image/boot-exit/post-installation.sh
rename to src/x86-ubuntu/disk-image/x86-ubuntu/post-installation.sh
index 5f8bf36..4e013d0 100755
--- a/src/boot-exit/disk-image/boot-exit/post-installation.sh
+++ b/src/x86-ubuntu/disk-image/x86-ubuntu/post-installation.sh
@@ -11,8 +11,9 @@
 ln -s /sbin/m5 /sbin/gem5
 
 mv /home/gem5/exit.sh /root/
+mv /home/gem5/gem5_init.sh /root/
 
-# Add exit script to bashrc
-echo "/root/exit.sh" >> /root/.bashrc
+# Add init script to bashrc
+echo "/root/gem5_init.sh" >> /root/.bashrc
 
 echo 'Post Installation Done'
diff --git a/src/boot-exit/disk-image/boot-exit/boot-exit.json b/src/x86-ubuntu/disk-image/x86-ubuntu/x86-ubuntu.json
similarity index 91%
rename from src/boot-exit/disk-image/boot-exit/boot-exit.json
rename to src/x86-ubuntu/disk-image/x86-ubuntu/x86-ubuntu.json
index 1ad08df..60fee86 100755
--- a/src/boot-exit/disk-image/boot-exit/boot-exit.json
+++ b/src/x86-ubuntu/disk-image/x86-ubuntu/x86-ubuntu.json
@@ -35,7 +35,7 @@
             "iso_checksum": "{{ user `iso_checksum_type` }}:{{ user `iso_checksum` }}",
             "iso_urls": [ "{{ user `iso_url` }}" ],
             "memory": "{{ user `vm_memory`}}",
-            "output_directory": "boot-exit/{{ user `image_name` }}-image",
+            "output_directory": "x86-ubuntu/{{ user `image_name` }}-image",
             "qemuargs":
             [
                 [ "-cpu", "host" ],
@@ -53,7 +53,12 @@
     [
         {
             "type": "file",
-            "source": "boot-exit/exit.sh",
+            "source": "x86-ubuntu/exit.sh",
+            "destination": "/home/gem5/"
+        },
+        {
+            "type": "file",
+            "source": "x86-ubuntu/gem5_init.sh",
             "destination": "/home/gem5/"
         },
         {
@@ -71,7 +76,7 @@
             "execute_command": "echo '{{ user `ssh_password` }}' | {{.Vars}} sudo -E -S bash '{{.Path}}'",
             "scripts":
             [
-                "boot-exit/post-installation.sh"
+                "x86-ubuntu/post-installation.sh"
             ]
         }
     ],
@@ -93,7 +98,7 @@
         "ssh_username": "gem5",
         "vm_cpus": "16",
         "vm_memory": "8192",
-        "image_name": "boot-exit"
+        "image_name": "x86-ubuntu"
   }
 
 }