#!/bin/sh #============================================================================= # # Copyright (C) 2011-2012 O. LODYGENSKY # at LAL, Univ Paris-Sud, IN2P3/CNRS, Orsay, France # License GPL v3 # # Shell script to create an ISO disk image # (for example for contextualization of virtual machines). # # Parameters : SOURCE : File or folder to be (recursively) copied to the # disk image # # Output : $SOURCE.iso # #============================================================================= LOGFILE="createiso.log" touch $LOGFILE #============================================================================= # Function usage() #============================================================================= usage() { if [ "$*" ]; then echo "$*" > /dev/stderr fi cat << "END_OF_HELP" > /dev/stderr Usage : $0 --help | SOURCE --help : This help SOURCE : After lexical compaction, SOURCE can NOT be / or contain .. SOURCE is copied to the disk image. If SOURCE is a folder, this script does NOT copy the folder itself, but its content. END_OF_HELP exit 1 } #============================================================================= # Function fatal() #============================================================================= fatal() { RC=$? if [ $RC -eq 0 ]; then RC=1; fi echo "$(date) $0 FATAL: ${*:-Ctrl+C}" exit $RC } #============================================================================= # Function warn() #============================================================================= warn() { echo "$(date) $0 WARN: ${*:-Ctrl+C}" >> $LOGFILE 2>&1 } #============================================================================= # MAIN #============================================================================= trap fatal SIGINT SIGTERM #----------------------------------------------------------------------------- # There must be 1 parameter : A file or folder name. In this parameter : # - Replace all '//' by '/', all '/./' by '/', remove leading './'. # - If empty or leading '../' or embedded '/../' : Prefix by $PWD, # replace all '/xyz/../' by '/', then forbid any remaining '/../'. # - Remove any trailing '/'. #----------------------------------------------------------------------------- if [ '(' "$1" = "" ')' -o '(' "$1" = "--help" ')' ]; then usage; fi SOURCE=$(echo "$1/" | perl -wpe 's=//+=/=g; s=(^|/)(\./)+=$1=g') if [ '(' -z "${SOURCE##../*}" ')' -o \ '(' -z "${SOURCE##[^/]*/../*}" ')' ]; then SOURCE="$PWD/$SOURCE" fi while expr "$SOURCE" : ".*/\.*[^./][^/]*/\.\./" > /dev/null do SOURCE=$(echo "$SOURCE" | perl -wpe 's=/\.*[^./][^/]*/\.\./=/=g') done if [ -z "${SOURCE##*/../*}" ]; then usage "SOURCE='$SOURCE' NOT allowed" fi SOURCE=$(echo "$SOURCE" | perl -wpe 's=(.)/+$=$1=') [ ! -r "$SOURCE" ] && fatal "Source NOT found : $SOURCE" [ '(' -d "$SOURCE" ')' -a '(' ! -f $SOURCE/context.sh ')' ] && \ warn "$SOURCE/context.sh does not exist" echo "$SOURCE" | grep -E '\.(qcow|qcow2|vdi|vmdk|vhd|iso)$' > /dev/null 2>&1 if [ $? -eq 0 ] ; then echo "$0 : $SOURCE is already a Virtual Disk" >> $LOGFILE 2>&1 echo $SOURCE exit 0 fi SOURCE_NAME=$(basename "$SOURCE" | tr ' .' '-_') ISO_FILENAME="$SOURCE_NAME.iso" [ -f "$ISO_FILENAME" ] && fatal "'$ISO_FILENAME' already exists." \ "Cowardly refuse to overwrite it." # # Next, because hdiutil expects a directory, not a file # TMPDIR=/tmp/xwcreatecontext_$USER/$SOURCE if [ -f "$SOURCE" ] ; then mkdir -p $TMPDIR cp -f $SOURCE $TMPDIR SOURCE=$TMPDIR fi # # The all trick is here # case $OSTYPE in darwin* ) echo "hdiutil makehybrid -iso -o $ISO_FILENAME $SOURCE" >> $LOGFILE 2>&1 hdiutil makehybrid -iso -o $ISO_FILENAME $SOURCE >> $LOGFILE 2>&1 [ $? -ne 0 ] && fatal "Can't create ISO from $SOURCE" ;; linux* ) echo "mkisofs -d -J -l -o $ISO_FILENAME $SOURCE" >> $LOGFILE 2>&1 mkisofs -d -J -l -o $ISO_FILENAME $SOURCE >> $LOGFILE 2>&1 [ $? -ne 0 ] && fatal "Can't create ISO from $SOURCE" ;; * ) fatal "$OSTYPE not supported" ;; esac #----------------------------------------------------------------------------- RC=$? [ -d $TMPDIR ] && rm -Rf $TMPDIR >> $LOGFILE 2>&1 [ $RC -eq 0 ] && echo "$(date) $0 INFO: Successfully stored '$SOURCE' in '$ISO_FILENAME'" >> $LOGFILE 2>&1 [ $RC -ne 0 ] && echo "$(date) $0 ERROR: Error generating " \ "'$ISO_FILENAME' from '$SOURCE'" >> $LOGFILE 2>&1 echo $ISO_FILENAME