在ARM模式下捕获VM并创建新VM
在ASM模式下,可以通过Manage Portal上捕获VM的Image,并创建新的VM。在ARM模式下,在Portal上目前还没有这个功能,要做VM镜像的捕获和创建新的VM需要用powershell或Azure CLI来实现。
本文将介绍如何用Powershell捕获VM的镜像,并用Powershell从这个Image创建新的VM。
一、VM的Generalized
本文采用的是Linux CentOS的机器,需要通过waageng对其进行Generalized。就是删除用户信息:
[root@hwhpc04 ~]# waagent -deprovision+user
WARNING! The waagent service will be stopped.
WARNING! All SSH host key pairs will be deleted.
WARNING! Cached DHCP leases will be deleted.
WARNING! Nameserver configuration in /etc/resolv.conf will be deleted.
WARNING! root password will be disabled. You will not be able to login as root.
WARNING! hengwei account and entire home directory will be deleted.
Do you want to proceed (y/n)? y
[root@hwhpc04 ~]#
结束后,在portal上关机。
二、在Powershell下捕获镜像
下面的Powershell脚本进行了
1. VM的Generalized
2. Capture VM image
3. 从捕获的Image创建一台VM
具体的Powershell代码如下:
function cpt-crtvm {
param(
#The VM resource group
[Parameter(Mandatory=$true)]
[String]$rg, #The VM name
[Parameter(Mandatory=$true)]
[String]$vm_name, #The new VM admin name
[Parameter(Mandatory=$true)]
[String]$newuser, #The new VM password
[Parameter(Mandatory=$true)]
[String]$newpwd, #The new VM size
[Parameter(Mandatory=$true)]
[String]$newvmsize, #haset name
[Parameter(Mandatory=$true)]
[String]$haset )
#Get a random text as the VM new name
$subhash = $null
for ($i = 0; $i -le 4; $i++){
$j = (97..122) | Get-Random -Count 1 | % {[char]$_}
$subhash = $subhash + $j
}
for ($i = 0; $i -le 4; $i++){
$j = (48..57) | Get-Random -Count 1 | % {[char]$_}
$subhash = $subhash + $j
} #Get the VM need to be generalized
$vm = get-azurermvm -ResourceGroupName $rg -Name $vm_name
#New VM name from the radom text
$newvmname = $subhash + "vm" #old VM generalized
set-azurermvm -ResourceGroupName $vm.ResourceGroupName -Name $vm.Name -Generalized #Catpture the VM to image
Save-AzureRmVMImage -Name $vm.Name -DestinationContainerName $subhash -VHDNamePrefix $subhash -ResourceGroupName $vm.ResourceGroupName -Path d:\hwhpc.json #get the storage related information
$sa = Get-AzureRmStorageAccount -ResourceGroupName $vm.ResourceGroupName -Name $vm.StorageProfile.OsDisk.Vhd.Uri.Split("/")[2].split(".")[0]
$blob = $sa | Get-AzureStorageBlob -Container system | Where-Object {$_.name -match "$subhash.*vhd$"}
$url = $blob.Context.BlobEndPoint + "system/" + $blob.Name #get the network related information
$nicname = $vm.NetworkProfile.NetworkInterfaces[0].Id.Split("/")[-1]
$vmnic = Get-AzureRmNetworkInterface -Name $nicname -ResourceGroupName $vm.ResourceGroupName
$vnetname = $vmnic.IpConfigurations[0].Subnet.Id.Split("/")[-3]
$vmvnet = Get-AzureRmVirtualNetwork -Name $vnetname -ResourceGroupName $vm.ResourceGroupName #new VM pip name
$newvmnamepip = $subhash+"vmpip" #create new public ip address
New-AzureRmPublicIpAddress -Name $newvmname -ResourceGroupName $vm.ResourceGroupName -Location "China East" -AllocationMethod Dynamic -DomainNameLabel $newvmnamepip
$newvmpip = Get-AzureRmPublicIpAddress -Name $newvmname -ResourceGroupName $vm.ResourceGroupName #create new nic
New-AzureRmNetworkInterface -Name $newvmname -ResourceGroupName $vm.ResourceGroupName -Location "China East" -SubnetId $vmvnet.Subnets[0].Id -PublicIpAddressId $newvmpip.Id
$newvmnic = Get-AzureRmNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name $newvmname #create the new vm credential
$pwd=ConvertTo-SecureString $newpwd -AsPlainText -Force
$newvmcred=New-Object System.Management.Automation.PSCredential($newuser,$pwd) #newOSDiskName
$newosDiskName = $newvmname+"osdisk" #haset
$has = Get-AzureRmAvailabilitySet -ResourceGroupName $vm.ResourceGroupName
$harslt = $false
foreach ($h in $ha){if($h.name -eq $haset){$harslt = $true}}
if(-not $harslt) {New-AzureRmAvailabilitySet -ResourceGroupName $vm.ResourceGroupName -Name $haset -Location $vm.Location}
$has = Get-AzureRmAvailabilitySet -ResourceGroupName $vm.ResourceGroupName -Name $haset #Put the VM config to VM
$newvmconfig = New-AzureRmVMConfig -VMName $newvmname -VMSize $newvmsize -AvailabilitySetId $has.Id
$newvm = Set-AzureRmVMOperatingSystem -VM $newvmconfig -Linux -ComputerName $newvmname -Credential $newvmcred
$newvm = Add-AzureRmVMNetworkInterface -vm $newvm -Id $newvmnic.Id
$newosDiskUri = '{0}vhds/{1}-{2}.vhd' -f $sa.PrimaryEndpoints.Blob.ToString(), "vm",$newosDiskName
$newvm = Set-AzureRmVMOSDisk -VM $newvm -name $newosDiskName -VhdUri $newosDiskUri -CreateOption FromImage -SourceImageUri $url -Linux $result = new-azurermvm -ResourceGroupName $vm.ResourceGroupName -Location $vm.Location -VM $newvm
}
cpt-crtvm -rg hwhpc -vm_name hwhpc03 -newuser xxxxxxx -newpwd xxxxxxx -newvmsize Standard_D1 -haset a1
代码中用了随机的Text(5个字母5个数字)来定义VM的名字。当然也可以自己定义机器的名称。
但如果自己定义机器的名称,建议在创建VM的各个组件前先检查这些组件是否有重名的情况,否则创建VM会失败。
在ARM模式下捕获VM并创建新VM的更多相关文章
- Azure ARM (11) ARM模式下,创建虚拟机并配置负载均衡器
<Windows Azure Platform 系列文章目录> 本文内容比较多,请大家仔细阅读,谢谢! 在前几章中,我们做了准备工作: 1.创建ARM Resouce Group,叫Lei ...
- Azure ARM (9) 创建ARM模式下的虚拟机网络
<Windows Azure Platform 系列文章目录> 笔者在之前几章内容中,创建了ARM Resource Group,然后在这个ARM Resource Group下创建Azu ...
- ARM模式下创建Express Route
在Azure的ARM模式下,创建Express Route的命令和ASM模式下是有一些区别的. 本文将介绍在ARM模式下,如果创建Express Route的Circuit. 1. 查看支持的Serv ...
- Azure ARM (10) ARM模式下的虚拟机和Classic Model虚拟机的区别
<Windows Azure Platform 系列文章目录> 本文内容比较多,请大家仔细阅读,谢谢! 请读者注意,在Azure ARM平台,有两种虚拟机模式:经典虚拟机和ARM虚拟机 A ...
- 通过 Powershell 来替换 ARM 模式下虚拟机的网络接口
需求描述 客户在部署完 ARM 模式的虚拟机以后,由于误操作在虚拟机内部禁用了网卡导致远程访问虚拟机受到限制,以下是通过 Powershell 命令来替换原有虚拟网络接口实现虚拟网卡重置功能. Not ...
- Azure ARM (12) ARM模式下,在负载均衡器上设置多个公网IP地址
<Windows Azure Platform 系列文章目录> 最近在帮助一个客户设置WAF (Web Application Firewall),WAF厂商要求在负载均衡器上,设置多个公 ...
- 通过 Powershell 来调整 ARM 模式下虚拟机的尺寸
需求描述 在部署完 ARM 模式的虚拟机以后,可以通过 PowerShell 命令来调整虚拟机的尺寸,以下是通过 PowerShell 命令来调整 ARM 模式的虚拟机尺寸. Note 本文只限于 A ...
- Azure ARM模式下VNet配置中需要注意的几点事项
虚拟网络的配置是所有公有云中非常重要的环节.把虚拟网络配置好,对整个系统的管理.维护,以及安全性都非常重要. 本文将介绍Azure在ARM模式下VNet配置中需要特别注意的几点. 一 Azure的VN ...
- 【虚拟机-部署】通过 Powershell 来调整 ARM 模式下虚拟机的尺寸
需求描述 在部署完 ARM 模式的虚拟机以后,可以通过 PowerShell 命令来调整虚拟机的尺寸,以下是通过 PowerShell 命令来调整 ARM 模式的虚拟机尺寸. Note 本文只限于 A ...
随机推荐
- Shell编程之while循环和until循环
一.当型和直到型循环 1.while循环语句 while < 条件表达式 > do 指令... done while循环执行流程对应的逻辑图 2.until循环语句 until < ...
- get_called_class--后期静态绑定("Late Static Binding")类的名称
get_called_class--后期静态绑定("Late Static Binding")类的名称 string get_called_class ( void ) 获取静态方 ...
- Hadoop的Docker镜像构建
1.Dockerfile ###Dockerfile -- beagin FROM ubuntu:trusty #MAINTAINER The Hue Team "https://githu ...
- CentOS 7 安装 docker-machine
https://github.com/docker/machine/releases/ 指令: curl -L https://github.com/docker/machine/releases/d ...
- webview 最简单的demo
) { return; } view.loadUrl(url); }} <!--activity_test.xml> <?xml version="1.0" en ...
- How to use QToolBar and QToolButton in Qt
http://developer.nokia.com/Community/Wiki/How_to_use_QToolBar_and_QToolButton_in_Qt How to use QTool ...
- QT 使用QUdpSocket QUdpServer UDP 建立客户端与服务器端
1. 模拟天气监控,每隔两秒从Server发送天气信息到Client. 2. 示例代码 --------------------------- Server 端 ------------------- ...
- alibaba的JSON.toString会把值为null的字段去掉,谨记
alibaba的JSON.toString会把值为null的字段去掉,谨记 Map<String,Object> map = new HashMap<>(); map.put( ...
- form组件的总结
1.form组件(******) 局部钩子 全局钩子 ''' 实例化时: self.fields={ "username":"字段规则对象", "pa ...
- phalcon 连接多个数据库 phalcon multi-database
db: //This service returns a MySQL database $di->set('dbMaster', function() { return new \Phalcon ...