Search This Blog

Thursday 2 July 2015

how to create virtual machines on centos host

Installing the virtualization software
You can now execute any command that requires elevated privileges without switching to root, as long as you type sudo before it and supply your user password (not your root password).  You will need to install some software to allow your machine to act as a host for virtual machines

sudo yum -y install @virtualization virt-manager libvirt

Start and enable the virtualization service
sudo systemctl start libvirtd
sudo systemctl enable libvirtd

Note that when starting libvirtd you may receive a warning 'virGetHostname: getaddrinfo failed for "Machine Name": Name or service not known. This will not interfere with your machines.

Reboot your machine (if you have not already as part of the installation process). If you do not, you will not be able to install any virtual machines.  Start the graphical virtual machine manager.  You will need to enter your root password.

Create your own virtual network
  1. Right click 'localhost (QEMU)' and select 'Details'. Click on the 'Virtual Networks' tab.
  2. Stop and delete the 'default' network.
  3. Use the plus sign to add a new virtual network using the following options.
    • Name your virtual network 'ops335'
    • Use the network address space assigned to you. Example, if you were assigned 90, the network address would be 192.168.90.0/24.
    • Ensure the DHCP range will allow you to assign at least 3 static IP addresses outside it.
    • Choose 'Forwarding to physical network' radio button, 'Destination: Any physical device' and 'Mode: NAT'
    • Ensure the network is started at boot.
  4. Once completed open a terminal and observe and record the output of the following command:
iptables -t nat -L

Installing a Virtual Machine

With the virtualization software installed and your personal network created, you are now ready to create your first virtual machine.  Use your Centos 7 installation dvd (or alternately you can copy the ISO image to your hard drive and install from that).  The later option will result in a slightly faster install.

Installation

  • Click on the icon "Create a new virtual machine" to begin.
  • Name your machine "centos1" and choose your installation method - "Local install media". Choose the desired option to install from either the CD or iso. For "OS type" select "Linux" and for Version select "Red Hat Enterprise 6" then click on the "Forward" button.
  • Use the default memory and CPU options for use with lab computers (Depending on available hardware these settings can be adjusted). Then click on the "Forward" button to proceed.
  • Leave the disk image size set at 8GB, ensure "Allocate entire disk now" is checked, then click on the "Forward" button.
  • At the "Ready to begin installation" window click on 'Advanced options' arrow to review available options.
    • Select the Virtual Network named 'ops335'. Make note of any other available options (you will need them again in the assignments).
  • Select 'Install to Hard Drive' to begin your Centos installation. Select the appropriate default options (You may wish to review your OPS235 notes to remind yourself what these are).  The installation process should be identical to what you chose on the host, with the exception of the hostname.
  • During installation you will be prompted to set the root password and an initial user account. For the initial user, enter the same information you entered on your host machine.
  • Set your hostname to "centos1".
Firstboot - First user created
For successful completion of the labs, please ensure the first user created is named using your Learn ID.

First Boot

  • You may notice that graphical windows are not being displayed properly. The following steps will resolve this.
    • At the first opportunity, shut the VM down. If you perform these steps while the machine is running it will claim they will be applied the next time the machine boots, but that does not work.
    • While the VM is off, click on 'View' (from the menu at the top of the VM window), and select 'Details'.
    • From the menu on the left side, select 'Video'.
    • Change the drop-down list for Model from 'Cirrus' to 'vga', and click apply.
    • Switch the view back to 'Console' and start the machine again.
  • Ensure your machine has a network connection by running the command
host cbc.ca
If that did not work, edit the line in /etc/sysconfig/network-scripts/ifcfg-eth0
onboot=no
should be
onboot=yes
  • If you had to change that, bring the interface down and back up again
sudo ifdown eth0; sudo ifup eth0
  • Once you have a working connection update your machine (this may take a while -- while you are waiting it is probably a good idea to update your host as well).
sudo yum -y update
  • Reboot the virtual machine once it is updated.
  • If your virtual machine hangs on boot, you will need to change a graphics option:
    • While the VM is off, click on 'View' (from the menu at the top of the VM window), and select 'Details'.
    • From the menu on the left side, select 'Display'.
    • Change the drop-down list for Type from 'VNC' to 'Spice', and click apply.
    • Switch the view back to 'Console' and start the machine again.
  • Now run the following commands and note the output. Note how they differ from the Centos installation on your host.
df -hT
cat /etc/fstab
cat /etc/issue
uname -a

Cloning a Virtual Machine

  • Now that you have one virtual machine working, you will create two more. If you struggled with the previous steps, repeat them to create two more virtual machines (naming them centos2 and centos3, with hostnames centos2 and centos3 respectively).
  • If you are confident with what you have done so far, you may clone your existing machine to create the others.
    • To quickly create additional VM's shutdown 'centos1', right click and select 'Clone...'. Use the following options:
  • Name: centos2
  • Storage:
    • Click the drop down menu below 'centos1.img', choose details and rename the image to the centos2.img
    • Once successfully created boot the new VM and correct the host name. This can be done using the GUI or command line.
    • Record in your notes how each is done.
    • Check for connectivity.
host cbc.ca
    • After creating centos2 repeat the above steps to create centos3 and correct the host name.

Backing up your Virtual Machines

You are responsible for backing up your virtual machines on a regular basis.  It is good practice to copy your back ups to a separate location (like a USB Flash Drive) once completed, so in the event your drive becomes damaged to the point that you can't access them all you need do is reinstall the host and restore your back ups.  To back up your virtual machines you can do them manually (as you did in OPS235 -- review the steps to do so there), or you can use this script that I have provided you.  It requires minimum configuration to make it work on your system, and only needs to be done once. 
To configure this script so it will work on your host, some changes will be required.  Modify the contents of the variables near the top of the script (below the large comment block) so that dpath represents the path on YOUR host.  spath and vms can be left unmodified, unless you didn't follow the naming convention for virtual machines outlined above.  In that case, you must change these to reflect what is available on your system.  If you require any assistance configuring or using this script, please ask.
Once your script is configured properly, save and exit the text editor you used to make the necessary changes.  From your command line, create the directory "backup" in your home directory
cd
mkdir backup
Now, copy the script into the backup directory.  Change to that directory and make the script executable (using chmod +x).  To back up your virtual machines, issue the following command within the backup directory.
./vs -b
Backing up your virtual machines will still take on average 5-7 minutes per virtual machine.  The advantage to doing it this way is, you can go do something else while the process finishes.

No comments: