我已经建立了一个模拟 vexpress-a9 机器的 QEMU 虚拟机。我使用 buildroot 来创建 initrd 系统,并使用 buildroot 下载的 codesourcery 交叉编译器作为编译 Linux 内核的设置的一部分。这就是我调用 qemu 的方式
qemu-system-arm -m 512 -M vexpress-a9
-dtb linux-4.4.192/arch/arm/boot/dts/vexpress-v2p-ca9.dtb
-kernel linux-4.4.192/arch/arm/boot/zImage
-initrd buildroot/output/images/rootfs.cpio
-append "console=ttyAMA0,ip=dhcp"
-net nic,model=lan9118
-net user,hostfwd=tcp::2222-:22
-nographic
它启动得很好。与网络相关的引导消息,表明网络已初始化并且 dropbear sshd 已启动并运行:
Starting network: OK
Starting dropbear sshd: random: dropbear: uninitialized urandom read (32 bytes read, 1 bits of entropy available)
OK
但是eth0
必须手动为接口分配 IP。我在与我的 Linux 主机相同的子网中为其分配了一个未使用的 C 类地址,如下所示:
# ifconfig eth0 192.168.160.150 up
smsc911x 4e000000.ethernet eth0: SMSC911x/921x identified at 0xa12a0000, IRQ: 31
所以ifconfig
现在显示:
# ifconfig
eth0 Link encap:Ethernet HWaddr 52:54:00:12:34:56
inet addr:192.168.160.150 Bcast:192.168.160.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
Interrupt:31
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
netstat
告诉我我们正在监听 22 端口:
# netstat -tln
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
我什至可以像这样通过 ssh 进入 localhost:
# ssh root@127.0.0.1
random: ssh: uninitialized urandom read (32 bytes read, 24 bits of entropy available)
random: dropbear: uninitialized urandom read (32 bytes read, 24 bits of entropy available)
random: dropbear: uninitialized urandom read (32 bytes read, 24 bits of entropy available)
random: dropbear: uninitialized urandom read (32 bytes read, 24 bits of entropy available)
Host '127.0.0.1' is not in the trusted hosts file.
(ecdsa-sha2-nistp256 fingerprint sha1!! de:cf:f8:b3:ac:4d:96:47:22:d7:76:a1:ea:11:3d:bc:ec:c6:71:9a)
Do you want to continue connecting? (y/n) y
root@127.0.0.1's password:
random: dropbear: uninitialized urandom read (32 bytes read, 26 bits of entropy available)
而从主机上,这是我尝试从 linux 主机 SSH 到该 VM 时看到的:
$ ssh -vvv root@192.168.160.150
OpenSSH_7.4p1 Debian-10+deb9u7, OpenSSL 1.0.2s 28 May 2019
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug2: resolving "192.168.160.150" port 22
debug2: ssh_connect_direct: needpriv 0
debug1: Connecting to 192.168.160.150 [192.168.160.150] port 22.
debug1: connect to address 192.168.160.150 port 22: No route to host
ssh: connect to host 192.168.160.150 port 22: No route to host
我很高兴有一些关于此的指示,因为我找不到其他可以尝试的方法
更新 1:在配置 buildroot 时包含 dhcpd。现在无论我使用 dropbear 还是 openssh,日志消息都会出现,直到
Starting dhcpcd...
no interfaces have a carrier <--- the problem
forked to background, child pid 746
smsc911x 4e000000.ethernet eth0: SMSC911x/921x identified at 0xa12a0000, IRQ: 31
Starting dropbear sshd: random: dropbear: uninitialized urandom read (32 bytes read, 2 bits of entropy available)
OK
random: dhcpcd: uninitialized urandom read (120 bytes read, 2 bits of entropy available)
此时它只是停止了,我看不到登录提示。删除dhcpd
并使用 dropbear 或 openssh 后,我可以再次看到登录提示(意味着所有初始化脚本都运行完成),但再次没有任何类型的连接。我不明白为什么 dhcpd 认为我们没有载体(当它能够枚举和识别模拟的 NIC 卡时)
UPDATE2:我尝试udhcpc
手动调用以通过 DHCP 获取地址,它确实获得了地址
# udhcpc -S -a100
udhcpc: started, v1.31.0
udhcpc: sending discover
udhcpc: sending select for 10.0.2.15
udhcpc: lease of 10.0.2.15 obtained, lease time 86400
deleting routers
adding dns 10.0.2.3
# ifconfig
eth0 Link encap:Ethernet HWaddr 52:54:00:12:34:56
inet addr:10.0.2.15 Bcast:10.0.2.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:4 errors:0 dropped:0 overruns:0 frame:0
TX packets:5 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:2360 (2.3 KiB) TX bytes:1428 (1.3 KiB)
Interrupt:31
然而,主机仍然无法访问该机器。我是否需要从主机添加到此 VM 的手动路由?如果是,我该怎么做?QEMU 是否在主机上创建虚拟接口(像工作站一样)?