Booting linux in qemu (problem with ash: Bad substitution k)

Hello, I’m trying to boot linux kernel with only initramfs built from debian root.

This is my /init script:

#!/usr/bin/ash

udevd_running=0
mount_handler=default_mount_handler
init=/sbin/init
rd_logmask=0

. /init_functions

mount_setup

# parse the kernel command line
parse_cmdline </proc/cmdline

# setup logging as early as possible
rdlogger_start

for d in ${disablehooks//,/ }; do
    [ -e "/hooks/$d" ] && chmod 644 "/hooks/$d"
done

. /config

run_hookfunctions 'run_earlyhook' 'early hook' $EARLYHOOKS

if [ -n "$earlymodules$MODULES" ]; then
    modprobe -qab ${earlymodules//,/ } $MODULES
fi

run_hookfunctions 'run_hook' 'hook' $HOOKS

run_hookfunctions 'run_latehook' 'late hook' $LATEHOOKS
run_hookfunctions 'run_cleanuphook' 'cleanup hook' $CLEANUPHOOKS


# this should always be the last thing we do before the switch_root.
rdlogger_stop

exec env -i "TERM=$TERM" $init "$@"

And this is the error I’m getting:

/init: 18: /init: Bad substitution   k
[    4.471088] Kernel panic - not syncing: Attempted to kill init! exitcode=0x00000200
[    4.471088] 
[    4.472501] CPU: 1 PID: 1 Comm: init Not tainted 4.19.132-2-lts #1
[    4.473406] Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS ArchLinux 1.14.0-1 04/01/2014
[    4.474716] Call Trace:
[    4.475105]  dump_stack+0x66/0x90
[    4.475605]  panic+0xe7/0x24a
[    4.476052]  do_exit.cold+0x3f/0x83
[    4.476571]  ? vfs_write+0x161/0x1a0
[    4.477106]  do_group_exit+0x33/0xb0
[    4.477758]  __x64_sys_exit_group+0x14/0x20
[    4.478480]  do_syscall_64+0x4e/0x100
[    4.479089]  entry_SYSCALL_64_after_hwframe+0x44/0xa9
[    4.479963] RIP: 0033:0x7fae4651e9d6
[    4.480541] Code: 00 4c 8b 0d bc 44 0f 00 eb 19 66 2e 0f 1f 84 00 00 00 00 00 89 d7 89 f0 0f 05 48 3d 00 f0 ff ff 77 22 f4 89 d7 40
[    4.483456] RSP: 002b:00007ffdfd65f958 EFLAGS: 00000246 ORIG_RAX: 00000000000000e7
[    4.484629] RAX: ffffffffffffffda RBX: 0000000000000004 RCX: 00007fae4651e9d6
[    4.485585] RDX: 0000000000000002 RSI: 000000000000003c RDI: 0000000000000002
[    4.486590] RBP: 000055a3a37c2dc0 R08: 00000000000000e7 R09: ffffffffffffff80
[    4.487760] R10: 0000000000000005 R11: 0000000000000246 R12: 000055a3a37b1760
[    4.488871] R13: 00007ffdfd65fc30 R14: 0000000000000000 R15: 0000000000000000
[    4.490125] Kernel Offset: disabled
[    4.490680] ---[ end Kernel panic - not syncing: Attempted to kill init! exitcode=0x00000200
[    4.490680]  ]---
[    4.492282] ------------[ cut here ]------------
[    4.493000] sched: Unexpected reschedule of offline CPU#0!
[    4.493861] WARNING: CPU: 1 PID: 1 at arch/x86/kernel/smp.c:128 native_smp_send_reschedule+0x34/0x40
[    4.495261] Modules linked in:
[    4.495741] CPU: 1 PID: 1 Comm: init Not tainted 4.19.132-2-lts #1
[    4.496692] Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS ArchLinux 1.14.0-1 04/01/2014
[    4.498016] RIP: 0010:native_smp_send_reschedule+0x34/0x40
[    4.498748] Code: 05 81 70 2f 01 73 15 48 8b 05 78 df 0c 01 be fd 00 00 00 48 8b 40 30 e9 ca e9 ba 00 89 fe 48 c7 c7 f0 ee 06 82 e2
[    4.501242] RSP: 0018:ffff88817bb03e68 EFLAGS: 00010086
[    4.501953] RAX: 0000000000000000 RBX: 0000000000021c80 RCX: 0000000000000196
[    4.502888] RDX: 0000000000000001 RSI: 0000000000000096 RDI: 0000000000000046
[    4.503819] RBP: ffff88817afd5ac0 R08: 0000000000000196 R09: 0000000000000005
[    4.504752] R10: 0000000000000000 R11: ffffffff82886eed R12: ffff88817ba00000
[    4.505684] R13: 0000000000000000 R14: 0000000000000046 R15: ffff88817afd6204
[    4.506623] FS:  00007fae4661a580(0000) GS:ffff88817bb00000(0000) knlGS:0000000000000000
[    4.507681] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[    4.508449] CR2: 000055a3a4ba7120 CR3: 000000017b422002 CR4: 0000000000360ee0
[    4.509393] Call Trace:
[    4.509726]  <IRQ>
[    4.510013]  try_to_wake_up+0x425/0x4c0
[    4.510531]  swake_up_one+0x2e/0x60
[    4.511009]  rcu_process_callbacks+0x47b/0x4b0
[    4.511609]  __do_softirq+0xee/0x2e1
[    4.512106]  irq_exit+0xa4/0xe0
[    4.512526]  smp_apic_timer_interrupt+0x78/0x140
[    4.513160]  apic_timer_interrupt+0xf/0x20
[    4.513717]  </IRQ>
[    4.514027] RIP: 0010:panic+0x204/0x24a
[    4.514575] Code: eb a6 83 3d 65 8f 7f 01 00 74 05 e8 5e 54 02 00 48 c7 c6 00 f1 87 82 48 c7 c7 a0 8a 07 82 e8 c9 77 06 00 fb 66 0f
[    4.517178] RSP: 0018:ffffc90000637e10 EFLAGS: 00000246 ORIG_RAX: ffffffffffffff13
[    4.518233] RAX: 0000000000000056 RBX: ffff88817afb0000 RCX: 0000000000000194
[    4.519154] RDX: 0000000000000000 RSI: 0000000000000092 RDI: 0000000000000046
[    4.520085] RBP: ffffc90000637e80 R08: 0000000000000194 R09: 0000000000000005
[    4.521008] R10: 0000000000000000 R11: ffffffff82886f4d R12: 0000000000000000
[    4.522181] R13: 0000000000000000 R14: 0000000000000001 R15: 0000000000000000
[    4.523555]  do_exit.cold+0x3f/0x83
[    4.524266]  ? vfs_write+0x161/0x1a0
[    4.524984]  do_group_exit+0x33/0xb0
[    4.525695]  __x64_sys_exit_group+0x14/0x20
[    4.526565]  do_syscall_64+0x4e/0x100
[    4.527277]  entry_SYSCALL_64_after_hwframe+0x44/0xa9
[    4.528227] RIP: 0033:0x7fae4651e9d6
[    4.528750] Code: 00 4c 8b 0d bc 44 0f 00 eb 19 66 2e 0f 1f 84 00 00 00 00 00 89 d7 89 f0 0f 05 48 3d 00 f0 ff ff 77 22 f4 89 d7 40
[    4.531178] RSP: 002b:00007ffdfd65f958 EFLAGS: 00000246 ORIG_RAX: 00000000000000e7
[    4.532136] RAX: ffffffffffffffda RBX: 0000000000000004 RCX: 00007fae4651e9d6
[    4.533059] RDX: 0000000000000002 RSI: 000000000000003c RDI: 0000000000000002
[    4.533989] RBP: 000055a3a37c2dc0 R08: 00000000000000e7 R09: ffffffffffffff80
[    4.535339] R10: 0000000000000005 R11: 0000000000000246 R12: 000055a3a37b1760
[    4.536643] R13: 00007ffdfd65fc30 R14: 0000000000000000 R15: 0000000000000000
[    4.537920] ---[ end trace e3230de40993f100 ]---

qemu command used to boot the system:

qemu-system-x86_64 -enable-kvm -name testCommandLine -m 4096m -cpu host -M q35 -smp 2 -kernel /boot/vmlinuz-linux-lts -initrd ./debian-stable.cpio.gz -append "init=/init console=ttyS0 nokaslr" -nographic

When the loop at 18th line is removed I get those messages in a loop

:: running early [udev]
Warning: /lib/modules/4.19.132-2-lts/modules.devname not found - ignoring
Starting version 241
:: running hook [udev]
:: Triggering uevents...
:: running cleanup [udev]
mount: /proc: proc already mounted on /proc.
mount: /sys: sys already mounted on /sys.
mount: /dev: dev already mounted on /dev.
:: running early [udev]
Warning: /lib/modules/4.19.132-2-lts/modules.devname not found - ignoring
Starting version 241
:: running hook [udev]
:: Triggering uevents...
:: running cleanup [udev]
mount: /proc: proc already mounted on /proc.
mount: /sys: sys already mounted on /sys.
mount: /dev: dev already mounted on /dev.
:: running early [udev]
Warning: /lib/modules/4.19.132-2-lts/modules.devname not found - ignoring
Starting version 241
:: running hook [udev]
:: Triggering uevents...
:: running cleanup [udev]
mount: /proc: proc already mounted on /proc.
mount: /sys: sys already mounted on /sys.
mount: /dev: dev already mounted on /dev.

It boots “kinda” OK when I just boot systemd directly from the kernel command line (by specifying /sbin/init as init) but then my keyboard doesn’t work, I can’t interact with the VM at all.

you need login enabled on a serial port

I guess that’s to fix the problem with looped systemd messages on the serial?

WHat about the “broken” init script I’m using? I repurposed it from my os installation (ArchLinux), why is causing issues in the vm?

Not sure, at first look your -append from your first post is missing a root device to pass to the init scripts. But the ‘k’ errors is strange and interesting.

The point of the thing I’m doing is that there is no root device. I want to make this VM run with the kernel and cpio only.

Also, it’s stuck on the repeating message even if I dont attach everything to serial port.

Ok, it looks like commenting out “mount setup” function fixes nearly everything - I can now boot even to GUI target on this image.

However the problem that led me to mess around with the init script for so long in the first place is still not fixed - I can’t interact with the VM at all the mouse pointer nor the keyboard work.

Other distributions booted with the same qemu options (excluding the -append and kernel and image options) work just fine.
I just booted ubuntu 20.04 and made sure everything works as expected.

Do I need any special drivers to install in this image to make the keyboard and mouse work?

Input finally started working after installing linux kernel in the image and then booting that “extracted” from the image kernel with qemu, everything is still working 100% in ram (the VM have no storage so that’s what I wanted).

However it really bugs me that the keyboard/mouse driver modules(?) are probably dynamically loaded… I have no idea why they couldn’t be just included in the kernel itself.