Auto-deploy with KVM/OpenNebula

From PRAGMA wiki
Jump to: navigation, search

This is an automated VM deployment setup implemented on a KVM/OpenNebula system at AIST and documented by Yoshio Tanaka and Akihiko Ota.
To start, download File:Aist-vms.20120426.tar.gz

1. Installation

(1) extract tarball
Extract tarball in /opt directory.

$ sudo tar xvzpf vms.20120426.tar.gz -C /opt

(2) set environment variables
$ export PERL5LIB="/opt/vms"
$ vi ~/.bashrc

(3) configure sudo
$ sudo visudo

- Enable NOPASSWD for /opt/vms/vms-makeover
  The following is an example to set NOPASSWD for cloud group.

  %cloud ALL=(ALL) NOPASSWD:/opt/vms/vms-makeover

- add PERL5LIB to env_keep so that PERL5LIB environment variable is
  propagated to subshell invoked by sudo.

  Defaults    env_keep = "COLORS DISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR \
                        LS_COLORS MAIL PS1 PS2 QTDIR USERNAME \
                        LANG LC_ADDRESS LC_CTYPE LC_COLLATE LC_IDENTIFICATION \
                        LC_MEASUREMENT LC_MESSAGES LC_MONETARY LC_NAME LC_NUMERIC \
                        LC_PAPER LC_TELEPHONE LC_TIME LC_ALL LANGUAGE LINGUAS \
                        _XKB_CHARSET XAUTHORITY PERL5LIB"

(4) Modify the default value in the scripts
- /opt/vms/vms (Perl script)
    command path
    my $vms_makeover = "/opt/vms/vms-makeover";
    my $sudo = "/usr/bin/sudo";

    default value of parameters of OpenNebula commands
    my @one_networks = ("defaultpub", "defaultpriv"); # network name described in vm definition file
    my $cluster = "default";                          # attribute specified by REQUIREMENT= described in vm definition file
    my $smp = 8;                                      # number of CPUs of the VM described in vm definition file
    my $mem = 20480;                                  # memory size of the VM described in vm definition file
    my $vmname = "default";                           # name of the VM described in vm definition file
    my $img_persist = "NO";                           # persistent attribute described in image definition file
    my $imagename = "default";                        # image name described in image definition file
    my $description = "";                             # DESCRIPTION (comment0 described in image definition file

- /opt/vms/vms-makeover (Perl script)
    mount point and loop back device file
    my $mountpoint = "/mnt/file";
    my $loopfile   = "/dev/loop0";

- /opt/vms/makeover.pm (Perl module)
    command path
    my $mount      = "/bin/mount";
    my $umount     = "/bin/umount";
    my $losetup    = "/sbin/losetup";
    my $kpartx     = "/sbin/kpartx";

    init script path(in case where tarball is extracted in other than /opt/vms)
    my $default_ipaddress_rhel = "/opt/vms/one_ipaddress_rhel";
    my $default_context_rhel   = "/opt/vms/one_context_rhel";

- /opt/vms/register.pm (Perl module)
    OpenNebula command path
    my $oneimage   = "/srv/cloud/one/bin/oneimage";

- /opt/vms/compress.pm (Perl module)
    command path
    my $gunzip       = "/bin/gunzip";
    my $bunzip2      = "/usr/bin/bunzip2";

- /opt/vms/genconf.pm (Perl module)
    OpenNebula command path
    my $onevnet   = "/srv/cloud/one/bin/onevnet";
    my $oneimage  = "/srv/cloud/one/bin/oneimage";


# The followings are configuration of AIST's OpenNebula environment.
# These are site-specific and could be just a reference.

