Virtualization

From Network Security Wiki


KVM Installation

Ubuntu/Debian

  • Check hardware support for Virtualization:
egrep -o '(vmx|svm)' /proc/cpuinfo
  vmx
  vmx
  • Install KVM:
sudo apt-get install qemu-kvm libvirt-bin virt-manager bridge-utils
kvm-ok
sudo kvm-ok
  • Add your user to the libvirtd group:
sudo adduser `id -un` libvirtd

then logout and log back in to activate your group membership.

  • Verify:
virsh -c qemu:///system list
  • The ID Name and State fields should empty
virt-manager -c qemu:///system kvmhost

CentOS / RedHat with Multiple Virtual Networks

Source: cyberciti.biz, computingforgeeks.com

  • Check Virtualisation support
lscpu | grep Virtualization
  • Install kvm
sudo yum install qemu-kvm libvirt libvirt-python libguestfs-tools virt-install
  • Start the libvirtd service:
sudo systemctl enable libvirtd
sudo systemctl start libvirtd
  • Verify kvm installation
lsmod | grep -i kvm
  • Configure bridged networking
brctl show
virsh net-list
virsh net-dumpxml default


  • Create KVM bridge with virsh command
vim br10.xml

Add bridge details to the file:

<network>
  <name>br10</name>
  <forward mode='nat'>
    <nat>
      <port start='1024' end='65535'/>
    </nat>
  </forward>
  <bridge name='br10' stp='on' delay='0'/>
  <ip address='192.168.30.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='192.168.30.50' end='192.168.30.200'/>
    </dhcp>
  </ip>
</network>
  • To define a network from an XML file without starting it, use:
sudo virsh net-define  br10.xml


  • To start a (previously defined) inactive network, use:
sudo virsh net-start br10
  • To set network to autostart at service start:
sudo virsh net-autostart br10
  • Check to Confirm if autostart flag is turned to yes – Persistent should read yes as well.
sudo virsh net-list --all
  • Confirm bridge creation and IP address.
ip addr show dev br10
  • Download a test server disk file:
wget https://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud.qcow2
  • Customize the login
virt-customize -a CentOS-7-x86_64-GenericCloud-2111.qcow2 --root-password password:DDYrTXJZTJldOqimb68ZK5KCmRpbdBOe

Now able to login to new guest as root / DDYrTXJZTJldOqimb68ZK5KCmRpbdBOe

  • Start the Test VM:
sudo virt-install \
--name=syslog \
--description="CentOS Syslog" \
--disk path=/var/lib/libvirt/images/CentOS-7-x86_64-GenericCloud.SYSLOG.qcow2,bus=virtio,format=qcow2 \
--network=bridge=br10,model=virtio \
--network=bridge=virbr1,model=virtio \
--graphics none \
--console pty,target_type=serial \
--vcpus=1 --cpu host --ram=1024 \
--os-type=linux \
--os-variant=rhel6 \
--import --autostart --noautoconsole
  • Access Console of the VM:
sudo virsh console syslog
  • Increase bash Columns & Rows
nano .bash_profile
 
res() {

  old=$(stty -g)
  stty raw -echo min 0 time 5

  printf '\0337\033[r\033[999;999H\033[6n\0338' > /dev/tty
  IFS='[;R' read -r _ rows cols _ < /dev/tty

  stty "$old"

  # echo "cols:$cols"
  # echo "rows:$rows"
  stty cols "$cols" rows "$rows"
}

res2() {

  old=$(stty -g)
  stty raw -echo min 0 time 5

  printf '\033[18t' > /dev/tty
  IFS=';t' read -r _ rows cols _ < /dev/tty

  stty "$old"

  # echo "cols:$cols"
  # echo "rows:$rows"
  stty cols "$cols" rows "$rows"
}

Migrate Disk from VirtualBox to KVM

VBoxManage clonehd --format RAW windowsxp.vdi windowsxp.img
qemu-img convert -f raw windowsxp.img -O qcow2 windowsxp

