Dean Troyer | 274ec10 | 2011-10-11 20:32:07 -0500 | [diff] [blame] | 1 | #!/bin/bash -e |
| 2 | # build_usb_boot.sh - Create a syslinux boot environment |
| 3 | # |
Dean Troyer | e33c5f6 | 2011-10-26 15:13:00 -0500 | [diff] [blame] | 4 | # build_usb_boot.sh destdev |
Dean Troyer | 274ec10 | 2011-10-11 20:32:07 -0500 | [diff] [blame] | 5 | # |
| 6 | # Assumes syslinux is installed |
Dean Troyer | 274ec10 | 2011-10-11 20:32:07 -0500 | [diff] [blame] | 7 | # Needs to run as root |
| 8 | |
Dean Troyer | 274ec10 | 2011-10-11 20:32:07 -0500 | [diff] [blame] | 9 | DEST_DIR=${1:-/tmp/syslinux-boot} |
| 10 | PXEDIR=${PXEDIR:-/var/cache/devstack/pxe} |
| 11 | OPWD=`pwd` |
| 12 | PROGDIR=`dirname $0` |
| 13 | |
Dean Troyer | 55c0273 | 2011-11-01 17:44:03 -0500 | [diff] [blame] | 14 | # Clean up any resources that may be in use |
| 15 | cleanup() { |
| 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 | |
| 32 | trap cleanup SIGHUP SIGINT SIGTERM |
| 33 | |
Dean Troyer | 274ec10 | 2011-10-11 20:32:07 -0500 | [diff] [blame] | 34 | if [ -b $DEST_DIR ]; then |
| 35 | # We have a block device, install syslinux and mount it |
| 36 | DEST_DEV=$DEST_DIR |
Dean Troyer | e33c5f6 | 2011-10-26 15:13:00 -0500 | [diff] [blame] | 37 | DEST_DIR=`mktemp -d --tmpdir mntXXXXXX` |
Dean Troyer | d642f86 | 2011-10-17 14:55:10 -0500 | [diff] [blame] | 38 | mount $DEST_DEV $DEST_DIR |
| 39 | |
| 40 | if [ ! -d $DEST_DIR/syslinux ]; then |
| 41 | mkdir -p $DEST_DIR/syslinux |
| 42 | fi |
Dean Troyer | 274ec10 | 2011-10-11 20:32:07 -0500 | [diff] [blame] | 43 | |
| 44 | # Install syslinux on the device |
| 45 | syslinux --install --directory syslinux $DEST_DEV |
Dean Troyer | 274ec10 | 2011-10-11 20:32:07 -0500 | [diff] [blame] | 46 | else |
| 47 | # We have a directory (for sanity checking output) |
Dean Troyer | d642f86 | 2011-10-17 14:55:10 -0500 | [diff] [blame] | 48 | DEST_DEV="" |
| 49 | if [ ! -d $DEST_DIR/syslinux ]; then |
| 50 | mkdir -p $DEST_DIR/syslinux |
| 51 | fi |
Dean Troyer | 274ec10 | 2011-10-11 20:32:07 -0500 | [diff] [blame] | 52 | fi |
| 53 | |
| 54 | # Get some more stuff from syslinux |
| 55 | for i in memdisk menu.c32; do |
Dean Troyer | a0cf3c8 | 2011-10-26 15:52:34 -0500 | [diff] [blame] | 56 | cp -pu /usr/lib/syslinux/$i $DEST_DIR/syslinux |
Dean Troyer | 274ec10 | 2011-10-11 20:32:07 -0500 | [diff] [blame] | 57 | done |
| 58 | |
| 59 | CFG=$DEST_DIR/syslinux/syslinux.cfg |
| 60 | cat >$CFG <<EOF |
| 61 | default /syslinux/menu.c32 |
| 62 | prompt 0 |
| 63 | timeout 0 |
| 64 | |
| 65 | MENU TITLE Boot Menu |
| 66 | |
| 67 | EOF |
| 68 | |
| 69 | # Setup devstack boot |
| 70 | mkdir -p $DEST_DIR/ubuntu |
| 71 | if [ ! -d $PXEDIR ]; then |
| 72 | mkdir -p $PXEDIR |
| 73 | fi |
Dean Troyer | e33c5f6 | 2011-10-26 15:13:00 -0500 | [diff] [blame] | 74 | |
| 75 | # Get image into place |
| 76 | if [ ! -r $PXEDIR/stack-initrd.img ]; then |
Dean Troyer | 274ec10 | 2011-10-11 20:32:07 -0500 | [diff] [blame] | 77 | cd $OPWD |
Dean Troyer | e33c5f6 | 2011-10-26 15:13:00 -0500 | [diff] [blame] | 78 | $PROGDIR/build_ramdisk.sh $PXEDIR/stack-initrd.img |
Dean Troyer | 274ec10 | 2011-10-11 20:32:07 -0500 | [diff] [blame] | 79 | fi |
Dean Troyer | e33c5f6 | 2011-10-26 15:13:00 -0500 | [diff] [blame] | 80 | if [ ! -r $PXEDIR/stack-initrd.gz ]; then |
| 81 | gzip -1 -c $PXEDIR/stack-initrd.img >$PXEDIR/stack-initrd.gz |
| 82 | fi |
Dean Troyer | a0cf3c8 | 2011-10-26 15:52:34 -0500 | [diff] [blame] | 83 | cp -pu $PXEDIR/stack-initrd.gz $DEST_DIR/ubuntu |
Dean Troyer | e33c5f6 | 2011-10-26 15:13:00 -0500 | [diff] [blame] | 84 | |
| 85 | if [ ! -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 Troyer | a0cf3c8 | 2011-10-26 15:52:34 -0500 | [diff] [blame] | 99 | cp -pu $MNTDIR/boot/vmlinuz-*-generic $PXEDIR |
Dean Troyer | e33c5f6 | 2011-10-26 15:13:00 -0500 | [diff] [blame] | 100 | fi |
| 101 | umount $MNTDIR |
| 102 | rmdir $MNTDIR |
| 103 | fi |
| 104 | |
| 105 | # Get generic kernel version |
| 106 | KNAME=`basename $PXEDIR/vmlinuz-*-generic` |
| 107 | KVER=${KNAME#vmlinuz-} |
Dean Troyer | a0cf3c8 | 2011-10-26 15:52:34 -0500 | [diff] [blame] | 108 | cp -pu $PXEDIR/vmlinuz-$KVER $DEST_DIR/ubuntu |
Dean Troyer | 274ec10 | 2011-10-11 20:32:07 -0500 | [diff] [blame] | 109 | cat >>$CFG <<EOF |
| 110 | |
| 111 | LABEL 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 |
| 116 | EOF |
| 117 | |
| 118 | # Get Ubuntu |
| 119 | if [ -d $PXEDIR -a -r $PXEDIR/natty-base-initrd.gz ]; then |
Dean Troyer | a0cf3c8 | 2011-10-26 15:52:34 -0500 | [diff] [blame] | 120 | cp -pu $PXEDIR/natty-base-initrd.gz $DEST_DIR/ubuntu |
Dean Troyer | 274ec10 | 2011-10-11 20:32:07 -0500 | [diff] [blame] | 121 | cat >>$CFG <<EOF |
| 122 | |
| 123 | LABEL 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 |
| 127 | EOF |
| 128 | fi |
| 129 | |
| 130 | # Local disk boot |
| 131 | cat >>$CFG <<EOF |
| 132 | |
| 133 | LABEL local |
| 134 | MENU LABEL ^Local disk |
Dean Troyer | 274ec10 | 2011-10-11 20:32:07 -0500 | [diff] [blame] | 135 | LOCALBOOT 0 |
| 136 | EOF |
| 137 | |
| 138 | if [ -n "$DEST_DEV" ]; then |
| 139 | umount $DEST_DIR |
| 140 | rmdir $DEST_DIR |
| 141 | fi |