1 #!/usr/bin/bash 2 # 3 # CDDL HEADER START 4 # 5 # The contents of this file are subject to the terms of the 6 # Common Development and Distribution License (the "License"). 7 # You may not use this file except in compliance with the License. 8 # 9 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 # or http://www.opensolaris.org/os/licensing. 11 # See the License for the specific language governing permissions 12 # and limitations under the License. 13 # 14 # When distributing Covered Code, include this CDDL HEADER in each 15 # file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 # If applicable, add the following below this CDDL HEADER, with the 17 # fields enclosed by brackets "[]" replaced with your own identifying 18 # information: Portions Copyright [yyyy] [name of copyright owner] 19 # 20 # CDDL HEADER END 21 # 22 # Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23 # Use is subject to license terms. 24 # 25 # Script to install Belenix onto a Bootable LiveUSB 26 # By Anil Gulecha 27 # 28 29 if [ $# != 1 ]; then 30 echo "Usage: $0 <USB image path>" 31 exit 1 32 fi 33 34 img=$1 35 if [ ! -f $img ] && [ ! -c $img ]; then 36 echo "Error: $img does not exist." 37 exit 1 38 fi 39 40 let i=0 41 42 #nawk script to o/p the details of plugged in USB drives 43 rmformat 2>/dev/null | nawk 'BEGIN { 44 FS = ":"; 45 lnode=0; 46 physdev=""; 47 node = ""; 48 devname = ""; 49 bus = ""; 50 size = 0; 51 bustype = "USB"; 52 } { 53 if (lnode == 1 && match($1, "Logical Node")) { 54 if (match(bus, bustype)) 55 printf("%s\t%s\t%s\t%s\n", physdev, node, size, devname); 56 node = $2; 57 } else { 58 if (match($1, "Logical Node")) { 59 lnode = 1; 60 node = $2; 61 } else if (match($1, "Bus")) { 62 bus=$2 63 } else if (match($1, "Connected Device")) { 64 devname = $2; 65 } else if (match($1, "Size")) { 66 size = $2; 67 } else if (match($1, "Physical Node")) { 68 physdev=$2 69 } 70 } 71 } END { 72 if (lnode == 1) { 73 if (match(bus, bustype)) 74 printf("%s\t%s\t%s\t%s\n", physdev, node, size, devname); 75 } 76 }' >/tmp/ulst 77 78 while read p l s m d; do 79 phys[$i]=$p 80 log[$i]=$l 81 size[$i]=$s 82 mult[$i]=$m 83 desc[$i]=$d 84 let i=$i+1 85 done </tmp/ulst 86 rm /tmp/ulst 87 88 echo Found the following USB devices: 89 let j=0 90 while [ $j -lt $i ]; do 91 echo "$j: ${log[$j]} ${size[$j]} ${mult[$j]} ${desc[$j]}" 92 let j=$j+1 93 done 94 while read -p "Enter the number of your choice: " choice; do 95 if [ -z "${choice}" ]; then 96 continue 97 fi 98 if [ $choice -lt 0 ] || [ $choice -ge $i ]; then 99 echo "Invalid choice" 100 continue 101 fi 102 break 103 done 104 105 dev=${log[$choice]} 106 s0cdev=`echo $dev|sed -e 's/p0/s0/'` 107 s0bdev=`echo $s0cdev|sed -e 's/rdsk/dsk/'` 108 if [ ! -b $s0bdev ] || [ ! -c $s0cdev ]; then 109 echo "Missing device nodes for $dev" 110 exit 1 111 fi 112 113 if [ ! -n "$dev" ]; then 114 echo INFORMATION: No USB selected/found.. Please plug in and try again 115 exit 1 116 fi 117 118 sz=${size[$choice]} 119 multiplier=${mult[$choice]} 120 121 if [ "$multiplier" = "GB" ]; then 122 lvalue=`echo $sz | cut -f1 -d"."` 123 rvalue=`echo $sz | cut -f2 -d"."` 124 lvalue=`expr $lvalue \* 1000` 125 rvalue=`expr $rvalue \* 100` 126 sz=`expr $lvalue + $rvalue` 127 else 128 sz=`echo $sz | cut -f1 -d"."` 129 fi 130 131 while true; 132 do 133 echo "" 134 echo WARNING: All data on your USB storage will be lost. 135 echo Are you sure you want to install to 136 echo -n ${desc[$choice]}, $sz MB at $dev ? 137 read -p "(y/n) " yn 138 case $yn in 139 y* | Y* ) 140 break ;; 141 [nN]* ) 142 echo "Installation aborted" 143 exit 0 ;; 144 * ) 145 echo Invalid choice.. Exiting ;exit 0;; 146 esac 147 done 148 149 # Ensure we have things unmounted 150 umount -f $s0bdev 151 152 # Install fdisk table with Solaris using entire disk, default VTOC 153 fdisk -B $dev 154 155 # Now create root partition. We want to find number of cylinders in backup 156 # partition from label created by fdisk -B and then generate root partition 157 # using whole disk minus cylinder 1 158 acyls=`prtvtoc $dev | grep accessible | sed -e 's/ //g' -e 's/*//'|cut -f1 -d' '` 159 cyls=`expr ${acyls} - 1` 160 format -e $dev >/dev/null <<EOF 161 pa 162 0 163 root 164 wm 165 1 166 ${cyls}c 167 label 168 0 169 y 170 EOF 171 172 # Copy image to USB. 16k blocks seem to be about as fast as anything 173 echo "Copying image to USB device" 174 time dd if=$img of=$s0cdev bs=16384 175 176 # Mount image 177 mnt=/tmp/usb.$$ 178 mkdir $mnt 179 mount $s0bdev $mnt 180 # Install grub stages to usb 181 echo Installing grub to USB device $s0cdev 182 installgrub -mf $mnt/boot/grub/stage1 $mnt/boot/grub/stage2 $s0cdev > /dev/null 183 umount $mnt 184 rmdir $mnt 185 echo "Completed copy to USB" 186 187