-
Notifications
You must be signed in to change notification settings - Fork 0
/
luksmount_fake_sdcard.sh
executable file
·85 lines (73 loc) · 2.2 KB
/
luksmount_fake_sdcard.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/system/bin/sh
# https://blog.ja-ke.tech/2020/04/04/android-luks2.html
PARTITION=/dev/block/mmcblk0p2
LABEL=microsd_luks
# This script would replace the internal storage mounted at /sdcard/ with the
# micro-sd card inserted into the phone... except there are permission problems
# I'm not sure how to workaround :-(
# If you have any ideas, drop me a line
set -e -u
set -o pipefail
function usage()
{
cat << EOF
Usage: ${0##*/}" [OPTION]...
Options: --help, -h: show this help dialog
EOF
}
function main()
{
while getopts "h-:" opt; do
case ${opt} in
h) # help message
usage
exit 0
;;
-)
case "${OPTARG}" in
help)
usage
exit 0
;;
*)
printf 'Unknown option, exiting now\n' >&2
exit 1
;;
esac
;;
?)
printf 'Unknown option, exiting now\n' >&2
exit 1
;;
esac
done
shift $((OPTIND - 1))
[[ ${1:-} == '--' ]] && shift
if [ "$(id -u)" != 0 ]; then
echo "Aborting: This script needs root." >&2
exit 1
fi
sdcard_uuid="$(blkid "${PARTITION}" -o export|grep '^UUID='|cut -d= -f2)"
TARGET="/mnt/media_rw/${sdcard_uuid}"
# Try to open container
if [ ! -b "/dev/mapper/${LABEL}" ]; then
cryptsetup luksOpen "${PARTITION}" "${LABEL}"
else
echo "Container already open, skipped cryptsetup..." >&2
fi
# Mounting
nsenter -t 1 -m sh << EOF
# https://blog.ja-ke.tech/2020/04/04/android-luks2.html
mkdir -p $TARGET
mountpoint "${TARGET}" >/dev/null || mount -t ext4 -o nosuid,nodev,noatime "/dev/mapper/${LABEL}" "${TARGET}"
test -d "${TARGET}/0" || mkdir -p "${TARGET}/0"
test -d "${TARGET}/10" || mkdir -p "${TARGET}/10"
sdcard -u 1023 -g 1023 -U 0 -m -o "${TARGET}" emulated
EOF
am broadcast -a android.intent.action.MEDIA_MOUNTED -d "file:///storage/emulated/0"
}
if [ "${BASH_SOURCE[0]}" == "$0" ]; then
echo "This script doesn't work, bailing now"
exit 1
main "$@"
fi