From Jittery Hog, 11 Months ago, written in Plain Text.
  1. [orca@orcacomputers raid5]$ cat /usr/local/bin/mkvm.pl
  2. #!/bin/perl
  3. # This script simplifies the process of creating a KVM virtual machine by creating interactive prompts.
  4. use strict;
  5. use warnings;
  6. use v5.10;
  7.  
  8. # Declare some constants (can be configured).
  9. my $VM_ROOT = "/mnt/raid5/VM";
  10. my $ISO_ROOT = "$VM_ROOT/ISO";
  11. my $DISK_ROOT = "$VM_ROOT/Disks";
  12.  
  13. # Subroutine to create a menu prompt from a list of strings.
  14. sub menu {
  15.     my @items = @_;
  16.  
  17.     my $count = 0;
  18.     foreach my $item( @items ) {
  19.         printf "%d: %sn", ++$count, $item =~ s/.*///gr;
  20.     }
  21.  
  22.     print "n#: ";
  23.     while( my $line = <STDIN> ) {
  24.         chomp $line;
  25.         if ( $line =~ m/d+/ && $line <= @items && $line > 0 ) {
  26.             if ($line == @items + 1) {
  27.                 exit;
  28.             }
  29.             return $items[$line - 1];
  30.         }
  31.  
  32.         print "#: ";
  33.     }
  34. }
  35.  
  36. # Subroutine to prompt for a value with a given prompt, default value, minimum, and maximum value.
  37. sub prompt {
  38.     my $prompt = $_[0];
  39.     my $default = $_[1];
  40.     my $min = $_[2];
  41.     my $max = $_[3];
  42.  
  43.     print "n$prompt, $min-$max ($default): ";
  44.     while (my $input = <STDIN>) {
  45.         chomp $input;
  46.         if ($input eq "") {
  47.             return $default;
  48.         } elsif ($input =~ m/d+/ && $input >= $min && $input <= $max) {
  49.             return $input;
  50.         }
  51.         print "$prompt, $min-$max ($default): ";
  52.     }
  53. }
  54.  
  55.  
  56. # Select which disk image to use.
  57. print "Select ISO:nn";
  58. my $ISO = menu(glob("$ISO_ROOT/*"));
  59.  
  60. # Configure the size of the disk.
  61. my $disk = prompt("Disk size (GB)", 50, 5, 1000);
  62.  
  63. # Configure the amount of RAM to give to the VM.
  64. my $RAM = prompt("RAM (GB)", 1, 0.5, 10);
  65. $RAM *= $RAM * 1024;
  66.  
  67. # Configure the amount of virtual CPU cores the VM will have access to.
  68. my $CPU = prompt("CPU cores", 2, 1, 8);
  69.  
  70. # Prompt for the ID of the machine.
  71. print "nID: ";
  72. my $ID;
  73. while ($ID = <STDIN>) {
  74.     $ID =~ s/[^w-]//g;
  75.     if ($ID ne "") {
  76.         last;
  77.     }
  78.     print "ID: ";
  79. }
  80.  
  81. if (grep(/^.*/$ID.qcow2$/, glob("$DISK_ROOT/*"))) {
  82.     print "nFailed: VM with ID `$ID` already exists.n";
  83.     exit;
  84. }
  85.  
  86. # Prompt for the name of the OS.
  87. # print "nOS: ";
  88. # my $OS = <STDIN>;
  89. # chomp $OS;
  90.  
  91. my $OS = "centos7.0";
  92.  
  93. print "nnConfirm:nnISO: $ISOnDisk size: $disk GBnRAM: $RAM MBnCPU cores: $CPUnID: $IDnOS: $OSnn(y,n): ";
  94.  
  95. while (my $confirm = <STDIN>) {
  96.     chomp $confirm;
  97.     if ($confirm eq "y" || $confirm eq "Y") {
  98.         last;
  99.     } elsif ($confirm eq "n" || $confirm eq "N") {
  100.         exit;
  101.     }
  102.     print "(y,n): ";
  103. }
  104.  
  105. print "n";
  106.  
  107. system "sudo virt-install --virt-type=kvm --name $ID --ram $RAM --vcpus $CPU --os-variant "$OS" --cdrom "$ISO" --network=bridge=virbr0,model=virtio --graphics vnc --disk path="$DISK_ROOT/$ID.qcow2",size=$disk,bus=virtio,format=qcow2 &";
  108.  
  109. system "sleep 5s; sudo virsh dumpxml $ID | grep vnc";
  110.  
captcha