Create Linux VM with larger OS disk in Azure
Linux OS disk is 30 GB by default in Azure. Develop environment requires larger OS disk, you can do it by following steps. (For production environment, OS disk is only for OS, you should mount separate data disk for your request.)
- Create new VM.
- Expand OS disk
- Recognize the OS disk changes in VM
- Create custom image
- Create VMs from the image
Note: Achieve the target via Azure CLI 2.0 in this document.
Limitation: Create VM from customized image can only succeed within the same resource group.
Create VM
- Create resource group
az group create --name royhk --location eastasia
- Create VM
az vm create --resource-group royhk \
--name RoyRedhat \
--image RHEL \
--public-ip-address "" \
--size Standard_E8s_v3 \
--admin-username redhat \
--generate-ssh-keys
- ssh to the VM and check partition.
ssh redhat@172.16.1.5
[redhat@RoyRedhat ~]$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
fd0 2:0 1 4K 0 disk
sda 8:0 0 32G 0 disk
├─sda1 8:1 0 500M 0 part /boot
└─sda2 8:2 0 31.5G 0 part /
sdb 8:16 0 128G 0 disk
└─sdb1 8:17 0 128G 0 part /mnt/resource
sr0 11:0 1 628K 0 rom
verify that root disk /
was in /dev/sda2
, we need this info for fdisk command later.
Expand OS disk
- Deallocate VM
az vm deallocate -g royhk -n RoyRedhat
- Expand OS disk size
check disk
az disk list -g royhk -o table
expand size
az disk update -g royhk -n RoyRedhat_OsDisk_1_55f7af3d651548b49ce9971420023d7d --size-gb 100
Recognize the OS disk changes in VM
- Start VM
az vm start -g royhk -n RoyRedhat
- ssh to VM; delete and re-create partion (data will not be lost)
[root@RoyRedhat ~]# fdisk /dev/sda
The device presents a logical sector size that is smaller than
the physical sector size. Aligning to a physical sector (or optimal
I/O) size boundary is recommended, or performance may be impacted.
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): u
Changing display/entry units to cylinders (DEPRECATED!).
Command (m for help): p
Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk label type: dos
Disk identifier: 0x0008c758
Device Boot Start End Blocks Id System
/dev/sda1 * 1 64 512000 83 Linux
/dev/sda2 64 4178 33041408 83 Linux
Command (m for help): d
Partition number (1,2, default 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-13054, default 64):
Using default value 64
Last cylinder, +cylinders or +size{K,M,G} (64-13054, default 13054):
Using default value 13054
Partition 2 of type Linux and of size 99.5 GiB is set
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
Note: you can ignore the WARNING message.
- Tell the Linux kernel about the presence and numbering of on-disk partitions
partx -u /dev/sda2
- resize the disk
xfs_growfs -d /dev/sda2
- verify
df -h
[root@RoyRedhat ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 100G 2.7G 97G 3% /
devtmpfs 32G 0 32G 0% /dev
tmpfs 32G 0 32G 0% /dev/shm
tmpfs 32G 8.3M 32G 1% /run
tmpfs 32G 0 32G 0% /sys/fs/cgroup
/dev/sda1 497M 117M 380M 24% /boot
/dev/sdb1 126G 2.1G 118G 2% /mnt/resource
tmpfs 6.3G 0 6.3G 0% /run/user/1000
Create custom image
To create an image of a virtual machine, you need to prepare the VM by deprovisioning, deallocating, and then marking the source VM as generalized.
-
Deprovision the VM
Deprovisioning generalizes the VM by removing machine-specific information. During deprovisioning, the host name is reset to localhost.localdomain. SSH host keys, nameserver configurations, root password, and cached DHCP leases are also deleted.
ssh redhat@172.16.1.5
sudo waagent -deprovision+user -force
exit
- Deallocate VM
az vm deallocate -g royhk -n RoyRedhat
- mark the VM as generalized
az vm generalize --resource-group royhk --name RoyRedhat
- Create the image
az image create -g royhk \
-n redhat100gImage \
--source RoyRedhat
Create VMs from the image
az vm create -g royhk -n royVMfromImage --image redhat100gImage --generate-ssh-keys
Verify:
[centos@royVMfromImage ~]$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 100G 2.7G 97G 3% /
devtmpfs 1.7G 0 1.7G 0% /dev
tmpfs 1.7G 0 1.7G 0% /dev/shm
tmpfs 1.7G 8.3M 1.7G 1% /run
tmpfs 1.7G 0 1.7G 0% /sys/fs/cgroup
/dev/sda1 497M 117M 380M 24% /boot
/dev/sdb1 6.8G 2.1G 4.4G 32% /mnt/resource
tmpfs 344M 0 344M 0% /run/user/1000
[centos@royVMfromImage ~]$ ls /usr/java
jdk1.8.0_162
[centos@royVMfromImage ~]$
Customized packages, data disk and expanded OS disk are kept in new created VM.