-
Notifications
You must be signed in to change notification settings - Fork 2
/
write_sd.sh
executable file
·101 lines (82 loc) · 2.66 KB
/
write_sd.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#! /bin/sh
DEVICE=$1
FILES_PATH='.'
usage() { echo "usage: $0 <device> [<path>]"; }
if [ -d "$2" ]; then
FILES_PATH=$2
else
echo "Error: $2 is not a valid path."
usage
exit 2
fi
if [ -z "$DEVICE" ]; then
echo "Error: No device specified."
usage
exit 2
fi
if [ ! -b "$DEVICE" ]; then
echo "Error: $DEVICE is not a valid device."
usage
exit 2
fi
echo "This script will destroy all data on the device you have selected ($DEVICE) and create"
echo "a bootable Alpine installation for the Orange Pi Zero. Running fdisk from a script is"
echo "generally not recommended, you will not have an opportunity to inspect changes to the"
echo "partition table before they are written. You need to be confident that the device you"
echo "have specified ($DEVICE) is the correct device."
echo
while true; do
read -p "Are you sure you want to continue? " confirmation
case $(echo "$confirmation" | tr '[:upper:]' '[:lower:]') in
y|yes ) break;;
n|no ) echo "Discretion is the better part of valour. Exiting."; exit;;
* ) echo "Please answer yes or no.";;
esac
done
echo
if parted --script "$DEVICE"1 > /dev/null 2>&1; then
echo "Wiping signatures.."
wipefs --all --force "$DEVICE"1 > /dev/null 2>&1 \
|| { echo "Error: failed to remove signatures"; exit 1; }
fi
echo "Writing zeroes.."
dd if=/dev/zero of=$DEVICE bs=1M count=1 status=none \
|| { echo "Error: failed to write zeros"; exit 1; }
echo "Writing u-boot.."
dd if=$FILES_PATH/u-boot-sunxi-with-spl.bin of=$DEVICE bs=1024 seek=8 status=none \
|| { echo "Error: failed to write u-boot"; exit 1; }
echo "Creating partition.."
# from https://superuser.com/a/984637
# include comments so we can see what operations are taking place, but
# strip the comments with sed before sending arguments to fdisk
FDISK_OUTPUT=$(sed -e 's/\s*\([\+0-9a-zA-Z]*\).*/\1/' << EOF | fdisk $DEVICE
n # add new
p # primary partition
1 # numbered 1
2048 # from sector 2048
# to the last sector
t # of type
c # W95 FAT32 (LBA)
a # make it bootable
w # save changes
q # exit fdisk
EOF
)
echo "Formating partition.."
mkfs.vfat -n ALPINE "$DEVICE"1 >/dev/null \
|| { echo "Error: failed to make vfat partition"; exit 1; }
cleanup() {
mountpoint -q $TEMP_MOUNT && umount "$TEMP_MOUNT"
[ -d $TEMP_MOUNT ] && rm -rf $TEMP_MOUNT
}
TEMP_MOUNT=$(mktemp -d -t opizero-alpine-XXXXXXXX)
trap 'cleanup' EXIT
echo "Mounting device.."
mount "$DEVICE"1 $TEMP_MOUNT \
|| { echo "Error: failed to mount ${DEVICE}1"; exit 1; }
echo "Copying files.."
{ cp -r $FILES_PATH/apks "$TEMP_MOUNT"/ && cp -r $FILES_PATH/boot "$TEMP_MOUNT"/; } \
|| { echo "Error: failed to copy from $FILES_PATH"; exit 1; }
echo "Ejecting device.."
eject $DEVICE || true
echo "Done!"