Advertisement
Transfusion

xiaomi mi2s system2 partition ubuntu touch initrd modify

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