| 1 | #!/bin/shscip-shell filesystem . . `/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 | echoscip-shell system . . echo. "[init] starting" |
| 21 | mountscip-shell system . . mount. -t proc proc /procscip-shell filesystem . . `/proc`. |
| 22 | mountscip-shell system . . mount. -t sysfs sysfs /sysscip-shell filesystem . . `/sys`. |
| 23 | mountscip-shell system . . mount. -t devtmpfs devtmpfs /devscip-shell filesystem . . `/dev`. |
| 24 | mkdirscip-shell system . . mkdir. -p /dev/ptsscip-shell filesystem . . `/dev/pts`. |
| 25 | mountscip-shell system . . mount. -t devpts devpts /dev/ptsscip-shell filesystem . . `/dev/pts`. |
| 26 | echoscip-shell system . . 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 [scip-shell system . . `[`. -d /modulesscip-shell filesystem . . `/modules`. ] && [scip-shell system . . `[`. -f /modules/load_orderscip-shell filesystem . . `/modules/load_order`. ]; then |
| 32 | echoscip-shell system . . echo. "[init] loading modules..." |
| 33 | while read -r mod; do |
| 34 | if [scip-shell system . . `[`. -f "/modules/${mod}.ko" ]; then |
| 35 | insmod "/modules/${mod}.ko" 2>&1 || echoscip-shell system . . echo. "[init] WARN: insmod ${mod}.ko failed" |
| 36 | fi |
| 37 | done < /modules/load_orderscip-shell filesystem . . `/modules/load_order`. |
| 38 | echoscip-shell system . . echo. "[init] modules loaded" |
| 39 | elif [scip-shell system . . `[`. -d /modulesscip-shell filesystem . . `/modules`. ]; then |
| 40 | # Fallback: try all .ko files (may fail on dep order) |
| 41 | for mod in /modules/*.ko; do |
| 42 | [scip-shell system . . `[`. -f "$mod" ] && insmod "$mod" 2>/dev/nullscip-shell filesystem . . `/dev/null`. |
| 43 | done |
| 44 | fi |
| 45 | |
| 46 | # Step 3: detect mode |
| 47 | echoscip-shell system . . echo. "[init] checking for /dev/vda..." |
| 48 | lsscip-shell system . . ls. -la /dev/vd* 2>/dev/nullscip-shell filesystem . . `/dev/null`. || echoscip-shell system . . echo. "[init] no /dev/vd* devices found" |
| 49 | if [scip-shell system . . `[`. -b /dev/vdascip-shell filesystem . . `/dev/vda`. ]; then |
| 50 | # ===== Rootfs disk mode (RHEL kernels without 9p) ===== |
| 51 | echoscip-shell system . . echo. "[init] rootfs mode: mounting /dev/vda" |
| 52 | |
| 53 | mkdirscip-shell system . . mkdir. -p /mnt/rootfsscip-shell filesystem . . `/mnt/rootfs`. |
| 54 | mountscip-shell system . . mount. -o ro /dev/vdascip-shell filesystem . . `/dev/vda`. /mnt/rootfsscip-shell filesystem . . `/mnt/rootfs`. |
| 55 | if [scip-shell system . . `[`. $? -ne 0 ]; then |
| 56 | echoscip-shell system . . 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 | mountscip-shell system . . mount. -t tmpfs tmpfs /mnt/rootfs/tmpscip-shell filesystem . . `/mnt/rootfs/tmp`. |
| 62 | mountscip-shell system . . mount. -t tmpfs tmpfs /mnt/rootfs/runscip-shell filesystem . . `/mnt/rootfs/run`. |
| 63 | mountscip-shell system . . mount. -t tmpfs tmpfs /mnt/rootfs/var/tmpscip-shell filesystem . . `/mnt/rootfs/var/tmp`. 2>/dev/nullscip-shell filesystem . . `/dev/null`. |
| 64 | |
| 65 | # Guest needs its own /dev, /proc, /sys inside the chroot |
| 66 | mountscip-shell system . . mount. -t devtmpfs devtmpfs /mnt/rootfs/devscip-shell filesystem . . `/mnt/rootfs/dev`. |
| 67 | mkdirscip-shell system . . mkdir. -p /mnt/rootfs/dev/ptsscip-shell filesystem . . `/mnt/rootfs/dev/pts`. |
| 68 | mountscip-shell system . . mount. -t devpts devpts /mnt/rootfs/dev/ptsscip-shell filesystem . . `/mnt/rootfs/dev/pts`. |
| 69 | mountscip-shell system . . mount. -t proc proc /mnt/rootfs/procscip-shell filesystem . . `/mnt/rootfs/proc`. |
| 70 | mountscip-shell system . . mount. -t sysfs sysfs /mnt/rootfs/sysscip-shell filesystem . . `/mnt/rootfs/sys`. |
| 71 | |
| 72 | # Read command (from initramfs overlay, or default) |
| 73 | if [scip-shell system . . `[`. -f /cmdscip-shell filesystem . . `/cmd`. ]; then |
| 74 | CMD=$(catscip-shell system . . cat. /cmdscip-shell filesystem . . `/cmd`.) |
| 75 | else |
| 76 | CMD="/test/guest-run-all.shscip-shell filesystem . . `/test/guest-run-all.sh`." |
| 77 | fi |
| 78 | |
| 79 | # Run it inside the chroot (output goes to serial console) |
| 80 | if [scip-shell system . . `[`. "$CMD" = "__INTERACTIVE__" ]; then |
| 81 | chrootscip-shell system . . chroot. /mnt/rootfsscip-shell filesystem . . `/mnt/rootfs`. /test/guest-interactive.shscip-shell filesystem . . `/test/guest-interactive.sh`. 2>&1 |
| 82 | echoscip-shell system . . echo. "===QUOTATOOL_EXIT:0===" |
| 83 | else |
| 84 | echoscip-shell system . . echo. "[init] chroot: running $CMD" |
| 85 | chrootscip-shell system . . chroot. /mnt/rootfsscip-shell filesystem . . `/mnt/rootfs`. /bin/bashscip-shell filesystem . . `/bin/bash`. -c "export PATH=/bin:/sbin:/usr/bin:/usr/sbin; $CMD" 2>&1 |
| 86 | EXIT_CODE=$? |
| 87 | echoscip-shell system . . 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 | mkdirscip-shell system . . mkdir. -p /mnt/hostscip-shell filesystem . . `/mnt/host`. |
| 99 | mountscip-shell system . . mount. -t 9p -o trans=virtio,version=9p2000.L,msize=524288 hostroot /mnt/hostscip-shell filesystem . . `/mnt/host`. |
| 100 | if [scip-shell system . . `[`. $? -ne 0 ]; then |
| 101 | # Try without msize (older kernels may not support it) |
| 102 | mountscip-shell system . . mount. -t 9p -o trans=virtio,version=9p2000.L hostroot /mnt/hostscip-shell filesystem . . `/mnt/host`. |
| 103 | if [scip-shell system . . `[`. $? -ne 0 ]; then |
| 104 | echoscip-shell system . . 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 | mkdirscip-shell system . . mkdir. -p /mnt/resultsscip-shell filesystem . . `/mnt/results`. |
| 111 | mountscip-shell system . . mount. -t 9p -o trans=virtio,version=9p2000.L,msize=524288 results /mnt/resultsscip-shell filesystem . . `/mnt/results`. 2>/dev/nullscip-shell filesystem . . `/dev/null`. \ |
| 112 | || mountscip-shell system . . mount. -t 9p -o trans=virtio,version=9p2000.L results /mnt/resultsscip-shell filesystem . . `/mnt/results`. |
| 113 | |
| 114 | # Step 5: overlay writable areas on top of the read-only host mount |
| 115 | mountscip-shell system . . mount. -t tmpfs tmpfs /mnt/host/tmpscip-shell filesystem . . `/mnt/host/tmp`. |
| 116 | mountscip-shell system . . mount. -t tmpfs tmpfs /mnt/host/runscip-shell filesystem . . `/mnt/host/run`. |
| 117 | mountscip-shell system . . mount. -t tmpfs tmpfs /mnt/host/var/tmpscip-shell filesystem . . `/mnt/host/var/tmp`. 2>/dev/nullscip-shell filesystem . . `/dev/null`. |
| 118 | |
| 119 | # Guest needs its own /dev, /proc, /sys inside the chroot |
| 120 | mountscip-shell system . . mount. -t devtmpfs devtmpfs /mnt/host/devscip-shell filesystem . . `/mnt/host/dev`. |
| 121 | mkdirscip-shell system . . mkdir. -p /mnt/host/dev/ptsscip-shell filesystem . . `/mnt/host/dev/pts`. |
| 122 | mountscip-shell system . . mount. -t devpts devpts /mnt/host/dev/ptsscip-shell filesystem . . `/mnt/host/dev/pts`. |
| 123 | mountscip-shell system . . mount. -t proc proc /mnt/host/procscip-shell filesystem . . `/mnt/host/proc`. |
| 124 | mountscip-shell system . . mount. -t sysfs sysfs /mnt/host/sysscip-shell filesystem . . `/mnt/host/sys`. |
| 125 | |
| 126 | # Step 6: read the command |
| 127 | if [scip-shell system . . `[`. ! -f /mnt/results/cmdscip-shell filesystem . . `/mnt/results/cmd`. ]; then |
| 128 | echoscip-shell system . . echo. "FATAL: no command file at /mnt/results/cmd" >&2 |
| 129 | echoscip-shell system . . echo. "1" > /mnt/results/exit_codescip-shell filesystem . . `/mnt/results/exit_code`. |
| 130 | poweroff -f |
| 131 | fi |
| 132 | CMD=$(catscip-shell system . . cat. /mnt/results/cmdscip-shell filesystem . . `/mnt/results/cmd`.) |
| 133 | |
| 134 | # Step 7: run it inside the chroot |
| 135 | if [scip-shell system . . `[`. "$CMD" = "__INTERACTIVE__" ]; then |
| 136 | # Interactive mode: run setup, then drop to shell on serial console |
| 137 | SETUP_PATH=$(catscip-shell system . . cat. /mnt/results/setup_pathscip-shell filesystem . . `/mnt/results/setup_path`. 2>/dev/nullscip-shell filesystem . . `/dev/null`. || truescip-shell system . . true.) |
| 138 | if [scip-shell system . . `[`. -n "$SETUP_PATH" ]; then |
| 139 | chrootscip-shell system . . chroot. /mnt/hostscip-shell filesystem . . `/mnt/host`. "$SETUP_PATH" 2>&1 |
| 140 | else |
| 141 | chrootscip-shell system . . chroot. /mnt/hostscip-shell filesystem . . `/mnt/host`. /bin/bashscip-shell filesystem . . `/bin/bash`. --norc --noprofile -i 2>&1 |
| 142 | fi |
| 143 | echoscip-shell system . . echo. "0" > /mnt/results/exit_codescip-shell filesystem . . `/mnt/results/exit_code`. |
| 144 | else |
| 145 | # Normal mode: capture output to files |
| 146 | chrootscip-shell system . . chroot. /mnt/hostscip-shell filesystem . . `/mnt/host`. /bin/shscip-shell filesystem . . `/bin/sh`. -c "$CMD" > /mnt/results/stdoutscip-shell filesystem . . `/mnt/results/stdout`. 2>&1 |
| 147 | EXIT_CODE=$? |
| 148 | echoscip-shell system . . echo. "$EXIT_CODE" > /mnt/results/exit_codescip-shell filesystem . . `/mnt/results/exit_code`. |
| 149 | fi |
| 150 | |
| 151 | # Done |
| 152 | poweroff -f |