Advertisement
Guest User

Macindoe

a guest
Feb 9th, 2011
1,242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 10.16 KB | None | 0 0
  1. #! /bin/sh
  2. # Script to install debian onto /dev/md0 of a MyBook World Edition White Light
  3. # This is only intended as a demonstration of how to get debian working
  4. # Please install debian using one of the prepared images instead of this script if possible
  5. #
  6. # You must upgrade to the latest official WD firmware before running this script.
  7. #
  8. # If you want the power and recovery buttons to work, you'll need to download some
  9. # modified kernel modules from http://www.mediafire.com/?ro0ee294lq4oqqi (includes source code)
  10. #
  11. # References used while writing this script:
  12. # http://zeroone.homeunix.net/~blip/computer/mbwe/ [only online during certain hours]
  13. # http://devnull.samersoff.net/pub/MyBook_me/
  14. # http://mybookworld.wikidot.com/forum/t-163056/mylenny-firmware
  15. # WD GPL source code
  16. #
  17. # A huge thanks to the people behind those resources :)
  18.  
  19. WORKING_DIR="/var/upgrade/debian-install/"
  20.  
  21. # check if lenny has been moved to archive.debian.org and set mirror accordingly
  22. wget --spider http://ftp.debian.org/debian/dists/lenny/main/binary-armel/Packages.gz -O /dev/null --quiet
  23. RETURN=$?
  24. if [ "$RETURN" == "0" ]; then
  25.     LENNY_MIRROR="http://ftp.debian.org/debian/"
  26. else
  27.     wget --spider http://archive.debian.org/debian/dists/lenny/main/binary-armel/Packages.gz -O /dev/null --quiet
  28.     RETURN=$?
  29.     if [ "$RETURN" == "0" ]; then
  30.         LENNY_MIRROR="http://archive.debian.org/debian/"
  31.     else
  32.         echo 'Oh $#|t'! I cant find any lenny packages!
  33.         echo "Please edit this script and set the LENNY_MIRROR variable manually"
  34.     fi
  35. fi
  36.  
  37. SQUEEZE_MIRROR="http://ftp.debian.org/debian/"
  38.  
  39. if [ ! -f /etc/model ]; then
  40.     echo You must execute this on your mybook, not on a PC!
  41.     exit
  42. fi
  43.  
  44. # test that cdebootstrap is installed
  45. which cdebootstrap-static > /dev/null
  46. CDEB_EXISTS=$?
  47. if [ $CDEB_EXISTS != "0" ]; then
  48.     echo Understandably, you do not have cdebootstrap-static installed
  49.     echo Unfortunately this script depends on cdebootstrap and cannot install it automatically because ar and tar are not installed on the mybook.
  50.     echo To install cdebootstrap yourself, please go to http://ftp.debian.org/debian/pool/main/c/cdebootstrap/ on a PC and download a static copy for the armel architecture, for example http://ftp.debian.org/debian/pool/main/c/cdebootstrap/cdebootstrap-static_0.5.7_armel.deb
  51.     echo Next run \"ar x cdebootstrap-static*.deb\"
  52.     echo Then run \"tar xvf data.tar.gz\"
  53.     echo "Then copy the stuff in the newly created usr/ (not /usr/!) to your mybook, maybe like this:"
  54.     echo scp -r usr/* root@ip-of-your-mybook:/usr/
  55.     echo "then try running this script again :)"
  56.     echo I\'m sorry to inconvenience you like this, but it couldn\'t be helped. This is the only manual part of the installation.
  57.     exit
  58. fi
  59.  
  60. echo "This script will download and install a debian system to /dev/md0, wiping the vendor's firmware in the process. You will be able to ssh into the new system using the root password "mybook" once installation has finished, but note that the ip address of your mybook may have changed."
  61. echo
  62. echo "-- DISCLAIMER --"
  63. echo "This script is provided \"as is\" - it works for me, but I can't guarantee it will for you. I take no responsibility for anything that happens, especially if you don't make backups. A great idea would be to copy the first 10GB of your mybook hard drive onto a spare hard drive, and then run this script on the spare. That way, even if everything goes wrong, you'll still have your original hard drive intact."
  64. echo
  65. echo "Now then, which version of debian would you like to install?"
  66. echo "Your choices are:"
  67. echo "1) debian 5.0 lenny"
  68. echo "2) debian 6.0 squeeze"
  69. echo "Note that we have to use the kernel provided by Western Digital, which is very old. The version of udev provided with squeeze will not run on such an old kernel, so to make squeeze run it will be installed with the version of udev from lenny and apt-get will be told not to upgrade udev."
  70. echo -n "Please enter either 1 (for lenny) or 2 (for squeeze): "
  71. read INPUT
  72.  
  73. # validate input
  74. while [ "$INPUT" != "1" ] && [ "$INPUT" != "2" ]; do
  75.     echo -n "That is not a 1 or a 2! Please enter one of those numbers: "
  76.     read INPUT
  77. done
  78.  
  79. # process input
  80. if [ "$INPUT" == "1" ]; then
  81.     VERSION="lenny"
  82.     MIRROR="$LENNY_MIRROR"
  83. else
  84.     VERSION="squeeze"
  85.     MIRROR="$SQUEEZE_MIRROR"
  86. fi
  87.  
  88. # check working dir doesn't already exist
  89. if [ -e "$WORKING_DIR" ]; then
  90.     echo -n "$WORKING_DIR already exists! Delete it [y/n]?"
  91.     read DELETE
  92.     if [ "$DELETE" == "y" ] || [ "$DELETE" == "Y" ] || [ "$DELETE" == "yes" ]; then
  93.         rm -r "$WORKING_DIR"
  94.     elif [ "$DELETE" == "n" ] || [ "$DELETE" == "N" ] || [ "$DELETE" == "no" ]; then
  95.         exit
  96.     else
  97.         echo "Not a valid answer. Exiting..."
  98.         exit
  99.     fi
  100. fi
  101. mkdir "$WORKING_DIR"
  102.  
  103. echo "downloading a debian ${VERSION} system (this may take a while) ..."
  104.  
  105. # download the debian system
  106. cdebootstrap-static --allow-unauthenticated --include=openssh-server,udev,ntp,less,ntfs-3g "$VERSION" "$WORKING_DIR" "$MIRROR"
  107.  
  108. echo
  109. # set the root password - see, no nastiness!
  110. echo "I will now run passwd to set the root password. If you don't trust me with your root password, just check my source code - I swear my intentions are honorable!"
  111. chroot "$WORKING_DIR" passwd
  112.  
  113. # apply the squeeze udev hack
  114. if [ $VERSION == "squeeze" ]; then
  115.     echo "replacing squeeze's udev with lenny's..."
  116.     chroot "$WORKING_DIR" sh -c "yes | apt-get remove udev"
  117.     echo "deb $LENNY_MIRROR lenny main" > "${WORKING_DIR}/etc/apt/sources.list"
  118.     chroot "$WORKING_DIR" apt-get update
  119.     chroot "$WORKING_DIR" sh -c "yes | apt-get install udev"
  120.     chroot "$WORKING_DIR" sh -c "echo \"udev hold\" | dpkg --set-selections" # stop apt-get from ever upgrading udev
  121.     echo > "${WORKING_DIR}/etc/apt/sources.list"
  122. fi
  123.  
  124. # copy across the relevant files to the debian filesystem
  125. echo "copying required files from the vendor's firmware to the debian system..."
  126. cp -r "/lib/modules/2.6.24.4/" "${WORKING_DIR}/lib/modules/" 2> /dev/null # kernel modules
  127. [ -e "${WORKING_DIR}/lib/firmware/" ] || mkdir "${WORKING_DIR}/lib/firmware/"
  128. cp /usr/lib/hotplug/firmware/gmac_copro_firmware "${WORKING_DIR}/lib/firmware/" # ethernet firmware
  129. cp /etc/network/interfaces "${WORKING_DIR}/etc/network/interfaces" # network config
  130.  
  131. # write config files using here documents
  132. echo "writing config files..."
  133. cat >> "${WORKING_DIR}/etc/modules" << 'EOF'
  134. # gmac is loaded in /etc/inittab
  135. ehci_hcd
  136. nfsd
  137. rtc_ds1307
  138. i2c_oxnas_bitbash
  139. vfat
  140. nls_iso8859_1
  141. nls_cp437
  142. user_recovery_button
  143. thermAndFan
  144. rng-core
  145. scsi_wait_scan
  146. hfsplus
  147. aes_generic
  148. pcbc
  149. michael_mic
  150. arc4
  151. ecb
  152.  
  153. # 2-disk users can try "power_button invert_leds=0", but I don't think it does anything
  154. power_button
  155. # I haven't tested this, but I think 2-disk users will probably want to use "oxnas-wd810-leds negative_led_logic=1" instead
  156. oxnas-wd810-leds
  157. EOF
  158.  
  159. mv "${WORKING_DIR}/etc/inittab" "${WORKING_DIR}/etc/inittab.original"
  160. echo 'GM::sysinit:/sbin/modprobe gmac mac_adr=$mac_adr' > "${WORKING_DIR}/etc/inittab"
  161. cat "${WORKING_DIR}/etc/inittab.original" >> "${WORKING_DIR}/etc/inittab"
  162. echo 'T0:23:respawn:/sbin/getty -L ttyS0 115200 vt100' >> "${WORKING_DIR}/etc/inittab"
  163.  
  164. mknod "${WORKING_DIR}/dev/ttyS0" c 4 64 # create static serial port device node in case udev fails (not that it ever would...)
  165.  
  166. echo "deb ${MIRROR} ${VERSION} main non-free contrib" > "${WORKING_DIR}/etc/apt/sources.list"
  167.  
  168. # hack to allow mounting ntfs without specifying "-t ntfs-3g"
  169. chroot "${WORKING_DIR}" ln -s /sbin/mount.ntfs-3g /sbin/mount.ntfs
  170.  
  171. cat >> "${WORKING_DIR}/etc/fstab" << 'EOF'
  172. /dev/md0 / ext3 defaults 0 0
  173. /dev/md1 swap swap defaults 0 0
  174. /dev/md2 /mnt/data/ xfs defaults 0 0
  175. EOF
  176. mkdir "${WORKING_DIR}/mnt/data/"
  177.  
  178. echo "MyBookWorld" > "${WORKING_DIR}/etc/hostname"
  179.  
  180. cat > "${WORKING_DIR}"/etc/rc.power_button_short_press << 'EOF'
  181. #! /bin/bash
  182. # This script is executed when the power button is held for less than 3 seconds
  183.  
  184. echo 15 > /sys/class/leds/oxnas-wd810-leds\:st/brightness
  185. halt
  186. EOF
  187. chmod +x "${WORKING_DIR}"/etc/rc.power_button_short_press
  188.  
  189. cat > "${WORKING_DIR}"/etc/rc.power_button_long_press << 'EOF'
  190. #! /bin/bash
  191. # This script is executed when the power button is held for 3 seconds or more
  192.  
  193. echo 15 > /sys/class/leds/oxnas-wd810-leds\:st/brightness
  194. halt
  195. EOF
  196. chmod +x "${WORKING_DIR}"/etc/rc.power_button_long_press
  197.  
  198. cat > "${WORKING_DIR}"/etc/rc.recovery_button << 'EOF'
  199. #! /bin/bash
  200. # This script is executed when the recovery button is held for 3 seconds or more
  201.  
  202. # enter upgrade mode, this will replace the root filesystem with the contents of {md3}/upgrade/rootfs.arm.ext2
  203. echo -n 1 | dd of=/dev/sda bs=512 seek=290
  204. reboot
  205. EOF
  206. chmod +x "${WORKING_DIR}"/etc/rc.recovery_button
  207.  
  208. echo running apt-get update...
  209. chroot "$WORKING_DIR" apt-get update
  210.  
  211. # in case you want to distribute the generated filesystem ;p
  212. echo > "${WORKING_DIR}"/etc/resolv.conf
  213.  
  214. # the working dir now contains the finished debian system
  215. # to install it we'll use the "firmware upgrade" feature of u-boot
  216. # this loads an upgrade kernel and initrd from the start of the harddrive, which then perform the upgrade
  217. # we could provide our own but instead we'll just use the ones WD have already put there
  218. # WD's initrd expects a filesystem image at {md3}/upgrade/rootfs.arm.ext2
  219. # it will then call mkfs.ext3 on /dev/md0 and copy the contents of the image there
  220.  
  221. # create the image
  222. echo "Preparing for upgrade mode..."
  223. dd if=/dev/zero of=/var/upgrade/rootfs.arm.ext2 bs=1M count=200
  224. yes | mkfs.ext2 /var/upgrade/rootfs.arm.ext2
  225. mkdir /mnt/rootfs.arm.ext2
  226. mount -o loop /var/upgrade/rootfs.arm.ext2 /mnt/rootfs.arm.ext2
  227. echo "Copying filesystem..."
  228. cp -a "${WORKING_DIR}"/* /mnt/rootfs.arm.ext2/ 2>/dev/null
  229. umount /mnt/rootfs.arm.ext2
  230. rmdir /mnt/rootfs.arm.ext2
  231. rm -r "${WORKING_DIR}"
  232.  
  233. # WD's initrd also upgrades the kernel using {md3}/upgrade/uImage
  234. # We presumably don't want this to happen
  235. [ -f /var/upgrade/uImage ] && mv /var/upgrade/uImage /var/upgrade/uImage.moved
  236.  
  237. # set the upgrade flag
  238. echo -n 1 | dd of=/dev/sda bs=512 seek=290
  239.  
  240. # reboot
  241. echo
  242. echo "Finished! :)"
  243. echo "Debian will install on next reboot"
  244. echo "Press any key to reboot now"
  245. read REBOOT
  246. reboot
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement