Guest User

Untitled

a guest
Jul 17th, 2018
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. #!/bin/ksh -p
  2.  
  3. #
  4. # Copyright 2008 Sun Microsystems, Inc. All rights reserved.
  5. # Use is subject to license terms.
  6. #
  7. #ident "@(#)mkusb.ksh 1.1 08/05/14 SMI"
  8.  
  9. #
  10. # mkusb
  11. #
  12. # Given an AK ISO image (output by mkak -I), generate a bootable USB stick
  13. # by mounting the ISO, formatting the USB stick with a Solaris partition,
  14. # copying the ISO content there, and then installing bootable GRUB.
  15. #
  16.  
  17. if [[ $# -ne 2 ]]; then
  18. echo "Usage: $0 file.iso /dev/rdsk/<usb>p0" >& 2
  19. exit 2
  20. fi
  21.  
  22. function fail
  23. {
  24. cd /
  25.  
  26. if [[ -n "$lofidev" ]]; then
  27. umount $rmnt >/dev/null 2>&1
  28. lofiadm -d $lofidev >/dev/null 2>&1
  29. fi
  30.  
  31. umount $wmnt >/dev/null 2>&1
  32. rmdir $rmnt $wmnt >/dev/null 2>&1
  33.  
  34. echo "$0: $*" >&2
  35. exit 1
  36. }
  37.  
  38. iso=$1
  39. pdsk=$2
  40. rdsk=${pdsk%p0}
  41. dsk=$(echo $rdsk | sed 's:/rdsk:/dsk:')
  42.  
  43. rmnt=/tmp/mkusb-r.$$
  44. wmnt=/tmp/mkusb-w.$$
  45.  
  46. rm -rf $rmnt $wmnt >/dev/null 2>&1
  47. mkdir -m 0700 $rmnt || fail "failed to mkdir: $rmnt"
  48. mkdir -m 0700 $wmnt || fail "failed to mkdir: $wmnt"
  49.  
  50. [[ -r $iso ]] || fail "ISO is missing or not readable: $iso"
  51. lofidev=$(lofiadm -a $iso)
  52. [[ -n "$lofidev" ]] || fail "failed to mount ISO: $iso"
  53.  
  54. mount -F hsfs -o ro $lofidev $rmnt
  55. rlofidev=$(echo $lofidev | sed s/lofi/rlofi/)
  56.  
  57. rmformat -l $pdsk 2>/dev/null | grep 'Bus: USB' >/dev/null 2>&1 ||
  58. fail "USB device path does not appear to be valid: $pdsk"
  59.  
  60. echo "Formatting USB device ... \c"
  61. fdisk -B $pdsk || fail "failed to create USB partition"
  62.  
  63. set -- $(rmformat -l $pdsk 2>/dev/null | \
  64. egrep '(Device|Size):' | cut -d: -f2-)
  65.  
  66. eval $(prtvtoc $pdsk | grep '^\*' | awk '
  67. $3 == "bytes/sector" { printf("bytes_per_sec=%s\n", $2); }
  68. $3 == "sectors/cylinder" { printf("secs_per_cyl=%s\n", $2); }
  69. $3 == "accessible" { printf("cylinders=%s\n", $2); }')
  70.  
  71. [[ -z "$bytes_per_sec" || -z "$secs_per_cyl" || -z "$cylinders" ]] && \
  72. fail "failed to read vtoc attributes for $pdsk"
  73.  
  74. let sectors="$cylinders * $secs_per_cyl"
  75.  
  76. grub_sec0=0
  77. grub_secs=$secs_per_cyl
  78. grub_part=s8
  79.  
  80. let root_sec0="$grub_sec0 + $grub_secs"
  81. let root_secs="$sectors - $root_sec0"
  82. root_part=s0
  83.  
  84. dump_sec0=0
  85. dump_secs=$sectors
  86. dump_part=s2
  87.  
  88. [[ $root_sec0 -lt $sectors ]] || fail "root disk too small"
  89.  
  90. fmthard -s - $pdsk >/dev/null <<-EOF
  91. ${root_part#s} 2 0x00 $root_sec0 $root_secs
  92. ${dump_part#s} 5 0x00 $dump_sec0 $dump_secs
  93. ${grub_part#s} 1 0x01 $grub_sec0 $grub_secs
  94. EOF
  95.  
  96. [[ $? -eq 0 ]] && echo "$*" || fail "failed to format USB stick"
  97.  
  98. newfs -m 0 $rdsk$root_part </dev/null 2>/dev/null || "failed to newfs USB stick"
  99. mount -F ufs $dsk$root_part $wmnt || fail "failed to mount USB stick"
  100.  
  101. echo "Copying ISO contents to USB ... "
  102. cd $rmnt && find . -depth -print | cpio -pdVmu $wmnt || fail "cpio failed"
  103.  
  104. echo "Editing USB boot menu ... \c"
  105. mv -f $wmnt/boot/grub/menu.lst $wmnt/boot/grub/menu.lst.iso
  106.  
  107. sed 's/ak_media=cd/ak_media=usb/g' $wmnt/boot/grub/menu.lst.iso \
  108. >$wmnt/boot/grub/menu.lst || fail "failed to edit menu file"
  109.  
  110. chmod 0444 $wmnt/boot/grub/menu.lst
  111. echo "done."
  112.  
  113. echo "Installing USB boot ... \c"
  114. installgrub -f -m $wmnt/boot/grub/stage1 $wmnt/boot/grub/stage2 \
  115. $rdsk$root_part >/dev/null || fail "installgrub to $rdsk$root_part failed"
  116.  
  117. cd /
  118. echo "done."
  119.  
  120. umount $wmnt
  121. umount $rmnt
  122. lofiadm -d $lofidev
  123.  
  124. echo "done."
  125. exit 0
Add Comment
Please, Sign In to add comment