QEMUTips

Running Debian on macOS 12 (Monterey)

  1. Install qemu from MacPorts

  2. Create a disk image:

    $ qemu-img create -f qcow2 debian.qcow2 15G
    
  3. 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 and NetdevVmnetHostOptions in the Qemu docs for other macOS networking options, e.g:

    -nic vmnet-shared,isolated=true
    
  4. Run using the newly created image using the above command but omitting the -cdrom option.

References

Running FreeBSD

  1. Download qcow2 images from the Get FreeBSD site. It may be best to choose one of the STABLE images. Links for the qcow2 images are shown next to 'VM images'.

  2. Create a copy of the image, e.g.

    $ xz -dk FreeBSD-13.2-RELEASE-amd64.qcow2.xz
    
  3. 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.

  4. 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.

  5. Login as root without a password

  6. Enable SSH for subsequent restarts:

    # sysrc sshd_enable="YES"
    
  7. Manually start the SSH server:

    # service sshd start
    
  8. Set a password for the root user:

    # passwd
    
  9. Create a new user using the adduser command 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
    
  10. Optionally install the sudo package:

    # pkg install sudo
    

    or as the new user:

    $ su -l root -c 'pkg install sudo'
    
  11. Optionally, enable sudo for the wheel 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

  1. Download a qcow2 image from https://fedoraproject.org/server/download.

  2. Resize the image with sufficient space for your initial needs:

    $ qemu-img resize Fedora-Server-KVM-40-1.14.aarch64.qcow2 +10G
    
  3. 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.

  4. The first boot prompts you to setup an initial environment. Set the language and time zone appropriately and create a user with administrator access.

  5. 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.

  6. Resize the LVM physical volume:

    $ sudo pvresize /dev/vda3
    
  7. Extend the root logical volume by the amount the image size was increased by, e.g.

    $ sudo lvextend -L +10G /dev/sysvg/root
    
  8. Extend the xfs filesystem to fill the logical volume:

    $ sudo xfs_growfs /
    
  9. Check the available free space:

    $ df -h /
    
  10. 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.

See https://serverfault.com/questions/432119/is-there-any-way-to-shrink-qcow2-image-without-converting-it-raw

Automating a Debian Install

Some of a Debian install can be automated using a preseed file.

  1. 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
    
  2. Optionally, run debconf-set-selections -c preseed.cfg in a Debian system to check the file for errors.

  3. Run a web-server for the directory containing the preseed.cfg file.

    $ python3 -m http.server --directory .
    
  4. Perform an install of Debian as described above, choose Advanced options, then choose the Automated install option.

  5. 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.

  1. Increase the size of the QEMU disk image, e.g. on the host:

    $ qemu-img resize debian.qcow2 +20G
    
  2. 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
    
  3. You may want to resize the monitor. In LXDE this can be done by running lxrandr. Press Alt-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. The Screen resolution application icon is then visible on the desktop. There is full documentation on the GParted Live CD website.

  4. Run lxterminal to get an interactive terminal window.

  5. Make sure the volume group is not active:

    $ sudo vgchange -a n
    
  6. 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.

  7. Use sudo pvdisplay to view the PV Name and check it using the whole of the resized partition. If not run pvresize, e.g. for a PV Name of /dev/vda5:

    $ sudo pvresize /dev/vda5
    
  8. 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 an ext3 or ext4 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

  1. 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