Skip to content

Instantly share code, notes, and snippets.

@mbodo
Forked from ebal/cloud-init.sh
Created November 7, 2021 13:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbodo/c551b767b80d52a2df6a4dfde292d5d1 to your computer and use it in GitHub Desktop.
Save mbodo/c551b767b80d52a2df6a4dfde292d5d1 to your computer and use it in GitHub Desktop.
Cloud-init example using a Generic Cloud CentOS-7 on a libvirtd qmu/kvm lab
#!/bin/sh
if [ "$EUID" -ne 0 ]; then
echo -e "\nPlease run as root\neg. sudo $0 \n"
exit
fi
GITHUB_USERNAME="ebal"
cd `mktemp -d`
## Create meta-data
cat > meta-data <<EOF
instance-id: testingcentos7
local-hostname: testingcentos7
network-interfaces: |
iface eth0 inet static
address 192.168.122.228
network 192.168.122.0
netmask 255.255.255.0
broadcast 192.168.122.255
gateway 192.168.122.1
# vim:syntax=yaml
EOF
## Create user-data
cat > user-data <<EOF
#cloud-config
# Set default user and their public ssh key
# eg. https://github.com/ebal.keys
users:
- name: ${GITHUB_USERNAME}
ssh-authorized-keys:
- `curl -s -L https://github.com/${GITHUB_USERNAME}.keys`
sudo: ALL=(ALL) NOPASSWD:ALL
# Enable cloud-init modules
cloud_config_modules:
- resolv_conf
- runcmd
- timezone
- package-update-upgrade-install
# Set TimeZone
timezone: Europe/Athens
# Set DNS
manage_resolv_conf: true
resolv_conf:
nameservers: ['9.9.9.9']
# Install packages
packages:
- mlocate
- vim
- epel-release
# Update/Upgrade & Reboot if necessary
package_update: true
package_upgrade: true
package_reboot_if_required: true
# Remove cloud-init
runcmd:
- yum -y remove cloud-init
- updatedb
# Configure where output will go
output:
all: ">> /var/log/cloud-init.log"
# vim:syntax=yaml
EOF
## Create ISO with meta-data & user-data
genisoimage -output cloud-init.iso -volid cidata -joliet -rock user-data meta-data
## Destroy & Undefine previous Libvirt image
virsh destroy --domain centos7_test
virsh undefine --domain centos7_test
## Download Generic Image (if necessary)
curl -LO -C - http://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud.qcow2.xz
## Uncompress
xz -v --keep -d CentOS-7-x86_64-GenericCloud.qcow2.xz
## Give perms
chown -R nobody:kvm .
## Provision
virt-install \
--connect qemu:///system \
--name centos7_test \
--memory 2048 \
--vcpus 1 \
--metadata description="My centos7 cloud-init test" \
--import \
--disk CentOS-7-x86_64-GenericCloud.qcow2,format=qcow2,bus=virtio \
--disk cloud-init.iso,device=cdrom \
--network bridge=virbr0,model=virtio \
--os-type=linux \
--os-variant=centos7.0 \
--noautoconsole
## Print msg
echo -e " \n ssh ${GITHUB_USERNAME}@192.168.122.228 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no \n "
rm -rf `pwd -P`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment