Seed code for ipa-deployer
[ta/ipa-deployer.git] / work / iso-image-create
1 #!/bin/bash
2 #
3 # Copyright 2012 Hewlett-Packard Development Company, L.P.
4 # All Rights Reserved.
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License"); you may
7 # not use this file except in compliance with the License. You may obtain
8 # a copy of the License at
9 #
10 #     http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15 # License for the specific language governing permissions and limitations
16 # under the License.
17 #
18 # This script generates iso image from the given kernel and ramdisk
19
20 SCRIPTNAME=`basename $0`
21 TMP_BUILD_DIR="/tmp/$SCRIPTNAME.$$"
22 QEMU_IMG="/usr/bin/qemu-img"
23 MKISOFS="/usr/bin/mkisofs"
24
25
26 function show_options() {
27
28     echo "Usage: ${SCRIPTNAME} [options]"
29     echo
30     echo "Options:"
31
32     echo "    -o output filename "
33     echo "    -i initrd "
34     echo "    -k kernel "
35 }
36
37 function cleanup() {
38
39     v_print "Cleaning up.."
40     rm -rf $TMP_BUILD_DIR
41 }
42
43 function err_print() {
44     echo "ERROR: $@" 1>&2;
45 }
46
47 function v_print() {
48
49     echo "$*"
50 }
51
52
53 # Parse command line options
54 ARGS=`getopt -o "o:i:k:" -l "output,initrd,kernel:" \
55       -n "$SCRIPTNAME" -- "$@"`
56 if [ $? -ne 0 ];
57 then
58         exit 1
59 fi
60
61 eval set -- "$ARGS"
62
63 while true ; do
64     case "$1" in
65         -o) OUTPUT_FILENAME=$2; shift 2 ;;
66         -i) INITRD=$2; shift 2 ;;
67         -k) KERNEL=$2; shift 2 ;;
68         # *)  show_options ; exit 1 ;;
69         --) shift; break ;;
70     esac
71 done
72
73 # Verify whether kernel, initrd, and the image file is present
74 if [ -z "$OUTPUT_FILENAME" ]; then
75     err_print "Output filename not provided."
76     show_options
77     exit 1
78 fi
79
80 if [ -z "$INITRD" ]; then
81     err_print "Initrd not provided."
82     show_options
83     exit 1
84 fi
85
86 if [ -z "$KERNEL" ]; then
87     err_print "Kernel not provided."
88     show_options
89     exit 1
90 fi
91
92 # Create a temporary build directory for holiding the contents of iso
93 TMP_IMAGE_DIR="$TMP_BUILD_DIR/image"
94 v_print "Creating temporary directory $TMP_IMAGE_DIR"
95 mkdir -p "$TMP_IMAGE_DIR"
96
97 # Copy isolinux bin to the isolinux directory
98 mkdir -p "$TMP_IMAGE_DIR/isolinux"
99 v_print "Copying isolinux.bin"
100 if [ -f /usr/share/syslinux/isolinux.bin ]
101 then
102     cp /usr/share/syslinux/isolinux.bin "$TMP_IMAGE_DIR/isolinux"
103
104 elif [ -f /usr/lib/syslinux/isolinux.bin ]
105 then
106     cp /usr/lib/syslinux/isolinux.bin "$TMP_IMAGE_DIR/isolinux"
107 else
108     err_print "Could not find isolinux.bin. Install syslinux?"
109     cleanup
110     exit 1
111 fi
112
113
114 # Copy initrd, kernel
115 v_print "Copying kernel to $TMP_IMAGE_DIR/vmlinuz"
116 cp $KERNEL "$TMP_IMAGE_DIR/vmlinuz"
117 if [ $? -ne 0 ]; then
118     err_print "Failed to copy $KERNEL to $TMP_IMAGE_DIR"
119     cleanup
120     exit 1
121 fi
122
123 v_print "Copying initrd to $TMP_IMAGE_DIR/initrd"
124 cp $INITRD "$TMP_IMAGE_DIR/initrd"
125 if [ $? -ne 0 ]; then
126     err_print "Failed to copy $INITRD to $TMP_IMAGE_DIR"
127     cleanup
128     exit 1
129 fi
130
131 # Generate isolinux.cfg for default booting
132 v_print "Generating isolinux.cfg"
133 echo "\
134 DEFAULT install
135 LABEL install
136     menu label "Install image"
137     kernel /vmlinuz
138     append initrd=/initrd boot_method=vmedia console=tty0 console=ttyS1,115200 selinux=0 --
139 TIMEOUT 5
140 PROMPT 0 " > "$TMP_IMAGE_DIR/isolinux/isolinux.cfg"
141
142 # Convert relative path output filename to absolute path
143 echo $OUTPUT_FILENAME | grep -q '^/'
144 if [ $? -ne 0 ]; then
145     OUTPUT_FILENAME="$PWD/$OUTPUT_FILENAME"
146 fi
147
148 # Create the ISO
149 v_print "Generating the ISO"
150 cd $TMP_IMAGE_DIR && $MKISOFS -r -V "INSTALL_IMAGE" -cache-inodes -J -l -b isolinux/isolinux.bin  -no-emul-boot -boot-load-size 4 -boot-info-table -o $OUTPUT_FILENAME .
151
152 # Cleanup
153 cleanup
154