I'm learning the mit6.858. In Lab1, I need to set up the lab environment on my M2 Mac using qemu (version 7.2.0 installed by homebrew).
I follow the instruction of the lab hints and run the course VM Image with this shell scripts:
#!/bin/bash
if ! command -v qemu-system-x86_64 > /dev/null; then
echo "You do not have QEMU installed."
echo "If you are on a Linux system, install QEMU and try again."
echo "Otherwise, follow the lab instructions for your OS instead of using this script."
exit
fi
# can we use the -nic option?
version=$(qemu-system-x86_64 --version \
| grep 'QEMU emulator version' \
| sed 's/QEMU emulator version \([0-9]\)\.\([0-9]\).*/\1.\2/')
major=$(echo "$version" | cut -d. -f1)
minor=$(echo "$version" | cut -d. -f2)
net=()
if (( major > 2 || major == 2 && minor >= 12 )); then
net=("-nic" "user,ipv6=off,model=virtio,hostfwd=tcp:127.0.0.1:2222-:2222,hostfwd=tcp:127.0.0.1:8080-:8080,hostfwd=tcp:127.0.0.1:8888-:8888")
else
net=("-netdev" "user,id=n1,ipv6=off,hostfwd=tcp:127.0.0.1:2222-:2222,hostfwd=tcp:127.0.0.1:8080-:8080,hostfwd=tcp:127.0.0.1:8888-:8888" "-device" "virtio-net,netdev=n1")
fi
qemu-system-x86_64 \
-m 2048 \
-nographic -serial mon:stdio \
"$@" \
# -enable-kvm \
"${net[@]}" \
6.858-x86_64-v22.vmdk
But I got this output:
SeaBIOS (version rel-1.16.1-0-g3208b098f51a-prebuilt.qemu.org)
iPXE (http://ipxe.org) 00:03.0 CA00 PCI2.10 PnP PMM 7EFD11A0 7EF311A0 CA00
Booting from Hard Disk...
Boot failed: could not read the boot disk
Booting from Floppy...
Boot failed: could not read the boot disk
Booting from DVD/CD...
Boot failed: Could not read from CDROM (code 0003)
Booting from ROM...
iPXE (PCI 00:03.0) starting execution...ok
iPXE initialising devices...ok
iPXE 1.20.1 (g4bd0) -- Open Source Network Boot Firmware -- http://ipxe.org
Features: DNS HTTP iSCSI TFTP AoE ELF MBOOT PXE bzImage Menu PXEXT
net0: 52:54:00:12:34:56 using 82540em on 0000:00:03.0 (open)
[Link:up, TX:0 TXE:0 RX:0 RXE:0]
Configuring (net0 52:54:00:12:34:56)...... ok
net0: 10.0.2.15/255.255.255.0 gw 10.0.2.2
Nothing to boot: No such file or directory (http://ipxe.org/2d03e13b)
No more network devices
No bootable device.
When I type ctrlA X
to quit, I got another lines of output.
QEMU: Terminated
./6.858-x86_64-v22.sh: line 30: -nic: command not found
My homebrew installation is correct. I'd like to know how to start the course VM correctly on M2 mac.
CodePudding user response:
So, at first glance this looked off-topic as not a programming question as such (running Qemu is more of a superuser.com or serverfault.com question, or perhaps apple.stackexchange.com as we're talking about running Qemu on macOS), but looking more closely, your problem appears to be a bash
scripting one, which makes it on topic again!
The clues
One thing you don't explicitly mention in your question is that you modified the script, attempting to comment out this line:
# -enable-kvm \
(The reason to remove that flag is because kvm is not available on macOS hosts, and the alternative, hvf, is not available when using binary translation to run an x86-64 VM on an arm64 host CPU.)
Another clue to the problem is this error:
./6.858-x86_64-v22.sh: line 30: -nic: command not found
What's happened here is that the backslashes (\
) at the end of each of these lines in the original script turn the multi-line block into a single line:
qemu-system-x86_64 \
-m 2048 \
-nographic -serial mon:stdio \
"$@" \
-enable-kvm \
"${net[@]}" \
6.858-x86_64-v22.vmdk
Unfortunately, when commenting a line with #
, bash ignores any backslash at the end of the line - this interrupts and splits the multi-line command.
This means that your networking and disk image command line options are not making it into the qemu command line, which in turn is why it can't find the virtual disk image. The -nic
error comes from treating the following as a new command:
"${net[@]}" \
6.858-x86_64-v22.vmdk
Solution:
Don't comment out the flag -enable-kvm \
in place: either remove the line entirely, or move it out of the command and comment it out there.