-
Notifications
You must be signed in to change notification settings - Fork 39
/
mkeosdrive
executable file
·150 lines (117 loc) · 3.71 KB
/
mkeosdrive
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/bin/bash
# exit script on any error
set -e
if [ "$EUID" -ne 0 ]
then
echo "Must run as root or with sudo."
exit 1
fi
DEVICE=${1}
TARBALL=${2}
CONFIG=${3}
DRIVESIZE=${4}
DEVICE_BOOT=${DEVICE}1
DEVICE_ROOT=${DEVICE}2
BOOT_MNT=/mnt/boot
ROOT_MNT=/mnt/root
if [[ -z "$TARBALL" ]]
then
echo "Usage: ./mkeosdrive /dev/yourUSBdrive system_image.tar <optional 3rd parameter: /path/to/configfile.tar.gz>\
<optional 4th parameter: desired end cap size of root partition in GB>"
echo "If you want to provide a root partition end cap size but no config, use \"-\" without the quotes as the third parameter."
exit 1
fi
mkdir -p $BOOT_MNT
mkdir -p $ROOT_MNT
# Unmount all partitions on device
umount "${DEVICE}"? && echo "Unmounted all partitions on $DEVICE" || echo "no filesystem mounted from $DEVICE"
# Unmount folders only if already mounted
grep -qs "$BOOT_MNT" /proc/mounts && umount $BOOT_MNT
grep -qs "$ROOT_MNT" /proc/mounts && umount $ROOT_MNT
DIR=$(mktemp -d)
function cleanup {
if [ -d "$DIR" ]
then
rm -rf "$DIR"
fi
if [ -d "$BOOT_MNT" ]
then
rm -rf "$BOOT_MNT"
fi
if [ -d "$ROOT_MNT" ]
then
rm -rf "$ROOT_MNT"
fi
}
trap cleanup EXIT
echo "Press any key to continue formatting $DEVICE"
read -n1 -rsp $'or hit Ctrl+C to exit now...\n'
# deleting existing partiton table first
dd if=/dev/zero of="$DEVICE" bs=512 count=1 conv=notrunc
echo "Waiting for disk sync..."
sync
parted --script "$DEVICE" mktable msdos
parted --script "$DEVICE" mkpart primary fat32 1 150MB
# If no drive size was supplied as a parameter, default to full size root partition
if [ -n "$DRIVESIZE" ]
then
echo "Creating root partition with an end cap size of $DRIVESIZE GB"
(( SIZE = $DRIVESIZE * 1024 ))
parted --script "$DEVICE" mkpart primary ext3 150MB "${SIZE}"MB
else
parted --script "$DEVICE" mkpart primary ext3 150MB 100%
fi
echo "Waiting for disk sync..."
sync
# Add a delay to allow the system to recognize the new partitions
sleep 2
echo "Formatting $DEVICE partitions..."
mkfs.msdos -F 32 "$DEVICE_BOOT"
mkfs.ext3 -q "$DEVICE_ROOT"
echo "Waiting for disk sync..."
sync
mount "$DEVICE_BOOT" $BOOT_MNT
mount "$DEVICE_ROOT" $ROOT_MNT
echo "Unpacking EdgeOS release image"
tar xf "$TARBALL" -C "$DIR"
####### Kernel
echo "Verifying EdgeOS kernel"
if [ "$(md5sum "$DIR"/vmlinux.tmp | awk -F ' ' '{print $1}')" != \
"$(cat "$DIR"/vmlinux.tmp.md5)" ]; then
echo "Kernel in image is corrupted! Check your image and start over."
exit 1
fi
echo "Copying EdgeOS kernel to boot partition"
cp "$DIR"/vmlinux.tmp $BOOT_MNT/vmlinux.64
cp "$DIR"/vmlinux.tmp.md5 $BOOT_MNT/vmlinux.64.md5
####### System
echo "Verifying EdgeOS system image"
if [ "$(md5sum "$DIR"/squashfs.tmp | awk -F ' ' '{print $1}')" != \
"$(cat "$DIR"/squashfs.tmp.md5)" ]; then
echo "System in image is corrupted! Check your image and start over."
exit 1
fi
echo "Copying EdgeOS system image to root partition"
mv "$DIR"/squashfs.tmp $ROOT_MNT/squashfs.img
mv "$DIR"/squashfs.tmp.md5 $ROOT_MNT/squashfs.img.md5
echo "Copying version file to the root partition"
mv "$DIR"/version.tmp $ROOT_MNT/version
# Writable data dir
echo "Creating EdgeOS writable data directories"
mkdir $ROOT_MNT/w
mkdir $ROOT_MNT/www
chown 33:0 $ROOT_MNT/www
####### Extract config
if [ -n "$CONFIG" ] && [ -f "$CONFIG" ]
then
echo "Config archive $CONFIG was provided, extracting it to the root partition..."
tar xf "$CONFIG" -C $ROOT_MNT/w
else
echo "No config archive was provided, default settings will be used."
fi
echo "Waiting for disk sync..."
sync
echo "Unmounting filesystems on $DEVICE..."
umount $ROOT_MNT
umount $BOOT_MNT
echo "Done. You can now plug the USB drive back into your router and boot it."