Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resources: add scripts to make and extract arm kernel #60

Open
wants to merge 18 commits into
base: stable
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/ubuntu-generic-diskimages/22.04-dockerfile/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
FROM ubuntu:22.04

# Install necessary packages for kernel build
RUN apt update && apt install -y \
build-essential \
libncurses-dev \
bison \
flex \
libssl-dev \
libelf-dev \
bc \
wget \
git \
kmod \
apt-src \
vim \
curl \
file

RUN sed -Ei 's/^# deb-src /deb-src /' /etc/apt/sources.list
RUN apt update
RUN mkdir /workspace
RUN cd /workspace && apt source linux-image-unsigned-5.15.0-25-generic

RUN cd /workspace/linux-5.15.0 && \
make defconfig && \
make -j 32 && \
make INSTALL_MOD_PATH=/workspace/output modules_install

RUN git clone https://github.com/nkrim/gem5.git --depth=1 --filter=blob:none --no-checkout --sparse --single-branch --branch=gem5-bridge && \
cd gem5 && \
git sparse-checkout add util/m5 && \
git sparse-checkout add util/gem5_bridge && \
git sparse-checkout add include && \
git checkout

RUN cd gem5/util/gem5_bridge && \
make KMAKEDIR=/workspace/linux-5.15.0 INSTALL_MOD_PATH=/workspace/output build install

RUN cd /workspace/output/lib/modules/5.15.167 && \
rm -rf build source

CMD ["/bin/bash"]
42 changes: 42 additions & 0 deletions src/ubuntu-generic-diskimages/24.04-dockerfile/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Start from Ubuntu 24.04 base image
FROM ubuntu:24.04

# Install necessary packages for kernel and module build
RUN apt update && apt install -y \
build-essential \
libncurses-dev \
bison \
flex \
libssl-dev \
libelf-dev \
bc \
wget \
git \
kmod


RUN sed -i 's/^Types: deb$/Types: deb deb-src/' /etc/apt/sources.list.d/ubuntu.sources
RUN apt update
RUN mkdir /workspace
RUN cd /workspace && apt source linux-image-unsigned-6.8.0-47-generic

RUN cd /workspace/linux-6.8.0 && \
make defconfig && \
make -j 32 && \
make INSTALL_MOD_PATH=/workspace/output modules_install

RUN git clone https://github.com/nkrim/gem5.git --depth=1 --filter=blob:none --no-checkout --sparse --single-branch --branch=gem5-bridge && \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Must be changed before merging

cd gem5 && \
git sparse-checkout add util/m5 && \
git sparse-checkout add util/gem5_bridge && \
git sparse-checkout add include && \
git checkout

RUN cd gem5/util/gem5_bridge && \
make KMAKEDIR=/workspace/linux-6.8.0 INSTALL_MOD_PATH=/workspace/output build install

RUN cd /workspace/output/lib/modules/6.8.12 && \
rm -rf build

# Use bash as the default shell
CMD ["/bin/bash"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be removed so the image won't be used interactively.

2 changes: 2 additions & 0 deletions src/ubuntu-generic-diskimages/BUILDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ dd if=/dev/zero of=flash0.img bs=1M count=64
dd if=/usr/share/qemu-efi-aarch64/QEMU_EFI.fd of=flash0.img conv=notrunc
```

If you want to get the `gem5-bridge` driver and the kernel locally, please refer to the documentation [here](make-kernel-and-gem5-bridge-driver.md).

**Note**: The `build-arm.sh` will make this file for you.

Note: Building the image can take a while to run.
Expand Down
19 changes: 19 additions & 0 deletions src/ubuntu-generic-diskimages/files/arm/gem5_init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,25 @@ mount -t sysfs /sys /sys
cmdline=$(cat /proc/cmdline)
no_systemd=false

# Load gem5_bridge driver
## Default parameters (ARM64)
gem5_bridge_baseaddr=0x10010000
gem5_bridge_rangesize=0x10000
## Try to read overloads from kernel arguments
if [[ $cmdline =~ gem5_bridge_baseaddr=([[:alnum:]]+) ]]; then
gem5_bridge_baseaddr=${BASH_REMATCH[1]}
fi
if [[ $cmdline =~ gem5_bridge_rangesize=([[:alnum:]]+) ]]; then
gem5_bridge_rangesize=${BASH_REMATCH[1]}
fi
## Insert driver
modprobe gem5_bridge \
gem5_bridge_baseaddr=$gem5_bridge_baseaddr \
gem5_bridge_rangesize=$gem5_bridge_rangesize

# see if this modprode fails or not
# print warning if it fails, gem5-bridge module is not going to work, you will need sudo for running exit events

# gem5-bridge exit signifying that kernel is booted
# This will cause the simulation to exit. Note that this will
# cause qemu to fail.
Expand Down
2 changes: 1 addition & 1 deletion src/ubuntu-generic-diskimages/http/arm-22-04/user-data
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ autoinstall:
type: format
id: format-0
- device: disk-vda
size: 4257218560
size: 4557218560
wipe: superblock
flag: ''
number: 2
Expand Down
2 changes: 1 addition & 1 deletion src/ubuntu-generic-diskimages/http/arm-24-04/user-data
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ autoinstall:
type: format
id: format-0
- device: disk-vda
size: 4257218560
size: 4557218560
wipe: superblock
flag: ''
number: 2
Expand Down
194 changes: 194 additions & 0 deletions src/ubuntu-generic-diskimages/make-kernel-and-gem5-bridge-driver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@

# Make Kernel and the gem5 Bridge Driver

This document outlines the steps to build a Linux kernel and its modules with the gem5-bridge driver for the ARM Ubuntu disk images. Below are separate instructions for Ubuntu 24.04 and Ubuntu 22.04.

**Note**: These dockerfiles assume that you are running on arm host to build them. If you are not on ARM host then you would need to use a cross compiler to make the kernel and the modules.

## Table of Contents

- [Ubuntu 24.04 Disk Image](#ubuntu-2404-disk-image)
- [Ubuntu 22.04 Disk Image](#ubuntu-2204-disk-image)

## **Ubuntu 24.04 Disk Image**

### Build the Docker Image

- Navigate to the `24.04-dockerfile` directory and build the Docker image:

```bash
cd src/ubuntu-generic-diskimages/24.04-dockerfile
docker build -t ubuntu-kernel-build .
cd ..
```

### Build the Kernel and Modules

- Create a container from the built image:

```bash
docker create --name kernel-builder ubuntu-kernel-build
```

- Start the container to build the kernel:

```bash
docker start -a kernel-builder
```

- Copy the kernel and modules to the host:

```bash
mkdir my-arm-6.8.12-kernel
docker cp kernel-builder:/workspace/linux-6.8.0/vmlinux my-arm-6.8.12-kernel/
docker cp kernel-builder:/workspace/output/lib/modules/6.8.12 my-arm-6.8.12-kernel/
```

- Clean up the container:

```bash
docker rm kernel-builder
```

### Verify Output

- Check the contents of `my-arm-6.8.12-kernel`:

```bash
ls my-arm-6.8.12-kernel/
```

You should see:
- `vmlinux` — The built kernel image.
- `6.8.12/` — Directory containing the kernel modules.

### Add Kernel Modules to the Disk Image

- Add a Packer file provisioner to copy the modules to the disk image.
Harshil2107 marked this conversation as resolved.
Show resolved Hide resolved
Make sure that this provisioner is added before the shell provisioner as we will used these files in the `post-installation.sh`:
Harshil2107 marked this conversation as resolved.
Show resolved Hide resolved

```hcl
provisioner "file" {
destination = "/home/gem5"
source = "my-arm-6.8.12-kernel/6.8.12"
}
```

- Update the post-install script to move the modules into the correct location and regenerate the initramfs.
Harshil2107 marked this conversation as resolved.
Show resolved Hide resolved
Make sure the modules are moved before using `gem5-bridge` or compiling benchmarks with `gem5-bridge`:
Harshil2107 marked this conversation as resolved.
Show resolved Hide resolved

```bash
mv /home/gem5/6.8.12 /lib/modules/6.8.12
depmod --quick -a 6.8.12
update-initramfs -u -k 6.8.12
```

### Build the Disk Image

- Build the disk image using your build script:

```bash
./build-arm.sh 24.04
```

### Test with gem5

- Use the disk image and the kernel to run a gem5 filesystem simulation, ensuring the new kernel and modules are correctly set up.

## **Ubuntu 22.04 Disk Image**

### Build the Docker Image

- Navigate to the `22.04-dockerfile` directory and build the Docker image:

```bash
cd src/ubuntu-generic-diskimages/22.04-dockerfile
docker build -t ubuntu-22.04-kernel-build .
cd ..
```

### Build the Kernel and Modules

- Create a container from the built image:

```bash
docker create --name kernel-builder ubuntu-22.04-kernel-build
```

- Start the container to build the kernel:

```bash
docker start -a kernel-builder
```

- Copy the kernel and modules to the host:

```bash
mkdir my-arm-5.15.167-kernel
docker cp kernel-builder:/workspace/linux-5.15.0/vmlinux my-arm-5.15.167-kernel/
docker cp kernel-builder:/workspace/output/lib/modules/5.15.167 my-arm-5.15.167-kernel/
```

- Clean up the container:

```bash
docker rm kernel-builder
```

### Verify Output

- Check the contents of `my-arm-5.15.167-kernel`:

```bash
ls my-arm-5.15.167-kernel/
```

You should see:
- `vmlinux` — The built kernel image.
- `5.15.167/` — Directory containing the kernel modules.

### Add Kernel Modules to the Disk Image

- Add a Packer file provisioner to copy the modules to the disk image.
Harshil2107 marked this conversation as resolved.
Show resolved Hide resolved
Make sure that this provisioner is added before the shell provisioner as we will used these files in the `post-installation.sh`:

```hcl
provisioner "file" {
destination = "/home/gem5"
source = "my-arm-5.15.167-kernel/5.15.167"
}
```

- Update the post-install script to move the modules into the correct location and regenerate the initramfs.
Harshil2107 marked this conversation as resolved.
Show resolved Hide resolved
Make sure the modules are moved before using `gem5-bridge` or compiling benchmarks with `gem5-bridge`:
Harshil2107 marked this conversation as resolved.
Show resolved Hide resolved

```bash
mv /home/gem5/5.15.167 /lib/modules/5.15.167
depmod --quick -a 5.15.167
update-initramfs -u -k 5.15.167
```

### Build the Disk Image

- Build the disk image using your build script:

```bash
./build-arm.sh 22.04
```

### Test with gem5

- Use the disk image and the kernel to run a gem5 filesystem simulation, ensuring the new kernel and modules are correctly set up.
Harshil2107 marked this conversation as resolved.
Show resolved Hide resolved
Harshil2107 marked this conversation as resolved.
Show resolved Hide resolved

- You can use the following code snipped to use the disk image and kernel you made.
Harshil2107 marked this conversation as resolved.
Show resolved Hide resolved

```python
image = DiskImageResource("/path/to/gem5-resources/src/ubuntu-generic-diskimages/arm-disk-image-22-04/arm-ubuntu")
image._root_partition = "2"

board.set_kernel_disk_workload(
kernel=KernelResource("/path/to/gem5-resources/src/ubuntu-generic-diskimages/my-arm-5.15.167-kernel/vmlinux"),
disk_image=image,
bootloader=obtain_resource("arm64-bootloader-foundation"),
)
```
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ source "qemu" "initialize" {
"<wait>"
]
cpus = "4"
disk_size = "4600"
disk_size = "5000"
format = "raw"
headless = "true"
http_directory = local.iso_data[var.ubuntu_version].http_directory
Expand Down Expand Up @@ -139,4 +139,5 @@ build {
environment_vars = ["ISA=arm64"]
expect_disconnect = true
}

}
12 changes: 3 additions & 9 deletions src/ubuntu-generic-diskimages/scripts/post-installation.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@ apt-get install -y build-essential
echo "Installing serial service for autologin after systemd"
mv /home/gem5/[email protected] /lib/systemd/system/

# Make sure the headers are installed to extract the kernel that DKMS
# packages will be built against.
sudo apt -y install "linux-headers-$(uname -r)" "linux-modules-extra-$(uname -r)"

echo "Extracting linux kernel $(uname -r) to /home/gem5/vmlinux-x86-ubuntu"
sudo bash -c "/usr/src/linux-headers-$(uname -r)/scripts/extract-vmlinux /boot/vmlinuz-$(uname -r) > /home/gem5/vmlinux-x86-ubuntu"

echo "Installing the gem5 init script in /sbin"
mv /home/gem5/gem5_init.sh /sbin
mv /sbin/init /sbin/init.old
Expand All @@ -47,14 +40,15 @@ if [ -z "$ISA" ]; then
fi

# 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
git clone https://github.com/nkrim/gem5.git --depth=1 --filter=blob:none --no-checkout --sparse --single-branch --branch=gem5-bridge
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this is just a placeholder, but don't forget to fix this.

pushd gem5
# Checkout just the files we need
git sparse-checkout add util/m5
git sparse-checkout add util/gem5_bridge
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/\
cp -r include/gem5 /usr/local/include/

# Build the library and binary
pushd util/m5
Expand Down