Tuesday, 8 October 2013

Exalogic Virtual Tea Break Snippets – Modifying the Default LVM Guest Template

With the latest release of the Exalogic Virtual Environment (2.0.6.0.0) an number of modifications have been implemented and one of these is the introduction of an LVM based Guest Template. LVM was a much requested feature for the base template but it's introduction means the information provided in my blog entry Exalogic Virtual Tea Break Snippets - Modifying the Default Shipped (Base) Template is no longer appropriate because modifyjeos does not work with an LVM based System.img.

To resize the new Base Template we need to work with LVM directly and working with colleagues I have put together a simple script that will allow you to increase the size of the default root Volume and Swap Volume and thus generate a new template.

This script is best run on the Exalogic Control VM but can be run on one of the Exalogics Compute nodes at the OVS level. If you choose to run the script on a compute node you will need to modify the listing filter within the /etc/lvm/lvm.conf file on the chosen compute node. This can be done by editing the filter line as follows:

 # By default for OVS we restrict every block device:
 filter = [ "a/.*/" ]

Example

In the following example we will modify the Template to increase the root Volume by 20 GB and the and the swap by 1 GB.

Step 1 - Mount Images Directory

If you have installed the Base Template, mount the /export/common/images from the ZFS storage and you will be able to find the el_base_linux_guest_vm_template_2.0.6.0.0_64.tgz. Alternatively it can be downloaded from the Oracle Software Delivery Cloud (Part Number: V39450-01).

Step 2 - Extract Template

Once the Template el_base_linux_guest_vm_template_2.0.6.0.0_64.tgz is available, extract it as follows:
tar xvzf ./el_base_linux_guest_vm_template_2.0.6.0.0_64.tgz 
This will create a directory called BASE which will contain the System.img (VServer image) and vm.cfg (VServer Config information). Change directory into the newly created BASE directory.

Step 3 - Modify Syatem.img

Execute the script as follows:

ModifyLVMImg.sh -if System.img -er 20 -es 1
This will result in the root file system being extended by 20 GB to 25GB and the swap from 0.5 GB to 1.5 GB.

Step 4 - Re-Package Base Template

After running the script and taring the modified System.img into an appropriate .tgz it can but uploaded to EMOC.

Repackage the Base Template with the following command:

tar -pczvf ./el_base_linux_guest_vm_template_2.0.6.0.0_64_expanded.tgz ./BASE

Step 5 - Test Template

Creating a vServer from the new template and executing df -kh will display the following:

[root@test25Gb ~]# df -kh
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
 25G 3.3G 20G 15% /
/dev/xvda1 99M 23M 71M 25% /boot
tmpfs 2.0G 0 2.0G 0% /dev/shm
[root@test25Gb ~]#
As we can see the root volume has been successfully increased in size.

ModifyLVMImg.sh Script

Usage

[root@exalogic-iaas-vbox BASE]# ModifyLVMImg.sh -help



##########################################################################################
##                                                                                      ##
##                                     DISCLAIMER                                       ##
##                                                                                      ##
##  THIS SCRIPT IS PROVIDED ON AN AS IS BASIS, WITHOUT WARRANTY OF ANY KIND,            ##
##  EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES THAT         ##
##  THE COVERED SCRIPT IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR           ##
##  PURPOSE OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE        ##
##  OF THE COVERED SOFTWARE IS WITH YOU. SHOULD ANY COVERED SOFTWARE PROVE              ##
##  DEFECTIVE IN ANY RESPECT, YOU (NOT THE INITIAL DEVELOPER OR ANY OTHER               ##
##  CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY SERVICING, REPAIR OR CORRECTION.      ##
##                                                                                      ##
##  THIS SCRIPT IS NOT PART OF THE BASE PRODUCT SET AND AS SUCH, ALTHOUGH FUNCTIONAL,   ##
##  IS PROVIDED ONLY AS AN EXAMPLE OF WHAT CAN BE DONE.                                 ##
##                                                                                      ##
##########################################################################################




