OK, I'm in :) Now I need to either mount this VM image on my host and/or access the VM from LAN. I tried to add an addr to VM's NIC and set routing through my host but I cannot ping anything, localhost included. Of course eth0 is UP! So I guess I need to start again with a proper -net setup for QEMU. ATM it is -net nic,model=virtio.
apk info | wc -l
28
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Excellent! You've got the VM running. Now let's tackle connectivity and file access.
First, a critical diagnostic point:
I cannot ping anything, localhost included.
This is a big clue. Failing to ping localhost (127.0.0.1) means the problem is inside your guest VM's operating system configuration, specifically with the loopback interface (lo). This has nothing to do with QEMU's networking yet. The lo interface should always be up and available.
Fixing Ping Localhost (Inside the VM):
Boot your Alpine VM.
Log in as root.
Check the status of your interfaces:
sh
# Inside the VM
ip addr
You will probably see that lo is DOWN or doesn't have an IP. Fix it:
sh
# Bring the loopback interface up
ip link set lo up
# Add the standard localhost address
ip addr add 127.0.0.1/8 dev lo
Now, test again:
sh
ping localhost
This should work. If it doesn't, something is very wrong with the base OS installation. The above commands should fix it. To make it permanent, you'd edit /etc/network/interfaces in your Alpine guest.
Now, let's address your two main goals. You can choose whichever method suits you best.
Goal 1: Access the VM from your LAN (Bridged Networking)
This is the most common and powerful method. It makes your VM appear as another physical computer on your network, getting its own IP address from your LAN's DHCP server (e.g., your router).
We will create a "network bridge" on your host. Think of it as a virtual network switch. We'll unplug your host's physical NIC (eth0) from the kernel's IP stack and plug it into this virtual switch. Then, we'll plug the VM's virtual NIC into the same switch.
Host Setup (on your Alpine Host):
Install Bridge Utilities:
bash
# On the host
sudo apk add bridge
Create a Bridge Configuration Script: This is the cleanest way to manage it. We'll create a helper script that QEMU will call automatically to set up the network for the VM.
Create a file named /etc/qemu-ifup:
bash
sudo nano /etc/qemu-ifup
Paste the following content into it:
sh
#!/bin/sh
# QEMU network bridge helper script
# Name of the bridge interface
BRIDGE="br0"
# Check if bridge exists, create if not
if ! ip link show "$BRIDGE" >/dev/null 2>&1; then
echo "Bridge $BRIDGE does not exist. Please set it up first."
exit 1
fi
echo "Bringing up TAP device $1..."
# Bring up the TAP device (created by QEMU)
ip link set "$1" up
echo "Adding TAP device $1 to bridge $BRIDGE..."
# Add the TAP device to our bridge
brctl addif "$BRIDGE" "$1"
# All good
exit 0
Make the script executable:
bash
sudo chmod +x /etc/qemu-ifup
Configure the Bridge on the Host:
Identify your main physical network interface (e.g., eth0, enp3s0). Use ip addr to find it. Let's assume it's eth0.
Now, run these commands to create the bridge and move your host's IP configuration to it.
bash
# On the host
# 1. Create the bridge device
sudo brctl addbr br0
# 2. Add your physical NIC to the bridge
sudo brctl addif br0 eth0
# 3. Bring the bridge interface up
sudo ip link set br0 up
# 4. CRITICAL: Move the IP address from eth0 to br0.
# You can use DHCP or set a static IP. Let's use DHCP for simplicity.
# First, clear the IP from the physical NIC.
sudo ip addr flush dev eth0
# Now, get a new IP for the bridge from your router.
sudo dhcpcd br0
After this, your host should still have internet access, but via br0 instead of eth0. Verify with ip addr.
Relaunch the VM with a TAP Network Device:
Now, modify your QEMU command. We replace the simple -net options with a more explicit configuration that uses our qemu-ifup script.
-netdev tap,id=net0,script=/etc/qemu-ifup,downscript=no: Defines a "tap" backend network device. QEMU will create an interface (like tap0) and run our /etc/qemu-ifup script to configure it.
-device virtio-net-pci,netdev=net0: This creates the virtio network card inside the VM and connects it to the net0 backend we just defined.
Configure Networking inside the VM:
Once the VM boots, log in and configure its network to get an IP from your LAN router, just like any other computer.
sh
# Inside the VM
setup-interfaces # Run the Alpine network setup utility
# Or edit /etc/network/interfaces to use DHCP on eth0
# Then run:
service networking restart
Your VM should now get an IP address from your router and be fully accessible from any other machine on your LAN.
Goal 2: Mount the VM Image on your Host
This allows you to directly access the VM's filesystem from the host. This is very useful for copying files or making configuration changes while the VM is powered off.
IMPORTANT: Never mount the image while the VM is running. This will lead to filesystem corruption.
The my_vm_disk.img file is not a filesystem itself; it contains a partition table (like a real hard drive). We need to access the partitions within the file.
Find the Partition Offset: First, find out where the root partition starts inside the image file.
bash
# On the host
fdisk -l my_vm_disk.img
The output will look something like this:
code
Disk my_vm_disk.img: 10 GiB, 10737418240 bytes, 20971520 sectors
Units: sectors of 1 * 512 = 512 bytes
...
Device Boot Start End Sectors Size Id Type
my_vm_disk.img1 * 2048 526335 524288 256M 83 Linux
my_vm_disk.img2 526336 20971519 20445184 9.8G 83 Linux
We need the Start sector of the partition we want to mount (e.g., my_vm_disk.img2 which is likely the root partition) and the Units (sector size, usually 512 bytes).
The offset in bytes is: Start_Sector * Sector_Size.
In our example: 526336 * 512 = 269484032.
Mount the Partition using a Loop Device with an Offset:
bash
# On the host
# 1. Create a directory to mount to
mkdir -p /mnt/my_vm
# 2. Calculate the offset (as shown above)
OFFSET=269484032
# 3. Mount the partition from the image file
sudo mount -o loop,offset=$OFFSET my_vm_disk.img /mnt/my_vm
Access the Files: You can now browse the VM's filesystem.
bash
ls -l /mnt/my_vm
# You'll see etc/, home/, root/, etc.
Unmount When Done (CRITICAL!):
Before you start the VM again, you must unmount the filesystem.