-
Notifications
You must be signed in to change notification settings - Fork 2
/
nomadbsd-install.in
executable file
·818 lines (761 loc) · 20.7 KB
/
nomadbsd-install.in
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
#!/bin/sh
#
# Copyright (c) 2019 The NomadBSD Project. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# vim: set tabstop=4:
arch=$(sysctl -n hw.machine_arch)
version=$(cat /VERSION)
efipartsz="@EFI_PART_SIZE@"
DESTDIR="/tmp/nbsdinstaller.$$"
rootfs_label="nbsdrootfs"
swap_label="nbsdswap"
POOL="zroot"
PATH_SDDM_AUTOLOGIN="${DESTDIR}/usr/local/etc/sddm.conf.d/autologin.conf"
LOGFILE="/root/nomadbsd-installer.$(date '+%Y-%m-%d_%H:%M:%S')"
fstab_ufs() {
cat <<FSTAB_END
/dev/gpt/${rootfs_label} / ufs rw,noatime 1 1
tmpfs /tmp tmpfs rw,mode=1777 0 0
proc /proc procfs rw 0 0
FSTAB_END
}
fstab_zfs() {
cat <<FSTAB_END
tmpfs /tmp tmpfs rw,mode=1777,late 0 0
proc /proc procfs rw 0 0
FSTAB_END
}
sddm_autologin_file() {
local username=$1
cat << SDDM_AUTOLOGIN_END
[Autologin]
User=${username}
Session=xfce
SDDM_AUTOLOGIN_END
}
tee_logger()
{
local logfile=$1
awk -v logfile="${logfile}" '{
if ($0 ~ /cpio: / || $0 ~ /^find: /) {
s = $0;
i = match(s, "cpio: ");
if (i != 0)
s = substr(s, i);
print s >> logfile;
print s > "/dev/stderr"
} else
print $0
}'
}
cleanup() {
! (mount | grep -q ${DESTDIR}) && return
if [ ${fstype} = "ZFS" ]; then
zpool export ${POOL} || zpool export -f ${POOL}
else
umount ${DESTDIR} || umount -f ${DESTDIR}
fi
}
bail() {
echo "nomadbsd-install: $1" >&2
cleanup
exit 1
}
status() {
echo "!$1"
[ -n "${dummy}" ] && sleep 5
}
progress() {
echo "%$1"
[ -n "${dummy}" ] && sleep 1
}
show_extract() {
awk -v nfiles=$1 ' {
if (nfiles <= 0)
exit;
print "%0";
fflush();
while (getline > 0) {
if (($0 !~ /^\//) && ($0 !~ /^.\//))
continue;
p = int(++n * 100 / nfiles);
if (p == lastp)
continue;
lastp = p;
print "%" p;
fflush();
}
}'
}
#
# Traverses through the given directory and prints
# each filename to stdout, unless it matches any of
# the patterns read from stdin.
#
exclfind() {
local dir=$1
local args=$(tr ' \t' '\n\n' | awk '
BEGIN { printf("\\( "); }
{
if ($0 !~ /^$/) {
if (n++ > 0)
printf(" -or -path \"%s\" ", $0)
else
printf("-path \"%s\"", $0)
}
}
END { print "\\)" }
')
eval find "$dir" \
'\(' -type f -or -type d -or -type l -or -type p '\)' -and -not $args
}
get_ufs_rootdev()
{
local rootdev
rootdev=$(df / | tail -n 1 | awk '{ sub("/dev/", "", $1); print $1 }')
glabel status | grep $rootdev | awk -F" " '{ print $3 }' | \
sed -E 's/(.*)[sp][0-9][a-z]*/\1/'
}
get_zfs_rootdev()
{
local cmd="zpool list -H -v"
local pool=$(mount -p | awk '$2 == "/" {print $1}' | cut -d/ -f1)
local device=$(${cmd} | awk -v pool=${pool} -F"[\t ]+" '
BEGIN {
pool_pattern = sprintf("^%s[[:space:]]+", pool);
found_pool = 0
}
{
if (/^[^[:space:]]/) {
if ($0 ~ pool_pattern)
found_pool = 1
else
found_pool = 0
} else if (found_pool) {
if ($0 ~ /^[[:space:]]+[^[:space:]]+/) {
print($2); exit(0)
}
}
}
')
[ -z "${device}" ] && return
device=$(echo ${device} | sed -E 's#^/+dev/+(.*)$#\1#')
if (echo ${device} | egrep -q '^(gpt|label|gptid)'); then
disk_dev=$(glabel status | awk -v label=${device} -F"[\t ]+" '
BEGIN {
pattern = sprintf("^[[:space:]]+%s[[:space:]]", label);
}
$0 ~ pattern {
print($NF); exit(0)
}
')
[ -z "${disk_dev}" ] && return
device=${disk_dev}
fi
echo ${device} | sed -E 's#([a-zA-Z0-9]+)[sp]([0-9]{1,})[a-f]?$#\1#'
}
root_is_zfs()
{
[ $(mount -p | awk '$2 == "/" {print $3}') = "zfs" ]
}
getrootdev()
{
if root_is_zfs; then
get_zfs_rootdev
else
get_ufs_rootdev
fi
}
devlist() {
local rootdev=$(getrootdev)
local list=$(camcontrol devlist | awk -F " " '{
split($0, a, "[<>]"); descr = a[2];
n = split($NF, a, "[,()]");
for (i = 2; i <= n; i++) {
if (a[i] ~ /(ada|da)[0-9]/) {
printf("%s|%s\n", a[i], descr);
}
}
}';
nvmecontrol devlist | awk '/^[ \t]*nvme[0-9]:/ {
n = split($0, a, ":");
sub(/^[ \t]*nvme/, "nvd", a[1]);
sub(/^[ \t]*/, "", a[2]);
printf("%s|%s\n", a[1], a[2]);
}'
)
if [ -n "${rootdev}" ]; then
echo "$list" | grep -v "^${rootdev}"
else
echo "$list"
fi
}
#
# Returns the index of the last partition
#
get_part_idx() {
local disk=$1
gpart show ${disk} | sed '/^$/d' | tail -2 | head -1 | \
awk -F '[\t ]+' '{ print $4 }'
}
newfswrp() {
fifo="/tmp/newfs.fifo"
[ ! -f "${fifo}" ] && rm -f "${fifo}"
mkfifo "${fifo}"
$1 > "${fifo}"&
pid=$!
echo "%0"
cat "${fifo}" | awk '{
while (getline > 0) {
if ($0 ~ /^\tusing.*/) {
n = split($0, a, " ");
if (n < 2 || a[2] <= 0) {
print "ERROR" | "cat >&2"
exit(1);
}
ngroups = a[2];
} else if ($0 ~ /^super-block backups.*at:$/) {
RS=" ";
getblocks = 1;
} else if (getblocks == 1) {
if ($0 !~ /^[0-9]+/)
continue;
p = int(++gc * 100 / ngroups);
print "%" p;
fflush();
}
}
}'
wait $pid
retval=$?
rm -f "${fifo}"
[ $retval -ne 0 ] && bail "Command '$1' failed"
}
mkufs() {
status "Creating UFS filesystem ..."
newfswrp "newfs -t -U $1"
status "Mounting $1 on ${DESTDIR} ..."
mount "$1" ${DESTDIR} || bail "Failed to mount $1"
}
zfs_create() {
eval dataset=\${$#}
zfs create $* || bail "Couldn't create dataset/filesystem ${dataset}"
}
mkzfs() {
zpool create -o altroot=${DESTDIR} ${POOL} $1 || \
bail "Couldn't create pool ${POOL}"
zfs set compress=on ${POOL}
zfs set atime=off ${POOL}
zfs_create -o mountpoint=none ${POOL}/ROOT
zfs_create -o mountpoint=none ${POOL}/ROOT/default
mount -t zfs ${POOL}/ROOT/default ${DESTDIR}
zfs_create -o canmount=off -o mountpoint=/usr ${POOL}/usr
zfs_create ${POOL}/usr/home
zfs_create -o exec=off -o setuid=off ${POOL}/usr/src
zfs_create ${POOL}/usr/obj
zfs_create -o mountpoint=/usr/ports -o setuid=off \
${POOL}/usr/ports
zfs_create -o exec=off -o setuid=off -o compression=off \
${POOL}/usr/ports/distfiles
zfs_create -o exec=off -o setuid=off -o compression=off \
${POOL}/usr/ports/packages
zfs_create -o canmount=off -o mountpoint=/var ${POOL}/var
zfs_create -o exec=off -o setuid=off ${POOL}/var/audit
zfs_create -o exec=off -o setuid=off ${POOL}/var/crash
zfs_create -o exec=off -o setuid=off ${POOL}/var/log
zfs_create -o atime=on -o exec=off -o setuid=off \
${POOL}/var/mail
zfs_create -o exec=on -o setuid=off ${POOL}/var/tmp
ln -s /usr/home ${DESTDIR}/home
ln -s /usr/src/sys ${DESTDIR}/sys
chmod 1777 ${DESTDIR}/var/tmp
zpool set bootfs=${POOL}/ROOT/default ${POOL}
}
wipe_disk() {
local disk=$1
# Wipe ZFS metadata
status "Wiping old ZFS metadata ..."
for d in $(ls /dev/${disk}* | sort -r); do
zpool labelclear -f "$d"
done
status "Destroying old GPT partition scheme ..."
gpart destroy -F ${disk}
# Zero first and last 100 sectors
dd if=/dev/zero of=/dev/${disk} count=100
skipbs=$(geom disk list ${disk} | awk -F " " '/Mediasize:/ {
skipbs = int($2 / 512) - 100;
print skipbs
}')
dd if=/dev/zero of=/dev/${disk} bs=512 count=100 oseek=$skipbs
}
mkefi() {
local disk=$1
local idx
local EFIDIR="BOOT"
gpart add -a 4k -t efi -l gpefiboot -s ${efipartsz} ${disk} || \
bail "Couldn't create EFI partition"
idx=$(get_part_idx ${disk})
newfs_msdos -F 32 -c 1 /dev/${disk}p${idx} || \
bail "Couldn't create MSDOSFS on /dev/${disk}p${idx}"
mount -t msdosfs /dev/${disk}p${idx} ${DESTDIR} || bail
mkdir -p ${DESTDIR}/EFI/${EFIDIR} || \
bail "Couldn't create directory ${DESTDIR}/EFI/${EFIDIR}"
cp /boot/loader.efi ${DESTDIR}/EFI/${EFIDIR}/BOOTx64.EFI || \
bail "Couldn't copy /boot/boot1.efi to ${DESTDIR}/EFI/${EFIDIR}"
# Use OEM string of HDD/SSD as UEFI variable label
oem=$(devlist | grep ${disk} | cut -d'|' -f 2)
if [ -z "${oem}" ]; then
# Could not get OEM string. Use an alternative string
efilabel="NomadBSD-${version}"
else
efilabel="NomadBSD:${oem}"
fi
# Remove duplicated entries
efibootmgr | awk -F" " -v efilabel="${efilabel}" '
$0 ~ efilabel && $1 ~ /Boot[0-9]+/ {
bootvars[nb++] = substr($1, 5, 4)
}
END {
for (i = 0; i < nb; i++) {
cmd = sprintf("efibootmgr -B -b %s", bootvars[i]);
system(cmd);
}
}
'
bvar=$(efibootmgr -c -l ${DESTDIR}/EFI/${EFIDIR}/BOOTx64.EFI \
-L "${efilabel}" | grep -w "${efilabel}" | sort | tail -1 | \
sed -E 's/.*Boot([0-9]+).*/\1/')
if [ -n "${bvar}" ]; then
efibootmgr -a -b ${bvar}
fi
umount ${DESTDIR} || umount -f ${DESTDIR}
}
mkrootfs() {
local disk=$1
local idx=$2
if [ ${fstype} = "UFS" ]; then
gpart add -a 1m -t freebsd-ufs -l ${rootfs_label} ${disk} || \
bail "Couldn't create partition"
sleep 5
mkufs "/dev/${disk}p${idx}"
else
gpart add -a 1m -t freebsd-zfs -l ${rootfs_label} ${disk} || \
bail "Couldn't create partition"
sleep 5
mkzfs "/dev/gpt/${rootfs_label}"
fi
}
install_bootcode() {
local disk=$1
local idx=$(get_part_idx ${disk})
status "Installing boot code ..."
if [ ${fstype} = "UFS" ]; then
partcode="/boot/gptboot"
else
partcode="/boot/gptzfsboot"
fi
gpart bootcode -b /boot/pmbr -p ${partcode} \
-i ${idx} ${disk} || bail "Couldn't install boot code"
}
part_disk() {
local disk=$1
local idx
status "Creating GPT partition scheme ..."
gpart create -s gpt ${disk} || bail "Couldn't create GPT partition scheme"
status "Creating boot partition ..."
gpart add -a 4k -t freebsd-boot -l gpboot -b 40 -s 512K ${disk} || \
bail "Couldn't create partition"
install_bootcode ${disk}
if [ "${arch}" = "amd64" ]; then
status "Creating EFI partition ..."
mkefi ${disk}
fi
if [ -n "${lenovofix}" ]; then
status "Setting lenovofix ..."
gpart set -a lenovofix ${disk}
fi
if [ ${swapsize} -gt 0 ]; then
status "Creating swap partition ..."
gpart add -a 1m -t freebsd-swap -l ${swap_label} \
-s ${swapsize}M ${disk} || \
bail "Couldn't create swap partition"
fi
status "Creating partition for / ..."
idx=$(get_part_idx ${disk})
idx=$((idx + 1))
mkrootfs ${disk} ${idx}
}
sddm_enabled() {
[ $(sysrc -n sddm_enable) = "YES" ]
}
set_sddm_autologin() {
if [ ${autologin:=0} -eq 1 ]; then
mkdir -p "$(dirname "${PATH_SDDM_AUTOLOGIN}")"
sddm_autologin_file ${username} > "${PATH_SDDM_AUTOLOGIN}"
else
rm -f "${PATH_SDDM_AUTOLOGIN}"
fi
}
set_slim_autologin() {
path_slim="${DESTDIR}/usr/local/etc/slim.conf"
awk -v user=$username -v autologin=${autologin:=0} '
! /^default_user/ && ! /^auto_login/ {
print $0;
}
END {
if (autologin) {
printf("default_user%+11s\n", user);
printf("auto_login%+13s\n", "yes");
}
}' < $path_slim > $path_slim.tmp
mv $path_slim.tmp $path_slim
}
set_autologin() {
if sddm_enabled; then
set_sddm_autologin
else
set_slim_autologin
fi
}
pkgwrp() {
if ! (mount | grep ${DESTDIR}/dev); then
chroot ${DESTDIR} sh -c 'mount -t devfs devfs /dev'
fi
pkg -c ${DESTDIR} $* 2>> ${LOGFILE}
umount ${DESTDIR}/dev || umount -f ${DESTDIR}/dev
}
install_gfxdrv() {
local kmods=$(sysrc -n initgfx_kmods)
local path_rc_conf="${DESTDIR}/etc/rc.conf"
if [ -z "${kmods}" ]; then
kmods=$(cat /var/run/gfxdriver)
[ -z "${kmods}" ] && return
fi
if (echo ${kmods} |grep -qE '.*/nvidia/([0-9]+|latest)/.*'); then
nvidia_version=$(echo ${kmods} | \
sed -E 's/.*nvidia\/([0-9]+|latest)\/.*/\1/')
if [ "${nvidia_version}" = "latest" ]; then
status "Installing nvidia-driver"
pkgwrp install -y nvidia-driver || return
else
status "Installing nvidia-driver-${nvidia_version}"
pkgwrp install -y "nvidia-driver-${nvidia_version}" || return
fi
if [ "${nvidia_version}" = "latest" ] ||
[ "${nvidia_version}" -ge 390 ]; then
sysrc -f $path_rc_conf kld_list+=" /boot/modules/nvidia-modeset.ko"
fi
sysrc -f $path_rc_conf kld_list+=" /boot/modules/nvidia.ko"
sysrc -f $path_rc_conf initgfx_enable=NO
rm -rf "${DESTDIR}/nvidia"
rm -rf "${DESTDIR}/usr/local/nvidia"
return
fi
case "${kmods}" in
*radeon*)
sysrc -f $path_rc_conf kld_list+=" /boot/modules/radeonkms.ko" ;;
*amdgpu*)
sysrc -f $path_rc_conf kld_list+=" /boot/modules/amdgpu.ko" ;;
*i915*)
sysrc -f $path_rc_conf kld_list+=" /boot/modules/i915kms.ko" ;;
*)
return ;;
esac
sysrc -f $path_rc_conf initgfx_enable=NO
rm -rf "${DESTDIR}/nvidia"
rm -rf "${DESTDIR}/usr/local/nvidia"
}
replace_user() {
status "Changing username ..."
sed -i .bak -E "s/[[:<:]]nomad[[:>:]]/${username}/g" ${DESTDIR}/etc/master.passwd
sed -i .bak -E "s/[[:<:]]nomad[[:>:]]/${username}/g" ${DESTDIR}/etc/group
sed -i .bak -E '/^geli.*/d' ${DESTDIR}/etc/rc.conf
status "Counting config files ..."
progress -1
nfiles=$(find ${DESTDIR}/usr/home/nomad/ -type f -exec file {} \; | grep text | wc -l)
status "Replacing nomad by ${username} in config files ..."
find ${DESTDIR}/usr/home/nomad/ -type f | while read i; do
if (file "$i" | grep -q text); then
n=$(($n + 1))
p=$(($n * 100 / $nfiles))
progress $p
sed -i '' -E "s/[[:<:]]nomad[[:>:]]/${username}/g" "$i"
fi
done
sed -i '' -E "s/[[:<:]]nomad[[:>:]]/${username}/g" \
${DESTDIR}/usr/local/etc/sudoers
status "Moving ${DESTDIR}/usr/home/nomad to ${DESTDIR}/usr/home/$username ..."
mv ${DESTDIR}/usr/home/nomad ${DESTDIR}/usr/home/${username}
status "Running pwd_mkdb on /etc/master.passwd ..."
chroot ${DESTDIR} sh -c '/usr/sbin/pwd_mkdb -p /etc/master.passwd'
}
copy_home() {
local exlst="
/data
/data/*
*/.gvfs.*
"
if [ ! -d ${DESTDIR}/usr/home ]; then
mkdir ${DESTDIR}/usr/home || \
bail "Couldn't create ${DESTDIR}/usr/home"
fi
if [ ! -L ${DESTDIR}/home ]; then
rm -rf ${DESTDIR}/home
ln -s /usr/home ${DESTDIR}/home
fi
if [ -d "/data/home" ]; then
homedir="/data/home"
else
homedir="/home"
fi
status "Counting files to copy from ${homedir} ..."
progress -1
nfiles=$(cd ${homedir} && echo "${exlst}" | \
exclfind . 2>/dev/null | wc -l | tr -d ' ')
status "Copying files from ${homedir} ..."
(cd ${homedir} && echo "${exlst}" | \
exclfind . | cpio -vpdmu ${DESTDIR}/usr/home 2>&1) | \
tee_logger ${LOGFILE} | show_extract $nfiles
rm -rf ${DESTDIR}/usr/home/nomad/.config/DSB/dsblogoutmgr
unlink ${DESTDIR}/usr/home/nomad/.cache
}
copy_base() {
local exlst="
${DESTDIR}
${DESTDIR}/*
/dev/
/dev/*
/mnt
/mnt/*
/uzip
/uzip/*
/data
/data/*
/unionfs
/unionfs/*
/home
/home/*
/nomadbsd-backup.tar.xz
/etc/rc.d/mount_uzip
/media
/media/*
/tmp
/tmp/*
/usr/share/applications/nomadbsd-install-gui.desktop
"
local pseudofs=$(mount | awk -F"[\t (),]+" '
/procfs/ || /linprocfs/ || /linsysfs/ || /devfs/ || /tmpfs/ {
print $3
}
')
for i in ${pseudofs}; do
exlst="${exlst} $i"
exlst="${exlst} $i/*"
done
status "Counting files to copy ..."
progress -1
nfiles=$(echo "${exlst}" | exclfind / 2>/dev/null | wc -l | tr -d ' ')
status "Copying files ..."
echo "${exlst}" | \
exclfind / | cpio -vpdmu ${DESTDIR} 2>&1 | \
tee_logger ${LOGFILE} | show_extract $nfiles
cp -a /usr/local/share/applications/* ${DESTDIR}/usr/local/share/applications/
status "Creating ${DESTDIR}/dev ..."
mkdir ${DESTDIR}/dev
status "Creating ${DESTDIR}/proc ..."
mkdir ${DESTDIR}/proc
status "Creating ${DESTDIR}/media ..."
mkdir ${DESTDIR}/media
status "Creating ${DESTDIR}/tmp ..."
mkdir ${DESTDIR}/tmp
chmod a=rwxt ${DESTDIR}/tmp
for i in ${pseudofs}; do
mkdir -p "${DESTDIR}/$i" >/dev/null 2>> ${LOGFILE}
done
status "Replacing ${DESTDIR}/usr/local/etc by ${DESTDIR}/usr.local.etc ..."
unlink ${DESTDIR}/usr/local/etc
mv ${DESTDIR}/usr.local.etc ${DESTDIR}/usr/local/etc
if [ -h ${DESTDIR}/var/db/portsnap ]; then
unlink ${DESTDIR}/var/db/portsnap
fi
if [ ! -d ${DESTDIR}/var/db/portsnap ]; then
status "Creating ${DESTDIR}/var/db/portsnap"
mkdir ${DESTDIR}/var/db/portsnap
fi
if [ -L ${DESTDIR}/etc/wpa_supplicant.conf ]; then
unlink ${DESTDIR}/etc/wpa_supplicant.conf
fi
if [ -L ${DESTDIR}/etc/ppp ]; then
unlink ${DESTDIR}/etc/ppp
fi
(cd /data/etc && tar cf - .) | (cd ${DESTDIR}/etc && tar xmf -)
status "Creating ${DESTDIR}/mnt ..."
mkdir ${DESTDIR}/mnt
}
commit() {
if [ ! -d ${DESTDIR} ]; then
mkdir -p ${DESTDIR} || bail "Couldn't create ${DESTDIR}"
fi
echo "Log started on $(date)" > ${LOGFILE}
wipe_disk ${disk}
part_disk ${disk}
copy_base
copy_home
replace_user
status "Writing fstab ..."
if [ ${fstype} = "UFS" ]; then
fstab_ufs > ${DESTDIR}/etc/fstab
else
fstab_zfs > ${DESTDIR}/etc/fstab
fi
if [ ${swapsize} -gt 0 ]; then
status "Adding swap entry to fstab ..."
printf "%s\tswap\tswap\tsw\t0\t0\n" "/dev/gpt/${swap_label}" \
>> ${DESTDIR}/etc/fstab
fi
if [ ${fstype} = "ZFS" ]; then
echo zfs_load=\"YES\" >> ${DESTDIR}/boot/loader.conf
fi
sysrc -f ${DESTDIR}/etc/rc.conf -x gsched_enable
sysrc -f ${DESTDIR}/etc/rc.conf -x local_startup
sed -E -i '' '/^\/etc\/rc.d\/mount_uzip.*$/d' ${DESTDIR}/etc/rc.shutdown
sed -i '' 's#^PKG_CACHEDIR.*$#PKG_CACHEDIR = "/var/cache/pkg";#' \
${DESTDIR}/usr/local/etc/pkg.conf
if [ ${fstype} = "ZFS" ]; then
sysrc -f ${DESTDIR}/etc/rc.conf zfs_enable=YES
fi
for i in setup install; do
rm -f "${DESTDIR}/usr/libexec/nomadbsd-$i"
rm -f "${DESTDIR}/usr/bin/nomadbsd-$i-gui"
done
rm -f ${DESTDIR}/usr/local/share/applications/nomadbsd-install-gui.desktop
set_autologin
# Enable suspend, hibernate, and hybrid sleep.
for s in suspend hibernate hybridsleep; do
cp "${DESTDIR}"/usr/local/lib/ConsoleKit/scripts/ck-system-$s.sample \
"${DESTDIR}"/usr/local/lib/ConsoleKit/scripts/ck-system-$s
done
status "Updating pkg repository catalogues ..."
pkg --chroot ${DESTDIR} update -f -q 2>> ${LOGFILE}
status "Installing graphics driver"
install_gfxdrv
status "Unmounting ${DESTDIR} ..."
cleanup
status "Done!"
}
dummy_commit() {
status "Creating GPT partition scheme ..."
status "Creating boot partition ..."
status "Installing boot code ..."
status "Creating EFI partition ..."
[ -n "${lenovofix}" ] && status "Setting lenovofix ..."
status "Creating swap partition ..."
status "Creating partition for / ..."
status "Creating UFS filesystem ..."
status "Mounting /dev/${disk}p$idx on ${DESTDIR} ..."
status "Copying files ..."
status "Replacing ${DESTDIR}/usr/local/etc by ${DESTDIR}/usr.local.etc ..."
status "Creating ${DESTDIR}/mnt ..."
status "Writing fstab ..."
status "Adding swap entry to fstab ..."
status "Changing username ..."
status "Replacing nomad by $username in config files ..."
status "Moving ${DESTDIR}/home/nomad to ${DESTDIR}/home/${username} ..."
status "Running pwd_mkdb on /etc/master.passwd ..."
status "Unmounting ${DESTDIR} ..."
status "Done!"
}
usage() {
echo "Usage: $(basename "$0") devlist"
echo " $(basename "$0") commit [-n] -u user -f fs -s swap -d dev"
echo "Options"
echo "-n Dummy mode. Do not write anything to disk."
echo "-d Define target disk."
echo "-f Define filesystem type. Possible values: UFS or ZFS"
echo "-s Define size of the swap partition in MB. If set to 0,"
echo " no swap partition will be created."
echo "-u Define the username to replace nomad."
echo "-l Set lenovofix."
echo "-a Auto-login user (Slim)."
exit 1
}
[ -z "${version}" ] && bail "NomadBSD version not set. Check /VERSION"
[ $# -lt 1 ] && usage
case $1 in
-*)
usage
;;
esac
if [ $1 = "devlist" ]; then
devlist
elif [ $1 = "commit" ]; then
shift
while [ $# -gt 0 ]; do
case $1 in
-n)
dummy=1
;;
-u)
username=$2
shift
;;
-s)
swapsize=$2
shift
;;
-d)
disk=$2
shift
;;
-l)
lenovofix=1
;;
-f)
fstype=$2
if [ $fstype != "UFS" -a $fstype != "ZFS" ]; then
bail "Unknown filesystem type '$fstype'"
fi
shift
;;
-a)
autologin=1
;;
-*)
usage
;;
esac
shift
done
[ -z "$username" ] && bail "Username not defined"
[ -z "$swapsize" ] && bail "Swap size not defined"
[ -z "$disk" ] && bail "Target device not defined"
[ -z "$fstype" ] && bail "Filesystem type not defined"
if [ -n "$dummy" ]; then
dummy_commit
else
commit
fi
else
bail "Unknown command '$1'"
fi
exit 0