QEMUTips
Running Debian on macOS 12 (Monterey)
Install
qemu
from 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. Replaceen1
with whichever device is appropriate for your network connection. Runifconfig
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 usesudo
in that situation.See NetdevVmnetSharedOptions and NetdevVmnetHostOptions 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
-cdrom
option.
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
qcow2
images from the Get FreeBSD site. It may be best to choose one of theSTABLE
images. Links for theqcow2
images 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
root
without a passwordEnable SSH for subsequent restarts:
# sysrc sshd_enable="YES"
Manually start the SSH server:
# service sshd start
Set a password for the
root
user:# passwd
Create a new user using the
adduser
command interactively:# adduser
You can use the defaults for all questions other than
Username
. Add thewheel
group when prompted withInvite $USERNAME into other groups?
if you want the new user to be able to use thesu
command.If you need to add the user to a group later:
$ pw groupmod wheel -m $USERNAME
Optionally install the
sudo
package:# pkg install sudo
or as the new user:
$ su -l root -c 'pkg install sudo'
Optionally, enable
sudo
for thewheel
group:$ su -l root -i # chmod u+w /usr/local/etc/sudoers # sed -i '~' -r 's/# %wheel ALL=\(ALL:ALL\) ALL/%wheel ALL=(ALL:ALL) ALL/' \ /usr/local/etc/sudoers # exit $ sudo whoami
Fedora
Download a
qcow2
image 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 theResize
option. Select the maximum size available. Then choose theWrite
andQuit
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 +10G /dev/sysvg/root
Extend the
xfs
filesystem 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
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.cfg
file 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.cfg
in a Debian system to check the file for errors.Run a web-server for the directory containing the
preseed.cfg
file.$ python3 -m http.server --directory .
Perform an install of Debian as described above, choose
Advanced options
, then choose theAutomated install
option.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 theEsc
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-F2
to 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 resolution
application icon is then visible on the desktop. There is full documentation on the GParted Live CD website.Run
lxterminal
to 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 pvdisplay
to 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 vgdisplay
to 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 anext3
orext4
volume:$ 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
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:
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.
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