-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_microsd.sh
executable file
·59 lines (47 loc) · 1.77 KB
/
setup_microsd.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
#!/usr/bin/env bash
set -e -u -o pipefail
DEVICE=/dev/block/mmcblk0
FATPART="${DEVICE}p1"
EXT4PART="${DEVICE}p2"
LABEL=microsd_luks
cat << EOF
WARNING: this script is setup to format a block device. This means that all
data on this block device will be irrecoverably lost. Your phone may have
different block names - MAKE SURE YOUR BLOCK NAME IS THE RIGHT ONE OR YOU MAY
END UP WITH AN EXPENSIVE PAPERWEIGHT INSTEAD OF A PHONE.
IF YOU DO NOT UNDERSTAND THE ABOVE WARNING, THEN THIS SCRIPT IS NOT MEANT FOR
YOU. DO NOT RUN IT.
Current block device: ${DEVICE}
EOF
read -p "Enter YES at the prompt to continue, or anything else to abort: "
if [ "${REPLY}" != "YES" ]; then
echo "Aborting"
exit 1
fi
sudo sgdisk --zap-all "${DEVICE}"
sudo sgdisk --new=0:0:10M --typecode=0:0700 --gpttombr=1 "${DEVICE}"
# we use fdisk for partition 2 because sgdisk errors out for gawd knows what reason
cat << EOF
#fdisk is messed up and can't script properly, run the following:
sudo fdisk ${DEVICE}
n # create new partition
p # type primary
2 # number 2
20481 # start sector
# use default and fill sdcard
w # write to disk
EOF
sudo fdisk ${DEVICE}
read -rp "Once you're done with fdisk, press ENTER to continue: "
sgdisk --android-dump "${FATPART}"
mkfs.exfat -n android "${FATPART}"
sudo mkfs.exfat -n android "${FATPART}"
blkid -c /dev/null -s TYPE -s UUID -s LABEL "${FATPART}"
#In case next command doesn't work: try sudo fsck.exfat -a /dev/sdd1
sudo fsck.exfat -a "${FATPART}"
sudo cryptsetup luksFormat "${EXT4PART}"
sudo cryptsetup luksOpen "${EXT4PART}" "${LABEL}"
sudo mkfs.ext4 "/dev/mapper/${LABEL}"
sdcard_uuid="$(blkid "${EXT4PART}" -o export|grep '^UUID='|cut -d= -f2)"
TARGET="/mnt/media_rw/${sdcard_uuid}"
sudo mkdir -p "${TARGET}"