-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall-partition.func
496 lines (453 loc) · 13.3 KB
/
install-partition.func
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
#! /bin/bash
#=======================================
chroot_add_mount() {
mount "$@" && CHROOT_ACTIVE_MOUNTS=("$2" "${CHROOT_ACTIVE_MOUNTS[@]}")
}
#=======================================
chroot_maybe_add_mount() {
local cond=$1; shift
if eval "$cond"; then
chroot_add_mount "$@"
fi
}
#=======================================
chroot_teardown() {
if (( ${#CHROOT_ACTIVE_MOUNTS[@]} )); then
umount "${CHROOT_ACTIVE_MOUNTS[@]}"
fi
unset CHROOT_ACTIVE_MOUNTS
}
#=======================================
chroot_setup() {
CHROOT_ACTIVE_MOUNTS=()
[[ $(trap -p EXIT) ]] && show_message OVERWRITE_EXISTING_TRAP
trap 'chroot_teardown' EXIT
chroot_add_mount proc "$DIR_CHROOT/proc" -t proc -o nosuid,noexec,nodev &&
chroot_add_mount sys "$DIR_CHROOT/sys" -t sysfs -o nosuid,noexec,nodev,ro &&
ignore_error chroot_maybe_add_mount "[[ -d '$DIR_CHROOT/sys/firmware/efi/efivars' ]]" \
efivarfs "$DIR_CHROOT/sys/firmware/efi/efivars" -t efivarfs -o nosuid,noexec,nodev &&
chroot_add_mount udev "$DIR_CHROOT/dev" -t devtmpfs -o mode=0755,nosuid &&
chroot_add_mount devpts "$DIR_CHROOT/dev/pts" -t devpts -o mode=0620,gid=5,nosuid,noexec &&
chroot_add_mount shm "$DIR_CHROOT/dev/shm" -t tmpfs -o mode=1777,nosuid,nodev &&
chroot_add_mount /run "$DIR_CHROOT/run" --bind &&
chroot_add_mount tmp "$DIR_CHROOT/tmp" -t tmpfs -o mode=1777,strictatime,nodev,nosuid
}
#=======================================
find_part_label(){
local find_label=$1
lsblk -f --output NAME,LABEL,TYPE -p -n -P 2>&1 | while read line; do
eval $line
if [ "$TYPE" == "part" ] || [ "$TYPE" == "crypt" ] || [ "$TYPE" == "lvm" ]; then
if ! [ -z $LABEL ]; then
if [ "$LABEL" == "$find_label" ]; then
echo $NAME
return 1
fi
fi
fi
done
return 0
}
#=======================================
fix_grub_conf(){
local CRYPTODISKS_NAME DISK_NAME DISK_PART TEMP_CRYPTODEVICE GRUB_CMDLINE
sed -i ${DIR_CHROOT}/etc/default/grub -e "s|GRUB_TIMEOUT=5|GRUB_TIMEOUT=1|"
sed -i ${DIR_CHROOT}/etc/default/grub -e "s|GRUB_DISTRIBUTOR=\"Arch\"|GRUB_DISTRIBUTOR=\"Snug\"|"
sed -i ${DIR_CHROOT}/etc/default/grub -e "s|#GRUB_ENABLE_CRYPTODISK=y|GRUB_ENABLE_CRYPTODISK=y|"
CRYPTODISKS_NAME=`lsblk -o PATH | grep /dev/mapper`
for i in ${CRYPTODISKS_NAME[@]}; do
DISK_NAME=`echo $(basename $i)`;
TEMP_CRYPTODEVICE=`cryptsetup status $DISK_NAME | grep device | awk -F: '{print $2}'`
if [ -n "$TEMP_CRYPTODEVICE" ]; then
DISK_PART=${TEMP_CRYPTODEVICE//' '}":$DISK_NAME"
fi
CRYPT_ROOT=`echo $i | grep "root"`
if [ -n "$CRYPT_ROOT" ]; then
GRUB_CMDLINE="$GRUB_CMDLINE `echo "cryptdevice=$DISK_PART" root=$i`"
fi
done
GRUB_CMDLINE=${GRUB_CMDLINE/' '}
sed -i ${DIR_CHROOT}/etc/default/grub -e "s|GRUB_CMDLINE_LINUX=\"\"|GRUB_CMDLINE_LINUX=\"$GRUB_CMDLINE\"|"
}
#=======================================
install_grub_bios(){
local STR_READ AMSURE OK
OK=0
install_pkg "grub" "y"
fix_grub_conf;
while [ "${OK}" = 0 ] ; do
STR_READ=`show_message SPECIFY_PARTITION_INSTALL "grub" "/dev/sda"`
read -p "${STR_READ}" AMSURE
if [ "${AMSURE}" = "" ] ; then
AMSURE="/dev/sda"
fi
if ! ls -l "${AMSURE}" | grep "${AMSURE}" > /dev/null ; then
OK=0
else
#grub-install --root-directory=$DIR_CHROOT "${AMSURE}"
chroot $DIR_CHROOT grub-install "${AMSURE}"
if [ $? != 0 ]; then
show_message FAILED_EXECUTE_COMMAND "grub-install ${AMSURE}"
return 1
fi
chroot $DIR_CHROOT grub-mkconfig -o /boot/grub/grub.cfg
if [ $? != 0 ]; then
show_message FAILED_EXECUTE_COMMAND "grub-mkconfig -o /boot/grub/grub.cfg"
return 1
fi
OK=1
fi
done
return 0
}
#=======================================
install_grub_uefi(){
local STR_READ OK
if ! [ -d "$DIR_CHROOT/boot/efi" ]; then
STR_READ=`show_message UEFI_DIRECTORY_DOES_NOT_EXIST $DIR_CHROOT/boot/efi`
return 1
fi
install_pkg "grub" "y"
install_pkg "efibootmgr" "y"
fix_grub_conf;
OK=0
while [ "${OK}" = 0 ] ; do
chroot $DIR_CHROOT grub-install
if [ $? != 0 ]; then
show_message FAILED_EXECUTE_COMMAND "grub-install"
return 1
fi
chroot $DIR_CHROOT grub-mkconfig -o /boot/grub/grub.cfg
if [ $? != 0 ]; then
show_message FAILED_EXECUTE_COMMAND "grub-mkconfig -o /boot/grub/grub.cfg"
return 1
fi
OK=1
done
return 0
}
#=======================================
install_grub(){
if [ $UEFI = 0 ]; then
install_grub_bios
return $?;
else
install_grub_uefi
return $?;
fi
}
#=======================================
install_uboot(){
local STR_READ AMSURE OK
OK=0
install_pkg "uboot-$ARM_MODEL" "y"
while [ "${OK}" = 0 ] ; do
STR_READ=`show_message SPECIFY_PARTITION_INSTALL "uboot" "/dev/sdb"`
read -p "${STR_READ}" AMSURE
if [ "${AMSURE}" = "" ] ; then
AMSURE="/dev/sdb"
fi
if ! ls -l "${AMSURE}" | grep "${AMSURE}" > /dev/null ; then
OK=0
else
dd if=$DIR_CHROOT/boot/u-boot-sunxi-with-spl.bin of=${AMSURE} bs=1024 seek=8
OK=1
fi
done
}
#=======================================
# Install operating system loader
install_bootloader(){
lsblk -e7 -d -p -o NAME,MODEL,SIZE,SERIAL | grep -v "rom\|loop\|airoot\|sr"
if [ "$INSTALL_ARCH" == "arm" ] ; then
install_uboot;
if [ $? != 0 ]; then
exit 1;
fi
else
install_grub;
if [ $? != 0 ]; then
exit 1;
fi
fi
}
#=======================================
umount_part() {
local PATCH_PART
PATCH_PART=$1
if [ -n "`ps -A | grep gpg-agent`" ]; then
killall gpg-agent
fi
cd /
mount | grep ${PATCH_PART} > /dev/null
if [ $? = 0 ]; then
umount -R ${PATCH_PART}
fi
swapon | grep "${PATCH_PART}" > /dev/null
if [ $? = 0 ]; then
swapoff "${PATCH_PART}"
fi
mount | grep "${PATCH_PART}" > /dev/null
if [ $? != 0 ]; then
return 1
fi
return 0
}
#=======================================
mount_efi(){
local DEV AMSURE_FORMAT LABEL_PART
DEV=$1
LABEL_PART=$2
if ! [ -d "$DIR_CHROOT/boot" ]; then
mkdir "$DIR_CHROOT/boot"
fi
if ! [ -d "$DIR_CHROOT/boot/efi" ]; then
mkdir "$DIR_CHROOT/boot/efi"
fi
mount | grep "$DIR_CHROOT/boot/efi" > /dev/null
if [ $? = 0 ]; then
umount "$DIR_CHROOT/boot/efi"
fi
while [ "yn" = "${YN#*$AMSURE_FORMAT}" ] ; do
STR_READ=`show_message FORMAT_PARTITION "${DEV}" "Fat32"`
read -p "${STR_READ}" AMSURE_FORMAT
done
if [ "${AMSURE_FORMAT}" = "y" ] ; then
mkfs.vfat -F32 -n "${LABEL_PART}" "${DEV}"
if [ $? != 0 ]; then
show_message UNABLE_FORMAT_PARTITION "${DEV}"
exit 1
fi
else
fatlabel "${DEV}" "${LABEL_PART}"
if [ $? != 0 ]; then
show_message FAILED_SET_SECTION_LABEL "${DEV}"
return 1
fi
fi
mount "${DEV}" "$DIR_CHROOT/boot/efi"
if [ $? != 0 ]; then
show_message FAILED_MOUNT_PARTITION "${DEV}"
exit 1
fi
}
#=======================================
mount_swap(){
local DEV LABEL_PART
DEV=$1
LABEL_PART=$2
swapon | grep "${DEV}" > /dev/null
if [ $? = 0 ]; then
swapoff "${DEV}"
fi
mkswap -L "${LABEL_PART}" "${DEV}"
swapon "${DEV}"
}
#=======================================
mount_dev(){
local AMSURE AMSURE_FORMAT DEV PATCH_PART LABEL_PART
DEV=$1
LABEL_PART=$2
PATCH_PART=$3
if [ "${PATCH_PART:0:1}" == "/" ]; then
PATCH_PART=${PATCH_PART:1}
fi
if ! [ -d "$DIR_CHROOT/${PATCH_PART}" ]; then
mkdir "$DIR_CHROOT/${PATCH_PART}"
fi
mount | grep "$DIR_CHROOT/${PATCH_PART}" > /dev/null
if [ $? = 0 ]; then
umount "$DIR_CHROOT/${PATCH_PART}"
fi
while [ "yn" = "${YN#*$AMSURE_FORMAT}" ] ; do
STR_READ=`show_message FORMAT_PARTITION "${AMSURE}" "Ext4"`
read -p "${STR_READ}" AMSURE_FORMAT
done
if [ "${AMSURE_FORMAT}" = "y" ] ; then
mkfs.ext4 -F "${DEV}" -L "${LABEL_PART}"
if [ $? != 0 ]; then
show_message UNABLE_FORMAT_PARTITION "${DEV}"
exit 1
fi
else
e2label "${DEV}" "${LABEL_PART}"
if [ $? != 0 ]; then
show_message FAILED_SET_SECTION_LABEL "${DEV}"
return 1
fi
fi
mount "${DEV}" "$DIR_CHROOT/${PATCH_PART}"
if [ $? != 0 ]; then
show_message FAILED_MOUNT_PARTITION "${DEV}"
exit 1
fi
}
#=======================================
mount_part() {
local AMSURE_DEV AMSURE_FIND_LABEL OK PART PATCH_PART LABEL_PART STR_READ
PART=$1
PATCH_PART=$1
LABEL_PART=$1
if [ "${PATCH_PART}" == "" ] ; then
show_message NO_PARTITION_MOUNTING
exit 1
fi
FIND_DEV_LABEL=$(find_part_label "${LABEL_PART}")
OK=0
lsblk -p --output MODEL,NAME,LABEL,FSTYPE,TYPE,SIZE,UUID,SERIAL | grep -v "rom\|loop\|airoot\|sr"
while [ "${OK}" = 0 ]; do
if [ -n "${FIND_DEV_LABEL}" ]; then
while [ "$YN" = "${YN#*$AMSURE_FIND_LABEL}" ] ; do
STR_READ=`show_message FOUND_DRIVE_PARTITION "${PART}" "${FIND_DEV_LABEL}" "${LABEL_PART}"`
read -p "${STR_READ}" AMSURE_FIND_LABEL
done
fi
if [ "${AMSURE_FIND_LABEL}" == "y" ]; then
AMSURE_DEV=${FIND_DEV_LABEL}
else
STR_READ=`show_message SPECIFY_PARTITION_MOUNT "$1"`
read -p "${STR_READ}" AMSURE_DEV
fi
LAST_SYMBOL=`echo ${AMSURE_DEV:$((${#AMSURE_DEV}-1)):1}`
if [ -z "$(echo ${AMSURE_DEV} | grep "/dev/mapper")" ] && [[ "${LAST_SYMBOL}" != *[[:digit:]]* ]]; then
show_message NOT_RIGHT_VALUE
OK=0
continue
elif ! ls -l "${AMSURE_DEV}" | grep "${AMSURE_DEV}" > /dev/null ; then
OK=0
continue
else
STR_READ=`show_message SPECIFY_DISK_PARTITION_LABEL "$1"`
read -p "${STR_READ}" LABEL_PART
if [ "${LABEL_PART}" == "" ] ; then
LABEL_PART="$1"
fi
# swap
if [ "${PART}" = "swap" ] ; then
mount_swap "${AMSURE_DEV}" "${LABEL_PART}"
# efi
elif [ "${PART}" = "efi" ] ; then
mount_efi "${AMSURE_DEV}" "${LABEL_PART}"
else
mount_dev "${AMSURE_DEV}" "${LABEL_PART}" "${PATCH_PART}"
fi
OK=1
fi
done
}
#=======================================
deletes_disk_partition_table(){
local AMSURE OK STR_READ DEV
DEV=$1
while [ "$YN" = "${YN#*$AMSURE}" ] ; do
STR_READ=`show_message DELETES_DISK_PARTITION_TABLE $DEV`
read -p "${STR_READ}" AMSURE
done
if [ "${AMSURE}" = "y" ] ; then
dd if=/dev/zero of=$DEV bs=1M count=8;
fi
}
#=======================================
check_if_disk_exists(){
local DEV STR
DEV=$1
STR=`lsblk -e7 -d -o NAME -p -P| grep $DEV > /dev/null`
if [ $? = 0 ]; then
return 1
else
return 0
fi
}
#=======================================
partition_drive() {
local AMSURE OK STR_READ LAST_SYMBOL
while [ "$YN" = "${YN#*$AMSURE}" ] ; do
STR_READ=`show_message SPLIT_DISK_PARTITIONS`
read -p "${STR_READ}" AMSURE
done
umount_part "$DIR_CHROOT"
if [ $? = 0 ]; then
show_message UNABLE_UNMOUNT_PARTITION "$DIR_CHROOT"
exit
fi
if [ "${AMSURE}" = "y" ] ; then
lsblk -e7 -d -p -o NAME,MODEL,SIZE,SERIAL | grep -v "rom\|loop\|airoot\|sr"
OK=0
while [ "${OK}" = 0 ] ; do
STR_READ=`show_message SPECIFY_PARTITION`
AMSURE=
read -p "${STR_READ}" AMSURE
LAST_SYMBOL=`echo ${AMSURE:$((${#AMSURE}-1)):1}`
if [ "${AMSURE}" = "i" ]; then
OK=1
continue
elif [ "${AMSURE}" = "" ]; then
AMSURE="/dev/sda"
echo "/dev/sda"
else
check_if_disk_exists $AMSURE;
if [ $? = 0 ]; then
show_message NOT_RIGHT_VALUE
continue
fi
fi
definition_ssd "${AMSURE}";
# Deletes the disk partition table.
deletes_disk_partition_table "${AMSURE}";
cfdisk "${AMSURE}"
if [ $? = 0 ]; then
partprobe "${AMSURE}"
if ! [ $? = 0 ]; then
show_message FAILED_NOTIFY_PARTITION_TABLE
exit 1
fi
fi
done
fi
return 0
}
#=======================================
preparation_disk(){
local OK AMSURE
if [ "$INSTALL_ARCH" = "arm" ] ; then
UEFI=0;
else
if [ -d /sys/firmware/efi ]; then
UEFI=1;
else
UEFI=0;
fi
fi
partition_drive
mount_part "/"
#mount_part "/boot"
if [ $UEFI = 1 ]; then
mount_part "efi"
fi
mount_part "swap"
OK=0;
while [ "$OK" = 0 ] ; do
STR_READ=`show_message SPECIFY_MOUNTING_PARTITION`
read -p "${STR_READ}" AMSURE
if [ "${AMSURE}" = "i" ]; then
OK=1
continue
elif [ "${AMSURE}" = "" ]; then
show_message NOT_RIGHT_VALUE
OK=0
else
mount_part "${AMSURE}"
fi
done
}
#=======================================
# Определение SSD
definition_ssd(){
local PART
PART=$1
hdparm -I $PART | grep "TRIM" > /dev/null
if [ $? = 0 ]; then
#read -p "Для устройства /dev/sda есть поддержка TRIM, использовать ее?" AMSURE
show_message SUPPORT_TRIM $PART
fi
}