Advertisement
Guest User

emc_imager.sh

a guest
Jan 15th, 2012
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.52 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. . /common.sh
  4.  
  5. DISK1_PART1=/dev/sda1
  6. disk=/dev/sda
  7.  
  8. MD0=/dev/md0
  9. MD0VG=md0_vg
  10. BFDLV=BFDlv
  11.  
  12. BFDDEV=/dev/md0_vg/BFDlv
  13.  
  14. workarea="/usb_drive/emctools/hmnhd_images/temp"
  15. stage1File=$workarea/stage1.wrapped
  16. ubootFile=$workarea/u-boot.wrapped
  17. kernelFile=$workarea/zImage
  18. initrdFile=$workarea/initrd
  19. reinstallFile="/usb_drive/emctools/hmnhd_images/reinstall"
  20.  
  21. USB_MNT_DIR=usb_drive
  22.  
  23. mount_vol()
  24. {
  25.     mkdir -p /sysroot/boot
  26.         vgchange -ay
  27.         mount $BFDDEV sysroot/boot -o noatime
  28.         return $?
  29. }
  30.  
  31. create_partitions()
  32. {
  33.     local disk=$1
  34.  
  35.     echo "---Creating partitions"
  36.         dd if=/dev/zero of=$1 bs=1M count=32
  37.  
  38.     parted $disk mklabel gpt
  39.     parted $disk mkpart primary 65536s 42008576s
  40.     parted $disk set 1 raid on
  41.  
  42.     wait
  43. }
  44.  
  45. start_sys_raid()
  46. {
  47.     local disk=$1
  48.  
  49.     mdadm --create $MD0 --force --auto=md --raid-devices=1 --level=linear --run "${disk}1"
  50. }
  51.  
  52. create_volumes()
  53. {
  54.     # create lvm volumes for BFD and System
  55.         pvcreate -ff -y $MD0
  56.         vgcreate $MD0VG $MD0
  57.         lvcreate -L 4G -n $BFDLV $MD0VG
  58. }
  59.  
  60. format_sys_partitions()
  61. {
  62.     mke2fs $BFDDEV
  63. }
  64.  
  65. write_bootrom_directions()
  66. {
  67.     local disk=$1
  68.  
  69.     echo "---Writing bootrom directions"
  70. #
  71. # Boot ROM loading directions:
  72. #
  73. #  Secondary checksum (location + length)
  74. #  Secondary location in sectors
  75. #  Secondary length in sectors (minus half a sector due to boot ROM bug)
  76. #
  77. #  Primary checksum (location + length)
  78. #  Primary location in sectors
  79. #  Primary length in sectors (minus half a sector due to boot ROM bug)
  80. #
  81.     #perl <<EOF > $USB_MNT_DIR/bootrom.txt
  82.     #   print "\x00" x 0x1a4;
  83.     #   print "\x00\x5f\x01\x00";
  84.     #   print "\x00\xdf\x00\x00";
  85.     #   print "\x00\x80\x00\x00";
  86.     #   print "\x00" x (0x1b0 -0x1a4 -12 );
  87.     #   print "\x22\x80\x00\x00";
  88.     #   print "\x22\x00\x00\x00";
  89.     #   print "\x00\x80\x00\x00";
  90. #EOF
  91. dd if=$USB_MNT_DIR/bootrom.txt of="$disk" bs=512
  92.  
  93. }
  94.  
  95. hex_byte () {
  96.         dd if=/dev/urandom bs=1 count=1 2>/dev/null \
  97.         | od -x | head -1 | cut -d' ' -f2- \
  98.         | tr -d ' ' | cut -c 3-4 | tr '[a-f]' '[A-F]'
  99. }
  100.  
  101. write_hidden_sectors()
  102. {
  103.     local disk=$1
  104.  
  105.     echo "---Writing hidden sectors"
  106.  
  107.     dd if=$stage1File    of="$disk" bs=512 seek=34
  108.     dd if=$ubootFile     of="$disk" bs=512 seek=154
  109.     dd if=$kernelFile    of="$disk" bs=512 seek=1290
  110.     dd if=$initrdFile    of="$disk" bs=512 seek=16674
  111. # Secondary copies of critical blocks
  112. # TODO- configure U-Boot to use these if initial ones are bad
  113.     dd if=$stage1File    of="$disk" bs=512 seek=57088
  114.     dd if=$ubootFile     of="$disk" bs=512 seek=57208
  115.     dd if=$kernelFile    of="$disk" bs=512 seek=58344
  116.  
  117.     # write U-Boot environment, with new random MAC address
  118.     #oui="0x00,0xD0,0xB8"
  119.     #newmac=${oui},0x`hex_byte`,0x`hex_byte`,0x`hex_byte`
  120.     #echo "Setting MAC address to ${newmac}"
  121.     #cat $USB_MNT_DIR/ix1-env.txt \
  122.         #| sed -e "s/\(mac_adr=\)\S* /\1${newmac} /" \
  123.         #| /$USB_MNT_DIR/saveenv \
  124.         #> $USB_MNT_DIR/ix1-env_new.bin || true
  125.     dd if=/$USB_MNT_DIR/ix1-env_old.bin of="$disk" bs=512 seek=558 count=16
  126.     rm /$USB_MNT_DIR/ix1-env_old.bin
  127. }
  128.  
  129. readenv()
  130. {
  131.         # keep the env value same as from the install script
  132.     local disk=$1
  133.         echo "Reading the old environment values"
  134.         dd if="$disk" of=/$USB_MNT_DIR/ix1-env_old.bin bs=512 skip=558 count=16
  135. }
  136.  
  137.  
  138. stop_volumes()
  139. {
  140.         lvchange -a n $MD0VG
  141. }
  142.  
  143. stop_raid()
  144. {
  145.     mdadm --stop --scan
  146. }
  147.  
  148. destroy_raid()
  149. {
  150.         mdadm --zero-superblock $1"1"
  151. }
  152.  
  153. # EMC Imager Script
  154. if [ "$1" == "usb_imaging" ]
  155. then
  156.         echo "Started USB Imaging..."
  157.         mknod /dev/zero c 1 5
  158.  
  159.         # if there is no system partition then create it here
  160.         a=`cat /sys/block/sda/sda1/size`
  161.         UUID_1=`mdadm -E --brief $DISK1_PART1 | awk -F "=" '{print $4}'`
  162.  
  163.         mount_vol
  164.         ret=$?
  165.  
  166.         if [ "$ret" != "0" ] || [ "$a" == "" ] || [ "UUID_1" == "" ] || [ -f "$reinstallFile" ]
  167.         then
  168.                 echo "Going to do a full install"
  169.                 rm $reinstallFile
  170.                 readenv $disk
  171.                 umount /sysroot/boot
  172.                 stop_volumes
  173.                 stop_raid
  174.                 destroy_raid $disk
  175.                 create_partitions $disk
  176.                 sleep 2
  177.                 write_bootrom_directions $disk
  178.                 write_hidden_sectors $disk
  179.                 start_sys_raid $disk
  180.                 create_volumes
  181.                 format_sys_partitions
  182.                 mount_vol
  183.         fi
  184.         APPS_UPGRADE_IMAGE=/usb_drive/emctools/hmnhd_images/temp/apps
  185.         CONFIG_UPGRADE_IMAGE=/usb_drive/emctools/hmnhd_images/temp/config
  186.         OEM_UPGRADE_IMAGE=/usb_drive/emctools/hmnhd_images/temp/oem
  187.         NoExtractionFile=/usb_drive/emctools/hmnhd_images/noextraction
  188.  
  189.         copy_image
  190.         umount sysroot/boot
  191.  
  192.         if [ "hmnhd" == "hmnhd" ]
  193.         then
  194.  
  195.                 kernelFile=/usb_drive/emctools/hmnhd_images/temp/zImage
  196.                 initrdFile=/usb_drive/emctools/hmnhd_images/temp/initrd
  197.                 # update flash images
  198.                 dd if=$kernelFile    of="$disk" bs=512 seek=1290
  199.                 dd if=$initrdFile    of="$disk" bs=512 seek=16674
  200.         fi
  201.  
  202.         if [ -f $NoExtractionFile ]
  203.         then
  204.                 echo "No need to remove the temp folder."
  205.         else
  206.                 echo "Remove the temp folder."
  207.                 rm -fr /usb_drive/emctools/hmnhd_images/temp/
  208.         fi
  209. else
  210.         echo "Invalid argument"
  211. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement