Showing posts with label Virtual Machine. Show all posts
Showing posts with label Virtual Machine. 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/

Wednesday, September 27, 2017

Bhyve Exited to UEFI Shell

I have a FreeNAS host where I run a few Bhyve virtual machine (VM) instances. Upon upgrading FreeNAS, I found some VMs did not boot successfully. It turns out that the VMs exited to UEFI Shell, and complained that it could not locate some efi file. The solution that works for me is discussed in this post.

To summarize, I followed the two steps procedure as described in the post:
  1. Boot the VM into the guest operating system, in my case, CentOS 7, by (1) exiting the UEFI shell, (2) entering UEFI "Boot Maintenance Manager"; (3) choosing "Boot From File", and (4) browsing the directory and locating grubx64.efi under the folder named after the operating system, in my case, centos/grubx64.efi
  2. Copy the efi file to BOOT/BOOTX64.EFI

Tuesday, April 18, 2017

Heavy Disk Activity after Suspending a VMWare Virtual Machine Instance

I observed heavy disk activity when I suspended a VMware virtual machine instance. The VMware software is VMware Player 7.1.4 and the virtual machine instance is a Windows XP instance. The disk activity is so heavy that it almost renders the host machine non-responsive for a quite while. After some research online, I found these configuration options to be helpful,

mainMem.useNamedFile = "false"
mainMem.writeZeroes = "true"


You can apply these two configuration options in two different manners.
  • Apply them to a specific virtual machine instance by adding these two lines to the virtual machine configuration file, i.e., a .vmx file.
  • Apply them as the global and default options for all virtual machine instances by adding these two lines to VMware software's configuration file. For VMWare Player, it is %PROGRAMDATA%\VMWare\VMware Player\config.ini. If the file does not exist, you should create it. For other versions of VMware software, see this VMware KnowledgeBase article.

To understand why and how these may address the heavy disk activity issue and whether you should use these options, the following are a few excellent references that I came across in my research.
  1. VMware Workstation and Very High VMEM Fragmentation
  2. Maximize VMWare images performance
  3. Write Activity to .vmem file even though the VM is suspended
  4. Why does my PC slow down to a crawl when VMware Player is suspending to .vmem file?

Sunday, November 6, 2016

Mounting Shared Directory at the Host using Open VM Tools

Open VM Tools is a user space tools for Linux virtual machines. To mount a directory shared by the host to a VMware Linux guest, we can use vmhgfs-fuse.

First, we can figure out what has been shared at the host to the guest Linux system using vmware-hgfsclient, which is simply to enter the command in the Linux guest.

     vmware-hgfsclient

Assume that the output is shared_foo and we want to mount it at /mnt/hgfs/foo at the guest Linux system, we can run the following command at the Linux guest,

     vmhgfs-fuse .host:/shared_foo /mnt/hgfs/foo

To mount the directory at the boot time, we can add a line similar to the following to the /etc/fstab at the Linux guest,

     .host:/shared_foo /mnt/hgfs/foo fuse.vmhgfs-fuse allow_other 0 0

You can also add different mounting options. For instance, in the following, the shared directory will be shown as owned by user foo_user and group foo_group, and with umask=022 applied.

     .host:/shared_foo /mnt/hgfs/foo fuse.vmhgfs-fuse allow_other,uid=foo_user,gid=foo_group,umask=022 0 0

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, February 24, 2015

Creating Small Footprint Linux Virtual Machine - Revisited

Although many virtual machine appliances exist today and are conveniently to download, such as the VMWare Virtual Appliance Marketplace and the TurnKey Linux Virtual Appliance Library, it may be necessary to create a virtual machine image of your own and distribute it among the others. The issue is that a virtual machine image can easily become a monster and can be difficult to host, upload, download, and distribute such a large image. Previously, I attempted to create Linux virtual machine images with small footprint using the approach of installing the system and removing unnecessary packages. That approach leads to virtual machine images compressible to around 1GB archives.

Lately, I have opted to a second approach in which we install a bare bone Linux system and then install necessary packages. This approach, I found, often leads to smaller footprint than the previous approach.

The only caveat is that this approach may not work well with certain Linux distributions if the Linux distribution does not provide a means to install bare bone systems. If a Linux distribution does not provide a means to install the bare bone systems, it is likely to be a desktop edition, for instance, Ubuntu Linux's Desktop Edition has no option for a user to install a bare bone system during its installation process. A workaround is to create the system image using its corresponding server edition distribution, for instance, Ubuntu Linux's Server Edition allows you to install a bare bone system.

At present, I have a 64-bit Ubuntu Linux Server Edition image with PostGreSQL, Apache Web Server, and PHP but without GUI and the image is compressed to about 350MB archive.


Thursday, September 18, 2014

Creating Small Footprint Linux Virtual Machine

Although it may be easier to download a Linux virtual appliance, it can be fun to create linux virtual machines yourself. In particular, we may be interested in creating small footprint Linux virtual machines -- it would be easy to put it on a USB drive or let other people to download it. This post documents the process that I use to create Ubuntu 14.04 (Trusty) Linux virtual machines and reduce the image size of the virtual machines. The key idea is (1) to remove non-essential packages as much as possible; (2) to increase the compression of virtual machine images by zeroing virtual disks; and (3) to compact and compress the virtual machine image.
  1. Download 32-bit Ubuntu 14.04 (Trusty) ISO file and install it as a guest operating system under VMware Player or VirtualBox.  
  2. To make copy & paste instructions easier, install VMware Tools.  
    1. Install Linux headers and the build essential packages

      sudo apt-get install linux-headers-generic build-essential

    2. Then install VMware Tools
    3. Restart the virtual machine.
  3. By default, Ubuntu 14.04 runs the Ubuntu Unity desktop environment. It prevents us from entering the VMware Unity mode. I now get rid of the Ubuntu Unity desktop environment. I learned the command from this post.
    
    sudo apt-get purge \
         unity unity-common unity-services \
         unity-lens-\* unity-scope-\* unity-webapps-\* \
         gnome-control-center-unity \
         hud libunity-core-6\* libunity-misc4 \
         libunity-webapps\* appmenu-gtk \
         appmenu-gtk3 appmenu-qt\* overlay-scrollbar\* \
         activity-log-manager-control-center \
         firefox-globalmenu thunderbird-globalmenu \
         libufe-xidgetter0 xul-ext-unity \
         xul-ext-webaccounts webaccounts-extension-common \
         xul-ext-websites-integration \
         gnome-control-center gnome-session

  4. Then, install GNOME 3.
    
    sudo apt-get install gnome-session-fallback
    

  5. Now, remove the default games.
    
    sudo apt-get game\* purge ace-of-penguins gnome-mahjongg gnome-mines \
                 berusky biniax2 gnurobbo groundhog gtkballs hitori \
                 four-in-a-row gnect gbrainy gnome-sudoku chess\* \
                 nibbles\* solitaire\* iagno\* klotski\* tetravex\* \
                 drascula gtkboard oneisenough openyahtzee ri-li \
                 scribble swell-foop quadrapassel\* lightsoff \
                 five-or-more gnome-robots gtali
    

  6. Remove a few network applications.
    
    sudo apt-get purge webbrowser-app thunderbird\*
    

  7. Remove Libre Office. This can save a few hundred megabytes.
    
    sudo apt-get purge libreoffice\*
    

  8. Now get rid of the "Ubuntu Software Center".
    
    sudo apt-get purge software-center\*
    

  9. The /var/cache directory can easily have a few hundred megabytes data. We can trim it down.
    
    sudo apt-get clean
    

  10. Do an automove,
    
    sudo apt-get autoremove
    

  11. Now remove GNOME and Ubuntu documentation.
    
      sudo apt-get purge yelp\*
      

  12. Now, you can install whatever important software you want to include in the virtual machine, e.g., I am installing Octave,

    
    sudo apt-get install octave
    

  13. We would probably want to compress the virtual machine image before we pass it to someone. We need to replace every byte of free space on the disk by 0's.
    
    dd if=/dev/zero of=zerofile; \
       sleep 5; sync; \
       sleep 5; sync; \
       sleep 5; sync; \
       rm -f zerofile;
    

  14. I am using VMware Player. Using it, I do a "defragment" and then "compact" the virtual disk. The guest system must be off before you can do it. Of course, you can do the same using VirtualBox.
  15. Finally, I compress the folder of the virtual machine. The zip archieve file is about 1 GB. 
In addition, you may save a few megabytes, by removing all log and temporary files. The tool bleachbit can help. Before you zero the hard disk, install and run the tool.


sudo apt-get install bleachbit; sudo bleachbit