Skip to content

Instantly share code, notes, and snippets.

@yuvalif
Last active September 14, 2022 17:25
Show Gist options
  • Select an option

  • Save yuvalif/25c11e5b4efbb93239ff768c99fdb403 to your computer and use it in GitHub Desktop.

Select an option

Save yuvalif/25c11e5b4efbb93239ff768c99fdb403 to your computer and use it in GitHub Desktop.

Prerequisites

TODO

Create the VM

Use the "system" session by defaul:

export LIBVIRT_DEFAULT_URI="qemu:///system"

Create parameters for the setup:

export NAME=my-vm
export OSVERSION=fedora-35
export OSVARIANT=fedora35
export ARCHITECTURE=x86_64
export DISK_SIZE=30G
export CPUS=8
export RAM=8192
export POOL=vms
export POOL_DIR=~/$POOL
export DOMAIN="$NAME"-"$OSVERSION"-"$ARCHITECTURE"
export IMAGE_FILE_A="$NAME"-"$OSVERSION"-"$ARCHITECTURE"-sda.qcow2
export IMAGE_FILE_B="$NAME"-"$OSVERSION"-"$ARCHITECTURE"-sdb.qcow2

build the "vms" pool if does not exists:

mkdir -p $POOL_DIR

build an image file and store it locally:

virt-builder "$OSVERSION" -o "$POOL_DIR"/"$IMAGE_FILE_A" \
        --no-network --format qcow2 --arch "$ARCHITECTURE" --size "$DISK_SIZE" --root-password=password:fedora \
        --hostname "$NAME"

add another disk (needed for the storage software):

qemu-img create -f raw "$POOL_DIR"/"$IMAGE_FILE_B" 30G

add myself to the libvirt group:

sudo usermod --append --groups libvirt `whoami`

since the files are stored in the home directory, we want to give the "qemu" user access:

sudo setfacl -m u:qemu:rx $HOME

install the VM:

virt-install -n "$DOMAIN" --vcpus "$CPUS" --cpu host-passthrough,cache.mode=passthrough \
    --arch "$ARCHITECTURE" --memory "$RAM" --import --os-variant "$OSVARIANT" --controller scsi,model=virtio-scsi \
    --disk path="$POOL_DIR"/"$IMAGE_FILE_A",device=disk,bus=scsi,discard=unmap \
    --disk path="$POOL_DIR"/"$IMAGE_FILE_B",device=disk,bus=scsi,discard=unmap \
    --network network=default,model=virtio \
    --graphics spice --channel unix,name=org.qemu.guest_agent.0 --noautoconsole --noreboot

start the VM:

virsh start my-vm-fedora-35-x86_64

open console to the VM, to allow for root ssh using password (password is "fedora"):

virsh console "$DOMAIN"

edit /etc/ssh/sshd_config and set:

PermitRootLogin yes

then restart ssh service:

systemctl restart sshd

get its IP address:

MAC=$(virsh dumpxml ${NAME}-${OSVERSION}-${ARCHITECTURE} | grep 'mac address' | cut -d\' -f2)
virsh net-dhcp-leases default --mac ${MAC} | grep ipv4 | awk '{print $5}' | cut -d/ -f1

and ssh to the VM as root

Misc

create a snapshot only for main disk (e.g. after installing microshift):

virsh snapshot-create-as --domain $DOMAIN --name "$DOMAIN-microshift" --atomic  --disk-only  --no-metadata  --diskspec sda

TODOs

  • in virt-builder define a non-root user with sudo privilages and a password so we skip the step to allow ssh to root
  • use static IP, so it does not change:
virsh net-update default add ip-dhcp-host \
          "<host mac='$MAC' \
           name='$NAME' ip='$IP' />" \
           --live --config
@yuvalif
Copy link
Author

yuvalif commented Sep 14, 2022

if iso is manually downloaded, e.g. Fedora-Server-netinst-x86_64-36-1.5.iso is downloaded to the iso directory. use:

virt-install -n "$DOMAIN" --vcpus "$CPUS" --cpu host-passthrough,cache.mode=passthrough \
    --arch "$ARCHITECTURE" --memory "$RAM" --cdrom ./iso/Fedora-Server-netinst-x86_64-36-1.5.iso --controller scsi,model=virtio-scsi \                                                                                                        
    --disk path="$POOL_DIR"/"$IMAGE_FILE",device=disk,bus=scsi,discard=unmap \
    --network network=default,model=virtio \
    --graphics spice --channel unix,name=org.qemu.guest_agent.0 --noautoconsole --noreboot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment