change_kernels 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. #!/bin/bash
  2. # this script changes a kernel in woof-ce based puppy linux
  3. # 3rd revision. GPL v2,(c)Michael Amadio, 01micko@gmail.com, 2015
  4. set -e
  5. # error codes
  6. # 65 - no /etc/DISTRO_SPECS file
  7. # 66 - no /etc/rc.d/PUPSTATE file
  8. # 7 - booted off optical media
  9. # 255 - no 'Huge Kernel' or pup is too old
  10. # 1 - error from external program
  11. # 2 - full install to fast media
  12. # 3 - full install to slow media
  13. # 77 - save back to optical media install
  14. # 21 - no valid kernel image (vmlinuz)
  15. # 22 - no valid kernel modules sfs (zdrv_xxx_xxx.sfs)
  16. # 100 - backup on same date found - technically not an error
  17. # 23 - failed to backup vmlinuz
  18. # 24 - failed to backup zdrv_xxx_xxx.sfs
  19. # 25 - failed to copy new vmlinuz
  20. # 26 - failed to copy new zdrv_xxx_xxx.sfs
  21. # 123 - failed to restore vmlinuz backup
  22. # 124 - failed to restore zdr_xxx_xx.sfs backup
  23. export TEXTDOMAIN=change_kernels
  24. export OUTPUT_CHARSET=UTF-8
  25. # current running kernel - throw away appendages
  26. CURK=`uname -r`
  27. CURK=${CURK%%-*}
  28. # Translations
  29. # buttons
  30. OKE=$(gettext "OK")
  31. CAN=$(gettext "Cancel")
  32. YEP=$(gettext "Yes")
  33. NOPE=$(gettext "No")
  34. HELP=$(gettext "Help")
  35. ROLLBTN=$(gettext "Rollback")
  36. KDATE=$(gettext "kernel from date")
  37. BUTTONY="<button>
  38. <input file stock=\"gtk-yes\"></input>
  39. <label>$YEP</label>
  40. <action type=\"exit\">Yes</action>
  41. </button>"
  42. BUTTONN="<button>
  43. <input file stock=\"gtk-no\"></input>
  44. <label>$NOPE</label>
  45. <action type=\"exit\">No</action>
  46. </button>"
  47. BUTTONO="<button>
  48. <input file stock=\"gtk-ok\"></input>
  49. <label>$OKE</label>
  50. <action type=\"exit\">OK</action>
  51. </button>"
  52. BUTTONC="<button>
  53. <input file stock=\"gtk-cancel\"></input>
  54. <label>$CAN</label>
  55. <action type=\"exit\">Cancel</action>
  56. </button>"
  57. # dialogs
  58. BMSG=$(gettext "Your puppy is too old or broken")
  59. CMSG=$(gettext "Unfortunately this doesn't work in CD/DVD installs")
  60. FMSG=$(gettext "Unfortunately this doesn't work in full installs")
  61. LMSG=$(gettext "Unfortunately this doesn't work with live CD installs")
  62. OMSG=$(gettext "Unfortunately this doesn't work with older puppy installs")
  63. KMSG=$(gettext "Not a valid kernel file")
  64. MMSG=$(gettext "Not a valid module file")
  65. FMMSG=$(gettext "Failed to move")
  66. WARN=$(gettext "WARNING:")
  67. READ_HELP=$(gettext "Please read the Help")
  68. CHOOSE_LOC=$(gettext "Choose the location of kernel file named")
  69. FIND_MOD=$(gettext "Find the module file named")
  70. FND_KRN=$(gettext "We have found that your kernel is located at:")
  71. VMSG=$(gettext "Failed to copy vmlinuz: restoring original")
  72. FSMSG1=$(gettext "Failed to copy")
  73. FSMSG2=$(gettext "restoring original")
  74. RMSG=$(gettext "Now reboot to test your new kernel.")
  75. ERR=$(gettext "Error")
  76. SUCC=$(gettext "Success")
  77. MSG=$(gettext "message")
  78. CTITLE=$(gettext "Confirmation")
  79. CONFMSG1=$(gettext "You have chosen the following kernel files to install")
  80. CONFMSG2=$(gettext "Are you sure?")
  81. KERNMSG=$(gettext "or 'kernel-modules.sfs-XXXXX' if built with kernel-kit. You can try other kernels built in other Puppies")
  82. TT=$(gettext "Drag and drop is supported from Rox Filer")
  83. ROLLTT=$(gettext "Roll back to a previous kernel.")
  84. ROLL1=$(gettext "Choose an earlier kernel to roll back to...")
  85. KRRERR=$(gettext "There are no kernels to roll back to.")
  86. EXISTS=$(gettext "Overwrite")
  87. OVERMSG=$(gettext "backup from today with kernel version")
  88. OVERMSG1=$(gettext "already exists. Press 'Yes' to overwrite, 'No' to continue without overwriting or 'Cancel' to exit")
  89. INF=$(gettext "Info")
  90. FMNT=$(gettext "Failed to mount")
  91. # help
  92. HHELP=HELP
  93. HWARN=$(gettext "WARNING!")
  94. HSYS=$(gettext "You may render your system unbootable!")
  95. HVMLINUZ=$(gettext "Find a vmlinuz file for the first entry box.")
  96. HMODULES1=$(gettext "Find a zdrive file (which may have an obscure name like")
  97. HMODULES2=$(gettext "'kernel-modules.sfs-XXXXX'if it was built it in kernel-kit)")
  98. HMODULES3=$(gettext "for the second entry box.")
  99. HMATCH=$(gettext "These files MUST match!")
  100. HROX=$(gettext "If you use Rox Filer you can drag and drop to the entry boxes.")
  101. HOK=$(gettext "Hit OK.")
  102. HID1=$(gettext "The distro ID string is appended to the vmlinuz and zdrive.")
  103. HID2=$(gettext "Hopefully you can reboot.")
  104. HFAIL=$(gettext "IF IT FAILS")
  105. HBACK1=$(gettext "There are backups of the old vmlinuz and zdrive in place appended")
  106. HBACK2=$(gettext "with the today's date. Hit the Rollback button to restore a backup.")
  107. HGOOD=$(gettext "GOOD LUCK")
  108. HBUG=$(gettext "Bugs to 01micko @ murga forum")
  109. HGPL=GPL2
  110. HCARE=$(gettext "While all care is taken, no responsibility accepted.")
  111. export HHELP HWARN HSYS HVMLINUZ HMODULES1 HMODULES2 HMODULES3 HMATCH HROX HOK HID1 HID2 HFAIL HBACK1 HBACK2 HGOOD HBUG HGPL HCARE
  112. EXITVAL=""
  113. # generic message tool
  114. _msg() {
  115. if [ "$4" = "cancel" ];then
  116. CANCEL="$BUTTONC"
  117. BUTTONZO="${BUTTONY}${BUTTONN}"
  118. elif [ "$4" = "not" ];then
  119. CANCEL=''
  120. unset BUTTONZO
  121. fi
  122. export SPLMSG="<window title=\"$1\">
  123. <vbox>
  124. <frame>
  125. <hbox space-expand=\"true\" space-fill=\"true\">
  126. <text>
  127. <label>\"$1 $MSG:\"</label>
  128. </text>
  129. </hbox>
  130. <hbox space-expand=\"true\" space-fill=\"true\">
  131. <text>
  132. <label>\"$2\"</label>
  133. </text>
  134. </hbox>
  135. </frame>
  136. <hbox>${CANCEL}${BUTTONO}${BUTTONZO}</hbox>
  137. </vbox>
  138. </window>"
  139. eval $(gtkdialog -p SPLMSG -c)
  140. [ "$3" != "100" ] && exit $3 || EXITVAL=$EXIT
  141. }
  142. [ -f /etc/DISTRO_SPECS ] && . /etc/DISTRO_SPECS || _msg $ERR "$BMSG" 65
  143. [ -f /etc/rc.d/PUPSTATE ] && . /etc/rc.d/PUPSTATE || _msg $ERR "$BMSG" 66
  144. # test we can do this
  145. case $PUPMODE in
  146. 2|3)_msg $ERR "$FMSG" $PUPMODE;;
  147. 77)_msg $ERR "$CMSG" $PUPMODE;;
  148. esac
  149. [ "$PMEDIA" = "cd" ] && _msg $ERR "$LMSG" 7
  150. [ "$DISTRO_KERNEL_PET" = "Huge_Kernel" ] || _msg $ERR "$OMSG" 255
  151. # find where kernel and modules live
  152. MODULES_HOME=/initrd${PUP_HOME}${PSUBDIR}
  153. VHOME=$PDEV1
  154. MHOME=${PUPSFS%%,*}
  155. if [ "$VHOME" = "$MHOME" ];then
  156. KERNEL_HOME=/initrd${PUP_HOME}${PSUBDIR}
  157. else
  158. KERNEL_HOME=/mnt/$VHOME
  159. if mount | grep -q "${VHOME}" ;then
  160. echo "${KERNEL_HOME} is mounted"
  161. if ls ${KERNEL_HOME} | grep -q 'vmlinuz' ;then
  162. echo "This is where the kernel lives: ${KERNEL_HOME}"
  163. else
  164. echo "This is NOT where the kernel lives: ${KERNEL_HOME}"
  165. KERNEL_HOME=/initrd${PUP_HOME}${PSUBDIR}
  166. echo "We'll try $KERNEL_HOME"
  167. fi
  168. else
  169. UMT=1 # in case kernel lives elsewhere
  170. [ -f "${KERNEL_HOME}" ] || mkdir -p ${KERNEL_HOME}
  171. mount /dev/${VHOME} ${KERNEL_HOME}
  172. ret=$?
  173. [ $ret -ne 0 ] && $ERR "$FMNT" $ret
  174. if ls ${KERNEL_HOME} | grep -q 'vmlinuz' ;then
  175. echo "This is where the kernel lives: ${KERNEL_HOME}"
  176. else
  177. echo "This is NOT where the kernel lives: ${KERNEL_HOME} .. unmounting"
  178. umount /dev/${VHOME}
  179. KERNEL_HOME=/initrd${PUP_HOME}${PSUBDIR}
  180. echo "We'll try $KERNEL_HOME"
  181. fi
  182. fi
  183. fi
  184. DATE=`date +%y%m%d`
  185. # fix the id string
  186. id_string_fix () {
  187. # check ID sting
  188. ID=`tail -c2 $KERNEL_HOME/vmlinuz` # sanity check for ID string
  189. case "$ID" in
  190. XX) #exists so check and fix if needed
  191. C_ID=`tail -c32 $KERNEL_HOME/vmlinuz`
  192. if [ "$C_ID" != "$DISTRO_IDSTRING" ];then # fix it
  193. sed -i "s%$C_ID%$DISTRO_IDSTRING%" $KERNEL_HOME/vmlinuz
  194. sed -i "s%$C_ID%$DISTRO_IDSTRING%" $MODULES_HOME/${DISTRO_ZDRVSFS}
  195. fi
  196. ;;
  197. *) #returns garbage so doesn't exist.. append
  198. echo -n "$DISTRO_IDSTRING" >> $KERNEL_HOME/vmlinuz
  199. echo -n "$DISTRO_IDSTRING" >> $MODULES_HOME/${DISTRO_ZDRVSFS}
  200. ;;
  201. esac
  202. }
  203. # the main operation
  204. change() {
  205. echo "$1" | grep -q 'vmlinuz' || _msg $ERR "$KMSG" 21
  206. echo "$2" | grep 'sfs' | egrep -q 'zdrv|kernel\-modules' || _msg $ERR "$MMSG" 22
  207. export SURE="<window title=\"$CTITLE\">
  208. <vbox>
  209. <frame>
  210. <hbox space-expand=\"true\" space-fill=\"true\">
  211. <text>
  212. <label>\"${CONFMSG1}:\"</label>
  213. </text>
  214. </hbox>
  215. <hbox space-expand=\"true\" space-fill=\"true\">
  216. <text use-markup=\"true\">
  217. <label>\"<b>${1##*/}</b>\"</label>
  218. </text>
  219. </hbox>
  220. <hbox space-expand=\"true\" space-fill=\"true\">
  221. <text use-markup=\"true\">
  222. <label>\"<b>${2##*/}</b>\"</label>
  223. </text>
  224. </hbox>
  225. <hseparator></hseparator>
  226. <hbox space-expand=\"true\" space-fill=\"true\">
  227. <text use-markup=\"true\">
  228. <label>\"<big>${CONFMSG2}</big>\"</label>
  229. </text>
  230. </hbox>
  231. </frame>
  232. <hbox>
  233. $BUTTONY
  234. $BUTTONN
  235. </hbox>
  236. </vbox>
  237. </window>"
  238. eval $(gtkdialog -p SURE -c)
  239. case $EXIT in
  240. No|abort)echo "abort"; exit 0;;
  241. esac
  242. # move it
  243. OV=yes
  244. if [ -f "$KERNEL_HOME/vmlinuz.${DATE}-${CURK}" ];then
  245. _msg "$EXISTS" "vmlinuz.${DATE}-${CURK} $OVERMSG $CURK $OVERMSG1" 100 cancel
  246. case $EXITVAL in
  247. Cancel|abort)echo "aborting"; exit 0;;
  248. No)echo "no overwrite"; OV=no ;;
  249. esac
  250. fi
  251. if [ "$OV" != "no" ];then
  252. mv $KERNEL_HOME/vmlinuz $KERNEL_HOME/vmlinuz.${DATE}-${CURK}
  253. [ $? -ne 0 ] && _msg $ERR "$FMMSG ${1##*/}" 23
  254. mv $MODULES_HOME/$DISTRO_ZDRVSFS $MODULES_HOME/$DISTRO_ZDRVSFS.${DATE}-${CURK}
  255. [ $? -ne 0 ] && _msg $ERR "$FMMSG ${2##*/}" 24
  256. fi
  257. cp -a "$1" $KERNEL_HOME/vmlinuz
  258. [ $? -ne 0 ] && mv -f $KERNEL_HOME/vmlinuz.${DATE}-${CURK} $KERNEL_HOME/vmlinuz \
  259. && _msg $ERR "$VMSG" 25
  260. cp -a "$2" $MODULES_HOME/$DISTRO_ZDRVSFS
  261. [ $? -ne 0 ] && mv -f $MODULES_HOME/${DISTRO_ZDRVSFS}.${DATE}-${CURK} $MODULES_HOME/$DISTRO_ZDRVSFS \
  262. && _msg $ERR "$FSMSG1 $DISTRO_ZDRVSFS: $FSMSG2" 26
  263. id_string_fix
  264. [ "$UMT" = "1" ] && umount $KERNEL_HOME
  265. _msg $SUCC "$RMSG" 0 not
  266. }
  267. # rollback to a backup
  268. rollback() {
  269. VTRUE=`ls $KERNEL_HOME|grep 'vmlinuz'|cut -d '.' -f2,3,4,5|grep -E '^1|^2'`
  270. [ -z "$VTRUE" ] && _msg $ERR $KRRERR 42
  271. ls $MODULES_HOME|grep "$DISTRO_ZDRVSFS"|grep -q "$VTRUE" || _msg $ERR "$KRRERR" 43
  272. for i in `echo $VTRUE`; do
  273. BTNS="$BTNS""<hbox space-expand=\"true\" space-fill=\"true\">
  274. <button>
  275. <label>$KDATE : $i</label>
  276. <action type=\"exit\">$i</action>
  277. </button>
  278. </hbox>"
  279. done
  280. export RROLL="<window title=\"$ROLLBTN\">
  281. <vbox>
  282. <frame>
  283. <hbox space-expand=\"true\" space-fill=\"true\">
  284. <text>
  285. <label>\"${ROLL1}:\"</label>
  286. </text>
  287. </hbox>
  288. $BTNS
  289. </frame>
  290. <hbox>
  291. $BUTTONC
  292. </hbox>
  293. </vbox>
  294. </window>"
  295. eval $(gtkdialog -p RROLL -c)
  296. case $EXIT in
  297. Cancel|abort)echo "abort"; exit 0;;
  298. [1-2][0-9][0-1][0-9][0-3][0-9]-[3-4].[0-9]*) #any yymmdd date between 2010 and 2029
  299. KIMAGE=vmlinuz.${EXIT}
  300. KMODULES=${DISTRO_ZDRVSFS}.${EXIT};;
  301. *)echo bad; exit 99;;
  302. esac
  303. #copy - retain the backup
  304. cp -af $KERNEL_HOME/${KIMAGE} $KERNEL_HOME/vmlinuz
  305. [ $? -ne 0 ] && _msg $ERR "$FMMSG ${KIMAGE}" 123
  306. cp -af $MODULES_HOME/${KMODULES} $MODULES_HOME/${DISTRO_ZDRVSFS}
  307. [ $? -ne 0 ] && _msg $ERR "$FMMSG ${DISTRO_ZDRVSFS}" 124
  308. id_string_fix
  309. [ "$UMT" = "1" ] && umount $KERNEL_HOME
  310. _msg $SUCC "$RMSG" 0
  311. }
  312. help_me() {
  313. cat > /tmp/kernelhelp.txt << _EOF
  314. ${HHELP}
  315. =========================== $HWARN =====================================
  316. ${HSYS}
  317. =========================== $HWARN =====================================
  318. ${HVMLINUZ}
  319. ${HMODULES1}
  320. ${HMODULES2}
  321. ${HMODULES3}
  322. ${HMATCH}
  323. ${HROX}
  324. ${HOK}
  325. ${HID1}
  326. ${HID2}
  327. ${HFAIL}
  328. ${HBACK1}
  329. ${HBACK2}
  330. ${HGOOD}
  331. ${HBUG}
  332. ${HGPL}
  333. ${HCARE}
  334. _EOF
  335. defaulttextviewer /tmp/kernelhelp.txt
  336. rm /tmp/kernelhelp.txt
  337. }
  338. export -f help_me
  339. # main
  340. export GUI="<window title=\"Change Kernels\">
  341. <vbox>
  342. <frame>
  343. <hbox space-expand=\"true\" space-fill=\"true\">
  344. <text use-markup=\"true\">
  345. <label>\"<big>$WARN</big> $READ_HELP\"</label>
  346. </text>
  347. </hbox>
  348. <hbox space-expand=\"true\" space-fill=\"true\">
  349. <text><label>$CHOOSE_LOC vmlinuz</label></text>
  350. </hbox>
  351. <hbox space-expand=\"true\" space-fill=\"true\">
  352. <entry accept=\"file\" tooltip-text=\"$TT\" space-expand=\"true\" space-fill=\"true\">
  353. <variable>VMLINUZ</variable>
  354. </entry>
  355. <button space-fill=\"false\">
  356. <input file stock=\"gtk-open\"></input>
  357. <action type=\"fileselect\">VMLINUZ</action>
  358. </button>
  359. </hbox>
  360. <hbox space-expand=\"true\" space-fill=\"true\">
  361. <text><label>$FIND_MOD $DISTRO_ZDRVSFS $KERNMSG</label></text>
  362. </hbox>
  363. <hbox space-expand=\"true\" space-fill=\"true\">
  364. <entry accept=\"file\" tooltip-text=\"$TT\" space-expand=\"true\" space-fill=\"true\">
  365. <variable>MODULES</variable>
  366. </entry>
  367. <button space-fill=\"false\">
  368. <input file stock=\"gtk-open\"></input>
  369. <action type=\"fileselect\">MODULES</action>
  370. </button>
  371. </hbox>
  372. <frame $INF>
  373. <hbox space-expand=\"true\" space-fill=\"true\">
  374. <text>
  375. <label>$FND_KRN</label>
  376. </text>
  377. </hbox>
  378. <hbox space-expand=\"true\" space-fill=\"true\">
  379. <text use-markup=\"true\">
  380. <label>\"<b>$MODULES_HOME</b>\"</label>
  381. </text>
  382. </hbox>
  383. </frame>
  384. </frame>
  385. <hbox>
  386. <button>
  387. <label>$HELP</label>
  388. <input file stock=\"gtk-help\"></input>
  389. <action>help_me</action>
  390. </button>
  391. <button tooltip-text=\"$ROLLTT\">
  392. <label>$ROLLBTN</label>
  393. <input file stock=\"gtk-refresh\"></input>
  394. <action type=\"exit\">roll</action>
  395. </button>
  396. $BUTTONO
  397. $BUTTONC
  398. </hbox>
  399. </vbox>
  400. </window>"
  401. eval $(gtkdialog -p GUI -c)
  402. case $EXIT in
  403. OK) change $VMLINUZ $MODULES;;
  404. roll) rollback;;
  405. *)exit 0;;
  406. esac