User Tools

Site Tools


xen_vm_creation

Xen VM Creation

This script will create a vm from a image.

#!/bin/env perl
## Build vm providing options
## $1 Hostname
## $2 IP Address - This will be checked to make sure that it currently not being used
 
# test ip address is free
# test vg for free space
# create lv if test is good
# dd image file to lv
# mount root lv to test mount and edit files
# mount var lv to test mount and edit files
# create xen configuration
# start xen
 
use strict;
 
usage(); 
check_user();
 
my $hostname=$ARGV[0];
my $ipaddress=$ARGV[1];
my $memory="1024";
 
if (defined($ARGV[2])){ print "yay memory is set";}
#my $hostname="testium";
#my $ipaddress="192.168.1.240";
my $lvprefix = "$hostname-x86_64";
my $tmpmnt = "/home/systems/mnt";
my $tmpdir = "/var/tmp";
my $vgpath = "/dev/vg";
 
 
check_ip($ipaddress);
create_lv($hostname);
dd_lv($hostname);
root_fs($hostname);
var_fs($hostname);
mk_swap();
make_it_so(build_xenconfig($hostname, $memory));
 
sub build_xenconfig{
 
my ($hostname, $memory) = @_;
my $xenconfdir = "/etc/xen/vmconfigs";
my $xenfile = "$hostname-x86_64-pygrub.cfg";
my @cmd = ("sudo", "mv", "$tmpdir/$xenfile", "$xenconfdir/$xenfile");
my @xenconfig = (
  "bootloader = \"/usr/bin/pygrub\"",
  "memory = $memory",
  "name = \"$hostname\"",
  "vif = [ 'bridge=xenbr0' ]",
  "disk = [ 'phy:vg/$hostname-x86_64-root,sda1,w', 'phy:vg/$hostname-x86_64-swap,sda2,w', 'phy:vg/$hostname-x86_64-var,sdb1,w' ]",);
 
  if (-e "$xenconfdir/$xenfile"){
    print STDERR "File $xenfile exists! Please choose another name or check!\n";
  }
  else{
    open(CFGFILE, ">>", "$tmpdir/$xenfile") or die "Couldn't open file for writing: $!\n";
      foreach (@xenconfig){
      print CFGFILE "$_\n";
      }
    system(@cmd); 
    close CFGFILE;
  }
return "$xenconfdir/$xenfile";
}
 
sub root_fs{
 
my $suffix = "root";
my @changeme = (
  "192.168.1.240",
  "lithium",
  );
my @editfiles = ( 
  "$tmpmnt/etc/hosts",
  "$tmpmnt/etc/motd",
  "$tmpmnt/etc/ssh/sshd_config",
  "$tmpmnt/etc/varnish/default.vcl",
  "$tmpmnt/etc/conf.d/hostname",
  "$tmpmnt/etc/conf.d/net",
  "$tmpmnt/etc/init.d/apache2",
  "$tmpmnt/etc/apache2/httpd.conf",
  "$tmpmnt/opt/jetty/etc/jetty.xml",
  "$tmpmnt/opt/jetty/contexts/podalaya.xml",
  );
 
mount_lv($suffix, $hostname);
prep_fs($suffix, \@changeme, \@editfiles);
umount_lv();
}
 
sub var_fs{
 
my ($hostname) = @_;
my $suffix = "var";
my @changeme = (
  "turbonium",
  );
my @editfiles = ( 
  "$tmpmnt/www/dev/new/conf/what/$changeme[0]",
  "$tmpmnt/www/dev/new/conf/what/$hostname",
  "$tmpmnt/www/dev/new/conf/what/$hostname/new.conf",
  );
 
mount_lv($suffix, $hostname);
prep_fs($suffix, \@changeme, \@editfiles);
umount_lv();
}
 