- /opt/vms/one_ipaddress_rhel (shell script)
    Translate MAC address to IP address which is assigned to VM.

    _one_mac_prefix="02:00"          # specify the same value with MAC_PREFIX in oned.conf
    _eth0_mac_prefix="02:00:a3:dc:"  # upper 4 octet for eth0 (MAC_PREFIX + Class A, B of the IP address)
    _eth1_mac_prefix="02:00:c0:a8:"  # upper 4 octet for eth1 (MAC_PREFIX + Class A, B of the IP address)

    _hostname_prefix="onevm"         # prefix of the hostname
    _pub_netmask="255.255.254.0"     # netmask (public)
    _priv_netmask="255.255.254.0"    # netmask (private)
    _gateway="163.220.56.254"        # default gateway
    _domainname="hpcc.jp"            # domain name
    _dns="163.220.2.34"              # DNS address
    _dns_search="${_domainname}"     # domain name which is specified by search directive in /etc/resolve.conf


- /opt/vms/one_context_rhel (shell script)
    This is a script which is executed when CONTEXT attribute in VM definition file is specified.
    This script creates the default user, adds SSH public key, and creates /etc/hosts file.

    _default_group="users"           ... group in which the default user belongs
    _pub_mac_prefix="02:00:a3:dc:"   ... upper 4 octet of the MAC address of the NIC for public interface


----------------------------------------------------------------------
2. How to use
vms script has the following four commands.

a. makeover
   Loop back mounts the image file and copies one_ipaddress_rhel and one_context_rhel scripts
   in the image file.  Currently, RHEL5 and RHEL6 are supported, but image files which use LVM are not supported.

    usage: vms makeover [OPTION] imagefile
      -M, --mountpoint Mountpoint directory

  specify image file path as an argument.
    $ /opt/vms/vms makeover /work/centos5.img
    Makeover a VM imagefile is done.


b. register
  Register the image file to OpenNebula (oneimage create)

    usage: vms register [OPTION] imagefile
      -t, --tmp        Temporary directory
      -I, --imagename  Image name used by OpenNebula

  Specify image file path as an argument.
    $ Create an Image Template.
      (/home/ascadm/vmtmp/image_template.J0OZ6oyA)
    ID: 45
    Register a VM imagefile to OpenNebula repository is done.
    VM Image name: default.2


c. deploy
  Do the same with makeover followed by register commands.

    usage: vms deploy [OPTION] imagefile
      -t, --tmp        Temporary directory
      -I, --imagename  Image name used by OpenNebula
      -M, --mountpoint Mountpoint directory

  Specify image file path as an argument.
    $ /opt/vms/vms deploy /work/centos5.img
    Makeover a VM imagefile is done.
    Deploy a VM imagefile is done.
    Create an Image Template.
      (/home/ascadm/vmtmp/image_template.kHr8nIid)
    ID: 46
    Register a VM imagefile to OpenNebula repository is done.
    VM Image name: default.2


d. genconf
  creates VM definition file.

    usage: vms genconf [OPTION] imagename
      -n, --network    OpenNebula Network (CSV)
      -o, --one        OpenNebula VM definition file name
      -t, --tmp        Temporary directory
      -s, --smp        Number of CPUs
      -m, --mem        Memory size
      -C, --cluster    Cluster name
      -N, --vmname     Virtual Machine name


  Specify image name listed by oneimage list command.  
    $ oneimage list
      ID USER     GROUP    NAME            SIZE TYPE          REGTIME PER STAT  RVMS
      46 ota      users    default.2         5G   OS   04/26 16:28:47  No  rdy     0

    $ /opt/vms/vms genconf default.2
    Generate a config file is done.
    VM configfile: /home/ascadm/vmtmp/default.2.one

  Modify VM definition file and boot the VM.

    $ vi /home/ascadm/vmtmp/default.2.one
    $ onevm create /home/ascadm/vmtmp/default.2.one
    ID: 131
    $ onevm list
        ID USER     GROUP    NAME         STAT CPU     MEM        HOSTNAME        TIME
       131 ota      users    default      runn  28     20G       asccmp015 00 00:23:38