Archive for the 'bash' Category

Script para crear isos de ESX con kickstart personalizado


#!/bin/bash
#
# This script creates an esx iso using a custom kickstart
# Usage:
# createiso [kickstart file] [iso file]
# Parameters:
# kickstart file = Kickstart to embed
# iso file = Iso to modify

# Save the program name into PROGNAME using basename command
PROGNAME=$(basename $0)

# Exit the script if something goes wrong, using a descriptive text and an error code
function error_exit() {

echo "Error $1"
exit $2
}

# Show the usage if parameters are incorrect
function usage() {
echo "Usage: ${PROGNAME} [kickstart file] [iso file]"
error_exit "checking parameters" 1
}

# Check if the parameters are ok
function check_parms() {
# Check if there are only 2 parameters
[ $# -ne 2 ] && usage

# Check if kickstart file is a text file
[ -f $1 ] || error_exit "$1 is not a valid file" "1"

# Check if the iso is really an iso (using file command)
file $2 | grep -q "CD-ROM"
RES=$?

[ $RES -eq 0 ] || error_exit "$2 is not a valid iso" "1"
}

# Clean up
function remove_temp() {
rm -Rf temp
rm -f $1
rm -f ks_`date +%Y%m%d`
umount mountpoint
rm -Rf newiso
rmdir mountpoint
}

# Main

check_parms $1 $2

POSTINSTALL=./resources/postinstall
BACK=./resources/back.jpg

# Check if mkisofs is available
[ -x /usr/bin/mkisofs ] || error_exit "mkisofs not found" 1

# Create a kickstart file copying the original
KICKSTART=ks_`date +%Y%m%d`

cp $1 $KICKSTART

# Create directory structure
mkdir -p {temp/init,mountpoint,newiso}

# Copy the iso to another temp, just in case
echo -n "Copying iso to temp..."
cp $2 $2.new || error_exit "copying $2" 1
echo "Done"

# Mount loopback temp iso file
echo -n "Mounting temp iso..."
mount -o loop $2.new mountpoint || error_exit "mounting $2" 1
echo "Done"

# Copy the initrd (the iso file is read-only)
echo -n "Copying initrd..."
cp mountpoint/isolinux/initrd.img temp/init || error_exit "copying initrd.img" 1
echo "Done"

# Copy the isolinux.cfg to a temp
echo -n "Copying isolinux.cfg..."
cp mountpoint/isolinux/isolinux.cfg temp/ || error_exit "copying isolinux.cfg" 1
echo "Done"

# Use cm as default boot option
echo -n "Setting cm to default boot option..."
sed -i 's/default esx/default cm/g' temp/isolinux.cfg || error_exit "setting cm to default boot option" 1
echo "Done"

# Initrd.img is a compressed file, so, uncompress it
echo -n "Uncompressing initrd..."
cd temp/init
`zcat initrd.img | cpio --extract` || error_exit "uncompressing initrd" 1
echo "Done"
cd -

# Remove the old initrd.img
echo -n "Removing old initrd..."
rm -f temp/init/initrd.img || error_exit "removing old initrd" 1
echo "Done"

# Embed the kickstart into the initrd
echo -n "Copying kickstart to initrd..."
cp $KICKSTART temp/init/ks.cfg || error_exit "copying kickstart to initrd" 1
echo "Done"

# Copy the postinstall script
echo -n "Copying postinstall to initrd..."
cp $POSTINSTALL temp/init/postinstall || error_exit "copying postinstall to initrd" 1
echo "Done"

# Recreate the initrd image
echo -n "Recreating initrd..."
cd temp/init/
find ./ | cpio -H newc -o > ../initrd || error_exit "recreating initrd" 1
gzip ../initrd --suffix .img || error_exit "recreating initrd" 1
cd -
echo "Done"

# Create the boot entry in isolinux.cfg
echo -n "Creating isolinux.cfg entry..."
echo "LABEL auto" >> temp/isolinux.cfg
echo "menu label ESX automatic installation" >> temp/isolinux.cfg
echo "kernel vmlinuz" >> temp/isolinux.cfg
echo "append initrd=initrd.img vmkopts=debugLogToSerial:1 mem=512M ks=file:///ks.cfg quiet" >> temp/isolinux.cfg
echo "Done"

# Copy the back image
echo -n "Copying back image to initrd..."
cp $BACK temp/back.jpg || error_exit "copying back.jpg to initrd" 1
echo "Done"

# Create the custom iso file
echo -n "Creating new iso file..."
# Copy all iso files
cp -pr mountpoint/* newiso/ || error_exit "copying files" 1
# Sync filesystem, just in case
sync
# Sleep for 5 secs., just in case too
sleep 5
# Overwrite the two modified files
cp -f temp/{isolinux.cfg,initrd.img,back.jpg} newiso/isolinux/
cd newiso
# Create the iso into esx-XXXXYYZZ.iso
mkisofs -l -J -R -r -T -o ../esx-vsphere-`date +%Y%m%d`.iso -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table ./ || error_exit "creating iso" 1
cd -
echo "Done"

# Clean environment
echo -n "Cleaning..."
remove_temp $2.new
echo "Done"

# Finish!
echo "All done!"
echo "Now burn your new iso (esx-vsphere-`date +%Y%m%d`.iso) to a blank cd and boot the auto option"

# Exit with no errors :)
exit 0

Post to Twitter Post to Facebook Send Gmail Post to LinkedIn

No Comments »

minWi on noviembre 8th 2011 in bash, VMware

less tip

A veces la ayuda de un comando puede resultar muy interesante :D
Por ejemplo, si estas viendo un fichero con less y quieres ver los numeros de linea:
-N <enter>
Pondría alguno mas, pero prefiero el mejor:
man less :P

Post to Twitter Post to Facebook Send Gmail Post to LinkedIn

No Comments »

minWi on septiembre 29th 2008 in bash, Linux

Bash “tricks”

Habia pensado en copy & paste, pero mejor lo linko.
Aqui una buena lista, en ingles.

Post to Twitter Post to Facebook Send Gmail Post to LinkedIn

No Comments »

minWi on septiembre 18th 2007 in bash, Linux

Script para prevenir reboot y shutdown en maquinas de producción

Realmente vale para cualquier comando, lo unico que hace es pedir el hostname antes de ejecutar el mismo comando que se ha invocado (para prevenir un shutdown -h now en un servidor critico)

Lo ideal es colocarlo en /usr/local/bin/shutdown con 100 de permisos, y
luego en el /etc/profile/, colocar un:

alias shutdown=”/usr/local/bin/shutdown”
alias reboot=”/usr/local/bin/reboot”

#!/bin/bash

# Script para impedir el reboot de maquinas de producción
# Para ello, una vez invocado shutdown o el reboot, pide el nombre del host

HOSTNAME=`hostname`
BIN_DIR=/sbin/

if [ `id -u` != 0 ]
then
echo "No eres root"
exit
fi

read -p "Introduce el nombre del host: " ENTRADA

if [ "$HOSTNAME" == "$ENTRADA" ];
then
COMANDO=`echo $0 | awk -F/ '{print $5}'`
$BIN_DIR$COMANDO $*
else
echo "El hostname introducido no coincide"
fi

Post to Twitter Post to Facebook Send Gmail Post to LinkedIn

No Comments »

minWi on agosto 28th 2007 in bash, Linux

  • RSS
  • Facebook
  • Google+
  • LinkedIn
  • Twitter
  • Picasa
  • Flickr
  • YouTube