1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
| #!/usr/bin/perl -w
use strict;
use warnings;
use VMware::VIRuntime;
Opts::parse();
Opts::validate();
Util::connect();
# 部署 Windows 目标虚机并配置网络
deploy_W2K8();
Util::disconnect();Virt-top documentation
sub deploy_W2K8 {
my $vmhost = "9.115.208.49"; # 目标虚机所在 ESX server 的地址
my $ds = "datastore1"; # 目标虚机所在的 datastore
my $vm_template = "WIN2K8R2"; # 部署目标虚机用的模板
my $respool = "ResPool"; # 目标虚机所在的 resource pool
my $vm_name = "WIN2K8R2A"; # 目标虚机的虚机名
# 获得模板视图
my $vm_template_view = Vim::find_entity_view(
view_type => 'VirtualMachine',
filter => {name => $vm_template}
);
# 获得 ESX server 视图
my $vmhost_view = Vim::find_entity_view(
view_type => 'HostSystem',
filter => {name => $vmhost}
);
# 获得 resource pool 视图
my $respool_view = Vim::find_entity_view(
view_type => 'ResourcePool',
filter => {name => $respool}
);
# 获得 datastore 视图
my $ds_view = Vim::find_entity_view(
view_type => 'Datastore',
filter => {name => $ds}
);
# 目标虚机重定位信息,指定目标虚机的 datastore/host/resource pool
my $relocate_spec = VirtualMachineRelocateSpec->new(
datastore => $ds_view,
host => $vmhost_view,
pool => $respool_view,
);
# 自定义 Guest Windows os 的网络配置信息
# 主机名 /DNS 域 /IP 地址 / 网关 / 子网掩码 /DNS 服务器 / 用户及组织 / 序列号 / 密码等
my $host_name = "WIN2K8R2A";
my $domain = "cn.ibm.com";
my $ip_address = "9.115.208.62";
my @gateway = ("9.115.208.1");
my $netmask = "255.255.255.0";
my @dnsServers = ("9.181.2.101", "9.181.2.102");
my $full_name = "IBMCN";
my $org_name = "IBMCN";
my $prod_ID = "";
my $password = "passw0rd";
# Windows Guest OS 的定制不需要指定 Global IP 设置
my $cust_global_settings = CustomizationGlobalIPSettings->new();
# 加入 workgroup 组。
# 若使用域配置,需提供 domainAdmin/domainAdminPassword/joinDomain 参数
my $cust_identification = CustomizationIdentification->new(
joinWorkgroup => "workgroup",
);
my $cust_gui_unattended = CustomizationGuiUnattended->new(
autoLogon => 1,
autoLogonCount => 1,
timeZone => 190,
password => CustomizationPassword->new(
plainText => "true",
value => $password
),
);
my $cust_user_data = CustomizationUserData->new(
computerName => CustomizationFixedName->new(name => $host_name),
fullName => $full_name,
orgName => $org_name,
productId => $prod_ID,
);
my $win_prep = CustomizationSysprep->new(
guiUnattended => $cust_gui_unattended,
identification => $cust_identification,
userData => $cust_user_data,
);
my $cust_IP_settings = CustomizationIPSettings->new(
dnsDomain => $domain,
dnsServerList => \@dnsServers,
ip => CustomizationFixedIp->new(ipAddress => $ip_address),
gateway => \@gateway,
subnetMask => $netmask,
);
my $cust_adapter_mapping = CustomizationAdapterMapping->new(
adapter => $cust_IP_settings,
);
my $cust_adapter_mapping_list = [$cust_adapter_mapping];
my $cust_spec = CustomizationSpec->new(
globalIPSettings => $cust_global_settings,
identity => $win_prep,
nicSettingMap => $cust_adapter_mapping_list,
);
my $clone_spec = VirtualMachineCloneSpec->new(
powerOn => 1,
template => 0,
location => $relocate_spec,
customization => $cust_spec,
);
# 启动任务
$vm_template_view->CloneVM_Task(
folder => $vm_template_view->parent,
name => $vm_name,
spec=>$clone_spec
);
}
|