Arch-specific builds, aarch64 support
[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     echo "    -e efiboot.img "
36     echo "    -E EFI directory "
37 }
38
39 function cleanup() {
40
41     v_print "Cleaning up.."
42     rm -rf $TMP_BUILD_DIR
43 }
44
45 function err_print() {
46     echo "ERROR: $@" 1>&2;
47 }
48
49 function v_print() {
50
51     echo "$*"
52 }
53
54
55 # Parse command line options
56 ARGS=`getopt -o "o:i:k:e:E:" -l "output,initrd,kernel,efiboot,EFI:" \
57       -n "$SCRIPTNAME" -- "$@"`
58 if [ $? -ne 0 ];
59 then
60         exit 1
61 fi
62
63 eval set -- "$ARGS"
64
65 while true ; do
66     case "$1" in
67         -o) OUTPUT_FILENAME=$2; shift 2 ;;
68         -i) INITRD=$2; shift 2 ;;
69         -k) KERNEL=$2; shift 2 ;;
70         -e) EFIBOOT=$2; shift 2 ;;
71         -E) EFIDIR=$2; shift 2 ;;
72         # *)  show_options ; exit 1 ;;
73         --) shift; break ;;
74     esac
75 done
76
77 # Verify whether kernel, initrd, and the image file is present
78 if [ -z "$OUTPUT_FILENAME" ]; then
79     err_print "Output filename not provided."
80     show_options
81     exit 1
82 fi
83
84 if [ -z "$INITRD" ]; then
85     err_print "Initrd not provided."
86     show_options
87     exit 1
88 fi
89
90 if [ -z "$KERNEL" ]; then
91     err_print "Kernel not provided."
92     show_options
93     exit 1
94 fi
95
96 if [ -z "$EFIBOOT" ]; then
97     err_print "efiboot.img not provided."
98     show_options
99     exit 1
100 fi
101
102 if [ -z "$EFIDIR" ]; then
103     err_print "EFI directory not provided."
104     show_options
105     exit 1
106 fi
107
108 # Create a temporary build directory for holiding the contents of iso
109 TMP_IMAGE_DIR="$TMP_BUILD_DIR/image"
110 v_print "Creating temporary directory $TMP_IMAGE_DIR"
111 mkdir -p "$TMP_IMAGE_DIR"
112
113 if [ "$(uname -m)" = 'x86_64' ]; then
114     # Copy isolinux bin to the isolinux directory
115     mkdir -p "$TMP_IMAGE_DIR/isolinux"
116     v_print "Copying isolinux.bin"
117     if [ -f /usr/share/syslinux/isolinux.bin ]
118     then
119         cp /usr/share/syslinux/isolinux.bin "$TMP_IMAGE_DIR/isolinux"
120
121     elif [ -f /usr/lib/syslinux/isolinux.bin ]
122     then
123         cp /usr/lib/syslinux/isolinux.bin "$TMP_IMAGE_DIR/isolinux"
124     else
125         err_print "Could not find isolinux.bin. Install syslinux?"
126         cleanup
127         exit 1
128     fi
129 fi
130 # Copy initrd, kernel
131 v_print "Copying kernel to $TMP_IMAGE_DIR/vmlinuz"
132 cp $KERNEL "$TMP_IMAGE_DIR/vmlinuz"
133 if [ $? -ne 0 ]; then
134     err_print "Failed to copy $KERNEL to $TMP_IMAGE_DIR"
135     cleanup
136     exit 1
137 fi
138
139 v_print "Copying initrd to $TMP_IMAGE_DIR/initrd"
140 cp $INITRD "$TMP_IMAGE_DIR/initrd"
141 if [ $? -ne 0 ]; then
142     err_print "Failed to copy $INITRD to $TMP_IMAGE_DIR"
143     cleanup
144     exit 1
145 fi
146
147 mkdir -p "$TMP_IMAGE_DIR/images"
148 v_print "Copying efiboot image to $TMP_IMAGE_DIR/images/efiboot.img"
149 cp $EFIBOOT "$TMP_IMAGE_DIR/images/efiboot.img"
150 if [ $? -ne 0 ]; then
151     err_print "Failed to copy $EFIBOOT to $TMP_IMAGE_DIR/images"
152     cleanup
153     exit 1
154 fi
155
156 v_print "Copying EFI directory to $TMP_IMAGE_DIR/EFI"
157 cp -r $EFIDIR "$TMP_IMAGE_DIR/EFI"
158 if [ $? -ne 0 ]; then
159     err_print "Failed to copy $EFIDIR to $TMP_IMAGE_DIR/EFI"
160     cleanup
161     exit 1
162 fi
163 # Generate grub.cfg for default booting
164 sed -i '/BEGIN/q' "$TMP_IMAGE_DIR/EFI/BOOT/grub.cfg"
165 echo "\
166 menuentry 'Install image' --class red --class gnu-linux --class gnu --class os {
167     linux /vmlinuz boot_method=vmedia console=tty0 console=ttyS1,115200 console=ttyS0,115200 console=ttyAMA0,115200 selinux=0
168     initrd /initrd
169 }" >> "$TMP_IMAGE_DIR/EFI/BOOT/grub.cfg"
170
171 if [ "$(uname -m)" = 'x86_64' ]; then
172     # Generate isolinux.cfg for default booting
173     v_print "Generating isolinux.cfg"
174     echo "\
175 DEFAULT install
176 LABEL install
177     menu label "Install image"
178     kernel /vmlinuz
179     append initrd=/initrd boot_method=vmedia console=tty0 console=ttyS1,115200 selinux=0 --
180 TIMEOUT 5
181 PROMPT 0 " > "$TMP_IMAGE_DIR/isolinux/isolinux.cfg"
182 fi
183
184 # Convert relative path output filename to absolute path
185 echo $OUTPUT_FILENAME | grep -q '^/'
186 if [ $? -ne 0 ]; then
187     OUTPUT_FILENAME="$PWD/$OUTPUT_FILENAME"
188 fi
189
190 # On x86_64 platforms, use isolinux for BIOS support
191 if [ "$(uname -m)" = 'x86_64' ]; then
192     MKISOFS_ARGS+=" -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot \
193                     -boot-load-size 4 -boot-info-table"
194 fi
195
196 # efiboot.img path is hard set, create an EFI compatible ISO
197 MKISOFS_ARGS+=" -eltorito-alt-boot -e images/efiboot.img -no-emul-boot"
198
199 # Create the ISO
200 v_print "Generating the ISO"
201 cd $TMP_IMAGE_DIR && $MKISOFS -r -V "INSTALL_IMAGE" -cache-inodes -J -l $MKISOFS_ARGS -o $OUTPUT_FILENAME .
202
203 if [ $? -ne 0 ]; then
204     err_print "Failed generating the ISO"
205     cleanup
206     exit 1
207 fi
208
209 # Cleanup
210 cleanup
211