Advertisement
Guest User

Untitled

a guest
Nov 10th, 2014
309
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. #
  3. # � 2010 Western Digital Technologies, Inc. All rights reserved.
  4. #
  5. # updateFirmwareFromFile.sh <filename> [check_filesize]"
  6. #
  7.  
  8. # post entries to updatelog
  9. uplog()
  10. {
  11. logtext=${@}
  12. logtag="`basename $0`:`date -u +"%D %T"`:"
  13. echo "${logtag}${logtext}" 2>&1 | tee -a ${updatelog}
  14. }
  15.  
  16. # flag error after attempting to apply upgrade using dpkg
  17. pre-update_error()
  18. {
  19. errortext=${@}
  20. echo "Error fw_update_status: ${errortext}" 2>&1 | tee -a ${updatelog}
  21. echo ${errortext} > /tmp/fw_update_status
  22. uplog ${errortext}
  23. rm -f ${FW_UPDATE_MUTEX}
  24. sleep 1
  25. exit 1
  26. }
  27.  
  28. # prepare for pkg upgrades
  29. pkg_upgrade_init()
  30. {
  31. ledCtrl.sh LED_EV_FW_UPDATE LED_STAT_IN_PROG
  32. incUpdateCount.pm firmware_update
  33.  
  34. ## - TODO: Use signal & run-levels
  35. ## Stop processes
  36. [ -e /etc/init.d/cron ] && /etc/init.d/cron stop 2>/dev/null
  37. [ -e /etc/init.d/monitorio ] && /etc/init.d/monitorio stop 2>/dev/null
  38. [ -e /etc/init.d/monitorTemperature ] && /etc/init.d/monitorTemperature stop 2>/dev/null
  39. [ -e /etc/init.d/twonky ] && /etc/init.d/twonky stop 2>/dev/null
  40. [ -e /etc/init.d/access ] && /etc/init.d/access stop 2>/dev/null
  41. [ -e /etc/init.d/itunes ] && /etc/init.d/itunes stop 2>/dev/null
  42. [ -e /etc/init.d/orion ] && /etc/init.d/orion stop 2>/dev/null
  43. [ -e /etc/init.d/wdphotodbmergerd ] && /etc/init.d/wdphotodbmergerd stop 2>/dev/null
  44. [ -e /etc/init.d/wdmcserverd ] && /etc/init.d/wdmcserverd stop 2>/dev/null
  45. [ -e /etc/init.d/samba ] && /etc/init.d/samba stop 2>/dev/null
  46. [ -e /etc/init.d/netatalk ] && /etc/init.d/netatalk stop 2>/dev/null
  47. [ -e /etc/init.d/upnp_nas ] && /etc/init.d/upnp_nas stop 2>/dev/null
  48. [ -e /etc/init.d/wddispatcherd ] && /etc/init.d/wddispatcherd stop 2>/dev/null
  49. [ -e /etc/init.d/wdnotifierd ] && /etc/init.d/wdnotifierd stop 2>/dev/null
  50. #
  51. wdAutoMountAdm.pm shutdownCleanup
  52. [ -e /etc/init.d/wdAutoMount ] && /etc/init.d/wdnotifierd stop 2>/dev/null
  53. [ -e /etc/init.d/nfs-kernel-server ] && /etc/init.d/nfs-kernel-server stop 2>/dev/null
  54. [ -e /etc/init.d/nfs-common ] && /etc/init.d/nfs-common stop 2>/dev/null
  55. sync
  56. sleep 2
  57. }
  58.  
  59. #
  60. pkg_upgrade_exec()
  61. {
  62. if [ "${system_boot_type}" == "flash" ]; then
  63. dpkg --fsys-tarfile ${filename} | tar xOf - ./upgrade.sh > /tmp/upgrade.sh
  64. status=$?
  65. if [ ${status} -eq 0 ]; then
  66. chmod +x /tmp/upgrade.sh
  67. /tmp/upgrade.sh ${filename} 2>&1 | tee -a ${updatelog}
  68. status=$?
  69. fi
  70. else
  71. #upgrade
  72. if [ -n "${update_container}" ] && [ -n "${package_newfile}" ]; then
  73. pkg_name=${package_newfile}
  74. pkg_type=${pkg_name%%-*}
  75. pkg_id=${pkg_name#*-}
  76. pkg_id=${pkg_id%%-*}
  77. uplog "Extracting wd.container packages for Package ID: ${package_newfile}, into ${pkg_upgrades_dir}/${pkg_type}_${pkg_id}"
  78. mkdir -p ${pkg_upgrades_dir}/${pkg_type}_${pkg_id}
  79. chmod -R 777 ${pkg_upgrades_dir}/${pkg_type}_${pkg_id}
  80. dpkg -i --force-overwrite ${filename} 2>&1 | tee -a ${updatelog}
  81. status=$?
  82. mv -v ${pkg_upgrades_dir}/*.deb ${pkg_upgrades_dir}/${pkg_type}_${pkg_id}/
  83. elif [ -z "${update_container}" ]; then
  84.  
  85. # if upgrade dir is empty, then copy from backup location
  86. [ ! "$( ls -A ${pkg_upgrades_dir} )" ] && cp -av /usr/local/nas/upgrade/* ${pkg_upgrades_dir}
  87.  
  88. # get upgrade-version
  89. tmp_dir=`mktemp -d`
  90. dpkg --control ${filename} ${tmp_dir}
  91. new_upgrade_version="0"
  92. [ -f $tmp_dir/upgrade-version ] && new_upgrade_version=`cat $tmp_dir/upgrade-version`
  93. current_upgrade_version="0"
  94. [ -f ${pkg_upgrades_dir}/upgrade-version ] && current_upgrade_version=`cat ${pkg_upgrades_dir}/upgrade-version`
  95. uplog "new_upgrade_version=$((new_upgrade_version))"
  96. uplog "current_upgrade_version=$((current_upgrade_version))"
  97. cp -v $tmp_dir/upgrade-version ${pkg_upgrades_dir}/upgrade-version
  98. if [ $((current_upgrade_version)) -gt $((new_upgrade_version)) ]; then
  99. # use current upgrade scripts to drive upgrade
  100. touch /tmp/downgrade
  101. uplog "Use current upgrade scripts to drive upgrade"
  102. ${pkg_upgrades_dir}/preinst 2>&1 | tee -a ${updatelog}
  103. uplog "Extract rootfs..."
  104. dpkg --fsys-tarfile ${filename} | tar -xvf - -C / --wildcards --no-anchored 'rootfs.*' 2>&1 | tee -a ${updatelog}
  105. uplog "Run postinst"
  106. ${pkg_upgrades_dir}/postinst 2>&1 | tee -a ${updatelog}
  107. else
  108. uplog "Newer upgrade scripts in update package, use normal update method"
  109. dpkg -i --force-overwrite --force-depends ${filename} 2>&1 | tee -a ${updatelog}
  110. fi
  111. status=$?
  112. fi
  113. fi
  114. }
  115.  
  116. # flag error after attempting to apply upgrade using dpkg
  117. update_error()
  118. {
  119. error=${1}
  120. echo ${error} > /tmp/fw_update_status
  121. uplog ${error}
  122. echo "Error fw_update_status: ${error}" 2>&1 | tee -a ${updatelog}
  123. [ -z "${update_container}" ] && restoreRaid
  124. ledCtrl.sh LED_EV_FW_UPDATE LED_STAT_OK
  125. rm -f ${FW_UPDATE_MUTEX}
  126. exit 1
  127. }
  128.  
  129. pkg_upgrade_verify()
  130. {
  131. cat ${updatelog} | grep -q "not a debian format archive"
  132. if [ $? -eq 0 ]; then
  133. update_error "failed 200 \"invalid firmware package\""
  134. fi
  135.  
  136. cat ${updatelog} | grep -q "Install not supported"
  137. if [ $? -eq 0 ]; then
  138. update_error "failed 200 \"invalid firmware package\""
  139. fi
  140.  
  141. cat ${updatelog} | grep -q "dpkg-deb: subprocess <decompress> returned error"
  142. if [ $? -eq 0 ]; then
  143. update_error "failed 202 \"upgrade download failure\""
  144. fi
  145.  
  146. if [ ${status} -ne 0 ]; then
  147. echo "dkpg exited with non-zero status: ${status}"
  148. update_error "Update failed. Check ${updatelog} for details."
  149. fi
  150. }
  151.  
  152. ## section: main script
  153.  
  154. PATH=/sbin:/bin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
  155. source /usr/local/sbin/data-volume-config_helper.sh 2>/dev/null
  156. source /etc/system.conf
  157. [ -f /usr/local/sbin/ledConfig.sh ] && . /usr/local/sbin/ledConfig.sh
  158. filename=${1}
  159. check_size=${2:-""}
  160. test=${3:-""}
  161. # check params
  162. if [ $# -lt 1 ]; then
  163. echo "usage: updateFirmwareFromFile.sh <filename> [check_filesize]"
  164. exit 1
  165. fi
  166. if [ ! -f ${filename} ]; then
  167. echo "File not found"
  168. exit 1
  169. fi
  170.  
  171. # set a 'non-idle' status for the manual update-from-file case
  172. [ ! -f /tmp/fw_update_status ] && echo "downloading" > /tmp/fw_update_status
  173.  
  174. logtag="`basename $0`:`date -u +"%D %T"`:"
  175. logger -s -t "${logtag}" "( $@ )"
  176.  
  177. # initiate updatelog
  178. updatelog="/CacheVolume/update.log"
  179. echo "${logtag}upgrade start: $@" 2>&1 | tee ${updatelog}
  180.  
  181. FW_UPDATE_MUTEX=/tmp/fw_update_mutex
  182. [ -f ${FW_UPDATE_MUTEX} ] && exit 0
  183. touch ${FW_UPDATE_MUTEX}
  184.  
  185. uplog "check_size=${check_size}"
  186.  
  187. # require that all internal drives be present before upgrade
  188. driveList=(`internalDrives`)
  189. if [ "${#driveList[@]}" -lt "${MIN_ROOTFS_DRIVE_COUNT}" ]; then
  190. error="failed 205 \"all drives must be present for upgrade\""
  191. pre-update_error $error
  192. fi
  193.  
  194. # check disk usage
  195. dfout=`df | grep /DataVolume`
  196. avail=`echo "$dfout" | awk '{printf("%d",$2-$3)}'`
  197. if [ "${avail}" -lt "${fwUpdateSpace}" ] && [ "${system_boot_type}" != "flash" ]; then
  198. error="failed 201 \"not enough space on device for upgrade\""
  199. echo ${error} > /tmp/fw_update_status
  200. pre-update_error $error
  201. fi
  202.  
  203. if [ "${check_size}" != "" ] && [ "${test}" == "test_size" ]; then
  204. uplog "truncating file for size test..."
  205. blocksize_ls_original=$(ls -l ${filename} | cut -d' ' -f5)
  206. uplog "blocksize_ls_original=$blocksize_ls_original"
  207. dd if=${filename} of=${filename}-new bs=1M count=120
  208. mv ${filename}-new ${filename}
  209. fi
  210.  
  211. # check file size if file was downloaded
  212. if [ "${check_size}" != "" ]; then
  213. FileSize=( $(cat /tmp/fw_upgrade_filesize) )
  214. blocksize_dpkg0="${FileSize[0]}"
  215. blocksize_dpkg1="${FileSize[1]}"
  216. blocksize_ls=$(ls -l ${filename} | cut -d' ' -f5)
  217. uplog "blocksize_dpkg0=$blocksize_dpkg0"
  218. uplog "blocksize_dpkg1=$blocksize_dpkg1"
  219. uplog "blocksize_ls=$blocksize_ls"
  220. if [ "${blocksize_dpkg0}" != "${blocksize_ls}" ] && [ "${blocksize_dpkg1}" != "${blocksize_ls}" ]; then
  221. error="failed 202 \"failed download file size check\""
  222. pre-update_error "${error}"
  223. fi
  224. fi
  225.  
  226. version_current=`cat /etc/version | tr -d .-`
  227. version_newfile=`dpkg -f ${filename} Version`
  228. version_newfile=`echo ${version_newfile} | tr -d .-`
  229. package_newfile=`dpkg -f ${filename} Package`
  230. # extract master package and update-container names
  231. master_package_new=${package_newfile%%-*}
  232. update_container=${package_newfile#*-}
  233. update_container=${update_container%%-*}
  234. [ "${master_package_name}" == "${update_container}" ] && update_container=''
  235.  
  236. uplog "version_current=$version_current"
  237. uplog "version_newfile=$version_newfile"
  238. uplog "package_newfile=$package_newfile"
  239. uplog "master_package_name=$master_package_name"
  240. uplog "master_package_new=$master_package_new"
  241. uplog "update_container=$update_container"
  242.  
  243. # declare arithmetic variable types (ensure conversion to base 10)
  244. let "vnew = $((10#$version_newfile))"
  245. package_newfile=`dpkg -f ${filename} Package`
  246. # extract master package and update-container names
  247. master_package_new=${package_newfile%%-*}
  248. update_container=${package_newfile#*-}
  249. update_container=${update_container%%-*}
  250. [ "${master_package_name}" == "${update_container}" ] && update_container=''
  251.  
  252. uplog "version_current=$version_current"
  253. uplog "version_newfile=$version_newfile"
  254. uplog "package_newfile=$package_newfile"
  255. uplog "master_package_name=$master_package_name"
  256. uplog "master_package_new=$master_package_new"
  257. uplog "update_container=$update_container"
  258.  
  259. # declare arithmetic variable types (ensure conversion to base 10)
  260. let "vnew = $((10#$version_newfile))"
  261. let "vnow = $((10#$version_current))"
  262. if [ "${master_package_new}" == "wd.container" ] || [ "${master_package_new}" == "wd.group" ] || [ "${master_package_new}" == "wd.supergroup" ]; then
  263. uplog "Package compatiblities will be enforced by the incomming policy rules."
  264. else
  265. # enforce same 'master-package' name
  266. if [ "${master_package_name}" != "${master_package_new}" ]; then
  267. error="failed 200 \"invalid firmware package\""
  268. pre-update_error $error
  269. fi
  270. # ITR#34229: don't allow down rev code to be applied
  271. # -but allow 'patch updates" with any version
  272. if [ -z "${update_container}" ] && [ "${vnew}" -lt "${vnow}" ]; then
  273. error="failed 200 \"invalid firmware package\""
  274. pre-update_error $error
  275. fi
  276. fi
  277.  
  278. status=1
  279.  
  280. # Prepare for package upgrade
  281. pkg_upgrade_init
  282. # Do pkg upgrades
  283. pkg_upgrade_exec
  284. # Verify upgrade
  285. pkg_upgrade_verify
  286.  
  287. # Unmount & reboot
  288. rm -f ${FW_UPDATE_MUTEX}
  289. umount -f /nfs/Public
  290. umount -f /nfs/TimeMachineBackup
  291. umount -f /nfs/SmartWare
  292. umount -f /proc/fs/nfsd
  293.  
  294. # sleep 10 seconds to allow UI to detect update
  295. sleep 10
  296. reboot -f -h -i &
  297.  
  298. exit ${status}
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement