blob: e4dabc0edb605358c8a62b18cb6e1dcef8f4cf29 [file] [log] [blame]
Dean Troyer274ec102011-10-11 20:32:07 -05001#!/bin/bash -e
2# build_usb_boot.sh - Create a syslinux boot environment
3#
Dean Troyere33c5f62011-10-26 15:13:00 -05004# build_usb_boot.sh destdev
Dean Troyer274ec102011-10-11 20:32:07 -05005#
6# Assumes syslinux is installed
Dean Troyer274ec102011-10-11 20:32:07 -05007# Needs to run as root
8
Dean Troyer274ec102011-10-11 20:32:07 -05009DEST_DIR=${1:-/tmp/syslinux-boot}
10PXEDIR=${PXEDIR:-/var/cache/devstack/pxe}
11OPWD=`pwd`
12PROGDIR=`dirname $0`
13
Dean Troyer55c02732011-11-01 17:44:03 -050014# Clean up any resources that may be in use
15cleanup() {
16 set +o errexit
17
18 # Mop up temporary files
19 if [ -n "$DEST_DEV" ]; then
20 umount $DEST_DIR
21 rmdir $DEST_DIR
22 fi
23 if [ -n "$MNTDIR" -a -d "$MNTDIR" ]; then
24 umount $MNTDIR
25 rmdir $MNTDIR
26 fi
27
28 # Kill ourselves to signal any calling process
29 trap 2; kill -2 $$
30}
31
32trap cleanup SIGHUP SIGINT SIGTERM
33
Dean Troyer274ec102011-10-11 20:32:07 -050034if [ -b $DEST_DIR ]; then
35 # We have a block device, install syslinux and mount it
36 DEST_DEV=$DEST_DIR
Dean Troyere33c5f62011-10-26 15:13:00 -050037 DEST_DIR=`mktemp -d --tmpdir mntXXXXXX`
Dean Troyerd642f862011-10-17 14:55:10 -050038 mount $DEST_DEV $DEST_DIR
39
40 if [ ! -d $DEST_DIR/syslinux ]; then
41 mkdir -p $DEST_DIR/syslinux
42 fi
Dean Troyer274ec102011-10-11 20:32:07 -050043
44 # Install syslinux on the device
45 syslinux --install --directory syslinux $DEST_DEV
Dean Troyer274ec102011-10-11 20:32:07 -050046else
47 # We have a directory (for sanity checking output)
Dean Troyerd642f862011-10-17 14:55:10 -050048 DEST_DEV=""
49 if [ ! -d $DEST_DIR/syslinux ]; then
50 mkdir -p $DEST_DIR/syslinux
51 fi
Dean Troyer274ec102011-10-11 20:32:07 -050052fi
53
54# Get some more stuff from syslinux
55for i in memdisk menu.c32; do
Dean Troyera0cf3c82011-10-26 15:52:34 -050056 cp -pu /usr/lib/syslinux/$i $DEST_DIR/syslinux
Dean Troyer274ec102011-10-11 20:32:07 -050057done
58
59CFG=$DEST_DIR/syslinux/syslinux.cfg
60cat >$CFG <<EOF
61default /syslinux/menu.c32
62prompt 0
63timeout 0
64
65MENU TITLE Boot Menu
66
67EOF
68
69# Setup devstack boot
70mkdir -p $DEST_DIR/ubuntu
71if [ ! -d $PXEDIR ]; then
72 mkdir -p $PXEDIR
73fi
Dean Troyere33c5f62011-10-26 15:13:00 -050074
75# Get image into place
76if [ ! -r $PXEDIR/stack-initrd.img ]; then
Dean Troyer274ec102011-10-11 20:32:07 -050077 cd $OPWD
Dean Troyere33c5f62011-10-26 15:13:00 -050078 $PROGDIR/build_ramdisk.sh $PXEDIR/stack-initrd.img
Dean Troyer274ec102011-10-11 20:32:07 -050079fi
Dean Troyere33c5f62011-10-26 15:13:00 -050080if [ ! -r $PXEDIR/stack-initrd.gz ]; then
81 gzip -1 -c $PXEDIR/stack-initrd.img >$PXEDIR/stack-initrd.gz
82fi
Dean Troyera0cf3c82011-10-26 15:52:34 -050083cp -pu $PXEDIR/stack-initrd.gz $DEST_DIR/ubuntu
Dean Troyere33c5f62011-10-26 15:13:00 -050084
85if [ ! -r $PXEDIR/vmlinuz-*-generic ]; then
86 MNTDIR=`mktemp -d --tmpdir mntXXXXXXXX`
87 mount -t ext4 -o loop $PXEDIR/stack-initrd.img $MNTDIR
88
89 if [ ! -r $MNTDIR/boot/vmlinuz-*-generic ]; then
90 echo "No kernel found"
91 umount $MNTDIR
92 rmdir $MNTDIR
93 if [ -n "$DEST_DEV" ]; then
94 umount $DEST_DIR
95 rmdir $DEST_DIR
96 fi
97 exit 1
98 else
Dean Troyera0cf3c82011-10-26 15:52:34 -050099 cp -pu $MNTDIR/boot/vmlinuz-*-generic $PXEDIR
Dean Troyere33c5f62011-10-26 15:13:00 -0500100 fi
101 umount $MNTDIR
102 rmdir $MNTDIR
103fi
104
105# Get generic kernel version
106KNAME=`basename $PXEDIR/vmlinuz-*-generic`
107KVER=${KNAME#vmlinuz-}
Dean Troyera0cf3c82011-10-26 15:52:34 -0500108cp -pu $PXEDIR/vmlinuz-$KVER $DEST_DIR/ubuntu
Dean Troyer274ec102011-10-11 20:32:07 -0500109cat >>$CFG <<EOF
110
111LABEL devstack
112 MENU LABEL ^devstack
113 MENU DEFAULT
114 KERNEL /ubuntu/vmlinuz-$KVER
115 APPEND initrd=/ubuntu/stack-initrd.gz ramdisk_size=2109600 root=/dev/ram0
116EOF
117
118# Get Ubuntu
119if [ -d $PXEDIR -a -r $PXEDIR/natty-base-initrd.gz ]; then
Dean Troyera0cf3c82011-10-26 15:52:34 -0500120 cp -pu $PXEDIR/natty-base-initrd.gz $DEST_DIR/ubuntu
Dean Troyer274ec102011-10-11 20:32:07 -0500121 cat >>$CFG <<EOF
122
123LABEL ubuntu
124 MENU LABEL ^Ubuntu Natty
125 KERNEL /ubuntu/vmlinuz-$KVER
126 APPEND initrd=/ubuntu/natty-base-initrd.gz ramdisk_size=419600 root=/dev/ram0
127EOF
128fi
129
130# Local disk boot
131cat >>$CFG <<EOF
132
133LABEL local
134 MENU LABEL ^Local disk
Dean Troyer274ec102011-10-11 20:32:07 -0500135 LOCALBOOT 0
136EOF
137
138if [ -n "$DEST_DEV" ]; then
139 umount $DEST_DIR
140 rmdir $DEST_DIR
141fi