QEMUTips
Running Debian on macOS 12 (Monterey)
Install
qemufrom MacPortsCreate a disk image:
$ qemu-img create -f qcow2 debian.qcow2 15G
Install from an ISO image:
$ sudo qemu-system-x86_64 -name MyTest
-M accel=hvf
-cpu host -smp cpus=2 -m 2G
-drive file=debian.qcow2,if=none,id=hd
-vga virtio -display cocoa,show-cursor=on
-device virtio-blk-pci,drive=hd
-usb -device usb-audio -device usb-tablet
-nic vmnet-bridged,ifname=en1
-cdrom debian-12.0.0-amd64-netinst.iso
The `-nic` option creates a bridged network with the VM. Replace `en1`
with whichever device is appropriate for your network connection. Run
`ifconfig` on the Mac to list the network interfaces. You can skip that
element if you don't want a bridged network. You also won't need to use
`sudo` in that situation.
See
[NetdevVmnetSharedOptions](https://www.qemu.org/docs/master/interop/qemu-qmp-ref.html#qapidoc-1450)
and
[NetdevVmnetHostOptions](https://www.qemu.org/docs/master/interop/qemu-qmp-ref.html#qapidoc-1446)
in the Qemu docs for other macOS networking options, e.g:
-nic vmnet-shared,isolated=true
- Run using the newly created image using the above command but
omitting the
-cdromoption.
References
- QEMU
- https://medium.com/code-uncomplicated/virtual-machines-on-macos-with-qemu-intel-based-351b28758617
- https://www.sobyte.net/post/2022-10/mac-qemu-bridge/
- http://www.redfelineninja.org.uk/daniel/2018/02/running-an-iso-installer-image-for-arm64-aarch64-using-qemu-and-kvm/
- https://wiki.freebsd.org/arm64/QEMU
- https://www.kraxel.org/blog/2019/09/display-devices-in-qemu/
Running FreeBSD
Download
qcow2images from the Get FreeBSD site. It may be best to choose one of theRELEASEimages, as these can be updated with thefreebsd-updateutility. Links for theqcow2images are shown next to ‘VM images’.Create a copy of the image, e.g.
$ xz -dk FreeBSD-13.2-RELEASE-amd64.qcow2.xz
Resize the image with sufficient space for your initial needs:
$ qemu-img resize FreeBSD-13.2-RELEASE-amd64.qcow2 +10G
On first boot, the image should call `growfs /` to expand the root
partition to fill the available space. If you expand the image
thereafter, boot into single-user mode and manually issue the command as
root.
Start the virtual machine, e.g.
$ qemu-system-x86_64
-name FreeBsd -M accel=hvf -cpu host -smp cpus=2 -m 2G
-drive file=FreeBSD-13.2-RELEASE-amd64.qcow2,if=none,id=hd
-vga virtio
-device virtio-blk-pci,drive=hd
-usb -device usb-tablet -device usb-audio -nic vmnet-bridged,ifname=en1
-display cocoa,show-cursor=on
You may need to modify the command to suit your environment, most likely
the last two lines for the network and display.
Login as
rootwithout a passwordEnable SSH for subsequent restarts:
sysrc sshd_enable=“YES”
Manually start the SSH server:
service sshd start
Set a password for the
rootuser:passwd
Create a new user using the
addusercommand interactively:adduser
You can use the defaults for all questions other than `Username`. Add the
`wheel` group when prompted with `Invite $USERNAME into other groups?` if
you want the new user to be able to use the `su` command.
If you need to add the user to a group later:
$ pw groupmod wheel -m $USERNAME
- Optionally install the
sudopackage:
# pkg install sudo
or as the new user:
$ su -l root -c 'pkg install sudo'
- Optionally, enable
sudofor thewheelgroup:
$ su -l root -i
# echo "%wheel ALL=(ALL:ALL) ALL" >/usr/local/etc/sudoers.d/wheel
# exit
$ sudo whoami
Fedora
Download a
qcow2image from https://fedoraproject.org/server/download.Resize the image with sufficient space for your initial needs:
$ qemu-img resize Fedora-Server-KVM-40-1.14.aarch64.qcow2 +10G
Start the VM using qemu, e.g.
This example is for an Apple Silicon device:
$ qemu-system-aarch64 \
-name trip-dev-fedora-40 -M virt,accel=hvf -cpu host -smp cpus=1 -m 2G \
-bios /opt/local/share/qemu/edk2-aarch64-code.fd \
-drive file=Fedora-Server-KVM-40-1.14.aarch64.qcow2,if=none,id=hd0 \
-device virtio-blk-pci,drive=hd0,bootindex=1 \
-device virtio-gpu-pci \
-nic vmnet-bridged,ifname=en0 \
-usb -device qemu-xhci -device usb-kbd -device usb-tablet \
-display cocoa,show-cursor=on,left-command-key=off,swap-opt-cmd=off
You may need to modify the command to suit your environment, most likely
the last two lines for the network and display.
The first boot prompts you to setup an initial environment. Set the language and time zone appropriately and create a user with administrator access.
Resize the disk partition on the guest. It may be
/dev/vda, e.g.$ sudo pvdisplay $ sudo cfdisk /dev/vda
Select the main `Linux LVM` partition. It may be `/dev/vda3`. Select the
`Resize` option. Select the maximum size available. Then choose the
`Write` and `Quit` options to save the changes.
Resize the LVM physical volume:
$ sudo pvresize /dev/vda3
Extend the root logical volume by the amount the image size was increased by, e.g.
$ sudo lvextend -l+100%FREE -r systemVG/LVRoot
Check the output of the previous command. LVM may have used xfs_growfs to fill the logical volume. If LVM did not, extend the
xfsfilesystem to fill the logical volume:$ sudo xfs_growfs /
Check the available free space:
$ df -h /
Create a user, e.g. optionally specifying the same user and group IDs as used on the host system:
adduser -u 1000 -g 1000 -G wheel -s /usr/bin/bash $USERNAME
Rocky Linux
- Download a boot ISO image from https://rockylinux.org/download
e.g. Rocky-10.0-aarch64-boot.iso
Create a disk image:
$ qemu-img create -f qcow2 rockylinux.qcow2 15G
Install from an ISO image:
This example is for an M2 MacBook Air with Qemu installed via MacPorts:
qemu-system-aarch64 \
-name rockylinux -M virt,accel=hvf -cpu host -smp cpus=2 -m 2G \
-bios /opt/local/share/qemu/edk2-aarch64-code.fd \
-drive file=rockylinux.qcow2,if=none,id=hd0 \
-device virtio-blk-pci,drive=hd0 \
-device virtio-gpu-pci \
-nic vmnet-bridged,ifname=en0 \
-usb -device qemu-xhci -device usb-kbd -device usb-tablet \
-cdrom Rocky-10.0-aarch64-boot.iso \
-display cocoa,show-cursor=on,left-command-key=off,swap-opt-cmd=off
The first boot prompts you to setup an initial environment. Set the language and time zone appropriately and create a user with administrator access. Rocky 10 with a minimal install failed to boot from Grub for some unknown reason, so it may be best to choose the default server install to start with.
Check the available free space. About 3G is lost to EFI System and Linux extended boot partitions, so the size should be shown as about 12G:
$ df -h /
If necessary, create a user, e.g. optionally specifying the same user and group IDs as used on the host system:
# adduser -u 1001 -g 1001 -G wheel -s /usr/bin/bash $USERNAME
Snapshots
Managing these snapshots must be done when the VM is offline.
Create a snapshot:
$ qemu-img snapshot -c snap01 debian.qcow2
List snapshots:
$ qemu-img snapshot -l debian.qcow2
Revert to a previous snapshot:
$ qemu-img snapshot -a snap01 debian.qcow2
Delete snapshot:
$ qemu-img snapshot -d snap01 debian.qcow2
Shrinking a qcow2 image
$ qemu-img convert -O qcow2 source.qcow2 shrunk.qcow2
Fully test the shrunk image before deleting the original source image.
Automating a Debian Install
Some of a Debian install can be automated using a
preseed file.
- Create a
preseed.cfgfile as described in the Debian Wiki.
You can create a `preseed` file from an existing installation with:
$ sudo apt-get install debconf-utils
$ echo "#_preseed_V1" | tee preseed.cfg
$ sudo debconf-get-selections --installer | tee -a preseed.cfg
$ sudo debconf-get-selections | tee -a preseed.cfg
Optionally, run
debconf-set-selections -c preseed.cfgin a Debian system to check the file for errors.Run a web-server for the directory containing the
preseed.cfgfile.$ python3 -m http.server –directory .
Perform an install of Debian as described above, choose
Advanced options, then choose theAutomated installoption.After the installer has installed support for networking, enter the url to the file on the host web server started in the earlier step. e.g.
http://192.168.1.100:8000/preseed.cfg.
All being well, the installation should complete and reboot into the newly installed system.
Examine /var/log/installer/syslog to debug any
installation failures.
Increasing an LVM Volume Size
Resizing is a risky operation. Make sure you have backups before proceeding.
Increase the size of the QEMU disk image, e.g. on the host:
$ qemu-img resize debian.qcow2 +20G
Boot from a live ISO image, e.g. https://www.debian.org/CD/live/ or if your QEMU image doesn’t have Internet access (needed to install GParted, which is missing from the Debian live CD) use the GParted Live ISO image, which is also a considerably smaller image size.
Use the `bootindex` property to boot by default from the live ISO image.
The `-boot menu=on` option can also be used to choose the boot device by
pressing the `Esc` key on the first boot screen. E.g.:
$ sudo qemu-system-x86_64 \
-name Bookworm -M accel=hvf -cpu host -smp cpus=2 -m 2G \
-drive format=qcow2,file=debian.qcow2,if=none,id=hd \
-device virtio-blk-pci,drive=hd \
-nic "vmnet-bridged,ifname=en1" \
-drive file=debian-live-12.2.0-amd64-lxde.iso,if=none,index=2,media=cdrom,id=cd \
-device virtio-blk-pci,drive=cd,bootindex=1 \
-vga virtio \
-usb -device usb-tablet -device usb-audio \
-display cocoa,show-cursor=on \
-boot menu=on
You may want to resize the monitor. In LXDE this can be done by running
lxrandr. PressAlt-F2to open the command window or use the menu. If using the GParted Live ISO image, you can minimise the GParted window by clicking on its icon in the bottom toolbar. TheScreen resolutionapplication icon is then visible on the desktop. There is full documentation on the GParted Live CD website.Run
lxterminalto get an interactive terminal window.Make sure the volume group is not active:
$ sudo vgchange -a n
Resize the partions using
gparted.$ sudo apt-get install gparted $ sudo gparted
If the partition is within an extended partition, you will need to grow
that one first. Then grown the partition containing the volume group.
Use
sudo pvdisplayto view the PV Name and check it using the whole of the resized partition. If not runpvresize, e.g. for a PV Name of /dev/vda5:$ sudo pvresize /dev/vda5
Use
sudo vgdisplayto see how much space is now free and extend any logical volumes as required. After extending the logical volume, activate the volume group and resize the underlying partition. E.g. for anext3orext4volume:$ sudo vgchange -a y $ sudo lvresize -L+20G /dev/mapper/debian–vg-root $ sudo e2fsck -f /dev/mapper/debian–vg-root $ sudo resize2fs /dev/mapper/debian–vg-root
Reboot into the QEMU image as normal and the additional space will be available.
Mounting Local Path
Using 9pfs
In the running guest, confirm the kernel has been compiled with 9pfs support. The following command should output something similar to the following:
$ sudo cat /boot/config-$(uname -r) | grep -E 'CONFIG_(NET_)?9P'
CONFIG_NET_9P=m
CONFIG_NET_9P_FD=m
CONFIG_NET_9P_VIRTIO=m
CONFIG_NET_9P_USBG=m
CONFIG_NET_9P_RDMA=m
# CONFIG_NET_9P_DEBUG is not set
CONFIG_9P_FS=m
CONFIG_9P_FSCACHE=y
CONFIG_9P_FS_POSIX_ACL=y
CONFIG_9P_FS_SECURITY=y
If the kernel has not been compiled with 9pfs support, you will need to compile a kernel image which includes the necessary modules. See https://wiki.qemu.org/Documentation/9psetup for further information.
To configure a shared folder, add the following to the QEMU startup command:
-virtfs local,path=/home/user/some_path,mount_tag=mymount,security_model=mapped-xattr,id=fs1
After starting the VM, you can manually mount the filesystem with:
$ sudo mkdir /mnt/destination
$ sudo mount -t 9p -o trans=virtio mymount /mnt/destination
Add the following entry to /etc/fstab to automatically
mount after restart:
mymount /mnt/destination 9p trans=virtio,msize=104857600 0 0
See also:
- https://wiki.qemu.org/Documentation/9psetup
- https://wiki.freebsd.org/VirtFS
- https://docs.freebsd.org/en/books/handbook/virtualization/
You’ll probably need to create a user on the VM with the same UID and GID as on the host system to avoid permission issues.
Using Virtio-FS
See https://virtio-fs.gitlab.io
See also https://forums.rockylinux.org/t/does-rocky-linux-include-9p-filesystem/15821/3
Trouble Shooting
- Incorrect keyboard mappings with host on macOS See also Keyboard layouts in lightweight virtualisation
References
– Frank Dean - 5 Oct 2023
Related Topics: DebianTips, InstallingMacPorts, LogicalVolumeManagement