first commit

This commit is contained in:
Renich Bon Ćirić 2022-09-05 20:42:49 -05:00
commit c6e5f6315f
3 changed files with 204 additions and 0 deletions

57
README.rst Normal file
View file

@ -0,0 +1,57 @@
=======================
CentOS Stream 9 @ Azure
=======================
Description
-----------
This repo provides some scripts to be able to generate a customized CentOS Stream 9 image that is usable on the Azure cloud.
The scripts were developed on Fedora 36 so I will asume you're using that or something very similar. Also, that you understand the
use of libvirt/kvm/qemu in order to run these images.
Pre-requisites
--------------
Install the following:
* libvirt-daemon
* qemu-img
* virt-manager
Instructions
------------
Installation
############
First, you need to deploy a minimal CentOS Stream 9 image. I did not use the OpenStack images (GenericCloud) or anything of the
sorts. I used the latest boot iso:
http://mirror.stream.centos.org/9-stream/BaseOS/x86_64/iso/CentOS-Stream-9-latest-x86_64-boot.iso
Make a minimal installation. A few requirements:
* The drive type should be raw.
* The drive size should be as small as possible. I used 5 GiB
* When partitioning, just create a regular set of partitions (/boot and /), no swap, no home and no LVM please.
* Do not create a regular user.
Setup
#####
After you finish the installation, login as root and run the setup script. You can just make it executable and execute it if you
prefer.
Otherwise, make sure you run every command.
The script will poweroff the machine. It should remove all keys and history.
Drive resize
############
Next, we need to resize the drive to Azure's specifications. For this, we use the resize script. The script requires the path to the
image. It will only calculate and print the commands that need to be ran.
Once you convert and resize the drive, you just need to upload it into a container and deploy it from there. You should be able to
follow the steps outlined here:
https://www.ibm.com/docs/en/sva/9.0.6?topic=mas-uploading-azure-compliant-vhd-azure-creating-azure-image
I couldn't find these instructions in Azure's documentation. They insist on one using their CLI tool.

120
azure-centos-setup.bash Executable file
View file

@ -0,0 +1,120 @@
#!/usr/bin/env bash
# This script sets up CentOS Stream 9 to be usable in Azure.
# It, mostly, follows the reference provided but, also, improves the procedure a bit.
#
# Reference: https://docs.microsoft.com/en-us/azure/virtual-machines/linux/create-upload-centos#centos-70
# verbosity
set -x
# step 1 and two are irrelevant
# step 3
cat << 'EOF' > /etc/default/networking
NETWORKING=yes
HOSTNAME=localhost.localdomain
EOF
# step 4
cat << 'EOF' > /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=dhcp
TYPE=Ethernet
USERCTL=no
PEERDNS=yes
IPV6INIT=no
EOF
## migrate to NetworkManager
nmcli conn migrate
# step 5
ln -s /dev/null /etc/udev/rules.d/75-persistent-net-generator.rules
# step 6
cat << 'EOF' > /etc/yum.repos.d/openlogic.repo
[openlogic]
name=CentOS-8 - openlogic packages for $basearch
baseurl=http://olcentgbl.trafficmanager.net/openlogic/8/openlogic/$basearch/
enabled=1
gpgcheck=0
EOF
# step 7
dnf -y upgrade
# step 8
grubby \
--update-kernel=ALL \
--remove-args='rhgb quiet crashkernel=1G-4G:192M,4G-64G:256M,64G-:512M' \
--args='rootdelay=300 console=ttyS0 earlyprintk=ttyS0 net.ifnames=0'
# step 9 (no need to do this)
#grub2-mkconfig -o /boot/grub2/grub.cfg
# step 10
cat << 'EOF' > /etc/dracut.conf.d/azure.conf
add_drivers+=" hv_vmbus hv_netvsc hv_storvsc "
EOF
dracut -fv
# step 11
dnf -y install python-pyasn1 WALinuxAgent
systemctl enable waagent
# step 12
dnf -y install cloud-init cloud-utils-growpart gdisk hyperv-daemons
## Configure waagent for cloud-init
sed -i 's/Provisioning.UseCloudInit=n/Provisioning.UseCloudInit=y/g' /etc/waagent.conf
sed -i 's/Provisioning.Enabled=y/Provisioning.Enabled=n/g' /etc/waagent.conf
echo "Adding mounts and disk_setup to init stage"
sed -i '/ - mounts/d' /etc/cloud/cloud.cfg
sed -i '/ - disk_setup/d' /etc/cloud/cloud.cfg
sed -i '/cloud_init_modules/a\\ - mounts' /etc/cloud/cloud.cfg
sed -i '/cloud_init_modules/a\\ - disk_setup' /etc/cloud/cloud.cfg
echo "Allow only Azure datasource, disable fetching network setting via IMDS"
cat << 'EOF' > /etc/cloud/cloud.cfg.d/91-azure_datasource.cfg
datasource_list: [ Azure ]
datasource:
Azure:
apply_network_config: False
EOF
if [[ -f /mnt/resource/swapfile ]]; then
echo Removing swapfile - RHEL uses a swapfile by default
swapoff /mnt/resource/swapfile
rm /mnt/resource/swapfile -f
fi
echo "Add console log file"
cat << 'EOF' >> /etc/cloud/cloud.cfg.d/05_logging.cfg
## This tells cloud-init to redirect its stdout and stderr to
## 'tee -a /var/log/cloud-init-output.log' so the user can see output
## there without needing to look on the console.
output: {all: '| tee -a /var/log/cloud-init-output.log'}
EOF
# step 13
sed -i 's/ResourceDisk.Format=y/ResourceDisk.Format=n/g' /etc/waagent.conf
sed -i 's/ResourceDisk.EnableSwap=y/ResourceDisk.EnableSwap=n/g' /etc/waagent.conf
# step 14
rm -f /var/log/waagent.log
cloud-init clean
waagent -force -deprovision+user
rm -f ~/.bash_history
export HISTSIZE=0
systemctl poweroff

27
azure-resize.bash Executable file
View file

@ -0,0 +1,27 @@
#!/usr/bin/env bash
# This script resizes a drive to what Azure requires.
#
# reference: https://docs.microsoft.com/en-us/azure/virtual-machines/linux/create-upload-generic#resizing-vhds
rawdisk="$1"
MB=$(( 1024*1024 ))
size=$( qemu-img info -f raw --output json "$rawdisk" | gawk 'match($0, /"virtual-size": ([0-9]+),/, val) {print val[1]}' )
rounded_size=$(( ( ( $size + $MB - 1 ) / $MB ) * $MB ))
cat << EOF
Rounded Size = $rounded_size
Resize it with:
qemu-img resize $rawdisk $rounded_size
Convert it with:
qemu-img convert -f raw -o subformat=fixed,force_size -O vpc $rawdisk my_os.vhd
EOF