sub prep_fs{
# Prep root parition for startup
 
my ($suffix, $changeme, $filelist) = @_;
 
  if ($suffix eq "root"){
    foreach my $editfile (@$filelist){
      my $changeip = "sudo sed -i -e 's/@$changeme[0]/$ipaddress/g' $editfile";
      my $changehost = "sudo sed -i -e 's/@$changeme[1]/$hostname/g' $editfile";
      my @chattr = ("sudo", "chattr", "-i", $editfile);
      system (@chattr);
      system ($changeip);
      system ($changehost);
    }
  }
  elsif ($suffix eq "var"){
    my @mvconfdir = ("sudo", "mv", @$filelist[0], @$filelist[1]);
    my $changehost = "sudo sed -i -e 's/@$changeme[0]/$hostname/g' @$filelist[2]";
    system (@mvconfdir);
    system ($changehost);
  }
}
 
sub usage {
## Must provide following variables, will implement usage for only
 
  if (@ARGV != "2"){
   print "Usage: build_vm [hostname] [ip_address] <memory>\n";
   exit 1;
  }
}
 
sub check_user {
# Check if running as user systems, if not tell them no
 
  my $whoami = `id`;
 
  if ($whoami !~ m/systems/){
      print "You are not systems, exit!\n";
      exit 1;
  }
}
 
sub mount_lv {
 
my ($suffix, $hostname) = @_;
my @cmd = ("sudo", "mount", "$vgpath/$lvprefix-$suffix", "$tmpmnt");
 
system(@cmd);
 
  if ($? == 0){
    return 0;
  }
  else {
    print "Could not mount $suffix to $tmpmnt, exit!\n";
    exit 1;
  }
}
 
sub umount_lv {
my @cmd = ("sudo", "umount", $tmpmnt);
system(@cmd);
}
 
sub dd_lv {
 
my ($hostname) = @_;
my $imagepath = "/img/xen";
my $imageprefix = "lithium-x86_64";
my @lvimage = ( "root", "var" ); 
 
  print "Copying image file to lvm - this may take some time... \n";
  foreach (@lvimage){
    my $cmd = "gunzip -c $imagepath/$imageprefix-$_.img.gz | sudo dd of=$vgpath/$lvprefix-$_ bs=300k";
    open(STDOUT, ">/dev/null");
    system($cmd);
    close STDOUT;
  }
}
 
sub create_lv {
# Create lv for specified hostname
 
my ($hostname) = @_;
my %lvlist = (
  "root" => "32G",
  "var"  => "130G",
  "swap" => "2G", );
 
  if(check_vg() == "0"){
    for my $suffix( keys %lvlist) {
      my $size = $lvlist{$suffix};
      my @cmd = ("sudo", "lvcreate", "-L", "$size", "-n", "$lvprefix-$suffix", "vg");
      open(STDOUT, ">/dev/null");
      system(@cmd);
      close STDOUT;
      if( $? != "0" ){
        print STDERR "lvcreate failed: $!\n";
        exit 1;
      }
      print "LVM created... \n";
    }
  }
}
 
sub mk_swap{
 
my @cmd = ("sudo", "mkswap", "$vgpath/$lvprefix-swap");
open(STDOUT, ">/dev/null");
system(@cmd) == 0
or die  "Swap failed to create: $!\n";
close STDOUT;
}
 
sub check_vg {
## Check volume group for empty space
 
my $vmsize = "154";
my $vginfo = `sudo vgdisplay vg`;
 
  if($vginfo =~ /\s+Free\s+PE\s+\S+\s+Size\s+\d+\s+\S+\s+([\w+.]+)+\sGiB/g){
    if ( $vmsize > $1 ){
      print STDERR "Not enough space in vg, only $1 GB.\n";
      exit 1;
    }
    else{
      return 0;
    }
  }
}
 
sub check_ip {
## Check to see if ip address is used
 
my ($ipaddress) = @_;
my @cmd = ("/bin/ping", "-c1", "$ipaddress");
 
  open(STDOUT, ">/dev/null");
  system(@cmd) != 0
    or die  "$ipaddress is already used, please use another\n";
  close STDOUT;
}
 
sub make_it_so {
## start vm
 
my ($xmconfig) = @_;
my @cmd =("sudo", "xm", "create", "$xmconfig");
print "Starting vm... \n";
system(@cmd) == 0 or die "xm create failed!: $!\n";
}
xen_vm_creation.txt · Last modified: 2020/08/10 02:35 (external edit)