Advertisement
mattrix

Untitled

Apr 6th, 2018
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 11.00 KB | None | 0 0
  1. #!/bin/busybox sh
  2.  
  3. # Default PATH differs between shells, and is not automatically exported
  4. # by klibc dash.  Make it consistent.
  5. export PATH=/sbin:/usr/sbin:/bin:/usr/bin
  6.  
  7. [ -d /proc ] || mkdir /proc
  8. mount -t proc none /proc
  9. [ -d /sys ] || mkdir /sys
  10. mount -t sysfs none /sys
  11.  
  12. mknod /dev/null c 1 3
  13. mknod /dev/tty c 5 0
  14. [ -e /dev/console ] || mknod /dev/console c 5 1
  15.  
  16. mdev -s
  17.  
  18. #Defaults which may be overridden by cmdline parameters
  19. USE_KMSG="yes"
  20. HWDEVICE="empty"
  21.  
  22. # Display a message or print directly to /dev/kmsg
  23. print_msg() {
  24. if [ $USE_KMSG == yes ]; then
  25.   echo "initramfs:" $1 >> /dev/kmsg
  26. else
  27.   echo "initramfs:" $1
  28. fi
  29. }
  30.  
  31. # Parse the kernel command line
  32.  
  33. CMDLINE="$(cat /proc/cmdline)"
  34.  
  35. parse_disk() {
  36.   if [ "$(echo $1|cut -c -5)" = "UUID=" ]; then
  37.     # $1 is a UUID
  38.     echo $(findfs $1)
  39.   elif [ "$(echo $1|cut -c -6)" = "LABEL=" ]; then
  40.     # $1 is a LABEL
  41.     echo $(findfs $1)
  42.   elif [ "$(echo $1|cut -c -5)" = "/dev/" ]; then
  43.     # $1 is a device name
  44.     echo $1
  45.   else
  46.     # $1 is unrecognized.
  47.     echo "unknown-disk"
  48.   fi
  49. }
  50.  
  51. for p in ${CMDLINE};
  52. do
  53.   key=${p%%=*}
  54.   value=${p#*=}
  55.   case $key in
  56.   bootpart)
  57.     BOOTPART=`parse_disk $value`
  58.     ;;
  59.   imgpart) #required
  60.     IMGPART=`parse_disk $value`
  61.     ;;
  62.   datapart)
  63.     DATAPART=`parse_disk $value`
  64.     ;;
  65.   bootdev) #legacy
  66.     BOOTPART=/dev/$value
  67.     ;;
  68.   imgfile) #required
  69.     IMGFILE=$value
  70.     ;;
  71.   bootdelay)
  72.     BOOTDELAY=$value
  73.     ;;
  74.   use_kmsg)
  75.     USE_KMSG=$value
  76.     ;;
  77.   hwdevice)
  78.     HWDEVICE=$value
  79.     ;;
  80.   esac
  81. done
  82.  
  83. #Hardware specific adaptions
  84.  
  85. #When we did not already get the device name from the cmdline, try getting it from cpuinfo
  86. if [ $HWDEVICE == empty ]; then
  87.   HWDEVICE="$(cat /proc/cpuinfo | grep Hardware | awk '{print $3}' )"
  88. fi
  89.  
  90. if [ $HWDEVICE == ODROID-C2 ]; then
  91.    exec >/dev/kmsg 2>&1 </dev/console
  92. fi
  93.  
  94. # Odroid C1, SparkySBC, Banana PI, Pine64 and Cubox have overlayfs version < V22
  95. if [ $HWDEVICE == ODROIDC ] || [ $HWDEVICE == gs705a ] || [ $HWDEVICE == sun50iw1p1 ] || [ $HWDEVICE == sun8iw11p1 ] || [ $HWDEVICE == Freescale ]; then
  96.    OVERLAY=NOWRKDIR
  97.    exec >/dev/kmsg 2>&1 </dev/console
  98. else
  99.    OVERLAY=WITHWRKDIR
  100. fi
  101.  
  102. print_msg "Booting Volumio for ${HWDEVICE}"
  103. print_msg " This script mounts rootfs RO with an overlay RW layer."
  104. if [ $OVERLAY == WITHWRKDIR ]; then
  105.    # For overlayfs version V22 or higher (modulename 'overlay')
  106.    modprobe overlay
  107. else
  108.    # For overlayfs version V20/V21 (modulename ='overlayfs')
  109.    modprobe overlayfs
  110. fi
  111. modprobe squashfs
  112. modprobe nls_cp437
  113.  
  114. if [ -z "${IMGPART}" ]; then
  115.   print_msg "Specify the squash image partition after the kernel command ${CMDLINE}"
  116.   print_msg "example: kernel... imgpart=/dev/sda2 imgfile=/gentoo.sqs"
  117.   exec sh
  118.   exit 0
  119. fi
  120.  
  121. if [ -z "${IMGFILE}" ]; then
  122.   print_msg "Specify the squash image file after the kernel command ${CMDLINE}"
  123.   print_msg "example: kernel... imgpart=/dev/sda2 imgfile=/gentoo.sqs"
  124.   exec sh
  125.   exit 0
  126. fi
  127.  
  128. # Setup default partitions based off imgpart partition
  129. # Tested with /dev/sda2 /dev/sdp2 /dev/mmcblk0p2
  130. img_device=$(echo ${IMGPART} | sed -e 's/[0-9]*$//')
  131. img_partnum=$(echo ${IMGPART} | grep -o '[0-9]\+$')
  132.  
  133. if [ -z "${BOOTPART}" ]; then
  134.   BOOTPART=${img_device}$((img_partnum - 1))
  135. fi
  136.  
  137. if [ -z "${DATAPART}" ]; then
  138.   DATAPART=${img_device}$((img_partnum + 1))
  139. fi
  140.  
  141. print_msg BOOTPART=${BOOTPART}
  142. print_msg IMGPART=${IMGPART}
  143. print_msg DATAPART=${DATAPART}
  144. print_msg IMGFILE=${IMGFILE}
  145.  
  146. if [ ! -z "${BOOTDELAY}" ]; then
  147.   print_msg "Boot delay (except first time) will be ${BOOTDELAY} seconds"
  148. fi
  149.  
  150. # Retry mdev -s 3 times before throwing the towel
  151. for i in 1 2 3 4 5 6
  152.   do
  153.     if [ ! -b "${IMGPART}" ]; then
  154.       print_msg  "${IMGPART} not detected,retrying mdev in 5 seconds"
  155.       sleep 0.5
  156.       mdev -s
  157.     else
  158.       print_msg `blkid ${IMGPART}`
  159.       break
  160.     fi
  161.   done
  162.  
  163. if [ ! -b "${IMGPART}" ]; then
  164.   print_msg "No partition with ${IMGPART} has been found"
  165.   exec sh
  166.   exit 0
  167. fi
  168.  
  169. # ok, parsing done
  170. [ -d /mnt ] || mkdir /mnt
  171. # Mount the partitions
  172. # 1) mount the partition where the squash image resides
  173. [ -d /mnt/imgpart ] || mkdir /mnt/imgpart
  174. mount -t ext4 ${IMGPART} /mnt/imgpart
  175.  
  176. #create recovery image when not yet present
  177. #when already present and a bootdelay parameter was specified then give the kernel the additional headstart
  178. #
  179. if [ ! -e "/mnt/imgpart/volumio_factory.sqsh" ]; then
  180.   print_msg "Creating factory image, this will take a minute, please wait..."
  181.   cp /mnt/imgpart/volumio_current.sqsh /mnt/imgpart/volumio_factory.sqsh
  182.   print_msg "Factory image created"
  183.   print_msg "Creating archive for factory kernel..."
  184.   mkdir /mnt/factory
  185.   mount -t vfat ${BOOTPART} /mnt/factory
  186.   tar cf /mnt/imgpart/kernel_current.tar -C /mnt/factory .
  187.   cp /mnt/imgpart/kernel_current.tar /mnt/imgpart/kernel_factory.tar
  188.   umount /mnt/factory
  189.   rm -r /mnt/factory
  190. elif [ ! -z "${BOOTDELAY}" ]; then
  191.   print_msg "Doing a ${BOOTDELAY} second delay here to give kernel load a headstart"
  192.   sleep ${BOOTDELAY}
  193. fi
  194.  
  195. # Loop usb devices here instead?
  196. print_msg "Checking for USB updates"
  197. [ -e /dev/sda1 ] || mdev -s
  198. if [ -e /dev/sda1 ]; then
  199.   [ -d /mnt/usb ] || mkdir /mnt/usb
  200.   mount -t auto /dev/sda1 /mnt/usb
  201.   #If there is a firmware file inside the usb
  202.   if [ -e /mnt/usb/*.fir ]; then
  203.     print_msg "Firmware found, updating will take a few minutes, please wait..."
  204.     mkdir /mnt/boot
  205.     mount -t auto ${BOOTPART} /mnt/boot
  206.     #when the partitions are mounted we can launch the update script
  207.     volumio-init-updater
  208.     sync
  209.     print_msg "USB Update applied"
  210.     umount /mnt/boot
  211.     rm -r /mnt/boot
  212.     print_msg "Restarting"
  213.     echo b > /proc/sysrq-trigger
  214.   fi
  215.   if [ -e /mnt/usb/factory_reset ]; then
  216.     print_msg "Factory Reset on USB"
  217.     mkdir /mnt/factory
  218.     mount -t auto ${BOOTPART} /mnt/factory
  219.     echo " " > /mnt/factory/factory_reset
  220.     umount /mnt/factory
  221.     rm -r /mnt/factory
  222.     rm /mnt/usb/factory_reset
  223.   fi
  224.   umount /dev/sda1
  225.   rm -r /mnt/usb
  226. else
  227.   print_msg "No USB device detected (when incorrect, try adding 'bootdelay=5' to your boot cmdline)"
  228. fi
  229.  
  230.  
  231. # 2) init a loop pointing to the image file
  232. loop_free=$(losetup -f | sed s#p/#p#)
  233. if [ ! -e ${loop_free} ]; then
  234.   print_msg "Device node does not exist, creating it..."
  235.   # use last char from loop_device as minor device number
  236.   minor=$(echo ${loop_free} | sed 's/.*\(.\)/\1/')
  237.   mknod $loop_free b 7 $minor
  238. fi
  239. losetup $loop_free /mnt/imgpart/${IMGFILE}
  240.  
  241. # 3) mount the squashfs to /mnt/static
  242. [ -d /mnt/static ] || mkdir /mnt/static
  243. mount -t squashfs $loop_free /mnt/static
  244.  
  245. VOLUMIO_VERSION="$(cat /mnt/static/etc/os-release | grep VOLUMIO_VERSION)"
  246.  
  247. #if there is factory file then format data partition
  248. #
  249. mkdir /mnt/factory
  250. mount -t auto ${BOOTPART} /mnt/factory
  251. if [ -e "/mnt/factory/factory_reset" ]; then
  252.   print_msg "Executing factory reset"
  253.   mkfs.ext4 -F -E stride=2,stripe-width=1024 -b 4096 ${DATAPART} -L volumio_data
  254.   print_msg "Factory reset executed: part I - User DATA Part"
  255.   tar xf /mnt/imgpart/kernel_factory.tar -C /mnt/factory
  256.   print_msg "Factory reset executed: part II - Kernel"
  257.   cp  /mnt/imgpart/volumio_factory.sqsh /mnt/imgpart/volumio_current.sqsh
  258.   print_msg "Factory reset executed: part III - Squash"
  259.   print_msg "Factory reset successfully executed"
  260.   sync
  261.   rm /mnt/factory/factory_reset
  262.  
  263.   umount /mnt/factory
  264.   rm -r /mnt/factory
  265.   print_msg "Restarting"
  266.   echo b > /proc/sysrq-trigger
  267. fi
  268. if [ -e "/mnt/factory/user_data" ]; then
  269.   print_msg "Deleting User Data"
  270.   mkfs.ext4 -F -E stride=2,stripe-width=1024 -b 4096 ${DATAPART} -L volumio_data
  271.   rm /mnt/factory/user_data
  272.   print_msg "User Data successfully deleted "
  273. fi
  274. umount /mnt/factory
  275. rm -r /mnt/factory
  276.  
  277.  
  278. # if the update failed before completion
  279. mkdir boot
  280. mount -t vfat ${BOOTPART} /boot
  281. if [ -e "/boot/update_process" ]; then
  282. print_msg "Previous update attempt failed, restoring fallbacks"
  283.   cp /mnt/imgpart/kernel_fallback.tar /mnt/imgpart/kernel_current.tar
  284.   cp /mnt/imgpart/volumio_fallback.tar /mnt/imgpart/volumio_current.tar
  285.   if [-e "/boot/kernel_update" ]; then
  286.     rm /boot/kernel_update
  287.   fi
  288.   rm /boot/update_process
  289. fi
  290.  
  291. # if the kernel has been updated, and no error has occurred before completition
  292. if [ -e "/boot/kernel_update" ]; then
  293. print_msg "unpacking kernel"
  294.   cmdline="$(cat /boot/cmdline.txt)"
  295.   tar xf /mnt/imgpart/kernel_current.tar -C /boot
  296.   print_msg "Restoring cmdline.txt"
  297.   echo "$cmdline" > /boot/cmdline.txt
  298.   if [ -e "/mnt/imgpart/config.txt.bak" ]; then
  299.     print_msg "Restoring custom config.txt content"
  300.     I2S=`sed -n -e '/#### Volumio i2s setting below: do not alter ####/,$p' /mnt/imgpart/config.txt.bak`
  301.     echo "" >> /boot/config.txt
  302.     echo "$I2S" >> /boot/config.txt
  303.     rm /mnt/imgpart/config.txt.bak
  304.   fi
  305.   rm /boot/kernel_update
  306.   sync
  307.   umount /boot
  308.   rm -rf /boot
  309.   echo b > /proc/sysrq-trigger
  310. fi
  311.  
  312. # if data partition needs to be resized
  313. # Tested with /dev/sda2 /dev/sdp2 /dev/mmcblk0p2
  314. #mount -t auto ${BOOTPART} /boot
  315. if [ -e "/boot/resize-volumio-datapart" ]; then
  316.   print_msg "Re-sizing Volumio data partition"
  317.  
  318.   data_device=$(echo ${DATAPART} | sed -e 's/[0-9]*$//')
  319.   if [[ "${data_device}" = *[0-9]p ]]; then
  320.     data_device="${data_device%p}"
  321.   fi
  322.   data_partnum=$(echo ${DATAPART} | grep -o '[0-9]\+$')
  323.  
  324.   maxsize="$(parted -s ${data_device} unit MB print list | grep "Disk ${data_device}" | cut -d' ' -f3 | tr -d MB)"
  325.   parted -s ${data_device} resizepart ${data_partnum} ${maxsize}
  326.   e2fsck -fy ${DATAPART}
  327.   resize2fs ${DATAPART}
  328.   print_msg "Volumio data partition succesfully resized"
  329.   parted -s ${data_device} unit MB print
  330.  
  331.   rm /boot/resize-volumio-datapart
  332. fi
  333.  
  334. # clear the mountpoint
  335. umount /boot
  336. rm -rf /boot
  337.  
  338. # 4) mount a filesystem for write access to the static image
  339. # unclear: memory size? -o size=1024M
  340. [ -d /mnt/ext ] || mkdir -m 777 /mnt/ext
  341. mount -t ext4 -o noatime ${DATAPART} /mnt/ext
  342.  
  343. [ -d /mnt/ext/dyn ] || mkdir -m 777 /mnt/ext/dyn
  344. [ -d /mnt/ext/union ] || mkdir -m 777 /mnt/ext/union
  345.  
  346. # 5) mount the writable overlay to the static image
  347. if [ $OVERLAY == WITHWRKDIR ]; then
  348.   [ -d /mnt/ext/work ] || mkdir -m 777 /mnt/ext/work
  349.   print_msg "With Option:" $OVERLAY
  350.   mount -t overlay -olowerdir=/mnt/static,upperdir=/mnt/ext/dyn,workdir=/mnt/ext/work overlay /mnt/ext/union
  351. else
  352.   print_msg "Without Option:" $OVERLAY
  353.   mount -t overlayfs overlayfs /mnt/ext/union -olowerdir=/mnt/static,upperdir=/mnt/ext/dyn
  354. fi
  355.  
  356. [ -d /mnt/ext/union/static ] || mkdir -m 777 /mnt/ext/union/static
  357. [ -d /mnt/ext/union/imgpart ] || mkdir -m 777 /mnt/ext/union/imgpart
  358. mount --move /mnt/static /mnt/ext/union/static
  359. mount --move /mnt/imgpart /mnt/ext/union/imgpart
  360.  
  361. # Make sure our fstab is correct
  362. sed /mnt/ext/union/etc/fstab -i -e "s|^.* /boot |${BOOTPART}  /boot |"
  363.  
  364. chmod -R 777 /mnt/ext/union/imgpart
  365.  
  366. umount /proc
  367. umount /sys
  368.  
  369. print_msg ${VOLUMIO_VERSION}
  370. print_msg "Finish initramfs, continue booting Volumio"
  371. exec switch_root /mnt/ext/union /sbin/init
  372.  
  373. print_msg "Failed to switch_root, dropping to a shell"
  374. exec sh
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement