11#! /bin/bash
22# Create ext2 disk image for Breenix kernel testing
33#
4- # This script creates a 4MB ext2 filesystem image with test files.
4+ # This script creates a 4MB ext2 filesystem image with:
5+ # - Test files for filesystem testing
6+ # - Coreutils binaries in /bin/ (cat, ls, echo, mkdir, rmdir, rm, cp, mv)
7+ # - hello_world binary for exec testing
8+ #
59# Requires Docker on macOS (or mke2fs on Linux).
610#
711# Usage:
@@ -15,15 +19,22 @@ set -e
1519SCRIPT_DIR=" $( cd " $( dirname " ${BASH_SOURCE[0]} " ) " && pwd) "
1620PROJECT_ROOT=" $( cd " $SCRIPT_DIR /.." && pwd) "
1721TARGET_DIR=" $PROJECT_ROOT /target"
22+ USERSPACE_DIR=" $PROJECT_ROOT /userspace/tests"
1823OUTPUT_FILE=" $TARGET_DIR /ext2.img"
24+ TESTDATA_FILE=" $PROJECT_ROOT /testdata/ext2.img"
1925SIZE_MB=4
2026
27+ # Coreutils to install in /bin
28+ COREUTILS=" cat ls echo mkdir rmdir rm cp mv"
29+
2130echo " Creating ext2 disk image..."
2231echo " Output: $OUTPUT_FILE "
2332echo " Size: ${SIZE_MB} MB"
33+ echo " Coreutils: $COREUTILS "
2434
2535# Ensure target directory exists
2636mkdir -p " $TARGET_DIR "
37+ mkdir -p " $PROJECT_ROOT /testdata"
2738
2839# Check if we're on macOS or Linux
2940if [[ " $( uname) " == " Darwin" ]]; then
@@ -44,6 +55,7 @@ if [[ "$(uname)" == "Darwin" ]]; then
4455
4556 docker run --rm --privileged \
4657 -v " $TARGET_DIR :/work" \
58+ -v " $USERSPACE_DIR :/binaries:ro" \
4759 alpine:latest \
4860 sh -c '
4961 set -e
@@ -59,7 +71,31 @@ if [[ "$(uname)" == "Darwin" ]]; then
5971 mkdir -p /mnt/ext2
6072 mount /work/ext2.img /mnt/ext2
6173
62- # Create test files
74+ # Create /bin directory for coreutils
75+ mkdir -p /mnt/ext2/bin
76+
77+ # Copy coreutils binaries
78+ echo "Installing coreutils in /bin..."
79+ for bin in cat ls echo mkdir rmdir rm cp mv; do
80+ if [ -f /binaries/${bin}.elf ]; then
81+ cp /binaries/${bin}.elf /mnt/ext2/bin/${bin}
82+ chmod 755 /mnt/ext2/bin/${bin}
83+ echo " /bin/${bin} installed"
84+ else
85+ echo " WARNING: ${bin}.elf not found in /binaries/"
86+ fi
87+ done
88+
89+ # Copy hello_world for exec testing
90+ if [ -f /binaries/hello_world.elf ]; then
91+ cp /binaries/hello_world.elf /mnt/ext2/bin/hello_world
92+ chmod 755 /mnt/ext2/bin/hello_world
93+ echo " /bin/hello_world installed"
94+ else
95+ echo " WARNING: hello_world.elf not found"
96+ fi
97+
98+ # Create test files for filesystem testing
6399 echo "Hello from ext2!" > /mnt/ext2/hello.txt
64100 mkdir -p /mnt/ext2/test
65101 echo "Nested file content" > /mnt/ext2/test/nested.txt
@@ -69,12 +105,17 @@ if [[ "$(uname)" == "Darwin" ]]; then
69105 echo "Deep nested content" > /mnt/ext2/deep/path/to/file/data.txt
70106
71107 # Show what was created
72- echo "Files created:"
73- find /mnt/ext2 -type f -exec ls -la {} \;
108+ echo ""
109+ echo "ext2 filesystem contents:"
110+ echo " Binaries in /bin:"
111+ ls -la /mnt/ext2/bin/ 2>/dev/null || echo " (none)"
112+ echo " Test files:"
113+ find /mnt/ext2 -type f -not -path "/mnt/ext2/bin/*" -exec ls -la {} \;
74114
75115 # Unmount
76116 umount /mnt/ext2
77117
118+ echo ""
78119 echo "ext2 image created successfully"
79120 '
80121else
@@ -102,6 +143,28 @@ else
102143 MOUNT_DIR=$( mktemp -d)
103144 mount " $OUTPUT_FILE " " $MOUNT_DIR "
104145
146+ # Create /bin directory
147+ mkdir -p " $MOUNT_DIR /bin"
148+
149+ # Copy coreutils binaries
150+ echo " Installing coreutils in /bin..."
151+ for bin in cat ls echo mkdir rmdir rm cp mv; do
152+ if [ -f " $USERSPACE_DIR /${bin} .elf" ]; then
153+ cp " $USERSPACE_DIR /${bin} .elf" " $MOUNT_DIR /bin/${bin} "
154+ chmod 755 " $MOUNT_DIR /bin/${bin} "
155+ echo " /bin/${bin} installed"
156+ else
157+ echo " WARNING: ${bin} .elf not found"
158+ fi
159+ done
160+
161+ # Copy hello_world for exec testing
162+ if [ -f " $USERSPACE_DIR /hello_world.elf" ]; then
163+ cp " $USERSPACE_DIR /hello_world.elf" " $MOUNT_DIR /bin/hello_world"
164+ chmod 755 " $MOUNT_DIR /bin/hello_world"
165+ echo " /bin/hello_world installed"
166+ fi
167+
105168 # Create test files
106169 echo " Hello from ext2!" > " $MOUNT_DIR /hello.txt"
107170 mkdir -p " $MOUNT_DIR /test"
@@ -110,8 +173,10 @@ else
110173 echo " Deep nested content" > " $MOUNT_DIR /deep/path/to/file/data.txt"
111174
112175 # Show what was created
113- echo " Files created:"
114- find " $MOUNT_DIR " -type f -exec ls -la {} \;
176+ echo " "
177+ echo " ext2 filesystem contents:"
178+ ls -la " $MOUNT_DIR /bin/"
179+ find " $MOUNT_DIR " -type f -not -path " $MOUNT_DIR /bin/*" -exec ls -la {} \;
115180
116181 # Unmount and cleanup
117182 umount " $MOUNT_DIR "
@@ -120,16 +185,22 @@ else
120185 echo " ext2 image created successfully"
121186fi
122187
123- # Verify output
188+ # Copy to testdata/ for version control
124189if [[ -f " $OUTPUT_FILE " ]]; then
190+ cp " $OUTPUT_FILE " " $TESTDATA_FILE "
125191 SIZE=$( ls -lh " $OUTPUT_FILE " | awk ' {print $5}' )
126192 echo " "
127- echo " ext2 disk created: $OUTPUT_FILE "
193+ echo " ext2 disk created and copied to testdata/:"
194+ echo " $OUTPUT_FILE "
195+ echo " $TESTDATA_FILE "
128196 echo " Size: $SIZE "
129- echo " Contents:"
130- echo " /hello.txt - \" Hello from ext2!\" "
131- echo " /test/nested.txt - \" Nested file content\" "
132- echo " /deep/path/to/file/data.txt - \" Deep nested content\" "
197+ echo " "
198+ echo " Contents:"
199+ echo " /bin/cat, ls, echo, mkdir, rmdir, rm, cp, mv - coreutils"
200+ echo " /bin/hello_world - exec test binary (exit code 42)"
201+ echo " /hello.txt - test file"
202+ echo " /test/nested.txt - nested test file"
203+ echo " /deep/path/to/file/data.txt - deep nested test file"
133204else
134205 echo " Error: Failed to create ext2 image"
135206 exit 1
0 commit comments