diff --git a/src/arm-ubuntu/arm-ubuntu.pkr.hcl b/src/arm-ubuntu/arm-ubuntu.pkr.hcl new file mode 100644 index 000000000..30b3cabfa --- /dev/null +++ b/src/arm-ubuntu/arm-ubuntu.pkr.hcl @@ -0,0 +1,94 @@ +packer { + required_plugins { + qemu = { + source = "github.com/hashicorp/qemu" + version = "~> 1" + } + } +} + +variable "image_name" { + type = string + default = "arm-ubuntu" +} + +variable "ssh_password" { + type = string + default = "12345" +} + +variable "ssh_username" { + type = string + default = "gem5" +} + +source "qemu" "initialize" { + boot_command = [ + "c", + "linux /casper/vmlinuz autoinstall ds=nocloud-net\\;s=http://{{.HTTPIP}}:{{.HTTPPort}}/ --- ", + "", + "initrd /casper/initrd", + "", + "boot", + "", + "" + ] + cpus = "4" + disk_size = "4600" + format = "raw" + headless = "true" + http_directory = "http" + iso_checksum = "sha256:c209ab013280d3cd26a344def60b7b19fbb427de904ea285057d94ca6ac82dd5" + iso_urls = ["https://old-releases.ubuntu.com/releases/jammy/ubuntu-22.04-live-server-arm64.iso"] + memory = "8192" + output_directory = "disk-image" + qemu_binary = "/usr/bin/qemu-system-aarch64" + qemuargs = [ ["-boot", "order=dc"], + ["-bios", "./files/flash0.img"], + ["-cpu", "host"], + ["-enable-kvm"], + ["-machine", "virt"], + ["-machine", "gic-version=3"], + ["-vga", "virtio"], + ["-device","virtio-gpu-pci"], + ["-device", "qemu-xhci"], + ["-device","usb-kbd"], + + ] + shutdown_command = "echo '${var.ssh_password}'|sudo -S shutdown -P now" + ssh_password = "${var.ssh_password}" + ssh_username = "${var.ssh_username}" + ssh_wait_timeout = "60m" + vm_name = "${var.image_name}" + ssh_handshake_attempts = "1000" +} + +build { + sources = ["source.qemu.initialize"] + + provisioner "file" { + destination = "/home/gem5/" + source = "files/exit.sh" + } + + provisioner "file" { + destination = "/home/gem5/" + source = "files/gem5_init.sh" + } + + provisioner "file" { + destination = "/home/gem5/" + source = "files/after_boot.sh" + } + + provisioner "file" { + destination = "/home/gem5/" + source = "files/serial-getty@.service" + } + + provisioner "shell" { + execute_command = "echo '${var.ssh_password}' | {{ .Vars }} sudo -E -S bash '{{ .Path }}'" + scripts = ["scripts/post-installation.sh"] + } + +} diff --git a/src/arm-ubuntu/build.sh b/src/arm-ubuntu/build.sh new file mode 100755 index 000000000..f95d603cc --- /dev/null +++ b/src/arm-ubuntu/build.sh @@ -0,0 +1,13 @@ +PACKER_VERSION="1.10.0" + +if [ ! -f ./packer ]; then + wget https://releases.hashicorp.com/packer/${PACKER_VERSION}/packer_${PACKER_VERSION}_linux_arm64.zip; + unzip packer_${PACKER_VERSION}_linux_arm64.zip; + rm packer_${PACKER_VERSION}_linux_arm64.zip; +fi + +# Install the needed plugins +./packer init arm-ubuntu.pkr.hcl + +# Build the image +./packer build arm-ubuntu.pkr.hcl diff --git a/src/arm-ubuntu/disk-image/arm-ubuntu/arm-ubuntu.json b/src/arm-ubuntu/disk-image/arm-ubuntu/arm-ubuntu.json deleted file mode 100644 index ef3879375..000000000 --- a/src/arm-ubuntu/disk-image/arm-ubuntu/arm-ubuntu.json +++ /dev/null @@ -1,83 +0,0 @@ -{ - "_author": "Hoa Nguyen , Kaustav Goswami ", - "_license": "Copyright (c) 2022 The Regents of the University of California. SPDX-License-Identifier: BSD 3-Clause", - "builders": [ - { - "type": "null", - "ssh_host": "localhost", - "ssh_port": "5555", - "ssh_username": "ubuntu", - "ssh_agent_auth": true, - "ssh_ciphers": ["aes128-gcm@openssh.com", "chacha20-poly1305@openssh.com", "aes128-ctr", "aes192-ctr", "aes256-ctr"], - "ssh_certificate_file": "~/.ssh/id_rsa", - "ssh_clear_authorized_keys": true - } - ], - "provisioners": [ - { - "type": "file", - "source": "arm-ubuntu/init.gem5", - "destination": "/home/ubuntu/" - }, - { - "type": "shell", - "inline": "cd /home/ubuntu; chmod 775 init.gem5" - }, - { - "type": "file", - "source": "../gem5/util/m5/build/arm64/out/m5", - "destination": "/home/ubuntu/" - }, - { - "type": "file", - "source": "arm-ubuntu/gem5_init.sh", - "destination": "/home/ubuntu/gem5_init.sh" - }, - { - "type": "shell", - "inline": "cd /home/ubuntu; chmod +x gem5_init.sh" - }, - { - "type": "file", - "source": "shared/serial-getty@.service", - "destination": "/home/ubuntu/serial-getty@.service" - }, - { - "type": "file", - "source": "arm-ubuntu/post-installation.sh", - "destination": "/home/ubuntu/post-installation.sh" - }, - { - "type": "shell", - "inline": "cd /home/ubuntu; chmod +x post-installation.sh; sudo ./post-installation.sh; rm post-installation.sh" - }, - { - "type": "shell", - "inline": "sudo cp /home/ubuntu/init.gem5 /sbin/.." - }, - { - "type": "shell", - "inline": "sudo cp /home/ubuntu/init.gem5 /" - }, - { - "type": "shell", - "inline": "sudo mkdir /data" - }, - { - "type": "shell", - "inline": "sudo mv /sbin/init /sbin/init_old" - }, - { - "type": "shell", - "inline": "cd /sbin; sudo ln -s ../init.gem5 init" - }, - { - "type": "shell", - "inline": "sudo apt-get -y remove --purge landscape-common" - }, - { - "type": "shell", - "inline": "sudo apt-get -y remove lsb-release" - } - ] -} diff --git a/src/arm-ubuntu/disk-image/arm-ubuntu/cloud.txt b/src/arm-ubuntu/disk-image/arm-ubuntu/cloud.txt deleted file mode 100644 index 4eb2627be..000000000 --- a/src/arm-ubuntu/disk-image/arm-ubuntu/cloud.txt +++ /dev/null @@ -1,9 +0,0 @@ -#cloud-config -users: - - name: ubuntu ### change this name to the current user (use `whoami`) ### - ssh-authorized-keys: - - ssh-rsa ### insert the rsa key here ### - sudo: ['ALL=(ALL) NOPASSWD:ALL'] - groups: sudo - shell: /bin/bash - homedir: ### insert the home directory here ### diff --git a/src/arm-ubuntu/disk-image/arm-ubuntu/gem5_init.sh b/src/arm-ubuntu/disk-image/arm-ubuntu/gem5_init.sh deleted file mode 100755 index 39b0a8921..000000000 --- a/src/arm-ubuntu/disk-image/arm-ubuntu/gem5_init.sh +++ /dev/null @@ -1,23 +0,0 @@ -#!/bin/bash -# Copyright (c) 2022 The University of California -# Copyright (c) 2021 The University of Texas at Austin. -# SPDX-License-Identifier: BSD 3-Clause - -echo "Starting gem5 init... reading run script file." -# Calling m5 with --addr as m5ops_base address. -if ! m5 --addr 0x10010000 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 || true - echo "Done running script, exiting." - rm -f /tmp/script - m5 --addr 0x10010000 exit -fi diff --git a/src/arm-ubuntu/disk-image/arm-ubuntu/init.gem5 b/src/arm-ubuntu/disk-image/arm-ubuntu/init.gem5 deleted file mode 100644 index 9059445d1..000000000 --- a/src/arm-ubuntu/disk-image/arm-ubuntu/init.gem5 +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/sh -# This script is taken from ubuntu-18.04-arm64-docker.img, hosted -# on gem5 guest binaries. -set -ex -mount -t proc - /proc -mount -t sysfs - /sys -mount -t debugfs - /sys/kernel/debug/ -for DEV in /dev/vdb1; do - if [ -e "${DEV}" ]; then - mount "${DEV}" /data - break - fi -done -exec /sbin/getty -a root -L ttyAMA0 vt102 - diff --git a/src/arm-ubuntu/disk-image/arm-ubuntu/post-installation.sh b/src/arm-ubuntu/disk-image/arm-ubuntu/post-installation.sh deleted file mode 100755 index 1d0bae282..000000000 --- a/src/arm-ubuntu/disk-image/arm-ubuntu/post-installation.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/bin/bash - -# Copyright (c) 2022 The Regents of the University of California. -# SPDX-License-Identifier: BSD 3-Clause - -mv /home/ubuntu/serial-getty@.service /lib/systemd/system/ - -mv /home/ubuntu/m5 /sbin -ln -s /sbin/m5 /sbin/gem5 - -mv /home/ubuntu/gem5_init.sh /root/ -chmod +x /root/gem5_init.sh -echo "/root/gem5_init.sh" >> /root/.bashrc - diff --git a/src/arm-ubuntu/disk-image/build.sh b/src/arm-ubuntu/disk-image/build.sh deleted file mode 100644 index bea8905f6..000000000 --- a/src/arm-ubuntu/disk-image/build.sh +++ /dev/null @@ -1,76 +0,0 @@ -#!/bin/bash - -# Copyright (c) 2022 The Regents of the University of California. -# SPDX-License-Identifier: BSD 3-Clause - -# This file validates the `cloud.txt` and `arm-ubuntu.json` files before -# starting packer. - -USER=`whoami` -HOSTNAME=`hostname` - -# Check if the USER is `ubuntu`, otherwise tell the user to configure -# ssh-keygen and cloud.txt file. - -if [ ! $USER = "ubuntu" ]; then - echo "The username is $USER" - echo "The script expects that the user HAVE ALREADY executed \"ssh-keygen\"" - - # Checking for the cloud.txt file - - if [ ! -f "arm-ubuntu/cloud.txt" ]; then - echo "cloud.txt not found! Please refer to the README.md file on how to create the cloud.txt file." - exit - else - - # cloud.txt file exists. Need to check whether it is modified or not. - - count=`grep "name: ubuntu" arm-ubuntu/cloud.txt |wc -l` - if [ $count -ne 0 ]; then - echo "cloud.txt 'name' is not modified correctly! Please refer to the README.md file on how to modify name and ssh-rsa in the cloud.txt file." - exit - fi - - # Checking whether the ssh-rsa line is modified. - - KEY=`grep "ssh-rsa" arm-ubuntu/cloud.txt` - - if [[ "$KEY" == *"$USER@$HOSTNAME"* ]]; then - echo "cloud.txt verified!" - else - echo "cloud.txt 'ssh-rsa' key is not modified correctly! Please refer to the README.md file on how to modify ssh-rsa in the cloud.txt file." - exit - fi - fi - - echo "All files are modified accordingly." -fi - -# Modifying the json script. - -sed "s/\/home\/ubuntu/\/home\/${USER}/g" arm-ubuntu/arm-ubuntu.json > arm-ubuntu/.arm-ubuntu.json -mv arm-ubuntu/.arm-ubuntu.json arm-ubuntu/arm-ubuntu.json -sed "s/\"ssh_username\": \"ubuntu\",/\"ssh_username\": \"${USER}\",/g" arm-ubuntu/arm-ubuntu.json > arm-ubuntu/.arm-ubuntu.json -mv arm-ubuntu/.arm-ubuntu.json arm-ubuntu/arm-ubuntu.json - -# Modifying the post-installation script. - -sed "s/\/home\/ubuntu/\/home\/${USER}/g" arm-ubuntu/post-installation.sh > arm-ubuntu/.post-installation.sh -mv arm-ubuntu/.post-installation.sh arm-ubuntu/post-installation.sh - -# Downloading packer - -PACKER_VERSION="1.8.0" - -if [ ! -f ./packer ]; then - wget https://releases.hashicorp.com/packer/${PACKER_VERSION}/packer_${PACKER_VERSION}_linux_amd64.zip; - unzip packer_${PACKER_VERSION}_linux_amd64.zip; - rm packer_${PACKER_VERSION}_linux_amd64.zip; -fi - -# Validating and executing packer - -./packer validate arm-ubuntu/arm-ubuntu.json -./packer build arm-ubuntu/arm-ubuntu.json - -exit diff --git a/src/arm-ubuntu/files/after_boot.sh b/src/arm-ubuntu/files/after_boot.sh new file mode 100755 index 000000000..c503c0f1f --- /dev/null +++ b/src/arm-ubuntu/files/after_boot.sh @@ -0,0 +1,57 @@ +#!/bin/bash + +# Copyright (c) 2022,2024 The University of California. +# Copyright (c) 2021 The University of Texas at Austin. +# SPDX-License-Identifier: BSD 3-Clause + +# This file is executed at the end of the bashrc for the gem5 user. +# The script checks to see if we should run in interactive mode or not. +# If we are in interactive mode, the script will drop to a shell. +# If we are not in interactive mode, the script will check if we should +# run a script from the gem5-bridge. If so, it will run the script and +# exit. If there is no script and we are not in interactive mode, it will +# exit. This last option is used for testing purposes. + +# gem5-bridge exit signifying that after_boot.sh is running +printf "In after_boot.sh...\n" +gem5-bridge exit # TODO: Make this a specialized event. + +# Read /proc/cmdline and parse options + +cmdline=$(cat /proc/cmdline) +interactive=false +IGNORE_M5=0 +if [[ $cmdline == *"interactive"* ]]; then + interactive=true +fi + +printf "Interactive mode: $interactive\n" + +if [[ $interactive == true ]]; then + printf "Interactive mode enabled, dropping to shell." + /bin/bash +else + # Try to read the file from the host when running with gem5 + if ! [ -z $IGNORE_M5 ]; then + printf "Starting gem5 init... trying to read run script file via readfile.\n" + if ! gem5-bridge readfile > /tmp/script; then + printf "Failed to run gem5-bridge readfile, exiting!\n" + rm -f /tmp/script + # If we can't read the script exit the simulation. If we cannot exit the + # simulation, this probably means that we are running in QEMU. So, ignore + # future calls to gem5-bridge. + if ! gem5-bridge exit; then + # Useful for booting the disk image in (e.g.,) qemu for debugging + printf "gem5-bridge exit failed, dropping to shell.\n" + IGNORE_M5=1 /bin/bash + fi + else + printf "Running script from gem5-bridge stored in /tmp/script\n" + chmod 755 /tmp/script + /tmp/script + printf "Done running script from gem5-bridge, exiting.\n" + rm -f /tmp/script + gem5-bridge exit + fi + fi +fi diff --git a/src/arm-ubuntu/files/exit.sh b/src/arm-ubuntu/files/exit.sh new file mode 100755 index 000000000..4b147e01f --- /dev/null +++ b/src/arm-ubuntu/files/exit.sh @@ -0,0 +1,6 @@ +#!/bin/sh + +# Copyright (c) 2020 The Regents of the University of California. +# SPDX-License-Identifier: BSD 3-Clause + +m5 exit diff --git a/src/arm-ubuntu/files/gem5_init.sh b/src/arm-ubuntu/files/gem5_init.sh new file mode 100755 index 000000000..07a45afae --- /dev/null +++ b/src/arm-ubuntu/files/gem5_init.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +# mount /proc and /sys +mount -t proc /proc /proc +mount -t sysfs /sys /sys +# Read /proc/cmdline and parse options +cmdline=$(cat /proc/cmdline) +no_systemd=false + +# gem5-bridge exit signifying that kernel is booted +printf "Kernel booted, starting gem5 init...\n" +gem5-bridge exit + +if [[ $cmdline == *"no_systemd"* ]]; then + no_systemd=true +fi + +# Run systemd via exec if not disabled +if [[ $no_systemd == false ]]; then + # gem5-bridge exit signifying that systemd will be booted + printf "Starting systemd...\n" + gem5-bridge exit + exec /lib/systemd/systemd +else + exec su - gem5 +fi diff --git a/src/arm-ubuntu/disk-image/shared/serial-getty@.service b/src/arm-ubuntu/files/serial-getty@.service similarity index 95% rename from src/arm-ubuntu/disk-image/shared/serial-getty@.service rename to src/arm-ubuntu/files/serial-getty@.service index b0424f0e6..121e8e355 100644 --- a/src/arm-ubuntu/disk-image/shared/serial-getty@.service +++ b/src/arm-ubuntu/files/serial-getty@.service @@ -31,7 +31,7 @@ Before=rescue.service # The '-o' option value tells agetty to replace 'login' arguments with an # option to preserve environment (-p), followed by '--' for safety, and then # the entered username. -ExecStart=-/sbin/agetty --autologin root --keep-baud 115200,38400,9600 %I $TERM +ExecStart=-/sbin/agetty --autologin gem5 --keep-baud 115200,38400,9600 %I $TERM Type=idle Restart=always UtmpIdentifier=%I diff --git a/src/arm-ubuntu/http/meta-data b/src/arm-ubuntu/http/meta-data new file mode 100644 index 000000000..e69de29bb diff --git a/src/arm-ubuntu/http/user-data b/src/arm-ubuntu/http/user-data new file mode 100644 index 000000000..b82387329 --- /dev/null +++ b/src/arm-ubuntu/http/user-data @@ -0,0 +1,95 @@ + +#cloud-config +autoinstall: + apt: + disable_components: [] + geoip: true + preserve_sources_list: false + primary: + - arches: + - amd64 + - i386 + uri: http://archive.ubuntu.com/ubuntu + - arches: + - default + uri: http://us.ports.ubuntu.com/ubuntu-ports + + drivers: + install: false + identity: + hostname: gem5 + password: $6$TBZcqP/4zmP6cvBx$x4MAAfPL25/QAI6tRGfzJJG1.wcvTew15FVIZc3ys4jvbRiI/8eBBd9HLUm0HJAXNFsBDCdBBM0gZ4vauiH5s. + realname: gem5 + username: gem5 + kernel: + package: linux-generic + keyboard: + layout: us + toggle: null + variant: '' + locale: en_US.UTF-8 + network: + ethernets: + enp0s3: + dhcp4: true + version: 2 + ssh: + allow-pw: true + authorized-keys: [] + install-server: true + late-commands: + # https://github.com/hashicorp/packer-plugin-qemu/issues/66#issuecomment-1466049817 + - | + if [ -d /sys/firmware/efi ]; then + apt-get install -y efibootmgr + efibootmgr -o $(efibootmgr | perl -n -e '/Boot(.+)\* ubuntu/ && print $1') + fi + + storage: + config: + - ptable: gpt + path: /dev/vda + wipe: superblock-recursive + preserve: false + name: '' + grub_device: false + type: disk + id: disk-vda + - device: disk-vda + size: 564133888 + wipe: superblock + flag: boot + number: 1 + preserve: false + grub_device: true + type: partition + id: partition-0 + - fstype: fat32 + volume: partition-0 + preserve: false + type: format + id: format-0 + - device: disk-vda + size: 4257218560 + wipe: superblock + flag: '' + number: 2 + preserve: false + grub_device: false + type: partition + id: partition-1 + - fstype: ext4 + volume: partition-1 + preserve: false + type: format + id: format-1 + - path: / + device: format-1 + type: mount + id: mount-1 + - path: /boot/efi + device: format-0 + type: mount + id: mount-0 + updates: security + version: 1 \ No newline at end of file diff --git a/src/arm-ubuntu/scripts/post-installation.sh b/src/arm-ubuntu/scripts/post-installation.sh new file mode 100755 index 000000000..0e9eb1f55 --- /dev/null +++ b/src/arm-ubuntu/scripts/post-installation.sh @@ -0,0 +1,72 @@ +#!/bin/bash + +# Copyright (c) 2024 The Regents of the University of California. +# SPDX-License-Identifier: BSD 3-Clause + +echo 'Post Installation Started' + +echo 'installing packages' +apt-get update +apt-get install -y scons +apt-get install -y git +apt-get install -y vim +apt-get install -y build-essential + +echo "Installing serial service for autologin after systemd" +mv /home/gem5/serial-getty@.service /lib/systemd/system/ + +echo "Installing the gem5 init script in /sbin" +mv /home/gem5/gem5_init.sh /sbin +mv /sbin/init /sbin/init.old +ln -s /sbin/gem5_init.sh /sbin/init + +# Add after_boot.sh to bashrc in the gem5 user account +# This will run the script after the user automatically logs in +echo -e "\nif [ -z \"\$AFTER_BOOT_EXECUTED\" ]; then\n export AFTER_BOOT_EXECUTED=1\n /home/gem5/after_boot.sh\nfi\n" >> /home/gem5/.bashrc + +# Remove the motd +rm /etc/update-motd.d/* + +# Build and install the gem5-bridge (m5) binary, library, and headers +echo "Building and installing gem5-bridge (m5) and libm5" + +# Just get the files we need +git clone https://github.com/gem5/gem5.git --depth=1 --filter=blob:none --no-checkout --sparse --single-branch --branch=stable +pushd gem5 +# Checkout just the files we need +git sparse-checkout add util/m5 +git sparse-checkout add include +git checkout +# Install the headers globally so that other benchmarks can use them +cp -r include/gem5 /usr/local/include/\ + +# Build the library and binary +pushd util/m5 +scons build/arm64/out/m5 +cp build/arm64/out/m5 /usr/local/bin/ +cp build/arm64/out/libm5.a /usr/local/lib/ +popd +popd + +# rename the m5 binary to gem5-bridge +mv /usr/local/bin/m5 /usr/local/bin/gem5-bridge +# Set the setuid bit on the m5 binary +chmod 4755 /usr/local/bin/gem5-bridge +chmod u+s /usr/local/bin/gem5-bridge + +#create a symbolic link to the gem5 binary for backward compatibility +ln -s /usr/local/bin/gem5-bridge /usr/local/bin/m5 + +# delete the git repo for gem5 +rm -rf gem5 +echo "Done building and installing gem5-bridge (m5) and libm5" + +# You can extend this script to install your own packages here. + +# Disable network by default +echo "Disabling network by default" +echo "See README.md for instructions on how to enable network" +mv /etc/netplan/00-installer-config.yaml /etc/netplan/00-installer-config.yaml.bak +netplan apply + +echo "Post Installation Done"