Test:

kvm -m 512 -usbdevice tablet -hda ubuntu.qcow

Convert VDI,IMG,VMDK HD image formats

VMDK to KVM

qemu-img convert testvm.vmdk testvm.img

KVM to Virtualbox

VBoxManage convertfromraw --format VDI testvm.img testvm.vdi

KVM to VMware

qemu-img convert -O vmdk testvm.img testvm.vmdk

Virtualbox to KVM

vboxmanage internalcommands converttoraw testvm.vdi testvm.img

Converting VMware Image To Physical Machine

Cloning of the CentOS VM to an unpartitioned HDD:

Requirements

VMware Workstation, Server or Player.
CloneZilla ISO image.
Unpartitioned HDD with enough space to hold your VM image.

VMware Configuration

Use CloneZilla ISO image to boot the VM. Add physical HDD as part of your existing VM and select the option Use entire disk.

CloneZilla Cloning Process

Start VMware and boot from CloneZilla image
Don't touch keymap
Select Start_clonezilla
Select device-device option
Select Beginner mode
Select disk to local disk
Select your source and target disk and click OK, then press Enter to continue.

A series of questions will appear. Answer yes to all this prompts. After the cloning process is finished power off the VM by pressing 0 on the CloneZilla menu. The cloning process is complete. Now install your new HDD to your physical machine.

Troubleshooting Physical Machine:

Filesystem corruption alert with the following error: Unexpected Inconsistency will occur. Login withroot password and run fsck -y.

Now fix the NIC by any one method:

cd /etc/udev/rules.d 
rm 70-persistent-net.rules
reboot

or

nano /etc/sysconfig/network-scripts/ifcfg-eth0
HWADDR=<MAC address of the network interface>
reboot

Shrink VirtualBox HDD

Windows guest

1. Perform a hard disk defragmentation.

2. Download sdelete and run

 sdelete -c c:/

3. Shutdown the VM. Shrinking the VM: In your terminal type the following:

 vboxmanage clonehd Winxp-old.vdi winxp-new.vdi

4. Replace the new HD with the old one in VM settings.

Ubuntu Guest

Source howtogeek.com

  • Ensure You’re Using a Dynamic Disk
  • Install Zerofree in Ubuntu VM:
sudo apt install zerofree
  • Start Ubuntu VM in Recovery mode by pressing "SHIFT" key during bootup
  • Drop to Root Shell Prompt
  • Run Zerofree:
df
zerofree -v /dev/sda1
  • Shutdown VM:
sudo poweroff
  • Shrink the Disk:
VBoxManage modifymedium disk Ubuntu\ Server.vdi --compact

Virtualbox Stop Host Time Sync with Guests

First, find the name of the virtual machine:

vboxmanage list vms

Automatic time sync can be disabled with this command:

VBoxManage setextradata "VM Name" "VBoxInternal/Devices/VMMDev/0/Config/GetHostTimeDisabled" "1"

Restart VM

Serial Port between Two VMs In VirtualBox

Host = Ubuntu 10.10 Guest= Windows XP and Debian Lenny

First Guest VM Serial Port Settings - Debian

Enable Serial Port check box: checked (enabled)
Port Number: COM1
Port Mode: Host Pipe
Create Pipe check box: checked (enabled)
Port/File Path: /tmp/vboxS0

Second Guest VM Serial Port Settings - Microsoft Windows XP

Enable Serial Port check box: checked (enabled)
Port Number: COM1
Port Mode: Host Pipe
Create Pipe checking box: unchecked (disabled)
Port/File Path: /tmp/vboxS0

Starting VirtualBox Machines

First start the VM with checked "Create Pipe" check box(Debian VM). Afterwards start the second machine(Windows XP VM). There will be an error if you try doing other way.


{{#widget:DISQUS |id=networkm |uniqid=Virtualization |url=https://aman.awiki.org/wiki/Virtualization }}