usage: /root/bin/ModifyLVMImg.sh [-if ] [-er ] [-es ] [ -bs ] [-vg ] [-rlv ] [-slv ] [-nvg ] [-nrlv ] [-nslv ]

          -if  : This specificies the image file to be processed and will default to System.img
          -er  : This is the additional amount of space that will be added to the root Logical Volume (in GB). Defaults to 0
          -es  : This is the additional amount of space that will be added to the swap Logical Volume (in GB). Defaults to 0
          -bs  : Block size (in bytes) to be used whilst extending. Defaults to 1024 bytes
          -vg  : Name of the current Volume Group. Default VolGroup00
          -rlv  : Name of the current Root Logical Volume. Default LogVol00
          -slv  : Name of the current Swap Logical Volume. Default LogVol01
          -nvg  : Name of the new Volume Group. If not specified then the name will not be changed
          -nrlv  : Name of the new Root Logical Volume. If not specified then the name will not be changed
          -nslv  : Name of the new Swap Logical Volume. If not specified then the name will not be changed

Script

#!/bin/bash

################################################################################
#
# Exalogic Virtual (Linux x86-64) Simplified CLI
# HEADER START
#
# THIS SCRIPT IS PROVIDED ON AN AS IS BASIS, WITHOUT WARRANTY OF ANY KIND,
# EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES THAT
# THE COVERED SCRIPT IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR
# PURPOSE OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE
# OF THE COVERED SOFTWARE IS WITH YOU. SHOULD ANY COVERED SOFTWARE PROVE
# DEFECTIVE IN ANY RESPECT, YOU (NOT THE INITIAL DEVELOPER OR ANY OTHER
# CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY SERVICING, REPAIR OR CORRECTION.
# NO USE OF ANY COVERED SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER THIS
# DISCLAIMER.
#
# When distributing this Code, include this HEADER in each file.
# If applicable, add the following below this this HEADER, with the fields
# enclosed by brackets "[]" replaced with your own identifying information:
# Portions Copyright [yyyy] [name of copyright owner]
#
# HEADER END
#
#
# Copyright 2013 Andrew Hopkinson, Oracle Corporation UK Ltd.
#
################################################################################


function extendImg() {

echo "***********************************************"
echo "*** Extending $SYSTEMIMG"
echo "***********************************************"
dd if=/dev/zero of=$SYSTEMIMG bs=$BLOCKSIZE count=0 seek=$ADDBLOCKS

export LOOP=`losetup -f`
echo "***********************************************"
echo "*** Using loop $LOOP"
echo "***********************************************"

losetup $LOOP $SYSTEMIMG

# n = New Partition
# p = Primary
# 3 = Partition 3
# = Default first cylinder
# = Default last cylinder
# t = Change partition system id
# 3 = Partition Id
# 8e = Linux LVM
# w = Write & Exit

fdisk $LOOP <<EOF
n
p
3


t
3
8e
w
EOF

#echo "***********************************************"
#echo "*** Pausing $SLEEP seconds for previous command"
#echo "***********************************************"
#sleep $SLEEP

echo "***********************************************"
echo "*** Mounting $LOOP"
echo "***********************************************"
msgArray=( $(kpartx -av $LOOP | sed 's/ /|/g') )
echo "==============================================="
for msg in "${msgArray[@]}"
do
echo "$msg"
loopId=${msg%%|*}
msg=${msg#*|}
loopId=${msg%%|*}
msg=${msg#*|}
loopId=${msg%%|*}
msg=${msg#*|}
done
echo "Using loopId: $loopId"
echo "==============================================="

echo "***********************************************"
echo "*** Scanning Volume Groups"
echo "***********************************************"
vgscan
#pvdisplay
#vgdisplay
#lvdisplay

#echo "***********************************************"
#echo "*** Pausing $SLEEP seconds for previous command"
#echo "***********************************************"
#sleep $SLEEP

echo "***********************************************"
echo "*** Extending $VGNAME"
echo "***********************************************"
#msg=$(vgextend VolGroup00 /dev/mapper/`basename $LOOP`p3)
msg=$(vgextend $VGNAME /dev/mapper/$loopId 2>&1)
echo "==============================================="
echo "$msg"
echo "==============================================="
if [[ "$msg" == *"$VGNAME"* && "$msg" == *"successfully extended"* ]]
then

vgdisplay

echo "***********************************************"
echo "***** Extending Root $ROOTLVNAME"
echo "***********************************************"
lvextend -L+`echo $EXTROOT`G /dev/$VGNAME/$ROOTLVNAME

echo "***********************************************"
echo "***** Extending Swap $SWAPLVNAME"
echo "***********************************************"
lvextend -L+`echo $EXTSWAP`G /dev/$VGNAME/$SWAPLVNAME
#lvdisplay

echo "***********************************************"
echo "*** Changing $VGNAME"
echo "***********************************************"
vgchange -ay $VGNAME

echo "***********************************************"
echo "***** Checking Root $ROOTLVNAME"
echo "***********************************************"
e2fsck -f /dev/mapper/$VGNAME-$ROOTLVNAME

echo "***********************************************"
echo "***** Resizing Root $ROOTLVNAME"
echo "***********************************************"
resize2fs /dev/mapper/$VGNAME-$ROOTLVNAME

echo "***********************************************"
echo "***** Setting Swap $SWAPLVNAME"
echo "***********************************************"
mkswap /dev/mapper/$VGNAME-$SWAPLVNAME

vgchange -an $VGNAME
lvdisplay

else
echo ""
echo ""
echo "***********************************************"
echo "************** ERROR ********************"
echo "***********************************************"
echo ""
echo " Validate that the filter entry for OVS has been "
echo " changed from :"
echo " filter = [ "r/.*/" ]"
echo " to :"
echo " filter = [ "a/.*/" ]"
echo ""
echo " /etc/lvm/lvm.conf"
echo ""
echo "***********************************************"
echo ""
echo ""
fi

echo "***********************************************"
echo "*** Unmounting $LOOP"
echo "***********************************************"
kpartx -dv $LOOP
losetup -d $LOOP
}

function usage() {
echo ""
echo >&2 "usage: $0 [-if <System Image File>] [-er <Root Extend Size in Gb>] [-es <Swap Extend Size in Gb>] [ -bs <Block Size bytes>]"
echo >&2 " "

exit 1
}


export BASEIMAGESIZE=6
export SYSTEMIMG=System.img
export EXTROOT=0
export EXTSWAP=0
export BLOCKSIZE=1024
export GIGABYTE=`expr 1024 \* 1024 \* 1024`
export SLEEP=10
export VGNAME=VolGroup00
export ROOTLVNAME=LogVol00
export SWAPLVNAME=LogVol01

while [ $# -gt 0 ]
do
case "$1" in
-if) SYSTEMIMG="$2"; shift;;
-er) EXTROOT="$2"; shift;;
-es) EXTSWAP="$2"; shift;;
-bs) BLOCKSIZE="$2"; shift;;
-vg) VGNAME="$2"; shift;;
-rlv) ROOTLVNAME="$2"; shift;;
-slv) SWAPLVNAME="$2"; shift;;
*) usage;;
*) break;;
esac
shift
done

#rpm -qid lvm2

echo "***********************************************"
echo "*** Resizing $SYSTEMIMG"
echo "***********************************************"


# Calculate sizing
# Spare blocks to get around extent issue, i.e. missing 1 extent
SPAREBLOCKS=$((($GIGABYTE / $BLOCKSIZE) / 2))
BASEIMAGESIZE=$(stat -c%s "$SYSTEMIMG")
BASEIMGBLOCKS=$(($BASEIMAGESIZE / $BLOCKSIZE))
ROOTADD=$(($EXTROOT * $GIGABYTE / $BLOCKSIZE))
SWAPADD=$(($EXTSWAP * $GIGABYTE / $BLOCKSIZE))
ADDBLOCKS=$(($ROOTADD + $SWAPADD + $BASEIMGBLOCKS + $SPAREBLOCKS))

echo "Block size: $BLOCKSIZE"
echo "Base Image Size $BASEIMAGESIZE"
echo "Base Image $BASEIMGBLOCKS blocks"
echo "Adding $ROOTADD blocks to root file system"
echo "Adding $SWAPADD blocks to swap file system"
echo "Adding $SPAREBLOCKS spare blocks"
echo "Resizing image file to $ADDBLOCKS"

ls -lh

extendImg

ls -lh

echo "***********************************************"
echo "*** $SYSTEMIMG Resized"
echo "***********************************************"

1 comment:

  1. Thanks Adrew, very useful! I'll try this out asap

    ReplyDelete