Skip to content

Latest commit

 

History

History
202 lines (148 loc) · 5.9 KB

Demo_VM.adoc

File metadata and controls

202 lines (148 loc) · 5.9 KB

Deployment of Cloud / Virt ready OS Images

If you’re running this demo code on a cloud hosted environment then simply select their pre-configured cloud ready image, and skip to the setup of the demo tooling using Ansible.

Follow these instructions to run a local VM under KVM + Libvirt.

Validated Images

For the latest validated images reference the VM deployment script.

Centos

Red Hat Enterprise Linux

  • rhel-server-7.9-x86_64-kvm.qcow2

  • rhel-8.5-x86_64-kvm.qcow2

  • rhel-baseos-9.0-x86_64-kvm.qcow2

The process I’m using to create these ephemeral VM environments is to use virt-customize to

  • create a working copy on write snapshot from the standard kvm qcow2 image

  • remove cloud-init

  • customize the image to set a simple password for the root user

  • inject an SSH pubkey for passwordless access

Ideally we should be using the cloud-init capabilities to inject a valid user and credentials, but this is a quick hack to speed up this demo deployment.

Deployment Customisation

We provide a sample local environment file local.env.sample. You will need to copy this to local.env and update the following variables to suit your local KVM environment

  • MY_USER to your usename so that your ssh pubkey can be utilised

  • BASE_DIR to the location you use to store your base cloud qcow2 images

  • VM_DIR to your working directory for running KVM instances

  • VM_BRIDGE to match the network you’re using

Scripted deployment of Centos / RHEL VM under KVM

We now have a fully scripted approach once you’ve specified the required overrides in your local.env file.

# For Centos 7/8
sudo  ./deploy_vm.sh create [centos7|centos8]

# For RHEL 7/8/9
sudo  ./deploy_vm.sh create [rhel7|rhel8|rhel9]

Manual Deployment Approach

# Replace these with the locations you use to store base images
# and running VM Images
BASE_DIR=/opt/Virtual/ISO
VM_DIR=/opt/Virtual/images

# Username you're using for general management so we can add SSH Keys
MY_USER=sellis

# For Centos 7
OS_VARIANT=centos7
BASE_IMAGE=CentOS-7-x86_64-GenericCloud-2009.qcow2

# For Centos 8
OS_VARIANT=centos8
BASE_IMAGE=CentOS-8-GenericCloud-8.4.2105-20210603.0.x86_64.qcow2

# For RHEL 7.9
OS_VARIANT=rhel7.9
BASE_IMAGE=rhel-server-7.9-x86_64-kvm.qcow2

# For RHEL 8.5
OS_VARIANT=rhel8.5
BASE_IMAGE=rhel-8.5-x86_64-kvm.qcow2

# Define our virtual environment
# Make sure the VM_BRIDE matches a bridge or nat network in your
# libvirt environment

VM_NAME=LEAPP2RHEL
VM_MEMORY=2048
VM_CPU=2
VM_BRIDGE=virbr1

VM_BASE=${BASE_DIR}/${BASE_IMAGE}
VM_IMAGE=${VM_DIR}/Leepp2RHEL.qcow2

# Create our working snapshot
qemu-img create -f qcow2 -F qcow2 \
-o backing_file=${VM_BASE} ${VM_IMAGE}

# If we're going to add an SSH Key we need to re-label due to SELinux
ADD_SSH_KEY=

[ -f /home/${MY_USER}/.ssh/id_rsa.pub ] && \
  ADD_SSH_KEY="--ssh-inject root:file:/home/${MY_USER}/.ssh/id_rsa.pub --selinux-relabel"

# Enable root password login and remove cloud-init
# Add our user ssh pubkey if available

virt-customize -a ${VM_IMAGE} \
 --root-password password:password --uninstall cloud-init \
 ${ADD_SSH_KEY} \
 --run-command 'echo PermitRootLogin yes >> /etc/ssh/sshd_config'

# Then Create the VM

virt-install --name ${VM_NAME} --memory ${VM_MEMORY} --vcpus ${VM_CPU} \
    --disk path=$VM_IMAGE,format=qcow2,bus=scsi,discard=unmap \
    --os-variant=${OS_VARIANT} \
    --controller=type=scsi,model=virtio-scsi \
    --connect qemu:///system \
    --virt-type kvm \
    --noautoconsole \
    --import \
    --network type=bridge,source=${VM_BRIDGE} --graphics vnc

virsh console ${VM_NAME}

Post deployment steps

The Cloud Ready Centos 7 & 8 images currently allow ssh login as root. If you’re working with a vanilla cloud image on AWS or Azure you might need to type the following via a remote console.

echo "PermitRootLogin yes" > /etc/ssh/sshd_config.d/01-permitrootlogin.conf
systemctl restart sshd

I also recommend adding an entry in ~/.ssh/config with the correct IP address for your VM which will simplify running the ansible playbook. If you’re using KVM you might see the IP address in the virsh console.

Another option is to query arp and look for new addresses on your bridge device

arp -n | grep ${VM_BRIDGE}

Or you can use the virtual console to login as the root user and confirm the hosts IP Address

Host leapp2rhel
Hostname 192.168.124.138
StrictHostKeyChecking no
UserKnownHostsFile /dev/null
User root

The setup script will try to inject your ssh pub-key, but if that isn’t working you should copy your ssh-key over so that Ansible can automate the rest of the setup

ssh-copy-id leapp2rhel

Environment Cleanup

I recommend you remove your RHSM and Insights entitlements before deleting any virtual machines

# Clean up all hosts in our inventory
ansible-playbook  -i hosts rhsm_unsubscribe.yaml

You can now use the deployment script to clean up your environment

sudo ./deploy_vm.sh cleanup

Alternatively if you’ve used the manual deployment approayc, and providing you’ve got the same shell open, you can quickly clean up the deployed environment via

virsh destroy ${VM_NAME}
virsh undefine ${VM_NAME}
rm ${VM_IMAGE}