Create Linux VM with larger OS d

2018-06-08  本文已影响0人  华阳_3bcf

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.)

  1. Create new VM.
  2. Expand OS disk
  3. Recognize the OS disk changes in VM
  4. Create custom image
  5. 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

az group create --name royhk --location eastasia

az vm create --resource-group royhk \   
     --name RoyRedhat \  
     --image RHEL \  
     --public-ip-address "" \  
     --size Standard_E8s_v3 \  
     --admin-username redhat \  
     --generate-ssh-keys

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

az vm deallocate -g royhk -n RoyRedhat

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

az vm start -g royhk -n RoyRedhat

[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.

partx -u /dev/sda2

xfs_growfs -d /dev/sda2

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

tutorial-custom-images

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.

ssh redhat@172.16.1.5
sudo waagent -deprovision+user -force
exit

az vm deallocate -g royhk -n RoyRedhat

az vm generalize --resource-group royhk --name RoyRedhat

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.

上一篇 下一篇

猜你喜欢

热点阅读