Advertisement
Transfusion

xiaomi mi2s system2 partition ubuntu touch initrd modify

Jul 24th, 2014
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.62 KB | None | 0 0
  1. #transfusian@gmail.com
  2. # Local filesystem mounting -*- shell-script -*-
  3.  
  4. pre_mountroot()
  5. {
  6. [ "$quiet" != "y" ] && log_begin_msg "Running /scripts/local-top"
  7. run_scripts /scripts/local-top
  8. [ "$quiet" != "y" ] && log_end_msg
  9. }
  10.  
  11. mount_ubuntu_overlay() {
  12. source=$1
  13. target=$2
  14.  
  15. if [ -d ${source} ]; then
  16. OLD_PWD=$PWD
  17. cd ${source}
  18.  
  19. for overlay in `find . -type f`; do
  20. [ -f ${target}/${overlay} ] && mount --bind ${source}/${overlay} ${target}/${overlay}
  21. done
  22.  
  23. cd $OLD_PWD
  24. fi
  25. }
  26.  
  27. sync_dirs() {
  28. base=$1
  29. source=$2
  30. target=$3
  31.  
  32. OLD_PWD=$PWD
  33. cd $base
  34.  
  35. for file in $source/*
  36. do
  37. # Skip empty directories
  38. [ ! -e "$base/$file" ] && continue
  39.  
  40. # If the target already exists as a file or link, there's nothing we can do
  41. [ -e "$target/$file" -o -L "$target/$file" ] && [ ! -d "$target/$file" ] && continue
  42.  
  43. # If the target doesn't exist, just copy it over
  44. if [ ! -e "$target/$file" -a ! -L "$target/$file" ]; then
  45. cp -Ra "$base/$file" "$target/$file"
  46. continue
  47. fi
  48.  
  49. # That leaves us with directories and a recursive call
  50. [ -d $file ] && sync_dirs $base $file $target
  51. done
  52.  
  53. cd $OLD_PWD
  54. }
  55.  
  56. mountroot()
  57. {
  58. # list of possible userdata partition names
  59. partlist="userdata UDA DATAFS USERDATA"
  60.  
  61. pre_mountroot
  62.  
  63. [ "$quiet" != "y" ] && log_begin_msg "Running /scripts/local-premount"
  64. run_scripts /scripts/local-premount
  65. [ "$quiet" != "y" ] && log_end_msg
  66.  
  67. # Mount root
  68. #
  69. # Create a temporary mountpoint for the bindmount
  70. # Create a temporary mountpoint for the data partition which includes
  71. # data and data1
  72. mkdir -p /data_root
  73. mkdir -p /tmpmnt
  74.  
  75. # Make sure the device has been created by udev before we try to mount
  76. udevadm settle
  77.  
  78. # find the right partition
  79. for partname in $partlist; do
  80. part=$(find /dev -name $partname|tail -1)
  81. [ -z "$part" ] && continue
  82. path=$(readlink -f $part)
  83. [ -n "$path" ] && break
  84. done
  85.  
  86. # override with a possible cmdline parameter
  87. if grep -q datapart= /proc/cmdline; then
  88. for x in $(cat /proc/cmdline); do
  89. case ${x} in
  90. datapart=*)
  91. path=${x#*=}
  92. ;;
  93. esac
  94. done
  95. fi
  96.  
  97. if [ -z "$path" ]; then
  98. echo "initrd: Couldn't find data partition. Spawning adbd ..." >/dev/kmsg || true
  99. panic "Couldn't find data partition. Spawning adbd ..."
  100. fi
  101. echo "initrd: mounting $path" >/dev/kmsg || true
  102.  
  103. # Mount the data partition to a temporary mount point
  104. mount -o discard $path /data_root
  105. mount -o bind /data_root/system1 /tmpmnt
  106.  
  107. # Loop-mounted flipped model
  108. if [ -e /tmpmnt/system.img ]; then
  109. # Transition .developer_mode to .writable_image
  110. [ -e /tmpmnt/.developer_mode ] && mv /tmpmnt/.developer_mode /tmpmnt/.writable_image
  111.  
  112. # Prepare the root filesystem
  113. # NOTE: We mount it read-write in all cases, then remount read-only.
  114. # This is to workaround a behaviour change in busybox which now
  115. # uses read-only loops if the fs is initially mounted read-only.
  116. # An alternative implementation would be to add losetup support
  117. # to busybox and do the mount in two steps (rw loop, ro fs).
  118. mount -o loop,rw /tmpmnt/system.img ${rootmnt}
  119. if [ -e /tmpmnt/.writable_image ]; then
  120. echo "initrd: mounting system.img (image developer mode)" >/dev/kmsg || true
  121. mountroot_status="$?"
  122. else
  123. echo "initrd: mounting system.img (user mode)" >/dev/kmsg || true
  124. mount -o remount,ro ${rootmnt}
  125. mountroot_status="$?"
  126. fi
  127. mount --move /tmpmnt ${rootmnt}/userdata
  128.  
  129. # Mount the android system partition to a temporary location
  130. mkdir -p /android-system
  131. mount -o loop,ro ${rootmnt}/var/lib/lxc/android/system.img /android-system
  132.  
  133. # Get device information
  134. device=$(grep ^ro.product.device= /android-system/build.prop |sed -e 's/.*=//')
  135. [ -z "$device" ] && device="unknown"
  136. echo "initrd: device is $device" >/dev/kmsg || true
  137.  
  138. # Mount some tmpfs
  139. mkdir -p ${rootmnt}/android
  140. mount -o rw,size=4096 -t tmpfs none ${rootmnt}/android
  141. mount -o rw,nosuid,noexec,relatime,mode=755 -t tmpfs tmpfs ${rootmnt}/run
  142.  
  143. # Create some needed paths on tmpfs
  144. mkdir -p ${rootmnt}/android/data ${rootmnt}/android/system
  145.  
  146. # Prepare the fstab
  147. FSTAB=${rootmnt}/etc/fstab
  148. touch ${rootmnt}/run/image.fstab
  149. mount -o bind ${rootmnt}/run/image.fstab $FSTAB || panic "drop to adb"
  150. echo "/dev/root / rootfs defaults,ro 0 0" >> $FSTAB
  151.  
  152. # Process the list of bind-mounts
  153. # (but don't mount them, mountall will do it)
  154. cat ${rootmnt}/etc/system-image/writable-paths | while read line; do
  155. set -- $line
  156. # Skip invalid/commented entries
  157. ([ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ] || [ -z "$4" ] || [ -z "$5" ]) && continue
  158. [ "$1" = "#" ] && continue
  159.  
  160. # Skip invalid mount points
  161. dstpath="${rootmnt}/$1"
  162. [ ! -e "$dstpath" ] && continue
  163.  
  164. if [ "$3" = "temporary" ]; then
  165. # Temporary entries are simple, just mount a tmpfs
  166. echo "tmpfs $1 tmpfs $5 0 0" >> $FSTAB
  167. elif [ "$3" = "persistent" ] || [ "$3" = "synced" ]; then
  168. # Figure out the source path
  169. if [ "$2" = "auto" ]; then
  170. srcpath="${rootmnt}/userdata/system-data/$1"
  171. path="/userdata/system-data/$1"
  172. else
  173. srcpath="${rootmnt}/userdata/$2"
  174. path="/userdata/$2"
  175. fi
  176.  
  177. if [ ! -e "$srcpath" ]; then
  178. # Process new persistent or synced paths
  179. dstown=$(stat -c "%u:%g" $dstpath)
  180. dstmode=$(stat -c "%a" $dstpath)
  181. mkdir -p ${srcpath%/*}
  182. if [ ! -d "$dstpath" ]; then
  183. # Deal with redirected files
  184. if [ "$4" = "transition" ]; then
  185. cp -a $dstpath $srcpath
  186. else
  187. touch $srcpath
  188. chown $dstown $srcpath
  189. chmod $dstmode $srcpath
  190. fi
  191. else
  192. # Deal with redirected directories
  193. if [ "$4" = "transition" ] || [ "$3" = "synced" ]; then
  194. cp -aR $dstpath $srcpath
  195. else
  196. mkdir $srcpath
  197. chown $dstown $srcpath
  198. chmod $dstmode $srcpath
  199. fi
  200. fi
  201. elif [ "$3" = "synced" ]; then
  202. # Process existing synced paths
  203. sync_dirs $dstpath . $srcpath
  204. fi
  205.  
  206. # Write the fstab entry
  207. if [ "$5" = "none" ]; then
  208. echo "$path $1 none bind 0 0" >> $FSTAB
  209. else
  210. echo "$path $1 none bind,$5 0 0" >> $FSTAB
  211. fi
  212. else
  213. continue
  214. fi
  215. done
  216.  
  217. # Extract the fstab from the android initrd
  218. # NOTE: we should find a faster way of doing that or cache it
  219. OLD_CWD=$(pwd)
  220. mount -n -t tmpfs tmpfs ${rootmnt}/var/lib/lxc/android/rootfs
  221. cd ${rootmnt}/var/lib/lxc/android/rootfs
  222. cat /android-system/boot/android-ramdisk.img | gzip -d | cpio -i
  223. cd $OLD_CWD
  224.  
  225. # Mount all the Android partitions
  226. cat ${rootmnt}/var/lib/lxc/android/rootfs/fstab* | while read line; do
  227. set -- $line
  228.  
  229. # Skip any unwanted entry
  230. echo $1 | egrep -q "^#" && continue
  231. ([ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ] || [ -z "$4" ]) && continue
  232. ([ "$2" = "/system" ] || [ "$2" = "/data" ]) && continue
  233.  
  234. label=$(echo $1 | awk -F/ '{print $NF}')
  235. [ -z "$label" ] && continue
  236.  
  237. echo "initrd: checking mount label $label" >/dev/kmsg || true
  238.  
  239. for dir in by-partlabel by-name by-label by-path by-uuid by-partuuid by-id; do
  240. path="/dev/disk/$dir/$label"
  241. [ -e "$path" ] && break
  242. done
  243.  
  244. # MTD based devices, such as the emulator
  245. if [ ! -e "$path" ] && echo $label | egrep -q "^mtdblock"; then
  246. path="/dev/$label"
  247. fi
  248.  
  249. [ ! -e "$path" ] && continue
  250.  
  251. mkdir -p ${rootmnt}/android/$2
  252. echo "initrd: mounting $path as ${rootmnt}/android/$2" >/dev/kmsg || true
  253. mount $path ${rootmnt}/android/$2 -t $3 -o $4
  254. done
  255.  
  256. # system is a special case
  257. echo "initrd: mounting ${rootmnt}/var/lib/lxc/android/system.img as ${rootmnt}/android/system" >/dev/kmsg || true
  258. mount --move /android-system ${rootmnt}/android/system
  259.  
  260. # Ubuntu overlay available in the Android system image (hardware specific configs)
  261. mount_ubuntu_overlay ${rootmnt}/android/system/ubuntu ${rootmnt}
  262.  
  263. # Apply device-specific udev rules
  264. if [ ! -f ${rootmnt}/android/system/ubuntu/lib/udev/rules.d/70-android.rules ] && [ "$device" != "unknown" ]; then
  265. mount --bind ${rootmnt}/usr/lib/lxc-android-config/70-$device.rules ${rootmnt}/lib/udev/rules.d/70-android.rules
  266. fi
  267.  
  268. # Bind-mount /lib/modules from Android
  269. [ -e ${rootmnt}/android/system/lib/modules ] && mount --bind ${rootmnt}/android/system/lib/modules ${rootmnt}/lib/modules
  270.  
  271. # Bind-mount /var/lib/ureadahead if available on persistent storage
  272. # this is required because ureadahead runs before mountall
  273. if [ -e ${rootmnt}/userdata/system-data/var/lib/ureadahead ] && \
  274. [ -e ${rootmnt}/var/lib/ureadahead ]; then
  275. mount --bind ${rootmnt}/userdata/system-data/var/lib/ureadahead ${rootmnt}/var/lib/ureadahead
  276. fi
  277.  
  278. # Setup the swap device
  279. [ -e ${rootmnt}/userdata/SWAP.img ] && swapon ${rootmnt}/userdata/SWAP.img
  280.  
  281. # Apply customized content
  282. for user in ${rootmnt}/userdata/user-data/*
  283. do
  284. if [ -d ${rootmnt}/custom/home ] && [ ! -e "$user/.customized" ]; then
  285. echo "initrd: copying custom content tp " >/dev/kmsg || true
  286. cp -Rap ${rootmnt}/custom/home/* "$user/"
  287. cp -Rap ${rootmnt}/custom/home/.[a-zA-Z0-9]* "$user/"
  288. touch "$user/.customized"
  289. dstown=$(stat -c "%u:%g" "$user")
  290. chown -R $dstown "$user/"
  291. fi
  292. done
  293.  
  294.  
  295. # Old flipped model
  296. elif [ -d /tmpmnt/ubuntu ]; then
  297. mount --bind /tmpmnt/ubuntu ${rootmnt}
  298. mountroot_status="$?"
  299.  
  300. # Possibly a re-partitioned device
  301. else
  302. echo "initrd: Couldn't find a system partition." >/dev/kmsg || true
  303. panic "Couldn't find a system partition. Spawning adbd ..."
  304. fi
  305.  
  306. [ "$quiet" != "y" ] && log_begin_msg "Running /scripts/local-bottom"
  307. run_scripts /scripts/local-bottom
  308. [ "$quiet" != "y" ] && log_end_msg
  309. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement