Azure上批量建立OS Disk大于30G的Linux VM

Azure上VM的OS盘的大小在建立时是固定的。Windows是127G,Linux是30G。若是须要批量建立的VM的OS Disk有更大的容量。能够考虑用下面的方法实现。html

1 建立一台有Data-disk的CentOS VM,对其进行客户化,安装软件,挂载磁盘linux

2 扩大OS Disk的容量vim

3 在VM内resize OS Disk的容量ssh

4 把这台VM捕获成Azure的Imagespa

5 经过这个Image批量建立VM。建立VM的OS Disk容量是刚刚调整的容量code

本文将采用Azure CLI 2.0实现这些操做:htm

 

一 建立VMblog

1 建立Resource Groupip

az group create --name hwmd --location chinanorth

2 建立VMit

az vm create -g hwmd -n hwmd01 --image CentOS --authentication-type password --admin-username hengwei --admin-password xxxx --size Standard_D1 --storage-sku Standard_LRS

3 挂载Data-Disk

az vm disk attach --vm-name hwmd01 --resource-group hwmd --size-gb 30 --sku Standard_LRS --caching None --new --disk hwmd01data01 --lun 1

4 SSH到这台VM,进行客户化工做

iptables -F
yum install -y httpd
fdisk /dev/sdc
mkfs.ext4 /dev/sdc1
vim /etc/sysconfig/selinux
setenforce 0
mount /dev/sdc1 /var/www/html/
df -h
cd /var/www/html
echo "Hello World" > index.html
systemctl enable httpd
systemctl start httpd
systemctl status httpd
vim /etc/fstab
mount -a

 

二 扩大OS Disk的容量

1 VM停机

az vm deallocate -g hwmd -n hwmd01

2 扩大OS Disk的Size

查看disk状况;

az disk list --o table

扩大Disk的size:

az disk update --resource-group hwmd --name osdisk_3yQQnL1V5E --size-gb 60

 

三 在VM中Resize OS Disk的容量

1 start vm

az vm start -g hwmd -n hwmd01

2 ssh到VM进行删除partition,从新建立partition(数据不会丢失)

fdisk /dev/sda
Command (m for help): u
Changing display/entry units to cylinders (DEPRECATED!).
Command (m for help): p
Disk /dev/sda: 64.4 GB, 64424509440 bytes, 125829120 sectors
Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
/dev/sda2              64        3917    30944256   83  Linux
Command (m for help): d
Partition number (1,2, default 2): 2
Partition 2 is deleted
Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (2-4, default 2):
First cylinder (64-7832, default 64):
Using default value 64
Last cylinder, +cylinders or +size{K,M,G} (64-7832, default 7832):
Using default value 7832
Partition 2 of type Linux and of size 59.5 GiB is set
Command (m for help): p
Disk /dev/sda: 64.4 GB, 64424509440 bytes, 125829120 sectors
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
/dev/sda2              64        7832    62397516   83  Linux

此时全部的容量都用满了。保存后从新启动

3 resize OS Disk

CentOS7以上机器的命令为:

xfs_growfs -d /dev/sda2

CentOS6的机器命令为:

resize2fs /dev/sda

能够看到OS Disk已是60G的容量了。

[root@hwmd01 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 60G 1.3G 59G 3% /

 

四 捕获Image

1 VM内通用化

waagent -deprovision

2 Azure平台对VM进行通用化

az vm deallocate -g hwmd -n hwmd01
az vm generalize -g hwmd -n hwmd01

3 捕获Image

az image create -g hwmd --name hwmdimage --source hwmd01

 

五 从这个Image建立VM

1 建立VM

az vm create -g hwmd -n hwmd03 --authentication-type password --admin-user hengwei --admin-password xxxx --image hwmdimage

2 SSH到VM查看Disk和访问的状况

[root@hwmd03 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 60G 1.3G 59G 3% /
/dev/sdc1 30G 45M 28G 1% /var/www/html

能够看到OS Disk已是60G了,同时Image中挂载的30G的Disk也在。

因为Azure CLI会自动加载NSG到VM的网卡上,且Linux的NSG只容许22端口的访问,因此要开放80端口,或删除NSG,才能访问httpd的内容。

能够看到以前加载的内容在新建的机器中也运行起来了。

 

六 总结

经过把客户化的VM捕捉成Image,能够方便的进行复制。客户化的内容不光包括安装软件和用户数据,增添的数据盘、扩充的OS Disk,均可以被不许下来。