Skip to content

Instantly share code, notes, and snippets.

@tetsuyainfra
Forked from giovtorres/virt-install-centos
Last active March 20, 2017 04:17
Show Gist options
  • Save tetsuyainfra/21162977275a0a1b6b413ddf952b0f62 to your computer and use it in GitHub Desktop.
Save tetsuyainfra/21162977275a0a1b6b413ddf952b0f62 to your computer and use it in GitHub Desktop.
Install CentOS cloud images on KVM using cloud-init (noCloud) ※理解するために書き直し
#!/bin/bash
POOL=${POOL:-zfsimages}
POOL_PATH=${POOL_PATH:-"/ztank/libvirt/images"}
ORIGINAL_IMAGE=${ORIGINAL_IMAGE:-CentOS-7-x86_64-GenericCloud.qcow2}
VM_NAME=${1}
VM_RAM=1024
VM_CPU=1
VM_DISK=${1}.qcow2
VM_SEED_CD=seed_${1}.iso
if [[ -n $IPADDR ]]; then
VM_ETH1_IFACE=${VM_ETH1_IFACE:-"iface eth1 inet static"}
VM_ETH1_IPADDR="address ${IPADDR}"
VM_ETH1_MASK="netmask ${NETMASK:-"255.255.255.0"}"
fi
# Take one argument from the commandline: VM name
if ! [ $# -eq 1 ]; then
echo "Usage: $0 <node-name>"
exit 1
fi
# Check if domain already exists
virsh dominfo $1 > /dev/null 2>&1
if [ "$?" -eq 0 ]; then
echo -n "[WARNING] $1 already exists. "
read -p "Do you want to overwrite $1 (y/[N])? " -r
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo ""
virsh destroy $1 > /dev/null
virsh undefine $1 > /dev/null
else
echo -e "\nNot overwriting $1. Exiting..."
exit 1
fi
fi
LOG_FILE=${VM_NAME}.log
VM_UUID=$(uuidgen)
echo build kvm start from `date` > $LOG_FILE
echo "----------" | tee -a $LOG_FILE
echo "-- meta-data" | tee -a $LOG_FILE
cat <<EOS | tee -a meta-data
instance-id: ${VM_UUID}
local-hostname: ${VM_NAME}
network-interfaces: |
iface eth0 inet dhcp
${VM_ETH1_IFACE}
${VM_ETH1_IPADDR}
${VM_ETH1_MASK}
EOS
echo "-- user-data" | tee -a $LOG_FILE
cat <<EOS | tee -a user-data
#cloud-config
# user (centos)
password: passw0rd
chpasswd: { expire: False }
ssh_pwauth: True
manage_etc_hosts: true
hostname: ${VM_NAME}
fqdn: ${VM_NAME}.devopt.local
timezone: "Asia/Tokyo"
groups:
- ansible
# passwd: password
users:
- default
- name: ansible
gecos: ansible
primary-group: ansible
groups: wheel
selinux-user: staff_u
ssh-authorized-keys:
- "ssh-rsa HOGEHOGE"
sudo: "ALL=(ALL) NOPASSWD:ALL"
EOS
# create seed.iso
echo "-- genisoimage" | tee -a $LOG_FILE
genisoimage -output ${VM_SEED_CD} -volid cidata -joliet -rock user-data meta-data 2>&1 | tee -a $LOG_FILE
# volume delete if exists
virsh vol-info ${VM_DISK} --pool ${POOL} > /dev/null 2>&1
if [ "$?" -eq 0 ]; then
echo -n "[WARNING] ${VM_DISK} already exists. "
read -p "Do you want to delete ${VM_DISK} (y/[N])? " -r
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo ""
virsh vol-delete ${VM_DISK} --pool ${POOL}
else
echo -e "\nNot delete ${VM_DISK}. Exiting..."
exit 1
fi
fi
# volume clone
echo "-- qemu-img create " | tee -a $LOG_FILE
qemu-img create -f qcow2 -b "${POOL_PATH}/${ORIGINAL_IMAGE}" "${POOL_PATH}/${VM_DISK}" | tee -a $LOG_FILE
#virsh vol-clone ${ORIGINAL_IMAGE} ${VM_DISK} --pool ${POOL} | tee -a $LOG_FILE
virsh pool-refresh --pool ${POOL} | tee -a $LOG_FILE
virsh vol-list --pool ${POOL} | tee -a $LOG_FILE
virt-install \
--import \
--name ${VM_NAME} \
--uuid "${VM_UUID}" \
--hvm \
--virt-type kvm \
--ram ${VM_RAM} \
--vcpus ${VM_CPU} \
--arch x86_64 \
--os-type linux \
--os-variant rhel7 \
--boot hd \
--disk vol=${POOL}/${VM_DISK} \
--disk ${PWD}/${VM_SEED_CD},device=cdrom \
--network network=default,model=virtio \
--network bridge=br0,model=virtio \
--graphics none \
--console pty \
--serial pty
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment