Showing posts with label Virtualization. Show all posts
Showing posts with label Virtualization. Show all posts

Monday, January 16, 2023

Creating and Starting KVM Virtual Machine: Basic Steps

This is just a note for docummenting the basic steps to create and start KVM virtual machines on Linux systems

  1. Make a plan for virtual machine resources. For this, we should query host resources.
    
        # show available disk spaces
        df -h
        # show available memory
        free -m
        # CPUs
        lscpu
        
  2. Assume we are installing an Ubuntu server system. We shall download the ISO image for the system, e.g.,
    
        wget \
          https://releases.ubuntu.com/22.04.1/ubuntu-22.04.1-live-server-amd64.iso \
          -O /var/lib/libvirt/images/ubuntu-22.04.1-live-server-amd64.iso
        
  3. Create a virtual disk for the virtual machine, e.g.,
    
        sudo truncate --size=10240M /var/lib/libvirt/images/officeservice.img
        
  4. Decide how we should configure the virtual machine network. First, we query existing ones:
    
        virsh --connect qemu:///system  net-list --all
        
  5. Now create a virtual machine and set up Ubuntu Linux on it, e.g.,
    
        sudo virt-install --name ubuntu \
        --description 'Ubuntu Server LTS' \
        --ram 4096 \
        --vcpus 2 \
        --disk path=/var/lib/libvirt/images/officeservice.img,size=10 \
        --osinfo detect=on,name=ubuntu-lts-latest \
        --network network=default \
        --graphics vnc,listen=127.0.0.1,port=5901 \
        --cdrom /var/lib/libvirt/images/ubuntu-22.04.1-live-server-amd64.iso  \
        --noautoconsole \
        --connect qemu:///system
        
  6. Suppose that you connect to Linux host via ssh via a Windows host. We cannot directly access the console of the virtual machine (that is at 127.0.0.1:5901 via VNC). In this case, we tunnel to the Linux host (assume its host name is LinuxHost) from the Windows host:
    
        ssh -L 15901:localhost:5901 LinuxHost
        
  7. We can now access the control via a VNC Viewer at the Windows host at localhost:15901.
  8. Once Ubuntu installation is over, we would lose the VNC connectivity. But, we can list the virtual machine created.
    
        sudo virsh --connect qemu:///system list --all
        
  9. To start the virtual machine, we run
    
        sudo virsh --connect qemu:///system  start ubuntu
        
  10. To make the virtual machine to start when we boot the host, set the virtual machine to be autostart, e.g.,
    
    	virsh --connect qemu:///system autostart ubuntu
    	

References

  1. https://docs.fedoraproject.org/en-US/quick-docs/getting-started-with-virtualization/
  2. https://ubuntu.com/blog/kvm-hyphervisor
  3. https://askubuntu.com/questions/160152/virt-install-says-name-is-in-use-but-virsh-list-all-is-empty-where-is-virt-i
  4. https://www.cyberciti.biz/faq/rhel-centos-linux-kvm-virtualization-start-virtual-machine-guest/
  5. https://www.cyberciti.biz/faq/howto-linux-delete-a-running-vm-guest-on-kvm/

Monday, November 30, 2015

Cannot Start VirtualBox Virtual Machine after an Upgrade

Having upgraded Oracle Virtualbox on a Windows host, I found that I could not start any Virtual Machine instances. VirtualBox displays a dialogue window with the error message shown below,



Examining the virtual machine log file, i.e., Logs\VBoxHardening.log in the Virtual Machine instance directory, I found the following error message near the end of the log file,

1660.6c38: supR3HardenedVmProcessInit: Opening vboxdrv stub...
1660.6c38: Error opening VBoxDrvStub:  STATUS_OBJECT_NAME_NOT_FOUND
1660.6c38: supR3HardenedWinReadErrorInfoDevice: NtCreateFile -> 0xc0000034
1660.6c38: Error -101 in supR3HardenedWinReSpawn! (enmWhat=3)
1660.6c38: NtCreateFile(\Device\VBoxDrvStub) failed: 0xc0000034 STATUS_OBJECT_NAME_NOT_FOUND (0 retries)

Driver is probably stuck stopping/starting. Try 'sc.exe query vboxdrv' to get more information about its state. Rebooting may actually help.

The message indicates that Windows service vboxdrv, i.e., the Virtual Box Driver has some problem. Following the advice in the log, I issue sc.exe query vboxdrv in a Windows Command Prompt window,

C:\>sc start vboxdrv
[SC] StartService: OpenService FAILED 1060:

The specified service does not exist as an installed service.
C:\>

which indicates that Windows service vboxdrv does not exist. To fix the issue, I reinstalled the driver vboxdrv from the directory C:\Program Files\Oracle\VirtualBox\drivers\vboxdrv. From the Windows GUI, you can right click the VBoxDrv.inf and choose "install".
If the installation is successful and you query the driver status again, you should see something similar below,

C:\>sc query vboxdrv

SERVICE_NAME: vboxdrv
        TYPE               : 1  KERNEL_DRIVER
        STATE              : 4  RUNNING
                                (STOPPABLE, NOT_PAUSABLE, IGNORES_SHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0
C:\>

Now I can launch the virtual machine instances.

Tuesday, July 22, 2014

After Kernel Upgrade VMware Ubuntu Guest Hang During Boot

I am running a Ubuntu Saucy (13.04) guest and later a Ubuntu Trusty (14.04) guest using VMWare Player on Windows 8.1. What has troubled me is that the guest hangs every single time after the POST screen is shown during boot after I upgraded the kernel. I have to press "Ctrl-Alt-Del" to restart, enter "Advanced Option" from the Ubuntu boot menu and choose an old kernel to boot. I finally figured out how to solve the problem.  The hint of the solution came from my examining the VM log file. The location of the VM log file is on the VMware Player's "About VMware Player" window. From VMWare Player, you can open the window by clicking on the "Player" menu, next "Help", and then "About VMware Player". Below is an example of the "About VMware Player" window.

 

In the above example, the VM log file is at C:\Users\...\Ubuntu\vmware.log. When the Ubuntu is hangs, at the end of the VM log file, I saw some log messages similar to the below,

2014-07-22T19:21:19.299-04:00| vmx| I120: GuestRpcSendTimedOut: 
                                          message to toolbox timed out.
2014-07-22T19:21:19.299-04:00| vmx| I120: Vix: [5648 guestCommands.c:1924]: 
                                          Error VIX_E_TOOLS_NOT_RUNNING in 
VMAutomationTranslateGuestRpcError(): VMware Tools are not running in the guest
2014-07-22T19:21:25.635-04:00| vmx| I120: USBGW: Write arbitrator op:13 len:11

My guess is that the VMware Tools is not installed and configured properly for the new Linux kernel. The solution would to reinstall the VMware Tools. Since the Ubuntu guest hangs during boot, I have to find an alternate to do it. Luckily, I can boot the Ubuntu guest into the "Recovery Mode" with the problematic Linux kernel via the "Advanced Option" during boot. 

I observed two difficulties when I tried to reinstall the VMware Tools in the "Recovery Mode".
  1. When I tried to install the VMware Tools when I switched to shell in the "Recovery Mode" by running <code>sudo ./vmware-install.pl</code>, I observed many warning messages similar to below,
    
    The /tmp directory may be under attack
    


  2. You may have to download the VMware Tools, for which, we need to enable network in the "Recovery Mode". 

To address the above difficulties, my solutions is,

  1. In the "Recovery Mode", first enable "network" from the list of options.
  2. Then go to shell (by choosing "Drop to root shell  prompt").
  3. Start the sshd. This is a two-step procedure.

    
    mkdir /var/run/sshd
    /usr/sbin/sshd
    
    
  4. Now, figure out the IP address of the guest (e.g., using ifconfig)
  5. Log onto the virtual machine using a Secure Shell client as a regular user with sudo privilege.
  6. Install the VMware player as the regular user using sudo.
  7. Reboot. The the problem went away.

Sunday, September 29, 2013

Convert VirtualBox VDI to VMWare VMDK

You can run virtual machine instances created in one virtualization software in another. QEMU is a good tool to do this. The following example converts a VirtualBox VDI file to a VMDK file that can be loaded in VMWare.

I did the conversion on a Windows host. You can download the precompiled Windows binaries of QEMU, provided by Prashant Satish,  or those provided by Eric Lassauge.

The procedure is the same in Linux hosts.

  1. To convert VirtualBox virtual appliance to a VMDK file.
    qemu-img convert  virtualbox.vdi  -O vmdk vmware.vmdk
  2. If you have VMWare Workstation or more luxury version, you can start with creating a new virtual machine. During the process, you have the option of selecting an "existing disk". In this case, you simply select the .vmdk file.

    However, I would like to stick to the free VMWare Player that does not provide the option of selecting an "existing disk". I have to use a workaround.

    Start VMWare Player, then create a new virtual machine, for instance, a virtual machine called "vboxvmware".  You must select "I will install the operating system later." option as the two images show. 



    Since you created or downloaded the VirtualBox virtual machine, you should know what guest operating system it is and what memory capacity is allocated. Choose them accordingly.



    Next step, choose the option of "Store Virtual Disk as a Single File".


    Upon confirming your choice, you will see that the virtual machine is created.



    The last step is to edit the VMWare virtual machine configuration file (.vmx file). Go to the directory where the virtual machine is located, locate the .vmx file, and edit the file using your favorite text editor. In the editor, locate the line of "scsi0:0.fileName = ..." and replace the virtual disk file by the .vmdk file previously converted using QEMU. Note that when you edit the file, make sure the path of the .vmdk file is correct if it is not copied to the directory. Then, you can safely remove the vboxvwmare.vmdk file that your virtual machine does not really use. You can then safely launch the virtual machine from the summary screen.