Browse Source

Added ability to resize disk.

- set RESIZE_DISK to true and DISK_SIZE to your desired size
pull/1/head
Giovanni Torres 9 years ago
parent
commit
76ec4a3ed2
  1. 69
      virt-install-centos

69
virt-install-centos

@ -12,9 +12,15 @@ if [ "$?" -eq 0 ]; then
echo -n "[WARNING] $1 already exists. " echo -n "[WARNING] $1 already exists. "
read -p "Do you want to overwrite $1 (y/[N])? " -r read -p "Do you want to overwrite $1 (y/[N])? " -r
if [[ $REPLY =~ ^[Yy]$ ]]; then if [[ $REPLY =~ ^[Yy]$ ]]; then
echo "" echo "$(date -R) Destroying the $1 domain..."
virsh destroy $1 > /dev/null # Remove domain with the same name
virsh undefine $1 > /dev/null virsh destroy $1 >> $1.log 2>&1
virsh undefine $1 >> $1.log 2>&1
echo "$(date -R) Destroying the $1 storage pool..."
# Remove the associated pool
virsh pool-destroy $1 >> $1.log 2>&1
virsh pool-undefine $1 >> $1.log 2>&1
else else
echo -e "\nNot overwriting $1. Exiting..." echo -e "\nNot overwriting $1. Exiting..."
exit 1 exit 1
@ -28,11 +34,15 @@ DIR=~/virt/images
IMAGE=$DIR/CentOS-7-x86_64-GenericCloud.qcow2 IMAGE=$DIR/CentOS-7-x86_64-GenericCloud.qcow2
# Amount of RAM in MB # Amount of RAM in MB
MEM=768 MEM=1024
# Number of virtual CPUs # Number of virtual CPUs
CPUS=1 CPUS=1
# Disk Size
RESIZE_DISK=false
DISK_SIZE=20G
# Cloud init files # Cloud init files
USER_DATA=user-data USER_DATA=user-data
META_DATA=meta-data META_DATA=meta-data
@ -51,12 +61,6 @@ pushd $DIR/$1 > /dev/null
# Create log file # Create log file
touch $1.log touch $1.log
echo "$(date -R) Destroying the $1 domain (if it exists)..."
# Remove domain with the same name
virsh destroy $1 >> $1.log 2>&1
virsh undefine $1 >> $1.log 2>&1
# cloud-init config: set hostname, remove cloud-init package, # cloud-init config: set hostname, remove cloud-init package,
# and add ssh-key # and add ssh-key
cat > $USER_DATA << _EOF_ cat > $USER_DATA << _EOF_
@ -67,29 +71,34 @@ preserve_hostname: False
hostname: $1 hostname: $1
fqdn: $1.example.local fqdn: $1.example.local
# Remove cloud-init when finished with it
runcmd:
- [ yum, -y, remove, cloud-init ]
# Configure where output will go # Configure where output will go
output: output:
all: ">> /var/log/cloud-init.log" all: ">> /var/log/cloud-init.log"
# configure interaction with ssh server # configure interaction with ssh server
ssh_svcname: ssh ssh_genkeytypes: ['ed25519', 'rsa']
ssh_deletekeys: True
ssh_genkeytypes: ['rsa', 'ecdsa']
# Install my public ssh key to the first user-defined user configured # Install my public ssh key to the first user-defined user configured
# in cloud.cfg in the template (which is centos for CentOS cloud images) # in cloud.cfg in the template (which is centos for CentOS cloud images)
ssh_authorized_keys: ssh_authorized_keys:
- ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDBil2QzORhDcnKiVVNpO5daOSYVp8nshcIc7aTEkdlqCRir2Oni8BEStK7x7bvh0jrp9KptlHPeos87fQs//VXEb1FEprL2c6fPWmVdtjmYw3yzSkaFKMksL7FdUoEiwF6t8pQAg2mU0Qj9emSHBKg5ttdGqNoSvXc92k7iOzgauda7jdNak+Dx9dPhR3FJwHMcZSlQHO4cweZcK63bZitxlFkJ/FJdry/TBirDhRcXslOJ3ECU2xiyRXJVPs3VNLjMdOTTAoMmZj+GraUBbQ9VIqe683xe02sM83th5hj2C4gW3qXUoFkNLfKAMRxXLRMEwI3ABFB/AAUhACxyTJp giovanni@throwaway - ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDBil2QzORhDcnKiVVNpO5daOSYVp8nshcIc7aTEkdlqCRir2Oni8BEStK7x7bvh0jrp9KptlHPeos87fQs//VXEb1FEprL2c6fPWmVdtjmYw3yzSkaFKMksL7FdUoEiwF6t8pQAg2mU0Qj9emSHBKg5ttdGqNoSvXc92k7iOzgauda7jdNak+Dx9dPhR3FJwHMcZSlQHO4cweZcK63bZitxlFkJ/FJdry/TBirDhRcXslOJ3ECU2xiyRXJVPs3VNLjMdOTTAoMmZj+GraUBbQ9VIqe683xe02sM83th5hj2C4gW3qXUoFkNLfKAMRxXLRMEwI3ABFB/AAUhACxyTJp giovanni@throwaway
# Remove cloud-init when finished with it
#runcmd:
# - [ yum, -y, remove, cloud-init ]
_EOF_ _EOF_
echo "instance-id: $1; local-hostname: $1" > $META_DATA echo "instance-id: $1; local-hostname: $1" > $META_DATA
echo "$(date -R) Copying template image..." echo "$(date -R) Copying cloud image..."
cp $IMAGE $DISK cp $IMAGE $DISK
if $RESIZE_DISK
then
echo "$(date -R) Resizing the disk to $DISK_SIZE..."
qemu-img create -f qcow2 -o preallocation=metadata $DISK.new $DISK_SIZE >> $1.log 2>&1
virt-resize --quiet --expand /dev/sda1 $DISK $DISK.new >> $1.log 2>&1
mv $DISK.new $DISK
fi
# Create CD-ROM ISO with cloud-init config # Create CD-ROM ISO with cloud-init config
echo "$(date -R) Generating ISO for cloud-init..." echo "$(date -R) Generating ISO for cloud-init..."
@ -97,14 +106,23 @@ _EOF_
echo "$(date -R) Installing the domain and adjusting the configuration..." echo "$(date -R) Installing the domain and adjusting the configuration..."
echo "[INFO] Installing with the following parameters:" echo "[INFO] Installing with the following parameters:"
echo "virt-install --import --name $1 --ram $MEM --vcpus $CPUS --disk echo " virt-install --import --name $1 --ram $MEM --vcpus $CPUS --disk \
$DISK,format=qcow2,bus=virtio --disk $CI_ISO,device=cdrom --network $DISK,format=qcow2,bus=virtio --disk $CI_ISO,device=cdrom --network \
bridge=virbr0,model=virtio --os-type=linux --os-variant=rhel7 --noautoconsole" bridge=virbr0,model=virtio --os-type=linux --os-variant=rhel7 --noautoconsole"
virt-install --import --name $1 --ram $MEM --vcpus $CPUS --disk \ virt-install --import --name $1 --ram $MEM --vcpus $CPUS --disk \
$DISK,format=qcow2,bus=virtio --disk $CI_ISO,device=cdrom --network \ $DISK,format=qcow2,bus=virtio --disk $CI_ISO,device=cdrom --network \
bridge=virbr0,model=virtio --os-type=linux --os-variant=rhel7 --noautoconsole bridge=virbr0,model=virtio --os-type=linux --os-variant=rhel7 --noautoconsole
virsh dominfo $1 >> $1.log 2>&1
# Eject cdrom
echo "$(date -R) Cleaning up cloud-init..."
virsh change-media $1 hda --eject --config >> $1.log
# Remove the unnecessary cloud init files
rm $USER_DATA $META_DATA $CI_ISO
MAC=$(virsh dumpxml $1 | awk -F\' '/mac address/ {print $2}') MAC=$(virsh dumpxml $1 | awk -F\' '/mac address/ {print $2}')
while true while true
do do
@ -118,13 +136,6 @@ _EOF_
fi fi
done done
# Eject cdrom
echo "$(date -R) Cleaning up cloud-init..."
virsh change-media $1 hda --eject --config >> $1.log
# Remove the unnecessary cloud init files
rm $USER_DATA $CI_ISO
echo "$(date -R) DONE. SSH to $1 using $IP with username 'centos'." echo "$(date -R) DONE. SSH to $1 using $IP with username 'centos'."
popd > /dev/null popd > /dev/null

Loading…
Cancel
Save