You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

273 lines
9.7 KiB

#!/bin/bash
# create-cumulus-vx-image.sh
# HS-Fulda - sebastian.rieger@informatik.hs-fulda.de
#
# changelog:
#
# V0.2 added support to delete existing image with the same name and generating the default nova flavor
# V0.3 checking whether it is safe to unmount the working directory
# V0.3.1 added support for newer glance releases (e.g. kilo) used in VIRL 1.0.0
# V0.4 fixed check for existing images, added support for Cumulus VX 3.x
# V0.41 removed debug bash
# V0.5 fixed detection of missing loop files for Ubuntu 16.04 in VIRL >=1.3, added default subtype creation
# V0.51 changed position of grub.cfg in cumulusvx >= 3.x (thanks to bobskye)
# usage
if [ ! $# -eq 2 ] ; then
echo -e "usage: $0 <CumulusVX.qcow2> <new glance image name>, e.g.:\n"
echo "$0 CumulusVX-2.5.3-f3df86c478e1a4ef.qcow2 CumulusVX"
exit -1
fi
# sudo check
if [ ! $UID -eq 0 ] ; then
echo "Insufficient privileges. Please consider using sudo -s."
exit -1
fi
CUMULUS_QCOW2=$1
CUMULUS_QCOW2_BASENAME=$(basename -s .qcow2 $1)
CUMULUS_PATCHED_QCOW2=$1-patched.qcow2
GLANCE_IMAGE_NAME=$2
GLANCE_IMAGE_RELEASE=$CUMULUS_QCOW2_BASENAME
TMP_NAME="CumulusVX-$GLANCE_IMAGE_RELEASE"
TIMESTAMP=$(date +%Y%m%d%H%M%S)
function safe_unmount() {
echo -n "Unmounting $1..."
RETRY=0
until umount $1 &>/dev/null
do
echo -n "."
sleep 1
RETRY=$((RETRY+1))
if [ "$RETRY" -ge "5" ] ; then
echo
echo "ERROR: unable to unmount working directory $1"
exit 1
fi
done
echo
return 0
}
# check for an existing image with the same name and offer to delete it prior to creating a new one
CHECK_FOR_EXISTING_IMAGE=$(glance --os-image-api-version 1 image-show $GLANCE_IMAGE_NAME 2>&1)
if [ $? == 0 ] ; then
glance --os-image-api-version 1 image-show $GLANCE_IMAGE_NAME
echo
echo
read -r -p "There is already an image with the same name in glance. Do you want to overwrite it? [y/N] " RESPONSE
if [[ $RESPONSE =~ ^([yY][eE][sS]|[yY])$ ]] ; then
echo "Deleting existing image $GLANCE_IMAGE_NAME..."
echo "==========================================================="
glance --os-image-api-version 1 image-delete $GLANCE_IMAGE_NAME
else
echo "An image with the same name already exists. Either delete this image or choose another name."
exit 1
fi
fi
echo
echo "Creating CumulusVX image..."
echo "==========================================================="
# check version
if [[ "$CUMULUS_QCOW2_BASENAME" =~ ^cumulus-vx-2\.5 ]] ; then
# handle 2.5.x versions
CUMULUS_VERSION=2
elif [[ "$CUMULUS_QCOW2_BASENAME" =~ ^cumulus-linux-3\. ]] ; then
# handle 3.x versions
CUMULUS_VERSION=3
else
echo "Unsupported version of Cumulus VX, currently this script supports versions 2.5.x and 3.x"
exit 1
fi
qemu-img convert -O raw $CUMULUS_QCOW2 $CUMULUS_QCOW2_BASENAME.raw
LOOPDEV=$(kpartx -av $CUMULUS_QCOW2_BASENAME.raw)
if [ $CUMULUS_VERSION == 2 ]; then
# partition layout Cumulus VX 2.5.x: part1 = boot, part2 = root
LOOPDEV_PART_BOOT=$(echo "$LOOPDEV" | sed '1q;d' | cut -d " " -f 3)
LOOPDEV_PART_ROOT=$(echo "$LOOPDEV" | sed '2q;d' | cut -d " " -f 3)
elif [ $CUMULUS_VERSION == 3 ]; then
# partition layout Cumulus VX 3.x: part1 = grubboot, part2 = onieboot, part3 = boot, part4 = root
LOOPDEV_PART_BOOT=$(echo "$LOOPDEV" | sed '3q;d' | cut -d " " -f 3)
LOOPDEV_PART_ROOT=$(echo "$LOOPDEV" | sed '4q;d' | cut -d " " -f 3)
fi
echo -n "waiting for loop devs from kpartx"
LOOP_DEV_RETRIES=0
until dd if=/dev/mapper/$LOOPDEV_PART_BOOT of=/dev/null bs=1k count=1 &>/dev/null && dd if=/dev/mapper/$LOOPDEV_PART_ROOT of=/dev/null bs=1k count=1 &>/dev/null ; do
echo -n "."
LOOP_DEV_RETRIES=$(expr $LOOP_DEV_RETRIES + 1)
if [ $LOOP_DEV_RETRIES -eq 10 ]; then
echo
echo "ERROR: timeout waiting for loop devs from kaprtx"
exit
fi
sleep 1
done
echo
mkdir cumulusvx-boot-$TIMESTAMP
mkdir cumulusvx-root-$TIMESTAMP
echo
echo "Injecting changes to use serial console and startup script to get switch config..."
echo "=================================================================================="
mount /dev/mapper/$LOOPDEV_PART_BOOT cumulusvx-boot-$TIMESTAMP
mount /dev/mapper/$LOOPDEV_PART_ROOT cumulusvx-root-$TIMESTAMP
if [ $CUMULUS_VERSION == 2 ]; then
# changing grub and inittab to use a serial console on kernel command line
sed -i.bak -e s/"linux \/bzImage root=\/dev\/sda2"/"linux \/bzImage root=\/dev\/sda2 console=ttyS0 console=tty0"/g cumulusvx-root-$TIMESTAMP/vbox_grub.cfg
sed -i.bak -e s/"linux \/bzImage root=\/dev\/sda2"/"linux \/bzImage root=\/dev\/sda2 console=ttyS0 console=tty0"/g cumulusvx-boot-$TIMESTAMP/grub/grub.cfg
sed -i.bak -e s/"# S0:3:respawn:\/sbin\/getty -L \$(get-cmdline-console) vt100"/"S0:3:respawn:\/sbin\/getty -L \$(get-cmdline-console) vt100"/g cumulusvx-root-$TIMESTAMP/etc/inittab
elif [ $CUMULUS_VERSION == 3 ]; then
# changing grub to show boot log
sed -i.bak -e s/"${extra_cmdline} quiet"/"${extra_cmdline}"/g cumulusvx-root-$TIMESTAMP/boot/grub/grub.cfg
fi
# append a script to import the configuration defined in VM Maestro to rc.local
sed -i.bak -e s/"^exit 0$"/""/g cumulusvx-root-$TIMESTAMP/etc/rc.local
cat << EOF >> cumulusvx-root-$TIMESTAMP/etc/rc.local
mkdir /virl-config
mount /dev/sdb1 /virl-config
chmod +x /virl-config/cumulusvx.sh
/virl-config/cumulusvx.sh >/var/log/virl-startup.log
EOF
#DEBUG:
# run bash to allow manual changes to the image before packing
#
#bash
safe_unmount cumulusvx-boot-$TIMESTAMP
safe_unmount cumulusvx-root-$TIMESTAMP
rm -rf cumulusvx-boot-$TIMESTAMP
rm -rf cumulusvx-root-$TIMESTAMP
LOOP_DEV_DEL_RETRIES=0
echo "removing loop devs from kpartx"
until kpartx -vd $CUMULUS_QCOW2_BASENAME.raw ; do
echo -n "."
LOOP_DEV_DEL_RETRIES=$(expr $LOOP_DEV_DEL_RETRIES + 1)
if [ $LOOP_DEV_DEL_RETRIES -eq 10 ]; then
echo
echo "ERROR: timeout waiting for loop dev removal from kaprtx"
exit
fi
sleep 1
done
echo
echo "Converting new image to qcow2..."
echo "==========================================================="
qemu-img convert -O qcow2 $CUMULUS_QCOW2_BASENAME.raw $CUMULUS_PATCHED_QCOW2
echo
echo "Cleaning up..."
echo "==========================================================="
rm $CUMULUS_QCOW2_BASENAME.raw
echo
echo "Importing image into glance..."
echo "==========================================================="
# use e1000 for now as with virtio we get dhcp errors due to "bad udp checksum" in Debian
glance image-create --container-format bare --disk-format qcow2 --visibility public --name $GLANCE_IMAGE_NAME \
--file $CUMULUS_PATCHED_QCOW2 --property hw_disk_bus=ide --property serial=1 \
--property hw_vif_model=e1000 --property hw_cdrom_type=ide --property release="$GLANCE_IMAGE_RELEASE" --property subtype=CumulusVX --property config_disk_type=disk
# create default flavor
CHECKING_FOR_EXISTING_FLAVOR=$(nova flavor-show CumulusVX.small 2>&1)
if [ $? == 1 ]; then
echo "Creating default flavor CumulusVX.small..."
echo "==========================================================="
nova flavor-create --is-public true CumulusVX.small auto 256 0 1
fi
# create default subtype
cat << EOF > dynamic-subtype-$GLANCE_IMAGE_NAME.json.default.virl-above-1.3
{
"dynamic-subtypes": [
{
"plugin_base": "generic",
"device_type": "switch",
"plugin_desc": "CumulusVX",
"cli_protocol": "ssh",
"plugin_name": "$GLANCE_IMAGE_NAME",
"cli_serial": 1,
"interface_range": 25,
"gui_visible": true,
"interface_pattern": "swp{0}",
"baseline_image": "$GLANCE_IMAGE_NAME",
"config_disk_type": "disk",
"hw_vm_extra": "",
"gui_icon": "iosvl2",
"config_file": "/cumulusvx.sh",
"interface_first": 1,
"deprecated_use": "",
"baseline_flavor": "$GLANCE_IMAGE_NAME.small",
"interface_management": "eth0"
}
]
}
EOF
cat << EOF > dynamic-subtype-$GLANCE_IMAGE_NAME.json.default.virl-below-1.3
{
"dynamic-subtypes": [
{
"plugin_base": "generic",
"plugin_desc": "CumulusVX",
"cli_protocol": "ssh",
"plugin_name": "$GLANCE_IMAGE_NAME",
"cli_serial": 1,
"interface_range": 25,
"gui_visible": true,
"interface_pattern": "swp{0}",
"baseline_image": "$GLANCE_IMAGE_NAME",
"config_disk_type": "disk",
"hw_vm_extra": "",
"gui_icon": "iosvl2",
"config_file": "/cumulusvx.sh",
"interface_first": 1,
"deprecated_use": "",
"baseline_flavor": "$GLANCE_IMAGE_NAME.small",
"interface_management": "eth0"
}
]
}
EOF
CHECKING_FOR_EXISTING_SUBTYPE=$(virl_uwm_client subtype-info --name $GLANCE_IMAGE_NAME 2>&1)
if [ $? == 255 ]; then
echo "Creating default subtype $GLANCE_IMAGE_NAME..."
echo "==========================================================="
CHECKING_FOR_EXISTING_DEVICE_TYPE=$(virl_uwm_client subtype-info 2>&1 | grep u\'device_type\':)
if [ $? == 1 ]; then
# device_type attribute is not available in VIRL < 1.3, use JSON definition of subtype without this attribute
echo "detected VIRL version < 1.3, selecting appropriate subtype to import"
virl_uwm_client subtype-import --dynamic-subtypes @dynamic-subtype-$GLANCE_IMAGE_NAME.json.default.virl-below-1.3
else
echo "detected VIRL version > 1.3.0, selecting appropriate subtype to import"
# device_type attribute is available, use JSON definition of subtype including this attribute
virl_uwm_client subtype-import --dynamic-subtypes @dynamic-subtype-$GLANCE_IMAGE_NAME.json.default.virl-above-1.3
fi
else
echo "Default subtype $GLANCE_IMAGE_NAME already exists..."
fi
rm dynamic-subtype-$GLANCE_IMAGE_NAME.json.default.virl-above-1.3
rm dynamic-subtype-$GLANCE_IMAGE_NAME.json.default.virl-below-1.3
echo
echo "Image creation successful."
echo "==========================================================="
echo
echo "You can import and use the subtype $GLANCE_IMAGE_NAME in VM Maestro..."