xref: /OK3568_Linux_fs/device/rockchip/common/scripts/post-fstab.sh (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1#!/bin/bash -e
2
3source "${POST_HELPER:-$(dirname "$(realpath "$0")")/../post-hooks/post-helper}"
4
5FSTAB="$TARGET_DIR/etc/fstab"
6
7fixup_root()
8{
9	echo "Fixing up rootfs type: $1"
10
11	FS_TYPE=$1
12	sed -i "s#\([[:space:]]/[[:space:]]\+\)\w\+#\1${FS_TYPE}#" "$FSTAB"
13}
14
15del_part()
16{
17	echo "Deleting partition: $1 $2"
18
19	SRC="$1"
20	MOUNTPOINT="$2"
21
22	# Remove old entries with same mountpoint
23	sed -i "/[[:space:]]${MOUNTPOINT//\//\\\/}[[:space:]]/d" "$FSTAB"
24
25	if [ "$SRC" != tmpfs ]; then
26		# Remove old entries with same source
27		sed -i "/^${SRC//\//\\\/}[[:space:]]/d" "$FSTAB"
28	fi
29}
30
31fixup_part()
32{
33	del_part $@
34
35	echo "Fixing up partition: ${@//:/ }"
36
37	SRC="$1"
38	MOUNTPOINT="$2"
39	FS_TYPE="$3"
40	MOUNT_OPTS="$4"
41	PASS="$5"
42
43	# Append new entry
44	echo -e "$SRC\t$MOUNTPOINT\t$FS_TYPE\t$MOUNT_OPTS\t0 $PASS" >> "$FSTAB"
45
46	mkdir -p "$TARGET_DIR/$MOUNTPOINT"
47}
48
49fixup_basic_part()
50{
51	echo "Fixing up basic partition: $@"
52
53	FS_TYPE="$1"
54	MOUNTPOINT="$2"
55	MOUNT_OPTS="${3:-defaults}"
56
57	fixup_part "$FS_TYPE" "$MOUNTPOINT" "$FS_TYPE" "$MOUNT_OPTS" 0
58}
59
60fixup_device_part()
61{
62	echo "Fixing up device partition: $@"
63
64	DEV="$1"
65
66	# Dev is either <name> or /dev/.../<name> or <UUID|LABEL|PARTLABEL>=xxx
67	[ "$DEV" ] || return 0
68	echo $DEV | grep -qE "^/|=" || DEV="PARTLABEL=$DEV"
69
70	MOUNTPOINT="${2:-/${DEV##*[/=]}}"
71	FS_TYPE="${3:-ext4}"
72	MOUNT_OPTS="${4:-defaults}"
73
74	fixup_part "$DEV" "$MOUNTPOINT" "$FS_TYPE" "$MOUNT_OPTS" 2
75}
76
77echo "Fixing up /etc/fstab..."
78
79mkdir -p "$TARGET_DIR/etc"
80touch "$FSTAB"
81
82case "$RK_ROOTFS_TYPE" in
83	ext[234])
84		fixup_root "$RK_ROOTFS_TYPE"
85		;;
86	*)
87		fixup_root auto
88		;;
89esac
90
91fixup_basic_part proc /proc
92fixup_basic_part devtmpfs /dev
93fixup_basic_part devpts /dev/pts mode=0620,ptmxmode=0666,gid=5
94fixup_basic_part tmpfs /dev/shm nosuid,nodev,noexec
95fixup_basic_part sysfs /sys
96fixup_basic_part configfs /sys/kernel/config
97fixup_basic_part debugfs /sys/kernel/debug
98fixup_basic_part pstore /sys/fs/pstore
99
100if [ "$POST_OS" = recovery ]; then
101	fixup_device_part /dev/sda1 /mnt/udisk auto
102	fixup_device_part /dev/mmcblk1p1 /mnt/sdcard auto
103fi
104
105for idx in $(seq 1 "$(rk_extra_part_num)"); do
106	DEV="$(rk_extra_part_dev $idx)"
107	MOUNTPOINT="$(rk_extra_part_mountpoint $idx)"
108	FS_TYPE="$(rk_extra_part_fstype $idx)"
109
110	# No fstab entry for built-in partitions
111	if rk_extra_part_builtin $idx; then
112		del_part "$DEV" "$MOUNTPOINT" "$FS_TYPE"
113		continue
114	fi
115
116	fixup_device_part "$DEV" "$MOUNTPOINT" "$FS_TYPE" \
117		"$(rk_extra_part_options $idx)"
118done
119