QEMU: /bin/sh: 无法访问 tty;作业控制关闭

QEMU: /bin/sh: can't access tty; job control turned off

作为 linux 内核的开发环境,我使用 qemu 设置 initramfshere 中显示的类似,几乎没有额外的可执行文件。基本上,它使用 busybox 创建最小环境并使用 cpio 将其打包。 init的内容如下所示。

$ cat init
mount -t proc none /proc
mount -t sysfs none /sys

echo -e "\nBoot took $(cut -d' ' -f1 /proc/uptime) seconds\n"
exec /bin/sh

使用以下命令启动虚拟机:

qemu-system-x86_64 -kernel bzImage -initrd initramfs -append "console=ttyS0" -nographic

它抛出以下错误:

/bin/sh: can't access tty; job control turned off

不过,大多数情况下系统运行正常。但是,我无法创建后台进程:

$ prog &
/bin/sh: can't open '/dev/null'
$ fg
/bin/sh: fg: job (null) not created under job control

所有问题的根源似乎都在于无法访问 tty。我该如何解决这个问题?

编辑:除了已接受的答案外,还可以使用 busybox cttyhack 绕过。

$cat init
#!/bin/sh

mount -t proc none /proc
mount -t sysfs none /sys
mknod -m 666 /dev/ttyS0 c 4 64

echo -e "\nBoot took $(cut -d' ' -f1 /proc/uptime) seconds\n"
setsid  cttyhack sh
exec /bin/sh

来自Linux From Scratch Chapter 6.8. Populating /dev

6.8.1. Creating Initial Device Nodes

When the kernel boots the system, it requires the presence of a few device nodes, in particular the console and null devices. Create these by running the following commands:

mknod -m 600 /dev/console c 5 1
mknod -m 666 /dev/null c 1 3

然后您应该继续执行“6.8.2。安装 tmpfs 和填充 /dev”中的步骤。请注意下面的 <--,我建议您阅读整个 free LFS

mount -n -t tmpfs none /dev
mknod -m 622 /dev/console c 5 1
mknod -m 666 /dev/null c 1 3
mknod -m 666 /dev/zero c 1 5
mknod -m 666 /dev/ptmx c 5 2
mknod -m 666 /dev/tty c 5 0 # <--
mknod -m 444 /dev/random c 1 8
mknod -m 444 /dev/urandom c 1 9
chown root:tty /dev/{console,ptmx,tty}