/
install.sh
executable file
·1510 lines (1340 loc) · 39.4 KB
/
install.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
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/sh
# vim: noexpandtab ts=8 sw=4 softtabstop=4
# Setup a semi-sane environment
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/rescue
export PATH
HOME=/root
export HOME
TERM=${TERM:-xterm}
export TERM
. /etc/avatar.conf
is_truenas()
{
test "$AVATAR_PROJECT" = "TrueNAS"
return $?
}
do_sata_dom()
{
if ! is_truenas ; then
return 1
fi
install_sata_dom_prompt
return $?
}
get_product_path()
{
echo /cdrom /.mount
}
get_image_name()
{
find $(get_product_path) -name "$AVATAR_PROJECT-$AVATAR_ARCH.img.xz" -type f
}
# The old pre-install checks did several things
# 1: Don't allow going from FreeNAS to TrueNAS or vice versa
# 2: Don't allow downgrading. (Not sure we can do that now.)
# 3: Check memory size and cpu speed.
# This does memory size only for now.
pre_install_check()
{
# We need at least this many GB of RAM
local readonly minmemgb=8
# subtract 1GB to allow for reserved memory
local readonly minmem=$(expr \( ${minmemgb} \- 1 \) \* 1024 \* 1024 \* 1024)
local memsize=$(sysctl -n hw.physmem)
if [ ${memsize} -lt ${minmem} ]; then
dialog --clear --title "${AVATAR_PROJECT}" --defaultno \
--yesno "This computer has less than the recommended 8 GB of RAM.\n\nOperation without enough RAM is not recommended. Continue anyway?" 7 74 || return 1
fi
return 0
}
# Convert /etc/version* to /etc/avatar.conf
#
# 1 - old /etc/version* file
# 2 - dist version of avatar.conf
# 3 - destination avatar.conf
upgrade_version_to_avatar_conf()
{
local destconf srcconf srcversion
local project version revision arch
srcversion=$1
srcconf=$2
destconf=$3
set -- $(sed -E -e 's/-amd64/-x64/' -e 's/-i386/-x86/' -e 's/(.*)-([^-]+) \((.*)\)/\1-\3-\2/' -e 's/-/ /' -e 's/-([^-]+)$/ \1/' -e 's/-([^-]+)$/ \1/' < $srcversion)
project=$1
version=$2
revision=$3
arch=$4
sed \
-e "s,^AVATAR_ARCH=\".*\",AVATAR_ARCH=\"$arch\",g" \
-e "s,^AVATAR_BUILD_NUMBER=\".*\"\$,AVATAR_BUILD_NUMBER=\"$revision\",g" \
-e "s,^AVATAR_PROJECT=\".*\"\$,AVATAR_PROJECT=\"$project\",g" \
-e "s,^AVATAR_VERSION=\".*\"\$,AVATAR_VERSION=\"$version\",g" \
< $srcconf > $destconf.$$
mv $destconf.$$ $destconf
}
build_config_old()
{
# build_config ${_disk} ${_image} ${_config_file}
local _disk=$1
local _image=$2
local _config_file=$3
cat << EOF > "${_config_file}"
# Added to stop pc-sysinstall from complaining
installMode=fresh
installInteractive=no
installType=FreeBSD
installMedium=dvd
packageType=tar
disk0=${_disk}
partition=image
image=${_image}
bootManager=bsd
commitDiskPart
EOF
}
build_config()
{
# build_config ${_disk} ${_image} ${_config_file}
local _disk=$1
local _image=$2
local _config_file=$3
cat << EOF > "${_config_file}"
# Added to stop pc-sysinstall from complaining
installMode=fresh
installInteractive=no
installType=FreeBSD
installMedium=dvd
packageType=tar
disk0=${_disk}
partscheme=GPT
partition=all
bootManager=bsd
commitDiskPart
EOF
}
wait_keypress()
{
local _tmp
read -p "Press ENTER to continue." _tmp
}
sort_disklist()
{
sed 's/\([^0-9]*\)/\1 /' | sort +0 -1 +1n | tr -d ' '
}
# return 0 if no raid devices, or !0 if there are some.
get_raid_present()
{
local _cnt
local _dummy
if [ ! -d "/dev/raid" ] ; then
return 0;
fi
_cnt=0
ls /dev/raid/ > /tmp/raidfiles
while read _dummy ; do _cnt=$(($_cnt + 1));done < /tmp/raidfiles
return $_cnt
}
get_physical_disks_list()
{
local _boot=$(glabel status | awk '/iso9660\/(FREE|TRUE)NAS/ { print $3 }')
local _disk
for _disk in $(sysctl -n kern.disks)
do
if [ "${_disk}" = "${_boot}" ]; then
continue
fi
VAL="${VAL} ${_disk}"
done
get_raid_present
if [ $? -ne 0 ] ; then
VAL="$VAL `cd /dev ; ls -d raid/* | grep -v '[0-9][a-z]'`"
fi
VAL=`echo $VAL | tr ' ' '\n'| grep -v '^cd' | sort_disklist`
export VAL
}
get_media_description()
{
local _media
local _description
local _cap
_media=$1
VAL=""
if [ -n "${_media}" ]; then
_description=`geom disk list ${_media} 2>/dev/null \
| sed -ne 's/^ descr: *//p'`
if [ -z "$_description" ] ; then
_description="Unknown Device"
fi
_cap=`diskinfo ${_media} | awk '{
capacity = $3;
if (capacity >= 1099511627776) {
printf("%.1f TiB", capacity / 1099511627776.0);
} else if (capacity >= 1073741824) {
printf("%.1f GiB", capacity / 1073741824.0);
} else if (capacity >= 1048576) {
printf("%.1f MiB", capacity / 1048576.0);
} else {
printf("%d Bytes", capacity);
}}'`
VAL="${_description} -- ${_cap}"
fi
export VAL
}
disk_is_mounted()
{
local _dev
for _dev
do
if mount -v | grep -qE "^/dev/${_dev}[sp][0-9]+"
then
return 0
fi
done
return 1
}
new_install_verify()
{
local _type="$1"
shift
local _upgradetype="$1"
shift
local _disks="$*"
local _tmpfile="/tmp/msg"
cat << EOD > "${_tmpfile}"
WARNING:
EOD
if [ "$_type" = "upgrade" -a "$_upgradetype" = "inplace" ] ; then
echo "- This will install into existing zpool on ${_disks}." >> ${_tmpfile}
else
echo "- This will erase ALL partitions and data on ${_disks}." >> ${_tmpfile}
fi
cat << EOD >> "${_tmpfile}"
- You can't use ${_disks} for sharing data.
NOTE:
- Installing on flash media is preferred to installing on a
hard drive.
Proceed with the ${_type}?
EOD
_msg=`cat "${_tmpfile}"`
rm -f "${_tmpfile}"
dialog --clear --title "$AVATAR_PROJECT ${_type}" --yesno "${_msg}" 13 74
[ $? -eq 0 ] || exit 1
}
ask_upgrade()
{
local _disk="$1"
local _tmpfile="/tmp/msg"
cat << EOD > "${_tmpfile}"
Upgrading the installation will preserve your existing configuration.
Do you wish to perform an upgrade or a fresh installation on ${_disk}?
EOD
_msg=`cat "${_tmpfile}"`
rm -f "${_tmpfile}"
dialog --title "Upgrade this $AVATAR_PROJECT installation" --no-label "Fresh Install" --yes-label "Upgrade Install" --yesno "${_msg}" 8 74
return $?
}
ask_upgrade_inplace()
{
local _tmpfile="/tmp/msg"
cat << EOD > "${_tmpfile}"
User configuration settings and storage volumes are preserved and not affected by this step.\n\n
The boot device can be formatted to remove old versions, or the upgrade can be installed in a new boot environment without affecting any existing versions.
EOD
_msg=`cat "${_tmpfile}"`
rm -f "${_tmpfile}"
dialog --trim --title "Update Method Selection" --yes-label "Install in new boot environment" --no-label "Format the boot device" --yesno "${_msg}" 0 0
return $?
}
ask_boot_method()
{
# If we are not on efi, set BIOS as the default selected option
dlgflags=""
if [ "$BOOTMODE" != "UEFI" ] ; then
dlgflags="--defaultno"
fi
local _tmpfile="/tmp/msg"
cat << EOD > "${_tmpfile}"
$AVATAR_PROJECT can be booted in either BIOS or UEFI mode.
BIOS mode is recommended for legacy and enterprise hardware,
whereas UEFI may be required for newer consumer motherboards.
EOD
_msg=`cat "${_tmpfile}"`
rm -f "${_tmpfile}"
dialog ${dlgflags} --title "$AVATAR_PROJECT Boot Mode" --no-label "Boot via BIOS" --yes-label "Boot via UEFI" --yesno "${_msg}" 8 74
return $?
}
install_loader() {
local _disk _disks
local _mnt
_mnt="$1"
shift
_disks="$*"
# When doing inplace upgrades, its entirely possible we've
# booted in the wrong mode (I.E. bios/efi)
# Default to re-stamping what was already used on the current install
_boottype="$BOOTMODE"
if [ "${_upgrade_type}" = "inplace" ] ; then
if glabel list | grep -q 'efibsd' ; then
_boottype="UEFI"
else
_boottype="BIOS"
fi
fi
for _disk in $_disks
do
if [ "$_boottype" = "UEFI" ] ; then
echo "Stamping EFI loader on: ${_disk}"
mkdir -p /tmp/efi
mount -t msdosfs /dev/${_disk}p1 /tmp/efi
# Copy the .efi file
mkdir -p /tmp/efi/efi/boot
cp ${_mnt}/boot/boot1.efi /tmp/efi/efi/boot/BOOTx64.efi
umount /tmp/efi
else
echo "Stamping GPT loader on: ${_disk}"
gpart modify -i 1 -t freebsd-boot ${_disk}
chroot ${_mnt} gpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 /dev/${_disk}
fi
done
return 0
}
save_serial_settings() {
_mnt="$1"
# If the installer was booted with serial mode enabled, we should
# save these values to the installed system
USESERIAL=$((`sysctl -n debug.boothowto` & 0x1000))
if [ "$USESERIAL" -eq 0 ] ; then return 0; fi
# BIOS has vidconsole, UEFI has efi.
if [ "$BOOTMODE" = "UEFI" ] ; then
videoconsole="efi"
else
videoconsole="vidconsole"
fi
# Enable serial/internal for BSD loader
echo 'boot_multicons="YES"' >> ${_mnt}/boot/loader.conf
echo 'boot_serial="YES"' >> ${_mnt}/boot/loader.conf
echo "console=\"comconsole,${videoconsole}\"" >> ${_mnt}/boot/loader.conf
chroot ${_mnt} /usr/local/bin/sqlite3 /data/freenas-v1.db "update system_advanced set adv_serialconsole = 1"
SERIALSPEED=`kenv hw.uart.console | sed -En 's/.*br:([0-9]+).*/\1/p'`
if [ -n "$SERIALSPEED" ] ; then
echo "comconsole_speed=\"$SERIALSPEED\"" >> ${_mnt}/boot/loader.conf
chroot ${_mnt} /usr/local/bin/sqlite3 /data/freenas-v1.db "update system_advanced set adv_serialspeed = $SERIALSPEED"
fi
SERIALPORT=`kenv hw.uart.console | sed -En 's/.*io:([0-9a-fx]+).*/\1/p'`
if [ -n "$SERIALPORT" ] ; then
chroot ${_mnt} /usr/local/bin/sqlite3 /data/freenas-v1.db "update system_advanced set adv_serialport = '$SERIALPORT'"
fi
}
mount_disk() {
local _mnt
if [ $# -ne 1 ]; then
return 1
fi
_mnt="$1"
mkdir -p "${_mnt}"
mount -t zfs -o noatime freenas-boot/ROOT/${BENAME} ${_mnt}
mkdir -p ${_mnt}/data
return 0
}
create_partitions() {
local _disk="$1"
local _size=""
if [ $# -eq 2 ]; then
_size="-s $2"
fi
if gpart create -s GPT -f active ${_disk}; then
if [ "$BOOTMODE" = "UEFI" ] ; then
# EFI Mode
sysctl kern.geom.debugflags=16
sysctl kern.geom.label.disk_ident.enable=0
if gpart add -s 260m -t efi ${_disk}; then
if ! newfs_msdos -F 16 /dev/${_disk}p1 ; then
return 1
fi
fi
else
# BIOS Mode
if ! gpart add -t freebsd-boot -i 1 -s 512k ${_disk}; then
return 1
fi
fi
if is_truenas; then
gpart add -t freebsd-swap -s 16g -i 3 ${_disk}
fi
if gpart add -t freebsd-zfs -a 4k -i 2 ${_size} ${_disk}; then
return 0
fi
fi
return 1
}
get_minimum_size() {
local _min=0
local _disk
local _size
# We use 1mbyte because the fat16 partition is 512k,
# and there's some header space.
# Now we use 8MBytes because gpart and some thumb drives
# misbehave.
local _m1=$(expr 1024 \* 1024 \* 8)
# If we decide we want to round it down,
# set this to the size (eg, 256 * 1024 * 1024)
local _round=0
local _g16=$(expr 16 \* 1024 \* 1024 \* 1024)
for _disk
do
_size=""
if create_partitions ${_disk} 1>&2; then
_size=$(diskinfo ${_disk}p2 | cut -f 3)
gmirror destroy -f swap || true
gpart destroy -F ${_disk} 1>&2
fi
if [ -z "${_size}" ]; then
echo "Could not do anything with ${_disk}, skipping" 1>&2
continue
fi
if [ ${_round} -gt 0 ]; then
_size=$(expr \( ${_size} / ${_round} \) \* ${_round})
fi
_size=$(expr ${_size} / 1024)
if [ ${_min} -eq 0 -o ${_size} -lt ${_min} ]; then
_min=${_size}
fi
done
echo ${_min}k
}
partition_disk() {
local _disks _disksparts
local _mirror
local _minsize
_disks=$*
if is_truenas; then
gmirror destroy -f swap || true
fi
# Erase both typical metadata area.
for _disk in ${_disks}; do
gpart destroy -F ${_disk} >/dev/null 2>&1 || true
dd if=/dev/zero of=/dev/${_disk} bs=1m count=2 >/dev/null
dd if=/dev/zero of=/dev/${_disk} bs=1m oseek=$(diskinfo /dev/${_disk} | awk '{ print int($3/(1024*1024))-2 }') >/dev/null || true
done
_minsize=$(get_minimum_size ${_disks})
if [ "${_minsize}" = "0k" ]; then
echo "Disk is too small to install ${AVATAR_PROJECT}" 1>&2
return 1
fi
_disksparts=$(for _disk in ${_disks}; do
create_partitions ${_disk} ${_minsize} >&2
if [ "$BOOTMODE" != "UEFI" ] ; then
# Make the disk active
gpart set -a active ${_disk} >&2
fi
echo ${_disk}p2
done)
if [ $# -gt 1 ]; then
_mirror="mirror"
else
_mirror=""
fi
zpool create -f -o cachefile=/tmp/zpool.cache -o version=28 -O mountpoint=none -O atime=off -O canmount=off freenas-boot ${_mirror} ${_disksparts}
zpool set feature@async_destroy=enabled freenas-boot
zpool set feature@empty_bpobj=enabled freenas-boot
zpool set feature@lz4_compress=enabled freenas-boot
zfs set compress=lz4 freenas-boot
zfs create -o canmount=off freenas-boot/ROOT
zfs create -o mountpoint=legacy freenas-boot/ROOT/${BENAME}
return 0
}
make_swap()
{
local _swapparts
# Skip the swap creation if installing into a BE (Swap already exists in that case)
if [ "${_upgrade_type}" != "inplace" ] ; then
_swapparts=$(for _disk in $*; do echo ${_disk}p3; done)
gmirror destroy -f swap || true
gmirror label -b prefer swap ${_swapparts}
fi
echo "/dev/mirror/swap.eli none swap sw 0 0" > /tmp/data/data/fstab.swap
}
disk_is_freenas()
{
local _disk="$1"
local _rv=1
local upgrade_style=""
local os_part=""
local data_part=""
# We have two kinds of potential upgrades here.
# The old kind, with 4 slices, and the new kind,
# with two partitions.
mkdir -p /tmp/data_old
if [ -c /dev/${_disk}s4 ]; then
os_part=/dev/${_disk}s1a
data_part=/dev/${_disk}s4
upgrade_style="old"
elif [ -c /dev/${_disk}p2 ]; then
os_part=/dev/${_disk}p2
upgrade_style="new"
else
return 1
fi
if [ "${upgrade_style}" = "new" ]; then
# This code is very clumsy. There
# should be a way to structure it such that
# all of the cleanup happens as we want it to.
if zdb -l ${os_part} | grep "name: 'freenas-boot'" > /dev/null ; then
:
else
return 1
fi
zpool import -N -f freenas-boot || return 1
# Now we want to figure out which dataset to use.
DS=$(zpool list -H -o bootfs freenas-boot | head -n 1 | cut -d '/' -f 3)
if [ -z "$DS" ] ; then
zpool export freenas-boot || true
return 1
fi
# There should always be a "set default=" line in a grub.cfg
# that we created.
if [ -n "${DS}" ]; then
# Okay, mount this pool
if mount -t zfs freenas-boot/ROOT/"${DS}" /tmp/data_old; then
# If the active dataset doesn't have a database file,
# then it's not FN as far as we're concerned (the upgrade code
# will go badly).
# We also check for the Corral database directory.
if [ ! -f /tmp/data_old/data/freenas-v1.db -o \
-d /tmp/data_old/data/freenas.db ]; then
umount /tmp/data_old || true
zpool export freenas-boot || true
return 1
fi
cp -pR /tmp/data_old/data/. /tmp/data_preserved
# Don't want to keep the old pkgdb around, since we're
# nuking the filesystem
rm -rf /tmp/data_preserved/pkgdb
if [ -f /tmp/data_old/conf/base/etc/hostid ]; then
cp -p /tmp/data_old/conf/base/etc/hostid /tmp/
fi
if [ -d /tmp/data_old/root/.ssh ]; then
cp -pR /tmp/data_old/root/.ssh /tmp/
fi
if [ -d /tmp/data_old/boot/modules ]; then
mkdir -p /tmp/modules
for i in `ls /tmp/data_old/boot/modules`
do
cp -p /tmp/data_old/boot/modules/$i /tmp/modules/
done
fi
if [ -d /tmp/data_old/usr/local/fusionio ]; then
cp -pR /tmp/data_old/usr/local/fusionio /tmp/
fi
if [ -f /tmp/data_old/boot.config ]; then
cp /tmp/data_old/boot.config /tmp/
fi
if [ -f /tmp/data_old/boot/loader.conf.local ]; then
cp /tmp/data_old/boot/loader.conf.local /tmp/
fi
umount /tmp/data_old || return 1
zpool export freenas-boot || return 1
return 0
fi
fi
fi # End of if NEW upgrade style
# This is now legacy code, to support the old
# partitioning scheme (freenas-9.2 and earlier)
if ! mount "${data_part}" /tmp/data_old ; then
return 1
fi
ls /tmp/data_old > /tmp/data_old.ls
if [ -f /tmp/data_old/freenas-v1.db ]; then
_rv=0
fi
# XXX side effect, shouldn't be here!
cp -pR /tmp/data_old/. /tmp/data_preserved
umount /tmp/data_old
if [ $_rv -eq 0 ]; then
# For GUI upgrades, we only have one OS partition
# that has conf/base/etc. For ISO upgrades, we
# have two, but only one is active.
slice=$(gpart show ${_disk} | awk '/\[active\]/ { print $3 }')
if [ -z "${slice}" ]; then
# We don't have an active slice, so something is wrong.
return 1
fi
mount /dev/${_disk}s${slice}a /tmp/data_old
ls /tmp/data_old > /dev/null
if [ ! -d /tmp/data_old/conf/base/etc ]
then
# Mount the other partition
if [ "${slice}" -eq 1 ]; then
slice=2
else
slice=1
fi
umount /tmp/data_old
mount /dev/${_disk}s${slice}a /tmp/data_old
ls /tmp/data_old > /dev/null
fi
if [ -f /tmp/data_old/conf/base/etc/hostid ]; then
cp -p /tmp/data_old/conf/base/etc/hostid /tmp/
fi
if [ -d /tmp/data_old/root/.ssh ]; then
cp -pR /tmp/data_old/root/.ssh /tmp/
fi
if [ -d /tmp/data_old/boot/modules ]; then
mkdir -p /tmp/modules
for i in `ls /tmp/data_old/boot/modules`
do
cp -p /tmp/data_old/boot/modules/$i /tmp/modules/
done
fi
if [ -d /tmp/data_old/usr/local/fusionio ]; then
cp -pR /tmp/data_old/usr/local/fusionio /tmp/
fi
if [ -f /tmp/data_old/boot.config ]; then
cp /tmp/data_old/boot.config /tmp/
fi
if [ -f /tmp/data_old/boot/loader.conf.local ]; then
cp /tmp/data_old/boot/loader.conf.local /tmp/
fi
umount /tmp/data_old
fi
rmdir /tmp/data_old
return $_rv
}
prompt_password() {
local values value password="" password1 password2 _counter _tmpfile="/tmp/pwd.$$"
cat << __EOF__ > /tmp/dialogconf
bindkey formfield TAB FORM_NEXT
bindkey formfield DOWN FORM_NEXT
bindkey formfield UP FORM_PREV
bindkey formbox DOWN FORM_NEXT
bindkey formbox TAB FORM_NEXT
bindkey formbox UP FORM_PREV
__EOF__
export DIALOGRC="/tmp/dialogconf"
while true; do
dialog --insecure \
--output-fd 3 \
--visit-items \
--passwordform "Enter your root password; cancel for no root password" \
10 50 0 \
"Password:" 1 1 "" 0 20 25 50 \
"Confirm Password:" 2 1 "" 2 20 25 50 \
3> ${_tmpfile}
if [ $? -ne 0 ]; then
rm -f ${_tmpfile}
return 1
fi
{ read password1 ; read password2; } < ${_tmpfile}
rm -f ${_tmpfile}
if [ "${password1}" != "${password2}" ]; then
dialog --msgbox "Passwords do not match." 7 60 2> /dev/null
elif [ -z "${password1}" ]; then
dialog --msgbox "Empty password is not secure" 7 60 2> /dev/null
else
password="${password1}"
break
fi
done
rm -f ${DIALOGRC}
unset DIALOGRC
echo -n "${password}" 1>&2
}
create_be()
{
if [ $# -ne 1 ]; then
return 1
fi
echo "Creating new Boot-Environment"
# When upgrading, we will simply create a new BE dataset and install
# fresh into that, so old datasets are not lost
zpool import -N -f freenas-boot || return 1
# Create the new BE
zfs create -o mountpoint=legacy freenas-boot/ROOT/${BENAME} || return 1
# Mount the new BE datasets
mkdir -p ${1}
mount -t zfs freenas-boot/ROOT/${BENAME} ${1} || return 1
mkdir -p ${1}/data
return 0
}
menu_install()
{
local _action
local _disklist
local _tmpfile
local _answer
local _cdlist
local _items
local _disk
local _disks=""
local _realdisks=""
local _disk_old
local _config_file
local _desc
local _list
local _msg
local _satadom
local _i
local _do_upgrade=""
local _menuheight
local _msg
local _dlv
local _password
local os_part
local data_part
local upgrade_style="new"
local whendone=""
local readonly CD_UPGRADE_SENTINEL="/data/cd-upgrade"
local readonly NEED_UPDATE_SENTINEL="/data/need-update"
# create a sentinel file for post-fresh-install boots
local readonly FIRST_INSTALL_SENTINEL="/data/first-boot"
local readonly TRUENAS_EULA_PENDING_SENTINEL="/data/truenas-eula-pending"
local readonly POOL="freenas-boot"
_tmpfile="/tmp/answer"
TMPFILE=$_tmpfile
REALDISKS="/tmp/realdisks"
while getopts "U:P:X:" opt; do
case "${opt}" in
U) if ${OPTARG}; then _do_upgrade=1 ; else _do_upgrade=0; fi
;;
P) _password="${OPTARG}"
;;
X) case "${OPTARG}" in
reboot) whendone=reboot ;;
"wait") whendone=wait ;;
halt) whendone=halt ;;
*) whendone="" ;;
esac
;;
*) echo "Unknown option ${opt}" 1>&2
;;
esac
done
shift $((OPTIND-1))
if [ $# -gt 0 ]
then
_disks="$@"
INTERACTIVE=false
else
INTERACTIVE=true
fi
if ${INTERACTIVE}; then
pre_install_check || return 0
fi
if do_sata_dom
then
_satadom="YES"
else
_satadom=""
if ${INTERACTIVE}; then
get_physical_disks_list
_disklist="${VAL}"
_list=""
_items=0
for _disk in ${_disklist}; do
get_media_description "${_disk}"
_desc="${VAL}"
_list="${_list} ${_disk} '${_desc}' off"
_items=$((${_items} + 1))
done
_tmpfile="/tmp/answer"
if [ ${_items} -ge 10 ]; then
_items=10
_menuheight=20
else
_menuheight=9
_menuheight=$((${_menuheight} + ${_items}))
fi
if [ "${_items}" -eq 0 ]; then
# Inform the user
eval "dialog --title 'Choose destination media' --msgbox 'No drives available' 5 60" 2>${_tmpfile}
return 0
fi
eval "dialog --title 'Choose destination media' \
--checklist 'Select one or more drives where $AVATAR_PROJECT should be installed (use arrow keys to navigate to the drive(s) for installation; select a drive with the spacebar).' \
${_menuheight} 60 ${_items} ${_list}" 2>${_tmpfile}
[ $? -eq 0 ] || exit 1
fi
fi # ! do_sata_dom
if [ -f "${_tmpfile}" ]; then
_disks=$(eval "echo `cat "${_tmpfile}"`")
rm -f "${_tmpfile}"
fi
if [ -z "${_disks}" ]; then
${INTERACTIVE} && dialog --msgbox "You need to select at least one disk!" 6 74
exit 1
fi
if disk_is_mounted ${_disks} ; then
${INTERACTIVE} && dialog --msgbox "The destination drive is already in use!" 6 74
exit 1
fi
_action="installation"
_upgrade_type="format"
# This needs to be re-done.
# If we're not interactive, then we have
# to assume _disks is correct.
# If we do have more than one disk given,
# we should also do something if they're all
# freenas disks. But how do we figure out which
# one to use? The current code in disk_is_freenas
# is very, very heavy -- it actually backs up the
# data from a freenas installation. It also does
# a zpool import.
for _disk in ${_disks}; do
if disk_is_freenas ${_disk} ; then
if ${INTERACTIVE}; then
if ask_upgrade ${_disk} ; then
_do_upgrade=1
_action="upgrade"
fi
else
if [ "${_do_upgrade}" != "0" ]; then
_do_upgrade=1
_action="upgrade"
fi
fi
if [ -c /dev/${_disk}s4 ]; then
upgrade_style="old"
elif [ -c /dev/${_disk}p2 ]; then
upgrade_style="new"
else
echo "Unknown upgrade style" 1>&2
exit 1
fi
# Ask if we want to do a format or inplace upgrade
if ${INTERACTIVE}; then
if ask_upgrade_inplace ; then
_upgrade_type="inplace"
fi
fi
break
elif [ "${_satadom}" = "YES" -a -c /dev/ufs/TrueNASs4 ]; then
# Special hack for USB -> DOM upgrades
_disk_old=`glabel status | awk '/ ufs\/TrueNASs4 / { print $3 }' | sed -e 's,s4$,,g'`
if disk_is_freenas ${_disk_old} ; then
if ask_upgrade ${_disk_old} ; then
_do_upgrade=2
_action="upgrade"
break
fi
fi
fi
done
# If we haven't set _do_upgrade by now, we're not
# doing an upgrade.
if [ -z "${_do_upgrade}" ]; then
_do_upgrade=0
fi
if [ "${_satadom}" = "YES" -a -n "$(echo ${_disks}|grep "raid/")" ]; then
_realdisks=$(cat ${REALDISKS})
else
_realdisks=$_disks
fi
${INTERACTIVE} && new_install_verify "$_action" "$_upgrade_type" ${_realdisks}
_config_file="/tmp/pc-sysinstall.cfg"
if ${INTERACTIVE} && [ "${_do_upgrade}" -eq 0 ]; then
prompt_password 2> /tmp/password
if [ $? -eq 0 ]; then
_password="$(cat /tmp/password 2> /dev/null)"
fi
fi
if [ ${_do_upgrade} -eq 0 ]; then
# With the new partitioning, disk_is_freenas may
# copy /data. So if we don't need it, remove it,
# or else it'll do an update anyway. Oops.
rm -rf /tmp/data_preserved
fi
# Start critical section.
if ${INTERACTIVE}; then
trap "set +x; read -p \"The $AVATAR_PROJECT $_action on ${_realdisks} has failed. Press enter to continue.. \" junk" EXIT
else
# trap "echo \"The ${AVATAR_PROJECT} ${_action} on ${_realdisks} has failed.\" ; sleep 15" EXIT
trap "set +x; read -p \"The $AVATAR_PROJECT $_action on ${_realdisks} has failed. Press enter to continue.. \" junk" EXIT
fi
set -e
# set -x
# _disk, _image, _config_file
# we can now build a config file for pc-sysinstall
# build_config ${_disk} "$(get_image_name)" ${_config_file}
if [ ${_do_upgrade} -eq 1 ]
then
/etc/rc.d/dmesg start
mkdir -p /tmp/data
if [ "${upgrade_style}" = "old" ]; then
# For old style, we have two potential
# partitions to look at: s1a and s2a.
#
slice=$(gpart show ${_disk} | awk '/\[active\]/ { print $3 }')
if [ -z "${slice}" ]; then
# We don't have an active slice, so something is wrong.
false
fi
mount /dev/${_disk}s${slice}a /tmp/data
ls /tmp/data > /dev/null
if [ ! -d /tmp/data/conf/base/etc ]