| 1 | #!/bin/sh |
| 2 | # Minimal init for raw QEMU boot path. |
| 3 | # |
| 4 | # Two modes, auto-detected: |
| 5 | # |
| 6 | # 9p mode (default): |
| 7 | # Mounts host filesystem via 9p, chroots in, runs the test command. |
| 8 | # Exit code propagated via writable 9p results share. |
| 9 | # Expects: hostroot (9p share), results (9p share), /mnt/results/cmd |
| 10 | # |
| 11 | # Rootfs mode (RHEL kernels without 9p): |
| 12 | # Mounts a virtio-blk disk image containing all binaries, libs, |
| 13 | # and test scripts. No 9p needed. Exit code printed to serial |
| 14 | # as a marker line for the host to parse. |
| 15 | # Expects: /dev/vda (ext4 disk image), /cmd (in initramfs overlay) |
| 16 | # |
| 17 | # Mode detection: /dev/vda present → rootfs mode, else 9p mode. |
| 18 | |
| 19 | # Step 1: essential filesystems |
| 20 | echo "[init] starting" |
| 21 | mount -t proc proc /proc |
| 22 | mount -t sysfs sysfs /sys |
| 23 | mount -t devtmpfs devtmpfs /dev |
| 24 | mkdir -p /dev/pts |
| 25 | mount -t devpts devpts /dev/pts |
| 26 | echo "[init] filesystems mounted" |
| 27 | |
| 28 | # Step 2: load modules if present |
| 29 | # The boot layer appends kernel-specific .ko files at /modules/ with |
| 30 | # a load_order file specifying dependency-first sequence. |
| 31 | if [ -d /modules ] && [ -f /modules/load_order ]; then |
| 32 | echo "[init] loading modules..." |
| 33 | while read -r mod; do |
| 34 | if [ -f "/modules/${mod}.ko" ]; then |
| 35 | insmod "/modules/${mod}.ko" 2>&1 || echo "[init] WARN: insmod ${mod}.ko failed" |
| 36 | fi |
| 37 | done < /modules/load_order |
| 38 | echo "[init] modules loaded" |
| 39 | elif [ -d /modules ]; then |
| 40 | # Fallback: try all .ko files (may fail on dep order) |
| 41 | for mod in /modules/*.ko; do |
| 42 | [ -f "$mod" ] && insmod "$mod" 2>/dev/null |
| 43 | done |
| 44 | fi |
| 45 | |
| 46 | # Step 3: detect mode |
| 47 | echo "[init] checking for /dev/vda..." |
| 48 | ls -la /dev/vd* 2>/dev/null || echo "[init] no /dev/vd* devices found" |
| 49 | if [ -b /dev/vda ]; then |
| 50 | # ===== Rootfs disk mode (RHEL kernels without 9p) ===== |
| 51 | echo "[init] rootfs mode: mounting /dev/vda" |
| 52 | |
| 53 | mkdir -p /mnt/rootfs |
| 54 | mount -o ro /dev/vda /mnt/rootfs |
| 55 | if [ $? -ne 0 ]; then |
| 56 | echo "FATAL: failed to mount rootfs disk /dev/vda" >&2 |
| 57 | poweroff -f |
| 58 | fi |
| 59 | |
| 60 | # Writable overlays (tests write to /tmp for loop devices) |
| 61 | mount -t tmpfs tmpfs /mnt/rootfs/tmp |
| 62 | mount -t tmpfs tmpfs /mnt/rootfs/run |
| 63 | mount -t tmpfs tmpfs /mnt/rootfs/var/tmp 2>/dev/null |
| 64 | |
| 65 | # Guest needs its own /dev, /proc, /sys inside the chroot |
| 66 | mount -t devtmpfs devtmpfs /mnt/rootfs/dev |
| 67 | mkdir -p /mnt/rootfs/dev/pts |
| 68 | mount -t devpts devpts /mnt/rootfs/dev/pts |
| 69 | mount -t proc proc /mnt/rootfs/proc |
| 70 | mount -t sysfs sysfs /mnt/rootfs/sys |
| 71 | |
| 72 | # Read command (from initramfs overlay, or default) |
| 73 | if [ -f /cmd ]; then |
| 74 | CMD=$(cat /cmd) |
| 75 | else |
| 76 | CMD="/test/guest-run-all.sh" |
| 77 | fi |
| 78 | |
| 79 | # Run it inside the chroot (output goes to serial console) |
| 80 | if [ "$CMD" = "__INTERACTIVE__" ]; then |
| 81 | chroot /mnt/rootfs /test/guest-interactive.sh 2>&1 |
| 82 | echo "===QUOTATOOL_EXIT:0===" |
| 83 | else |
| 84 | echo "[init] chroot: running $CMD" |
| 85 | chroot /mnt/rootfs /bin/bash -c "export PATH=/bin:/sbin:/usr/bin:/usr/sbin; $CMD" 2>&1 |
| 86 | EXIT_CODE=$? |
| 87 | echo "===QUOTATOOL_EXIT:${EXIT_CODE}===" |
| 88 | fi |
| 89 | |
| 90 | poweroff -f |
| 91 | fi |
| 92 | |
| 93 | # ===== 9p mode (default) ===== |
| 94 | |
| 95 | # Step 3b: mount host root via 9p |
| 96 | # Read-write to match vng behavior (CoW overlay). Tests only write to |
| 97 | # /tmp and loop devices, so host files are not modified in practice. |
| 98 | mkdir -p /mnt/host |
| 99 | mount -t 9p -o trans=virtio,version=9p2000.L,msize=524288 hostroot /mnt/host |
| 100 | if [ $? -ne 0 ]; then |
| 101 | # Try without msize (older kernels may not support it) |
| 102 | mount -t 9p -o trans=virtio,version=9p2000.L hostroot /mnt/host |
| 103 | if [ $? -ne 0 ]; then |
| 104 | echo "FATAL: 9p mount failed — check kernel has 9P_FS support" >&2 |
| 105 | poweroff -f |
| 106 | fi |
| 107 | fi |
| 108 | |
| 109 | # Step 4: mount results share (writable) |
| 110 | mkdir -p /mnt/results |
| 111 | mount -t 9p -o trans=virtio,version=9p2000.L,msize=524288 results /mnt/results 2>/dev/null \ |
| 112 | || mount -t 9p -o trans=virtio,version=9p2000.L results /mnt/results |
| 113 | |
| 114 | # Step 5: overlay writable areas on top of the read-only host mount |
| 115 | mount -t tmpfs tmpfs /mnt/host/tmp |
| 116 | mount -t tmpfs tmpfs /mnt/host/run |
| 117 | mount -t tmpfs tmpfs /mnt/host/var/tmp 2>/dev/null |
| 118 | |
| 119 | # Guest needs its own /dev, /proc, /sys inside the chroot |
| 120 | mount -t devtmpfs devtmpfs /mnt/host/dev |
| 121 | mkdir -p /mnt/host/dev/pts |
| 122 | mount -t devpts devpts /mnt/host/dev/pts |
| 123 | mount -t proc proc /mnt/host/proc |
| 124 | mount -t sysfs sysfs /mnt/host/sys |
| 125 | |
| 126 | # Step 6: read the command |
| 127 | if [ ! -f /mnt/results/cmd ]; then |
| 128 | echo "FATAL: no command file at /mnt/results/cmd" >&2 |
| 129 | echo "1" > /mnt/results/exit_code |
| 130 | poweroff -f |
| 131 | fi |
| 132 | CMD=$(cat /mnt/results/cmd) |
| 133 | |
| 134 | # Step 7: run it inside the chroot |
| 135 | if [ "$CMD" = "__INTERACTIVE__" ]; then |
| 136 | # Interactive mode: run setup, then drop to shell on serial console |
| 137 | SETUP_PATH=$(cat /mnt/results/setup_path 2>/dev/null || true) |
| 138 | if [ -n "$SETUP_PATH" ]; then |
| 139 | chroot /mnt/host "$SETUP_PATH" 2>&1 |
| 140 | else |
| 141 | chroot /mnt/host /bin/bash --norc --noprofile -i 2>&1 |
| 142 | fi |
| 143 | echo "0" > /mnt/results/exit_code |
| 144 | else |
| 145 | # Normal mode: capture output to files |
| 146 | chroot /mnt/host /bin/sh -c "$CMD" > /mnt/results/stdout 2>&1 |
| 147 | EXIT_CODE=$? |
| 148 | echo "$EXIT_CODE" > /mnt/results/exit_code |
| 149 | fi |
| 150 | |
| 151 | # Done |
| 152 | poweroff -f |