Skip to content

Commit

Permalink
resources: Update building kernel and modules on host
Browse files Browse the repository at this point in the history
  • Loading branch information
Harshil2107 committed Nov 20, 2024
1 parent 22d3fe2 commit d8d4d3c
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 28 deletions.
21 changes: 21 additions & 0 deletions src/ubuntu-generic-diskimages/24.04-dockerfile/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# 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

# Set the default working directory
WORKDIR /workspace

# Use bash as the default shell
CMD ["/bin/bash"]
108 changes: 108 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,108 @@
# Make kernel and the gem5 bridge driver

This document will highlight the steps needed to make a kernel and its modules with the gem5-bridge driver.

## Ubuntu 24.04 disk image

Assuming you are in the `src/ubuntu-generic-diskiamges` directory.
`cd` to the `24.04-dockerfile` directory and build the docker image.

```bash
cd 24.04-dockerfile
docker build -t ubuntu-kernel-build .
cd ..
```

Then lets make a new directory called `my-arm-6.8.12-kernel`.
This directory will have the kernel source and the modules of our built kernel.

```bash
mkdir my-arm-6.8.12-kernel
```

lets get the kernel source from `apt source`, we will be making the `6.8.12 96.8.0-47-generic)` kernel.

```bash
cd my-arm-6.8.12-kernel
apt source linux-image-unsigned-6.8.0-47-generic
```

You might need to update ubuntu source list to use the above command.
Checkout this post if you need to update: <https://askubuntu.com/questions/1512042/ubuntu-24-04-getting-error-you-must-put-some-deb-src-uris-in-your-sources-list>

Lets make an output directory that will have our built modules

```bash
mkdir output
```

Lets run the docker image from the `my-arm-6.8.12-kernel` directory.

```bash
docker run --rm -it -u $UID:$GID -v ./linux-6.8.0:/workspace/source -v ./output:/workspace/output --name kernel-builder ubuntu-kernel-build
```

Now in the docker terminal, lets build the kernel

```bash
cd source
make defconfig
make -j$nproc
```

The above commands will make the kernel and the modules.
lets install the modules to the output directory

```bash
make INSTALL_MOD_PATH=/workspace/output modules_install
```

After the modules are installed, lets install our gem5-bridge driver.

in the `/workspace/source`, lets get the driver files

```bash
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
```
Now lets make our driver

```bash
cd util/gem5_bridge
make KMAKEDIR=/workspace/source INSTALL_MOD_PATH=/workspace/output build install
```

The above command specifies the kernel source path and the output path.
You can find the kernel at `src/ubuntu-generic-diskimages/my-arm-6.8.12-kernel/linux-6.8.0/vmlinux`
and the modules at `src/ubuntu-generic-diskimages/my-arm-6.8.12-kernel/output/lib/modules/6.8.12`.

Now lets move the modules to our disk image.

First you will need to delete the `build` file in `src/ubuntu-generic-diskimages/my-arm-6.8.12-kernel/output/lib/modules/6.8.12` as that is a symlink to the `/workspace` directory in source

```bash
rm output/lib/modules/6.8.12/build
```

now add the following file provisioner to move the files from host to the disk

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

also add the following lines in the post install script to move the modules to `/lib/modules` and run `depmode` and `initramfs`

```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
```

Now you can run a gem5 fs simulation with this disk and the kernel we just made to use the new gem 5-bridge driver.
11 changes: 6 additions & 5 deletions src/ubuntu-generic-diskimages/packer-scripts/arm-ubuntu.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,16 @@ build {
source = "files/[email protected]"
}

provisioner "file" {
destination= "/home/gem5"
source = "my-arm-6.8.12-kernel/output/lib/modules/6.8.12"
}

provisioner "shell" {
execute_command = "echo '${var.ssh_password}' | {{ .Vars }} sudo -E -S bash '{{ .Path }}'"
scripts = ["scripts/post-installation.sh"]
environment_vars = ["ISA=arm64"]
expect_disconnect = true
}
provisioner "file" {
source = "/home/gem5/my-arm-kernel/linux-6.8.0/vmlinux"
destination = "./arm-disk-image-24-04/vmlinux"
direction = "download"
}

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

apt-get update
apt-get install -y fakeroot build-essential crash kexec-tools makedumpfile kernel-wedge
# Fixing the sources.list file to include deb-src: https://askubuntu.com/questions/1512042/ubuntu-24-04-getting-error-you-must-put-some-deb-src-uris-in-your-sources-list
sed -i 's/^Types: deb$/Types: deb deb-src/' /etc/apt/sources.list.d/ubuntu.sources
# apt-get update
# apt-get install -y fakeroot build-essential crash kexec-tools makedumpfile kernel-wedge
# # Fixing the sources.list file to include deb-src: https://askubuntu.com/questions/1512042/ubuntu-24-04-getting-error-you-must-put-some-deb-src-uris-in-your-sources-list
# sed -i 's/^Types: deb$/Types: deb deb-src/' /etc/apt/sources.list.d/ubuntu.sources

# apt update

# apt-get -y build-dep linux
# apt-get -y install git-core libncurses5 libncurses5-dev libelf-dev asciidoc binutils-dev
# apt-get -y install libssl-dev
# apt -y install flex bison
# apt -y install zstd

apt update
# mkdir my-arm-kernel
# cd my-arm-kernel
# apt source linux-image-unsigned-6.8.0-47-generic
# cd linux-6.8.0
# cp /boot/config-$(uname -r) .config
# make -j$(nproc) vmlinux

apt-get -y build-dep linux
apt-get -y install git-core libncurses5 libncurses5-dev libelf-dev asciidoc binutils-dev
apt-get -y install libssl-dev
apt -y install flex bison
apt -y install zstd
# chmod +x ./debian/scripts/sign-module
# make -j$(nproc) modules
# make -j modules_install

mkdir my-arm-kernel
cd my-arm-kernel
apt source linux-image-unsigned-$(uname -r)
cd linux-6.8.0
cp /boot/config-$(uname -r) .config
make -j$(nproc) vmlinux
mv /home/gem5/6.8.12 /lib/modules/6.8.12

chmod +x ./debian/scripts/sign-module
make -j$(nproc) modules
make -j modules_install
depmod --quick -a 6.8.12

update-initramfs -u -k 6.8.12

Expand Down Expand Up @@ -84,10 +88,11 @@ cp build/${ISA}/out/libm5.a /usr/local/lib/
popd # util/m5

# Build and insert the gem5-bridge driver
pushd util/gem5_bridge
make build install
depmod --quick
popd
# pushd util/gem5_bridge
# make build install


# popd

popd # gem5

Expand Down Expand Up @@ -121,3 +126,5 @@ systemctl disable systemd-networkd-wait-online.service
systemctl mask systemd-networkd-wait-online.service

echo "Post Installation Done"

sleep 10m

0 comments on commit d8d4d3c

Please sign in to comment.