chromeos_startup.sh 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #!/bin/bash
  2. rm -f /fakemurk_startup_log
  3. rm -r /fakemurk_startup_err
  4. rm -f /fakemurk-log
  5. touch /startup_log
  6. chmod 775 /startup_log
  7. exec 3>&1 1>>/startup_log 2>&1
  8. run_plugin() {
  9. bash "$1"
  10. }
  11. runjob() {
  12. clear
  13. trap 'kill -2 $! >/dev/null 2>&1' INT
  14. (
  15. # shellcheck disable=SC2068
  16. $@
  17. )
  18. trap '' INT
  19. clear
  20. }
  21. . /usr/share/misc/chromeos-common.sh
  22. get_largest_cros_blockdev() {
  23. local largest size dev_name tmp_size remo
  24. size=0
  25. for blockdev in /sys/block/*; do
  26. dev_name="${blockdev##*/}"
  27. echo "$dev_name" | grep -q '^\(loop\|ram\)' && continue
  28. tmp_size=$(cat "$blockdev"/size)
  29. remo=$(cat "$blockdev"/removable)
  30. if [ "$tmp_size" -gt "$size" ] && [ "${remo:-0}" -eq 0 ]; then
  31. case "$(sfdisk -l -o name "/dev/$dev_name" 2>/dev/null)" in
  32. *STATE*KERN-A*ROOT-A*KERN-B*ROOT-B*)
  33. largest="/dev/$dev_name"
  34. size="$tmp_size"
  35. ;;
  36. esac
  37. fi
  38. done
  39. echo "$largest"
  40. }
  41. DST=$(get_largest_cros_blockdev)
  42. if [ -z $DST ]; then
  43. DST=/dev/mmcblk0
  44. fi
  45. # funny boot messages
  46. # multi-liners
  47. cat <<EOF >/usr/share/chromeos-assets/text/boot_messages/en/block_devmode_virtual.txt
  48. Oh fuck - ChromeOS is trying to kill itself.
  49. ChromeOS detected developer mode and is trying to disable it to
  50. comply with FWMP. This is most likely a bug and should be reported to
  51. the murkmod GitHub Issues page.
  52. EOF
  53. cat <<EOF >/usr/share/chromeos-assets/text/boot_messages/en/self_repair.txt
  54. oops UwU i did a little fucky wucky and your system is trying to
  55. repair itself~ sorry OwO
  56. EOF
  57. # single-liners
  58. echo "i sure hope you did that on purpose (powerwashing system)" >/usr/share/chromeos-assets/text/boot_messages/en/power_wash.txt
  59. crossystem.old block_devmode=0 # prevent chromeos from comitting suicide
  60. # we stage sshd and mkfs as a one time operation in startup instead of in the bootstrap script
  61. # this is because ssh-keygen was introduced somewhere around R80, where many shims are still stuck on R73
  62. # filesystem unfuck can only be done before stateful is mounted, which is perfectly fine in a shim but not if you run it while booted
  63. # because mkfs is mean and refuses to let us format
  64. # note that this will lead to confusing behaviour, since it will appear as if it crashed as a result of fakemurk
  65. if [ ! -f /sshd_staged ]; then
  66. # thanks rory! <3
  67. echo "Staging sshd..."
  68. mkdir -p /ssh/root
  69. chmod -R 777 /ssh/root
  70. echo "Generating ssh keypair..."
  71. ssh-keygen -f /ssh/root/key -N '' -t rsa >/dev/null
  72. cp /ssh/root/key /rootkey
  73. chmod 600 /ssh/root
  74. chmod 644 /rootkey
  75. echo "Creating config..."
  76. cat >/ssh/config <<-EOF
  77. AuthorizedKeysFile /ssh/%u/key.pub
  78. StrictModes no
  79. HostKey /ssh/root/key
  80. Port 1337
  81. EOF
  82. touch /sshd_staged
  83. echo "Staged sshd."
  84. fi
  85. if [ -f /population_required ]; then
  86. echo "Populating crossystem..."
  87. /sbin/crossystem_boot_populator.sh
  88. echo "Done. Setting check_enrollment..."
  89. vpd -i RW_VPD -s check_enrollment=1
  90. echo "Removing flag..."
  91. rm -f /population_required
  92. fi
  93. echo "Launching sshd..."
  94. /usr/sbin/sshd -f /ssh/config &
  95. if [ -f /logkeys/active ]; then
  96. echo "Found logkeys flag, launching..."
  97. /usr/bin/logkeys -s -m /logkeys/keymap.map -o /mnt/stateful_partition/keylog
  98. fi
  99. if [ ! -f /stateful_unfucked ]; then
  100. echo "Unfucking stateful..."
  101. yes | mkfs.ext4 "${DST}p1"
  102. touch /stateful_unfucked
  103. echo "Done, rebooting..."
  104. reboot
  105. else
  106. echo "Stateful already unfucked, doing temp stateful mount..."
  107. stateful_dev=${DST}p1
  108. first_mount_dir=$(mktemp -d)
  109. mount "$stateful_dev" "$first_mount_dir"
  110. echo "Mounted stateful on $first_mount_dir, looking for startup plugins..."
  111. plugin_dir="$first_mount_dir/murkmod/plugins"
  112. temp_dir=$(mktemp -d)
  113. cp -r "$plugin_dir"/* "$temp_dir"
  114. echo "Copied files to $temp_dir, unmounting and cleaning up..."
  115. umount "$stateful_dev"
  116. rmdir "$first_mount_dir"
  117. echo "Finding startup plugins..."
  118. for file in "$temp_dir"/*.sh; do
  119. if grep -q "startup_plugin" "$file"; then
  120. echo "Starting plugin $file..."
  121. runjob run_plugin $file
  122. fi
  123. done
  124. echo "Plugins run. Handing over to real startup..."
  125. if [ ! -f /new-startup ]; then
  126. exec /sbin/chromeos_startup.sh.old
  127. else
  128. exec /sbin/chromeos_startup.old
  129. fi
  130. fi