fixed for when the nodeconf is a symlink
[bootcd.git] / cdcustom.sh
1 #!/bin/bash
2
3 # purpose : create a node-specific CD ISO image
4
5 # given a CD ISO image, and a node-specific config file, this command
6 # creates a new almost identical ISO image with the config file
7 # embedded as boot/plnode.txt 
8
9 set -e 
10 COMMAND=$(basename $0)
11
12 function usage () {
13
14    echo "Usage: $0 generic-iso node-config"
15    echo "Creates a node-specific ISO image"
16    echo "with the node-specific config file embedded as /boot/plnode.txt"
17    exit 1
18 }
19
20 ### read config file in a subshell and echoes host_name
21 function host_name () {
22   export CONFIG=$1; shift
23   ( . $CONFIG ; echo $HOST_NAME )
24 }
25
26 function cleanup () {
27    echo "Unmounting"
28    umount $ISOINROOT
29    echo "Cleaning mount-point"
30    rmdir $ISOINROOT
31    if [ -f $NODECONFPLAIN ] ; then
32      echo Cleaning $NODECONFPLAIN
33      rm -f $NODECONFPLAIN
34    fi
35 }
36
37 function abort () {
38    echo "Cleaning $ISOOUT"
39    rm -f $ISOOUT
40    cleanup
41 }
42
43 function main () {
44
45    [[ -n "$TRACE" ]] && set -x
46
47    [[ -z "$@" ]] && usage
48    ISOIN=$1; shift
49    [[ -z "$@" ]] && usage
50    NODECONF=$1; shift
51    [[ -n "$@" ]] && usage
52
53    NODENAME=$(host_name $NODECONF)
54    if [ -z "$NODENAME" ] ; then
55      echo "HOST_NAME not found in $NODECONF - exiting"
56      exit 1
57    fi
58    
59    ISODIR=$(dirname "$ISOIN")
60    ISOOUT=$ISODIR/$NODENAME.iso
61    ISOLOG=$ISODIR/$NODENAME.log
62
63
64    ### temporary mount point
65    ISOINROOT=$(basename $ISOIN .iso)
66
67    ### checking
68    if [ ! -f $ISOIN ] ; then
69      echo "Could not find template ISO image - exiting" ; exit 1
70    fi
71    if [ ! -f $NODECONF ] ; then
72      echo "Could not find node-specifig config - exiting" ; exit 1
73    fi
74    if [ -e "$ISOINROOT" ] ; then
75      echo "Temporary mount point $ISOINROOT exists, please clean up first - exiting" ; exit 1
76    fi
77    if [ -e  "$ISOOUT" ] ; then
78      echo "$ISOOUT exists, please remove first - exiting" ; exit 1
79    fi
80
81    ### in case the NODECONF is a symlink
82    NODECONFPLAIN=/tmp/$$
83    cp $NODECONF $NODECONFPLAIN
84
85    ### summary
86    echo -e "Generic ISO image:\r\t\t\t$ISOIN"
87    echo -e "Node-specific config:\r\t\t\t$NODECONF"
88    echo -e "Node-specific ISO:\r\t\t\t$ISOOUT"
89    echo -e "Temporary mount-point:\r\t\t\t$ISOINROOT"
90
91    echo -ne "OK ? "
92    read answer
93
94    echo -n "Creating mount-point "
95    mkdir -p $ISOINROOT
96    echo Done
97
98    echo -n "Mounting generic image "
99    mount -o ro,loop $ISOIN $ISOINROOT 
100    echo Done
101
102    ### mkisofs needs to have write access on the boot image passed with -b
103    ### and we cannot use the same name as isolinux.bin either, so we change it to isolinux
104    ### the good news is that this way we can check we start from a fresh image
105
106    if [ -e $ISOINROOT/isolinux/isolinux ] ; then
107      echo "$ISOIN already contains isolinux/isolinux"
108      echo "It looks like this is not a first-hand image - exiting"
109      cleanup
110      exit 1
111    fi
112
113    echo -n "Copying isolinux.bin in /tmp/isolinux "
114    cp $ISOINROOT/isolinux/isolinux.bin /tmp/isolinux
115    echo Done
116
117    echo -n "Writing custom image ... "
118    trap abort int hup quit err
119    mkisofs -o $ISOOUT -R -allow-leading-dots -J -r -b isolinux/isolinux \
120    -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table \
121    --graft-points $ISOINROOT isolinux/isolinux=/tmp/isolinux boot/plnode.txt=$NODECONFPLAIN > $ISOLOG 2>&1
122    trap - int hup quit 
123    echo Done
124    
125    cleanup
126
127    echo "CD ISO image for $NODENAME in $ISOOUT"
128
129 }
130
131 ####################
132 main "$@"