在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的更多相关文章

  1. Azure ARM (11) ARM模式下,创建虚拟机并配置负载均衡器

    <Windows Azure Platform 系列文章目录> 本文内容比较多,请大家仔细阅读,谢谢! 在前几章中,我们做了准备工作: 1.创建ARM Resouce Group,叫Lei ...

  2. Azure ARM (9) 创建ARM模式下的虚拟机网络

    <Windows Azure Platform 系列文章目录> 笔者在之前几章内容中,创建了ARM Resource Group,然后在这个ARM Resource Group下创建Azu ...

  3. ARM模式下创建Express Route

    在Azure的ARM模式下,创建Express Route的命令和ASM模式下是有一些区别的. 本文将介绍在ARM模式下,如果创建Express Route的Circuit. 1. 查看支持的Serv ...

  4. Azure ARM (10) ARM模式下的虚拟机和Classic Model虚拟机的区别

    <Windows Azure Platform 系列文章目录> 本文内容比较多,请大家仔细阅读,谢谢! 请读者注意,在Azure ARM平台,有两种虚拟机模式:经典虚拟机和ARM虚拟机 A ...

  5. 通过 Powershell 来替换 ARM 模式下虚拟机的网络接口

    需求描述 客户在部署完 ARM 模式的虚拟机以后,由于误操作在虚拟机内部禁用了网卡导致远程访问虚拟机受到限制,以下是通过 Powershell 命令来替换原有虚拟网络接口实现虚拟网卡重置功能. Not ...

  6. Azure ARM (12) ARM模式下,在负载均衡器上设置多个公网IP地址

    <Windows Azure Platform 系列文章目录> 最近在帮助一个客户设置WAF (Web Application Firewall),WAF厂商要求在负载均衡器上,设置多个公 ...

  7. 通过 Powershell 来调整 ARM 模式下虚拟机的尺寸

    需求描述 在部署完 ARM 模式的虚拟机以后,可以通过 PowerShell 命令来调整虚拟机的尺寸,以下是通过 PowerShell 命令来调整 ARM 模式的虚拟机尺寸. Note 本文只限于 A ...

  8. Azure ARM模式下VNet配置中需要注意的几点事项

    虚拟网络的配置是所有公有云中非常重要的环节.把虚拟网络配置好,对整个系统的管理.维护,以及安全性都非常重要. 本文将介绍Azure在ARM模式下VNet配置中需要特别注意的几点. 一 Azure的VN ...

  9. 【虚拟机-部署】通过 Powershell 来调整 ARM 模式下虚拟机的尺寸

    需求描述 在部署完 ARM 模式的虚拟机以后,可以通过 PowerShell 命令来调整虚拟机的尺寸,以下是通过 PowerShell 命令来调整 ARM 模式的虚拟机尺寸. Note 本文只限于 A ...

随机推荐

  1. js常用方法汇总

    产生在m.n之间的随机整数 //Math.round()把数四舍五入为最接近的整数. function random(m, n) { return Math.round(Math.random() * ...

  2. Python内置标准模块

    time 模块 1 >>> import time 2 >>> time.time() 3 1491064723.808669 4 >>> # t ...

  3. CCNA 课程 七

    WAN(Wide Area Network)广域网 运行在OSI模型的数据链路层.物理层. 数据链路层的协议主要有: HDLC  (High-Level Data Link Control 高级数据链 ...

  4. Myeclipse 快捷键使用

    MyEclipse快捷键大全-------------------------------------MyEclipse 快捷键1(CTRL)----------------------------- ...

  5. 轻松掌握XMLHttpRequest对象------【这是.net 版本】

    轻松掌握XMLHttpRequest对象 XmlHttp是什么? 最通用的定义为:XmlHttp是一套可以在Javascript.VbScript.Jscript等脚本语言中通过http协议传送或从接 ...

  6. 深入理解Servlet3.0异步请求

    异步请求的基础概念 异步请求最直接的用法就是处理耗时业务,Http协议是单向的,只能客户端拉不能服务器主推. 异步请求的核心原理主要分为两大类:1.轮询.2长连接 轮询:就是定时获取返回结果. 长连接 ...

  7. 添加语句<tx:annotation-driven transaction-manager="txManager"/>报错

    在添加<tx:annotation-driven transaction-manager="txManager"/>程序之前,applicationContext.xm ...

  8. 2017-03-01 Oracle10g的安装与配置使用

    今天项目中又用到了Oracle,时隔三年没有碰过Oracle,之前在做某城市公共自行车管理系统时使用的是Orace10g版本,Oracle给我最大的感觉就是安装上以后,电脑就会变得很卡,所以大家当不使 ...

  9. 自己动手写一个U盘拷贝小工具

    这是五一期间,参照知乎上一篇的文章<十行代码--用python写一个USB病毒>写成的,最初只是单纯的想写成死循环,直到文件占满硬盘为止,第一个遇到的问题是,拷贝到硬盘之后,由于要无限次拷 ...

  10. hdu6237 分解质因子

    题意:给一堆石子,每次移动一颗到另一堆,要求最小次数使得,所有石子数gcd>1 题解:枚举所有质因子,然后找次数最小的那一个,统计次数时,我们可以事先记录下每堆石子余质因子 的和,对所有石子取余 ...