#!/usr/bin/perl # # XenServer DomU vm-install with IP configuration # # Written by T.Hayashi/Rookie Inc. # ver 1.0 2014.04.14 Init # # use strict; use warnings; use utf8; use 5.010; use Getopt::Long qw(:config posix_default no_ignore_case gnu_compat); use Pod::Usage; use Net::OpenSSH; use String::Random; # ####################################### # # Get commandline option (コマンドラインオプション取得) # GetOptions( \my %opt, qw/ man help xensv_host=s xensv_user=s xensv_password=s vm_template=s vm_name=s vm_ip=s vm_netmask=s vm_gateway=s vm_dns=s vm_domain=s /) or pod2usage(2); # HELP 表示 pod2usage(1) if $opt{help}; pod2usage(-exitstatus => 0, -verbose => 2) if $opt{man}; # 必須オプションのチェック my @required_options = qw/xensv_host vm_template vm_ip/; pod2usage(2) if grep {!exists $opt{$_}} @required_options; # デフォルト値を指定 $opt{xensv_user}="root" unless $opt{xensv_user}; $opt{vm_name}= String::Random->new->randregex('[A-Za-z0-9]{8}') unless $opt{vm_name}; $opt{vm_netmask}= "255.255.255.0" unless $opt{vm_netmask}; $opt{vm_gateway}= "" unless $opt{vm_gateway}; $opt{vm_dns}= "" unless $opt{vm_dns}; $opt{vm_domain}= "" unless $opt{vm_domain}; # ログイン認証 say 'Connecting Xen Server Dom0.'; my $ssh; if ($opt{xensv_password}){ $ssh = Net::OpenSSH->new( $opt{xensv_host} ,(user => $opt{xensv_user},password => $opt{xensv_password}) , master_opts => [-o => "StrictHostKeyChecking=no"] ); }else{ $ssh = Net::OpenSSH->new( $opt{xensv_host} ,(user => $opt{xensv_user}) , master_opts => [-o => "StrictHostKeyChecking=no"] ); } $ssh->error and die "Can't ssh to ". $opt{xensv_host} .": " . $ssh->error; # create new vm my $vmuuid = $ssh->capture('xe vm-install template=' . $opt{vm_template} . ' new-name-label=' . $opt{vm_name}) or die "xe vm-install failed: " . $ssh->error; chomp($vmuuid); say 'create new vm: ' . $opt{vm_name} . ' UUID: ' . $vmuuid; # XenStore param set # IP $ssh->system('xe vm-param-set uuid=' . $vmuuid . ' xenstore-data:vm-data/ip=' . $opt{vm_ip}) or die "xe vm-param-set failed: " . $ssh->error; # Gateway $ssh->system('xe vm-param-set uuid=' . $vmuuid . ' xenstore-data:vm-data/gw=' . $opt{vm_gateway}) or die "xe vm-param-set failed: " . $ssh->error; # Netmask $ssh->system('xe vm-param-set uuid=' . $vmuuid . ' xenstore-data:vm-data/nm=' . $opt{vm_netmask}) or die "xe vm-param-set failed: " . $ssh->error; # Nameserver $ssh->system('xe vm-param-set uuid=' . $vmuuid . ' xenstore-data:vm-data/ns=' . $opt{vm_dns}) or die "xe vm-param-set failed: " . $ssh->error; # domain $ssh->system('xe vm-param-set uuid=' . $vmuuid . ' xenstore-data:vm-data/dm=' . $opt{vm_domain}) or die "xe vm-param-set failed: " . $ssh->error; # Boot new vm $ssh->system('xe vm-start uuid=' . $vmuuid) or die "xe vm-start failed: " . $ssh->error; say 'Booting new vm: ' . $opt{vm_name}; # #-------- END ------------------------------------------------------------- # __END__ =head1 NAME xen-domu-net-config - XenServer DomU vm-install with IP configuration =head1 SYNOPSIS xen-domu-net-config --xensv_host hostname|IP adress [--xensv_user user] [--xensv_password SECRET] --vm_template tempname|UUID [--vm_name vmhost] --vm_ip 192.168.11.111 [--vm_netmask 255.255.255.0] [--vm_gateway 192.168.11.1] [--vm_dns 192.168.11.2] [--vm_domain example.com] Options: --xensv_host XenServer Hostname or IP address --xensv_user XenServer Logon User name(Default root) --xensv_password XenServer Logon password (if not exist this option, inquire Password) --vm_template Used VM template name or UUID --vm_name New VM hostname(Default Random name) --vm_ip New VM IP address --vm_netmask New VM Netmask(Default 255.255.255.0) --vm_gateway New VM gateway IP address(Default '') --vm_dns New VM dns IP address(Default '') --vm_domain New VM domain name(Default '') --help brief help message --man full documentation =head1 OPTIONS =over 4 =item B<--xensv_host hostname> XenServer Hostname or IP address =item B<--xensv_user user > XenServer Logon User name(Default root) =item B<--xensv_password Secret > XenServer Logon password =item B<--vm_template 53452a7b-4e10-403d-9ed5-042382b9e59d > Used VM template name or UUID =item B<--vm_name vmhost> New VM hostname =item B<--vm_ip 192.168.11.111 > New VM IP address =item B<--vm_netmask 255.255.255.0> New VM Netmask =item B<--vm_gateway 192.168.11.1 > New VM gateway IP address =item B<--vm_dns 192.168.11.2> New VM dns IP address =item B<--vm_domain example.com> New VM domain name =back =head1 DESCRIPTION B create new vm from template. =head1 License(BSD License) Copyright(C) 2014 Toshinori Hayashi /Rookie Inc. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: =over 4 =item * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. =item * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. =item * Neither the name of the Toshinori Hayashi or Rookie Inc nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. =back THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. =cut