Linux Tutorials

How one can create VM utilizing the qcow2 picture file in KVM

SEOClerks

I have picture.qcow2 file. How do I set up the qcow2 picture offered within the qcow2 format? How do I create Linux KVM VM from offered picture (say picture.qcow2) file?

Introduction: qcow is nothing however a disk file format utilized by QEMU. qcow is an acronym for QEMU Copy On Write. There are two variations of the file format: .qcow and .qcow2. Naturally, it’s essential to use qcow2. It’s an up to date model of the qcow format. This web page reveals find out how to use Linux KVM to import the .qcow2 picture and create a VM from it.

How one can create VM utilizing the qcow2 in Linux KVM

Steps to import qcow2 to create VM on Linux:

Obtain cloud picture similar to rhel-Eight.Zero-beta-1-x86_64-kvm.qcow2 or CentOS-7-x86_64-GenericCloud.qcow2Create meta-data and user-data file for KVM VMCreate disk picture to create a brand new VMImport qcow2 to create VM in KVMCreate VM and confirm by by log in into the VM utilizing the ssh command

I’m assuming that you’ve already obtain cloud picture and arrange and configure KVM. If not see:

Allow us to see all steps in particulars.

Setup required directories and config recordsdata

Kind the next mkdir command:
# D=/var/lib/libvirt/photos ## <-- Dir to retailer new VM ##
# VM=nixcraft-rhel8 ## <-- set vm title ##
# mkdir -vp $D/$VM

meta-data file for KVM VM

You should create meta-data file utilizing a textual content editor similar to vi command to setup occasion hostname and ID:
# cd $D/$VM
# vi meta-data
Append the next:

instance-id: nixcraft-rhel8
local-hostname: nixcraft-rhel8

Save and shut the file in vi/vim textual content editor.

user-data file for KVM VM

Subsequent, it is advisable to modify pre constructed cloud photos as per your setup. For instance, add customers, ssh keys, setup time zone, and extra utilizing Cloud-init. Allow us to see find out how to create RHEL Eight (beta) vm with 2048MB ram, 60GB disk area, 2 vCPU, with person named vivek and ssh keys for login. Create a brand new file named user-data:
# cd $D/$VM
# vi user-data

#cloud-config
# Customise as per your want. A minimum of change username (vivek) and ssh-ed22519
# key along with your precise public key
 
# Hostname administration
preserve_hostname: False
hostname: nixcraft-rhel8
fqdn: nixcraft-rhel8.nixcraft.com
 
# Setup Customers with ssh keys in order that I can log in into new machine
customers:
– default
– title: vivek
teams: [‘wheel’]
shell: /bin/bash
sudo: ALL=(ALL) NOPASSWD:ALL
ssh-authorized-keys:
– ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIG+zTQJ5FBsD2dTPlPVvQUbMq3jt19HflOtAy1EPBmKw KVM VM Lab SSH Login key for person
 
# Configure the place output will go
output:
all: “>> /var/log/cloud-init.log”
 
# configure interplay with ssh server
ssh_genkeytypes: [‘ed25519’, ‘rsa’]
 
# Set up your public ssh key to the primary user-defined person configured
# in cloud.cfg within the template (non-compulsory since I created vivek)
ssh_authorized_keys:
– ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIG+zTQJ5FBsD2dTPlPVvQUbMq3jt19HflOtAy1EPBmKw KVM VM Lab SSH Login key for person
 
# set timezone for VM
timezone: Asia/Kolkata
 
# Take away cloud-init
runcmd:
– systemctl cease community && systemctl begin community
– yum -y take away cloud-init

Copy cloud picture

I’ve rhel-Eight.Zero-beta-1-x86_64-kvm.qcow2 saved at /var/lib/libvirt/boot:
# cd $D/$VM
# cp -v /var/lib/libvirt/boot/rhel-Eight.Zero-beta-1-x86_64-kvm.qcow2 $VM.qcow2
Pattern outputs:

‘/var/lib/libvirt/boot/rhel-Eight.Zero-beta-1-x86_64-kvm.qcow2’ -> ‘nixcraft-rhel8.qcow2’

Create 60GB disk picture for brand spanking new KVM VM

Allow us to create 60GB picture named ‘nixcraft-rhel8.new.picture’ to retailer our new VM:
# cd $D/$VM
# export LIBGUESTFS_BACKEND=direct
# qemu-img create -f qcow2 -o preallocation=metadata $VM.new.picture 60G
# ls -l
I’m going to make use of virt-resize command to resize a digital machine disk referred to as nixcraft-rhel8.new.picture:
# virt-resize –quiet –expand /dev/sda1 $VM.qcow2 $VM.new.picture
# ls -l
Lastly, overwrite newly resized picture nixcraft-rhel8.new.picture as nixcraft-rhel8.qcow2 utilizing the mv command
# mv -v $VM.new.picture $VM.qcow2
# ls -l
Create VM images using the qcow2 Image FileClick on to enlarge picture

Making a cloud-init ISO and a pool for our RHEL Eight (beta) VM

Create ISO9660 file from our user-data and meta-data:
# cd $D/$VM
# mkisofs -o $VM-cidata.iso -V cidata -J -r user-data meta-data
Lastly, create a storage pool for our KVM VM:
# virsh pool-create-as –name $VM –type dir –target $D/$VM
# ls -l
How to install the qcow2 image provided in the RHEL 8 downloads

How do I set up the rhel-Eight.Zero-beta-1-x86_64-kvm.qcow2 picture offered within the RHEL Eight downloads?

Now all it’s a must to do is set up VM by importing newly created nixcraft-rhel8.qcow2 picture, run:
# cd $D/$VM
# virt-install –import –name $VM
–memory 2048 –vcpus 2 –cpu host
–disk $VM.qcow2,format=qcow2,bus=virtio
–disk $VM-cidata.iso,machine=cdrom
–network bridge=virbr0,mannequin=virtio
–os-type=linux
–os-variant=rhel7.5
–graphics spice
–noautoconsole
Create VM using the qcow2

Delete undesirable recordsdata:

Use the rm command to take away recordsdata:
# cd $D/$VM
# virsh change-media $VM hda –eject –config
# rm -vi meta-data user-data *.iso

How one can discover out IP deal with of KVM VM offered by DHCP

# virsh net-dhcp-leases default
# virsh net-dhcp-leases default | grep $VM | awk ”
192.168.122.229/24

Log in to your RHEL Eight (beta) VM

All it’s a must to do is use the ssh command as follows:
$ ssh vivek@192.168.122.229
RHEL 8 cloud image screenshot

Conclusion

So there you could have it, RHEL Eight put in and operating picture offered within the RHEL Eight (beta) downloads part.

Posted by: Vivek Gite

The writer is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a coach for the Linux working system/Unix shell scripting. Get the newest tutorials on SysAdmin, Linux/Unix and open supply subjects by way of RSS/XML feed or weekly e mail e-newsletter.

Source link

Related Articles

Back to top button