-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathgen-omv_n1.sh
executable file
·324 lines (260 loc) · 7.37 KB
/
gen-omv_n1.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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#!/bin/sh
#genetate omv [phicomm-n1] image: chmod +x gen-omv_n1.sh && sudo ./gen-omv_n1.sh
#depends: dosfstools debootstrap xz
set -xe
die() {
printf '\033[1;31mERROR:\033[0m %s\n' "$@" >&2 # bold red
exit 1
}
if [ "$(id -u)" -ne 0 ]; then
die 'This script must be run as root!'
fi
export DEBIAN_FRONTEND=noninteractive
BUILD_DATE="$(date +%Y-%m-%d)"
usage() {
cat <<EOF
Usage: gen-omv_n1.sh [options]
Valid options are:
-b DEBIAN_BRANCH Debian branch to install (default is stretch).
-m DEBIAN_MIRROR URI of the mirror to fetch packages from
(default is http://mirrors.tuna.tsinghua.edu.cn/debian/).
-o OUTPUT_IMG Output img file
(default is BUILD_DATE-omv-n1--DEBIAN_BRANCH.img).
-h Show this help message and exit.
EOF
}
while getopts 'b:m:o:h' OPTION; do
case "$OPTION" in
b) DEBIAN_BRANCH="$OPTARG";;
m) DEBIAN_MIRROR="$OPTARG";;
o) OUTPUT_IMG="$OPTARG";;
h) usage; exit 0;;
esac
done
: ${DEBIAN_BRANCH:="stretch"}
: ${DEBIAN_MIRROR:="http://mirrors.tuna.tsinghua.edu.cn/debian/"}
: ${OUTPUT_IMG:="${BUILD_DATE}-omv-n1-${DEBIAN_BRANCH}.img"}
#======================= F u n c t i o n s =======================#
gen_image() {
fallocate -l $(( 1500 * 1024 *1024 )) "$OUTPUT_IMG"
cat > fdisk.cmd <<-EOF
o
n
p
1
+128MB
t
c
a
n
p
2
w
EOF
fdisk "$OUTPUT_IMG" < fdisk.cmd
rm -f fdisk.cmd
}
do_format() {
mkfs.vfat "$BOOT_DEV"
mkfs.ext4 "$ROOT_DEV"
mkdir -p mnt
mount "$ROOT_DEV" mnt
mkdir -p mnt/boot
mount "$BOOT_DEV" mnt/boot
}
do_debootstrap() {
#debootstrap --arch="arm64" "$DEBIAN_BRANCH" mnt "$DEBIAN_MIRROR"
local url="https://github.com/yangxuan8282/gen-rpi_os/releases/download/debian_rootfs/stretch-arm64_rootfs.tar.xz"
mkdir -p mnt
$(wget $url -O- | tar -C mnt -xJf -) || true
}
gen_sources_list() {
cat <<EOF
deb ${DEBIAN_MIRROR} ${DEBIAN_BRANCH} main contrib non-free
EOF
}
gen_keyboard_layout() {
cat <<EOF
# KEYBOARD CONFIGURATION FILE
# Consult the keyboard(5) manual page.
XKBMODEL="pc105"
XKBLAYOUT="us"
XKBVARIANT=""
XKBOPTIONS=""
BACKSPACE="guess"
EOF
}
gen_fstabs() {
echo "UUID=${BOOT_UUID} /boot vfat defaults 0 2
UUID=${ROOT_UUID} / ext4 defaults,noatime 0 1"
}
add_normal_user() {
echo "root:toor" | chpasswd
useradd -g sudo -ms /bin/bash n1
echo "n1:phicomm" | chpasswd
echo "n1 ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
sed -i 's/#force_color_prompt=yes/force_color_prompt=yes/g' /home/n1/.bashrc
}
config_network() {
cat >> /etc/network/interfaces << 'EOF'
# eth0 network interface
auto eth0
allow-hotplug eth0
iface eth0 inet dhcp
EOF
}
install_omv() {
apt-get update
apt-get install -y dirmngr
apt-key adv --keyserver keyserver.ubuntu.com --recv 7E7A6C592EF35D13
apt-key adv --keyserver keyserver.ubuntu.com --recv 24863F0C716B980B
cat <<EOF >> /etc/apt/sources.list.d/openmediavault.list
deb http://packages.openmediavault.org/public arrakis main
# deb http://downloads.sourceforge.net/project/openmediavault/packages arrakis main
## Uncomment the following line to add software from the proposed repository.
# deb http://packages.openmediavault.org/public arrakis-proposed main
# deb http://downloads.sourceforge.net/project/openmediavault/packages arrakis-proposed main
## This software is not part of OpenMediaVault, but is offered by third-party
## developers as a service to OpenMediaVault users.
# deb http://packages.openmediavault.org/public arrakis partner
# deb http://downloads.sourceforge.net/project/openmediavault/packages arrakis partner
EOF
export LANG=C
export DEBIAN_FRONTEND=noninteractive
export APT_LISTCHANGES_FRONTEND=none
apt-get update
apt-get install -y openmediavault-keyring
apt-get update
apt-get --yes --auto-remove --show-upgraded \
--allow-downgrades --allow-change-held-packages \
--no-install-recommends \
--option Dpkg::Options::="--force-confdef" \
--option DPkg::Options::="--force-confold" \
install postfix openmediavault
# Initialize the system and database.
#omv-initsystem
}
gen_resizeonce_scripts() {
cat > /etc/rc.local <<'EOF'
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
if [ -f /resize2fs_once ]; then /resize2fs_once && omv-initsystem ; fi
exit 0
EOF
chmod +x /etc/rc.local
cat > /resize2fs_once <<'EOF'
#!/bin/sh
set -x
ROOT_DEV=$(findmnt / -o source -n)
ROOT_START=$(fdisk -l $(echo "$ROOT_DEV" | sed -E 's/p?2$//') | grep "$ROOT_DEV" | awk '{ print $2 }')
cat > /tmp/fdisk.cmd <<-EOF
d
2
n
p
2
${ROOT_START}
w
EOF
fdisk "$(echo "$ROOT_DEV" | sed -E 's/p?2$//')" < /tmp/fdisk.cmd
rm -f /tmp/fdisk.cmd
partprobe &&
resize2fs "$ROOT_DEV" &&
mv /resize2fs_once /usr/local/bin/resize2fs_once
EOF
chmod +x /resize2fs_once
}
install_kernel() {
local url="https://github.com/yangxuan8282/phicomm-n1/releases/download/150balbes_kernel/kernel_3.14.29.tar.gz"
#local url="https://github.com/yangxuan8282/phicomm-n1/releases/download/150balbes_kernel/kernel_4.18.7_20180922.tar.gz"
wget $url
tar xf *.tar.gz
cp -R --no-preserve=mode,ownership kernel_*/boot/* /boot/
cp -a kernel_*/lib/* /lib/
sed -i "s|root=LABEL=ROOTFS|root=UUID=${ROOT_UUID}|" /boot/uEnv.ini
rm -rf kernel_*
}
install_uboot() {
local url="https://github.com/yangxuan8282/phicomm-n1/releases/download/20180917/u-boot.bin"
wget $url
dd if=u-boot.bin of=${LOOP_DEV} bs=1 count=442 conv=fsync
dd if=u-boot.bin of=${LOOP_DEV} bs=512 skip=1 seek=1 conv=fsync
rm -f u-boot.bin
}
gen_env() {
echo "LOOP_DEV=${LOOP_DEV}
export DEBIAN_FRONTEND=noninteractive
DEBIAN_BRANCH=${DEBIAN_BRANCH}
ROOT_UUID=${ROOT_UUID}"
}
setup_chroot() {
chroot mnt /bin/bash <<-EOF
set -xe
source /root/env_file
source /root/functions
rm -f /root/env_file /root/functions
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
apt-get update && apt-get install -y locales sudo
echo "en_US.UTF-8 UTF-8" | tee --append /etc/locale.gen
locale-gen
echo phicomm > /etc/hostname
echo "127.0.1.1 phicomm.localdomain phicomm" | tee --append /etc/hosts
config_network
install_omv
add_normal_user
gen_resizeonce_scripts
install_kernel
sync
install_uboot
#rm -rf /var/lib/apt/lists/* /tmp/*
EOF
}
mounts() {
mount -t proc /proc mnt/proc
mount -t sysfs /sys mnt/sys
mount -o bind /dev mnt/dev
}
umounts() {
umount mnt/dev
umount mnt/sys
umount mnt/proc
umount mnt/boot
umount mnt
losetup -d "$LOOP_DEV"
}
#======================= F u n c t i o n s =======================#
pass_function() {
sed -nE '/^#===.*F u n c t i o n s.*===#/,/^#===.*F u n c t i o n s.*===#/p' "$0"
}
gen_image
LOOP_DEV=$(losetup --partscan --show --find "${OUTPUT_IMG}")
BOOT_DEV="$LOOP_DEV"p1
ROOT_DEV="$LOOP_DEV"p2
do_format
do_debootstrap
gen_keyboard_layout > mnt/etc/default/keyboard
IMGID="$(dd if="${OUTPUT_IMG}" skip=440 bs=1 count=4 2>/dev/null | xxd -e | cut -f 2 -d' ')"
BOOT_UUID=$(blkid ${BOOT_DEV} | cut -f 2 -d '"')
ROOT_UUID=$(blkid ${ROOT_DEV} | cut -f 2 -d '"')
gen_fstabs > mnt/etc/fstab
gen_sources_list > mnt/etc/apt/sources.list
gen_env > mnt/root/env_file
pass_function > mnt/root/functions
mounts
setup_chroot
umounts
cat >&2 <<-EOF
---
Installation is complete
Flash to usb disk with: dd if=${OUTPUT_IMG} of=/dev/TARGET_DEV bs=4M status=progress
EOF