Created
January 9, 2021 19:13
-
-
Save oBuTr471b/74f08705af0131fc2a019ae5b7db5276 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# In The Name Of Allah . | |
# -- --- ---- -- ----- - | |
# Sat Aug 4 17:54:17 2018 | |
# Written By : zer0err0r . | |
# ======= == = ========= = | |
# Vanilla KVM CheatSheet Using CentOS7minimal With Ethernet Connection . | |
= a Remote [virt-manager] will be used to manage VMs if necessary . | |
= SELinux is enforcing and FirewallD is enabled . | |
= directory_based_pool will be used and its target will be [/home/zer0err0r/kvm/] . | |
= Remember that [Pool > Volume > VM] . | |
# Before We Start : | |
$ sudo -i # To configure as root . | |
$ egrep '(vmx|svm)' /proc/cpuinfo # To check if your CPU support virtualization or not . | |
$ lsmod | grep kvm # To check if kvm module is loaded or not . | |
$ yum install qemu-kvm qemu-img libvirt libvirt-client libvirt-python virt-install virt-top libguestfs-tools | |
$ usermod -a -G libvirt zer0err0r | |
$ systemctl enable --now libvirtd | |
$ exit # To exit root . | |
= Do [ssh-keygen] then [ssh-copy-id] to set a SSH passwordless authentication . | |
= Keep using or add an alias for [--connect qemu:///system] when using normal user . | |
= Use [--connect 'qemu+ssh://USER@IP/system'] for remote hypervisors . | |
$ exit | |
# Creating network bridge : | |
$ sudo -i | |
$ echo 'BRIDGE=br0' >> /etc/sysconfig/network-scripts/ifcfg-eth0 | |
$ vim /etc/sysconfig/network-scripts/ifcfg-br0 | |
DEVICE="br0" | |
BOOTPROTO=static | |
IPADDR=192.168.100.100 | |
NETMASK=255.255.255.0 | |
NETWORK=192.168.100.0 | |
GATEWAY=192.168.100.1 | |
DNS1=8.8.8.8 | |
IPV6INIT="yes" | |
IPV6_AUTOCONF="yes" | |
ONBOOT="yes" | |
TYPE="Bridge" # First letter is an upper-case letter . | |
DELAY="0" | |
$ systemctl restart NetworkManager | |
$ echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf | |
$ sysctl -p /etc/sysctl.conf | |
$ exit | |
# Creating a pool : | |
$ mkdir /home/zer0err0r/kvm | |
$ chmod o+x /home/zer0err0r | |
$ sudo yum -y install policycoreutils-python | |
$ sudo semanage fcontext --add -t virt_image_t '/home/zer0err0r/kvm(/.*)?' | |
$ sudo restorecon -R -v /home/zer0err0r/kvm | |
$ virsh pool-list --all # To verify current pools . | |
$ virsh pool-define-as my_pool dir - - - - /home/zer0err0r/kvm | |
$ virsh pool-build my_pool # To build the pool . | |
$ virsh pool-start my_pool | |
$ virsh pool-autostart my_pool | |
$ virsh pool-list --all # To verify current pools . | |
# Creating Volume : | |
$ virsh vol-create-as my_pool disk_1.qcow2 15G --format qcow2 # To create the pool . | |
$ virsh vol-info --pool my_pool vm1_disk1 | |
# Create the VM : | |
$ virt-install --network bridge:br0 --name vm1 --ram=1024 --vcpus=1 --disk "vol=my_pool/vm1_disk1.qcow2" --graphics vnc \ | |
--location /home/zer0err0r/kvm/cd.iso --extra-args="console=tty0 console=ttyS0,115200" --os-variant centos7.0 | |
# For windows : | |
$ virt-install --network bridge:br0 --name vm1 --ram=1024 --vcpus=1 --disk "vol=my_pool/vm1_disk1.qcow2" --graphics vnc \ | |
--cdrom /home/zer0err0r/kvm/cd.iso --os-variant win2k8 | |
# Connect to the VM : | |
= Either you use a remote [virt-manager, virt-viewer] to install it . | |
= Or you can use kickstart to install the system and a cli console . | |
= I prefer using [virt-manager] or [virt-viewer] from any other machine if you created the VM with vnc support, So : | |
$ virt-viewer --connect 'qemu+ssh://zer0err0r@vbox/system' | |
# Clone the VM : | |
$ virsh suspend vm1 # vm1 should be stopped . | |
$ virt-clone --original vm1 --name vm1-clone --file /home/zer0err0r/kvm/vm1-clone.img | |
= If vm1 uses a static network addressing, Then change the clone address and then resume the original . | |
$ virt-clone --auto-clone --original vm1 # To drop using [--disk, --name] . | |
= Dont warry, The cloned VM volume will be inside the same pool dir . | |
# Managing VMs : | |
$ virsh list --all # To list available VMs . | |
$ virsh dominfo vm1 # To show VM informations . | |
$ virt-top # Like [top] command for VMs . | |
$ virt-df vm1 # Show VM disk partitions, It may take a few moments . | |
$ virsh shutdown vm1 # Stop a VM . | |
$ virsh destroy vm1 # Force stop the VM . | |
$ virsh start vm1 # Start VM . | |
$ virsh reboot vm1 # Restart a VM . | |
$ virsh autostart vm1 # Mark VM for autostart, So VM will start automatically after host reboots . | |
$ virsh autostart --disable vm1 # Mark VM for manual start . | |
$ virsh list --all --autostart # Show autostarted VMs . | |
# Deleting VMs : | |
$ virsh list --all # To view running VMs . | |
$ virsh shutdown vm1 || virsh destroy vm1 # To stop the VMs . | |
$ virsh undefine vm1 # To undefine the VM configurations . | |
# Delete the Volume : | |
$ virsh vol-list my_pool | |
$ virsh vol-delete --pool my_pool vm1_disk1 | |
# Delete the pool if needed to : | |
$ virsh pool-list | |
$ virsh pool-destroy my_pool | |
$ virsh pool-undefine my_pool | |
# ===== == ==== ======== = | |
# Peace Be Up0n Muhammed . |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment