在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. CentOS 6.5 下的截图方法

    1.利用命令模式 捕获整个屏幕 : $ gnome-screenshot 截完屏之后我们可以设置自定义图片存储位置,如图: 捕获当前终端Terminal : $ gnome-screenshot -w ...

  2. Go 字符串相关-标准库

    标准库中有四个包对字符串处理尤为重要: bytes strings strconv unicode strings包提供了许多如字符串的查询.替换.比较.截断.拆分和合并等功能. bytes包也提供了 ...

  3. java基础11(IO流)-字符流

    转换流 由于字节流操作中文不是特别方便,所以java中提供了转换流 编码表:由现实世界的字符和对应的数值组成的一张表 编码:把看得懂的变成看不懂的(String-------byte[]) 解码:把看 ...

  4. js建造者(生成器)模式

    建造者模式将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示. 在软件系统中,有时需要创建一个复杂对象,并且这个复杂对象由其各部分子对象通过一定的步骤组合而成. 建造者模式类图: ...

  5. ps常用键

    @updata 2016-7-31 切图 界面设置 视图 --显示 ---智能参考线       72 标尺  ctrl + r 窗口  ----信息 字符  历史记录 颜色 选RGB   信息图选项 ...

  6. [Kafka] - Kafka内核理解:分布式机制

    一个Topic中的所有数据分布式的存储在kafka集群的所有机器(broker)上,以分区(partition)的的形式进行数据存储:每个分区允许存在备份数据/备份分区(存储在同一kafka集群的其它 ...

  7. 【Swoole】简单安装与创建TCP服务器

    pecl install swoole PHP的异步.并行.高性能网络通信引擎,使用纯C语言编写,提供了php语言的异步多线程服务器,异步TCP/UDP网络客户端,异步MySQL,异步Redis,数据 ...

  8. Unity3d 异常与解决方案集合(持续)

    1:更新完unity的5.3.1 版本 后,打开SimpleFrameworld_UGUI 后出现 error CS0117: 'System.IO.Directory' does not conta ...

  9. 5.2 Selenium2环境搭建

    1.Java开发环境的搭建      本课程中将使用Java语言编写Selenium自动化测试脚本,在Eclipse集成开发环境中运行. (1)jdk的安装 a.下载 官网下载,http://www. ...

  10. python直接赋值,浅拷贝和深拷贝

    本文参考自<Python 直接赋值.浅拷贝和深度拷贝解析> 定义 直接赋值:就是对象的引用(别名) 浅拷贝(copy):拷贝父对象,不拷贝对象内部的子对象 深拷贝(deepcopy):co ...