-
Notifications
You must be signed in to change notification settings - Fork 0
/
beadm
executable file
·962 lines (925 loc) · 29.3 KB
/
beadm
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
#!/bin/bash -e
# Copyright (c) 2012-2013 Slawomir Wojciech Wojtczak (vermaden)
# Copyright (c) 2012-2013 Bryan Drewery (bdrewery)
# Copyright (c) 2012-2013 Mike Clarke (rawthey)
# Copyright (c) 2013 Dancho Penev (dpslavov)
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that 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 AND CONTRIBUTORS '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 OR CONTRIBUTORS 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.
# 2014/12/04 - Ported to Linux - Andrew Barnes (b333z)
unset LC_ALL
unset LANG
PATH=${PATH}:/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin
#if [ $( uname -r | cut -d '-' -f1 | cut -d '.' -f1 ) -lt 8 ]
#then
# echo "ERROR: beadm works on FreeBSD 8.0 or later"
# exit 1
#fi
if [ "${1}" = "--version" -o "${1}" = "version" ]
then
echo "beadm 1.2.5 (2016-06-22)"
exit 0
fi
__usage() {
local NAME=${0##*/}
echo "usage:"
echo " ${NAME} activate <beName>"
echo " ${NAME} create [-e nonActiveBe | -e beName@snapshot] <beName>"
echo " ${NAME} create <beName@snapshot>"
echo " ${NAME} destroy [-F] <beName | beName@snapshot>"
echo " ${NAME} list [-a] [-s] [-D] [-H]"
echo " ${NAME} rename <origBeName> <newBeName>"
echo " ${NAME} mount <beName> [mountpoint]"
echo " ${NAME} { umount | unmount } [-f] <beName>"
exit 1
}
# check if system has a grub.cfg file and update it
__update_grub() {
if [ -e /boot/grub/grub.cfg ]
then
if grub-mkconfig -o /boot/grub/grub.cfg 1> /dev/null 2> /dev/null
then
echo "GRUB configuration updated successfully"
else
echo "WARNING: Failed to update GRUB configuration"
fi
fi
}
# check if boot environment exists
__be_exist() { # 1=DATASET
if ! zfs list -H -o name ${1} 1> /dev/null 2> /dev/null
then
echo "ERROR: Boot environment '${1##*/}' does not exist"
exit 1
fi
}
# check if argument is a snapshot
__be_snapshot() { # 1=DATASET/SNAPSHOT
echo "${1}" | grep -q "@" 2> /dev/null
}
# check if boot environment is mounted
__be_mounted() { # 1=BE
findmnt --first-only --source "${1}" > /dev/null
}
# find mount point of boot environment
__be_findmnt() { # 1=BE
echo declare $(findmnt -fPS "${1}")\; echo \$TARGET | sh
}
# check if boot environment is a clone
__be_clone() { # 1=DATASET
if zfs list ${1} 1> /dev/null 2> /dev/null
then
local ORIGIN="$( zfs list -H -o origin ${1} )"
if [ "${ORIGIN}" = "-" ]
then
# boot environment is not a clone
return 1
else
# boot environment is a clone
return 0
fi
else
# boot environment does not exist
return 2
fi
}
# create new boot environment
__be_new() { # 1=SOURCE 2=TARGET
local SOURCE=$( echo ${1} | cut -d '@' -f 1 )
if __be_snapshot ${1}
then
# create boot environment from snapshot
local SNAPSHOT=$( echo ${1} | cut -d '@' -f 2 )
zfs list -r -H -t filesystem -o name ${SOURCE} \
| while read FS
do
if ! zfs list -H -o name ${FS}@${SNAPSHOT} 1> /dev/null 2> /dev/null
then
echo "ERROR: Child snapshot '${FS}@${SNAPSHOT}' does not exist"
exit 1
fi
done
else
# create boot environment from other boot environment
if zfs list -H -o name ${1}@${2##*/} 1> /dev/null 2> /dev/null
then
echo "ERROR: Snapshot '${1}@${2##*/}' already exists"
exit 1
fi
# snapshot format
FMT=$( date "+%Y-%m-%d-%H:%M:%S" )
if ! zfs snapshot -r ${1}@${FMT} 1> /dev/null 2> /dev/null
then
echo "ERROR: Cannot create snapshot '${1}@${FMT}'"
exit 1
fi
fi
# clone properties of source boot environment
zfs list -H -o name -r ${SOURCE} \
| grep -v '@' \
| while read FS
do
local OPTS=""
while read NAME PROPERTY VALUE
do
if [ "${PROPERTY}" = "sharenfs" ]
then
local OPTS="-o ${PROPERTY}=\"${VALUE}\" ${OPTS}"
else
local OPTS="-o ${PROPERTY}=${VALUE} ${OPTS}"
fi
done << EOF
$( zfs get -o name,property,value -s local,received -H all ${FS} | awk '!/[\t ]canmount[\t ]/' )
EOF
DATASET=$( echo ${FS} | awk '{print $1}' | sed -E s#"^${POOL}\/${BEDS}\/${SOURCE##*/}"#"${POOL}\/${BEDS}\/${2##*/}"#g )
if [ "${OPTS}" = "-o = " ]
then
local OPTS=""
fi
if __be_snapshot ${1}
then
eval "zfs clone -o canmount=noauto ${OPTS} ${FS}@${1##*@} ${DATASET}"
else
eval "zfs clone -o canmount=noauto ${OPTS} ${FS}@${FMT} ${DATASET}"
fi
done
# check if we need to update grub
if [ "${GRUB}" = YES ]
then
__update_grub
fi
}
ROOTFS=$( mount | awk '/ \/ / {print $1}' )
if echo ${ROOTFS} | grep -q -m 1 -E "^/dev/"
then
echo "ERROR: This system does not boot from ZFS pool"
exit 1
fi
POOL=$( echo ${ROOTFS} | awk -F '/' '{print $1}' )
if [ $( echo ${ROOTFS} | awk -F '/' '{print NF}' ) -lt 3 ]
then
echo "ERROR: This system is not configured for boot environments"
exit 1
fi
BOOTFS=$( zpool list -H -o bootfs ${POOL} )
if [ -z "${BOOTFS}" -o "${BOOTFS}" = "-" ]
then
echo "ERROR: ZFS boot pool '${POOL}' has unset 'bootfs' property"
exit 1
fi
#acitve BE
BE=$( echo ${ROOTFS} | awk -F '/' '{print $(NF)}' )
if [ -f /etc/conf.d/beadm ]
then
. /etc/conf.d/beadm
fi
if [ -f /etc/beadm.conf ]
then
. /etc/beadm.conf
fi
# update GRUB bootloader instead of FreeBSD's loader(8)
: ${GRUB="NO"}
# use other prefix then the 'pool/ROOT/bename' default
# more agile methode to support more nested paths like 'pool/enc/ROOT/bename'
BEDS="${ROOTFS/$POOL\//}"
BEDS="${BEDS/\/$BE/}"
case ${1} in
(list) # --------------------------------------------------------------------
OPTION_a=0
OPTION_D=0
OPTION_s=0
shift
while getopts "aDHs" OPT
do
case ${OPT} in
(a) OPTION_a=1 ;;
(D) OPTION_D=1 ;;
(H) OPTION_H=1 ;;
(s) OPTION_s=1
OPTION_a=1 ;;
(*) __usage ;;
esac
done
awk -v POOL="${POOL}" \
-v BEDS="${BEDS}" \
-v ROOTFS="${ROOTFS}" \
-v BOOTFS="${BOOTFS}" \
-v OPTION_a="${OPTION_a}" \
-v OPTION_D="${OPTION_D}" \
-v OPTION_H="${OPTION_H}" \
-v OPTION_s="${OPTION_s}" \
'function __normalize(VALUE) {
if(VALUE == "-" || VALUE == 0)
return 0
else
return substr(VALUE, 1, length(VALUE) - 1) * MULTIPLIER[substr(VALUE, length(VALUE))]
}
function __show_units(VALUE) {
if(VALUE < 1024) { UNIT = "K"; }
else if(VALUE < 1048576) { VALUE /= 1024; UNIT = "M"; }
else if(VALUE < 1073741824) { VALUE /= 1048576; UNIT = "G"; }
else if(VALUE < 1099511627776) { VALUE /= 1073741824; UNIT = "T"; }
else if(VALUE < 1125899906842624) { VALUE /= 1099511627776; UNIT = "P"; }
else if(VALUE < 1152921504606846976) { VALUE /= 1125899906842624; UNIT = "E"; }
else { VALUE /= 1152921504606846976; UNIT = "Z"; }
return sprintf("%.1f%s", VALUE, UNIT)
}
function __get_bename(BENAME) {
sub(BENAME_BEGINS_WITH "/", "", BENAME)
sub("/.*", "", BENAME)
return BENAME
}
function __convert_date(DATE) {
CMD_DATE = "date --date=\"" DATE "\" +\"%Y-%m-%d %H:%M\""
CMD_DATE | getline NEW
close(CMD_DATE)
return NEW
}
BEGIN {
BENAME_BEGINS_WITH = POOL "/" BEDS
MULTIPLIER["K"] = 1
MULTIPLIER["M"] = 1024
MULTIPLIER["G"] = 1048576
MULTIPLIER["T"] = 1073741824
MULTIPLIER["P"] = 1099511627776
MULTIPLIER["E"] = 1125899906842624
MULTIPLIER["Z"] = 1152921504606846976
MOUNTPOINT_LENGTH = 10
FSNAME_LENGTH = 2
if(OPTION_a == 1)
FSNAME_LENGTH = 19
CMD_MOUNT="mount"
while(CMD_MOUNT | getline)
if($1 ~ "^" BENAME_BEGINS_WITH) {
if (!($1 in MOUNTS))
MOUNTS[$1] = $3
}
close(CMD_MOUNT)
FS = "\\t"
FSNAMESC = 0
CMD_ZFS_LIST = "zfs list -H -t all -s creation -o name,used,usedds,usedbysnapshots,usedrefreserv,refer,creation,origin -r "
while(CMD_ZFS_LIST BENAME_BEGINS_WITH | getline) {
if($1 != BENAME_BEGINS_WITH) {
FSNAME = $1
FSNAMES[FSNAMESC++] = FSNAME
USED = __normalize($2)
USEDBYDATASET = __normalize($3)
USEDBYSNAPSHOTS = __normalize($4)
USEDREFRESERV = __normalize($5)
REFER[FSNAME] = __normalize($6)
CREATIONS[FSNAME] = $7
ORIGINS[FSNAME] = $8
if(FSNAME ~ /@/)
SPACES[FSNAME] = USED
else {
SPACES[FSNAME] = USEDBYDATASET + USEDREFRESERV
if(OPTION_D != 1)
SPACES[FSNAME] += USEDBYSNAPSHOTS
BE = " " __get_bename(FSNAME) " "
if(index(BELIST, BE) == 0)
BELIST = BELIST " " BE
MOUNTPOINT = MOUNTS[FSNAME]
if(MOUNTPOINT) {
if((OPTION_a == 0 && FSNAME == (BENAME_BEGINS_WITH "/" __get_bename(FSNAME))) || (OPTION_a == 1)) {
LM = length(MOUNTPOINT)
if(LM > MOUNTPOINT_LENGTH)
MOUNTPOINT_LENGTH = LM
}
}
else
MOUNTPOINT = "-"
}
if(OPTION_a == 1)
LF = length(FSNAME)
else if(FSNAME !~ /@/)
LF = length(__get_bename(FSNAME))
if(LF > FSNAME_LENGTH)
FSNAME_LENGTH = LF
}
}
close(CMD_ZFS_LIST)
split(BELIST, BENAMES, " ")
if(OPTION_a == 1) {
BE_HEAD = "BE/Dataset/Snapshot"
printf "%-" FSNAME_LENGTH + 2 "s %-6s %-" MOUNTPOINT_LENGTH "s %6s %s\n", BE_HEAD, "Active", "Mountpoint", "Space", "Created"
}
else if(OPTION_H == 1)
BE_HEAD = ""
else {
BE_HEAD = "BE"
printf "%-" FSNAME_LENGTH "s %-6s %-" MOUNTPOINT_LENGTH "s %6s %s\n", BE_HEAD, "Active", "Mountpoint", "Space", "Created"
}
if(OPTION_s != 1)
SNAPSHOT_FILTER = "(/[^@]*)?$"
BENAMESC = length(BENAMES)
for(I = 1; I <= BENAMESC; I++) {
BENAME = BENAMES[I]
if(OPTION_a == 1) {
printf "\n"
print BENAME
FSNAMESC = length(FSNAMES)
for(J = 1; J <= FSNAMESC; J++) {
FSNAME = FSNAMES[J]
if(FSNAME ~ "^" BENAME_BEGINS_WITH "/" BENAME SNAPSHOT_FILTER) {
ACTIVE = ""
if(FSNAME == ROOTFS)
ACTIVE = ACTIVE "N"
if(FSNAME == BOOTFS)
ACTIVE = ACTIVE "R"
if(! ACTIVE)
ACTIVE = "-"
MOUNTPOINT = MOUNTS[FSNAME]
if(! MOUNTPOINT)
MOUNTPOINT = "-"
printf " %-" FSNAME_LENGTH "s %-6s %-" MOUNTPOINT_LENGTH "s %6s %s\n", FSNAME, ACTIVE, MOUNTPOINT, __show_units(SPACES[FSNAME]), __convert_date(CREATIONS[FSNAME])
ORIGIN = ORIGINS[FSNAME]
ORIGIN_DISPLAY = ORIGIN
sub(BENAME_BEGINS_WITH "/", "", ORIGIN_DISPLAY)
if(ORIGIN != "-") {
if(OPTION_D == 1)
SPACE = REFER[ORIGIN]
else
SPACE = SPACES[ORIGIN]
printf " %-" FSNAME_LENGTH "s %-6s %-" MOUNTPOINT_LENGTH "s %6s %s\n", " " ORIGIN_DISPLAY, "-", "-", __show_units(SPACE), __convert_date(CREATIONS[ORIGIN])
}
}
}
}
else {
SPACE = 0
ACTIVE = ""
NAME = BENAME_BEGINS_WITH "/" BENAME
if(NAME == ROOTFS)
ACTIVE = ACTIVE "N"
if(NAME == BOOTFS)
ACTIVE = ACTIVE "R"
if(! ACTIVE)
ACTIVE = "-"
FNAMESC = length(FSNAMES)
for(J = 1; J <= FSNAMESC; J++) {
FSNAME = FSNAMES[J]
if(FSNAME ~ "^" BENAME_BEGINS_WITH "/" BENAME "(/[^@]*)?$") {
if((BENAME_BEGINS_WITH "/" BENAME) == FSNAME) {
MOUNTPOINT = MOUNTS[FSNAME]
if(! MOUNTPOINT)
MOUNTPOINT = "-"
CREATION = __convert_date(CREATIONS[FSNAME])
}
ORIGIN = ORIGINS[FSNAME]
if(ORIGIN == "-")
SPACE += SPACES[FSNAME]
else {
if(OPTION_D == 1)
SPACE += REFER[FSNAME]
else
SPACE += SPACES[FSNAME] + SPACES[ORIGIN]
}
}
}
if(OPTION_H == 1)
printf "%s\t%s\t%s\t%s\t%s\n", BENAME, ACTIVE, MOUNTPOINT, __show_units(SPACE), CREATION
else
printf "%-" FSNAME_LENGTH "s %-6s %-" MOUNTPOINT_LENGTH "s %6s %s\n", BENAME, ACTIVE, MOUNTPOINT, __show_units(SPACE), CREATION
}
}
}'
;;
(create) # ------------------------------------------------------------------
case ${#} in
(4)
if ! [ ${2} = "-e" ]
then
__usage
fi
# check if argument for -e option is full path dataset
# argument for -e option must be 'beName' or 'beName@snapshot'
if echo ${3} | grep -q "/" 2> /dev/null
then
__usage
fi
__be_exist ${POOL}/${BEDS}/${3}
if zfs list -H -o name ${POOL}/${BEDS}/${4} 1> /dev/null 2> /dev/null
then
echo "ERROR: Boot environment '${4}' already exists"
exit 1
fi
__be_new ${POOL}/${BEDS}/${3} ${POOL}/${BEDS}/${4}
;;
(2)
if __be_snapshot ${2}
then
if ! zfs snapshot -r ${POOL}/${BEDS}/${2} 1> /dev/null 2> /dev/null
then
echo "ERROR: Cannot create '${2}' recursive snapshot"
exit 1
fi
else
__be_new ${ROOTFS} ${POOL}/${BEDS}/${2}
fi
;;
(*)
__usage
;;
esac
echo "Created successfully"
;;
(activate) # ----------------------------------------------------------------
if [ ${#} -ne 2 ]
then
__usage
fi
__be_exist ${POOL}/${BEDS}/${2}
if [ "${BOOTFS}" = "${POOL}/${BEDS}/${2}" ]
then
echo "Already activated"
exit 0
else
if __be_mounted ${POOL}/${BEDS}/${2}
then
MNT=$(__be_findmnt "${POOL}/${BEDS}/${2}")
if [ "${MNT}" != "/" ]
then
# boot environment is not current root and its mounted
echo "Attempt to unmount boot environment '${2}' mounted at '${MNT}'"
if ! umount "${MNT}" 1> /dev/null 2> /dev/null
then
echo "ERROR: Unable to unmount boot environment '${2}' mounted at '${MNT}'"
echo "ERROR: Cannot activate manually mounted boot environment '${2}'"
exit 1
fi
echo "Gracefully unmounted boot environment '${2}' from '${MNT}' mount point"
fi
fi
# do not change root (/) mounted boot environment mountpoint
if [ "${ROOTFS}" != "${POOL}/${BEDS}/${2}" ]
then
TMPMNT=$( mktemp -d -p /mnt BE-${2}.XXX )
if ! mkdir -p ${TMPMNT} 2> /dev/null
then
echo "ERROR: Cannot create '${TMPMNT}' directory"
exit 1
fi
MOUNT=0
while read FS MNT TYPE OPTS DUMP FSCK;
do
if [ "${FS}" = "${POOL}/${BEDS}/${2}" ]
then
MOUNT="${MNT}"
break
fi
done << EOF
$( cat /proc/mounts )
EOF
if [ ${MOUNT} -eq 0 ]
then
zfs set canmount=noauto ${POOL}/${BEDS}/${2}
zfs set mountpoint=${TMPMNT} ${POOL}/${BEDS}/${2}
zfs mount ${POOL}/${BEDS}/${2}
else
TMPMNT=${MOUNT}
fi
if [ -f /etc/zfs/zpool.cache ]
then
cp /etc/zfs/zpool.cache ${TMPMNT}/etc/zfs/zpool.cache
else
rm -f ${TMPMNT}/etc/zfs/zpool.cache
fi
#LOADER_CONFIGS=${TMPMNT}/boot/loader.conf
#if [ -f ${TMPMNT}/boot/loader.conf.local ]
#then
# LOADER_CONFIGS="${LOADER_CONFIGS} ${TMPMNT}/boot/loader.conf.local"
#fi
#sed -i '' -E s/"^vfs.root.mountfrom=.*$"/"vfs.root.mountfrom=\"zfs:${POOL}\/${BEDS}\/${2##*/}\""/g ${LOADER_CONFIGS}
if [ ${MOUNT} -eq 0 ]
then
zfs umount ${POOL}/${BEDS}/${2}
zfs set mountpoint=/ ${POOL}/${BEDS}/${2}
fi
fi
if ! zpool set bootfs=${POOL}/${BEDS}/${2} ${POOL} 1> /dev/null 2> /dev/null
then
echo "ERROR: Failed to activate '${2}' boot environment"
exit 1
fi
fi
# execute ZFS LIST only once
ZFS_LIST=$( zfs list -H -o name -r ${POOL}/${BEDS} )
# disable automatic mount on all inactive boot environments
echo "${ZFS_LIST}" \
| grep -v "^${POOL}/${BEDS}$" \
| grep -v "^${POOL}/${BEDS}/${2}$" \
| grep -v "^${POOL}/${BEDS}/${2}/" \
| while read NAME
do
zfs set canmount=noauto ${NAME}
done
# enable automatic mount for active boot environment and promote it
echo "${ZFS_LIST}" \
| grep -E "^${POOL}/${BEDS}/${2}(/|$)" \
| while read NAME
do
while __be_clone ${NAME}
do
zfs promote ${NAME}
done
done
# check if we need to update grub
if [ "${GRUB}" = YES ]
then
__update_grub
fi
echo "Activated successfully"
;;
(destroy) # -----------------------------------------------------------------
if [ "${2}" != "-F" ]
then
DESTROY=${2}
else
DESTROY=${3}
fi
__be_exist ${POOL}/${BEDS}/${DESTROY}
case ${#} in
(2)
echo "Are you sure you want to destroy '${2}'?"
echo -n "This action cannot be undone (y/[n]): "
read CHOICE
;;
(3)
if [ "${2}" != "-F" ]
then
__usage
fi
CHOICE=Y
;;
(*)
__usage
;;
esac
if [ "${BOOTFS}" = "${POOL}/${BEDS}/${DESTROY}" ]
then
echo "ERROR: Cannot destroy active boot environment"
exit 1
fi
case ${CHOICE} in
(Y|y|[Yy][Ee][Ss])
# destroy snapshot or boot environment
if __be_snapshot ${POOL}/${BEDS}/${DESTROY}
then
# destroy desired snapshot
if ! zfs destroy -r ${POOL}/${BEDS}/${DESTROY} 1> /dev/null 2> /dev/null
then
echo "ERROR: Snapshot '${2}' is origin for other boot environment"
exit 1
fi
else
if __be_clone ${POOL}/${BEDS}/${DESTROY}
then
# promote clones dependent on snapshots used by destroyed boot environment
zfs list -H -t all -o name,origin -r ${POOL} \
| while read NAME ORIGIN
do
if echo "${ORIGIN}" | grep -q -E "${POOL}/${BEDS}/${DESTROY}(/.*@|@)" 2> /dev/null
then
zfs promote ${NAME}
fi
done
# get origins used by destroyed boot environment
ORIGIN_SNAPSHOTS=$( zfs list -H -t all -o origin -r ${POOL}/${BEDS}/${DESTROY} | grep -v '^-$' | awk -F "@" '{print $2}' | sort -u )
fi
# check if boot environment was created from existing snapshot
ORIGIN=$( zfs list -H -o origin ${POOL}/${BEDS}/${DESTROY} )
CREATION=$( zfs list -H -o creation ${POOL}/${BEDS}/${DESTROY} )
CREATION=$( date --date="${CREATION}" +"%Y-%m-%d-%H:%M" )
SNAPSHOT_NAME=$( echo "${ORIGIN}" | cut -d '@' -f 2 | sed -E 's/:[0-9]{2}$//g' )
if [ "${2}" = "-F" ]
then
CHOICE=1
elif [ "${SNAPSHOT_NAME}" != "${CREATION}" ]
then
ORIGIN=$( basename ${ORIGIN} )
echo "Boot environment '${DESTROY}' was created from existing snapshot"
echo -n "Destroy '${ORIGIN}' snapshot? (y/[n]): "
read CHOICE
case ${CHOICE} in
(Y|y|[Yy][Ee][Ss])
CHOICE=1
;;
(*)
CHOICE=0
echo "Origin snapshot '${ORIGIN}' will be preserved"
;;
esac
else
CHOICE=1
fi
# destroy boot environment
zfs destroy -r ${POOL}/${BEDS}/${DESTROY}
# check if boot environment is a clone
if __be_clone ${POOL}/${BEDS}/${DESTROY}
then
# promote datasets dependent on origins used by destroyed boot environment
ALL_ORIGINS=$( zfs list -H -t all -o name,origin -r ${POOL} )
echo "${ORIGIN_SNAPSHOTS}" \
| while read S
do
echo "${ALL_ORIGINS}" \
| grep "${S}" \
| awk '{print $1}' \
| while read I
do
zfs promote ${I}
done
done
fi
# destroy origins used by destroyed boot environment
SNAPSHOTS=$( zfs list -H -t snapshot -o name -r ${POOL} )
echo "${ORIGIN_SNAPSHOTS}" \
| while read S
do
echo "${SNAPSHOTS}" \
| grep "@${S}$" \
| while read I
do
if [ ${CHOICE} -eq 1 ]
then
zfs destroy ${I}
fi
done
done
fi
# check if we need to update grub
if [ "${GRUB}" = YES ]
then
__update_grub
fi
echo "Destroyed successfully"
;;
(*)
echo "Boot environment '${DESTROY}' has not been destroyed"
;;
esac
;;
(rename) # ------------------------------------------------------------------
if [ ${#} -ne 3 ]
then
__usage
fi
__be_exist ${POOL}/${BEDS}/${2}
if [ "${BOOTFS}" = "${POOL}/${BEDS}/${2}" ]
then
echo "ERROR: Renaming active boot environment is not supported"
exit 1
fi
if zfs list -H -o name ${POOL}/${BEDS}/${3} 2> /dev/null
then
echo "ERROR: Boot environment '${3}' already exists"
exit 1
fi
zfs rename ${POOL}/${BEDS}/${2} ${POOL}/${BEDS}/${3}
# check if we need to update grub
if [ "${GRUB}" = YES ]
then
__update_grub
fi
echo "Renamed successfully"
;;
(mount) # ------------------------------------------------------------
if [ ${#} -eq 2 ]
then
TARGET=$( mktemp -d -p /mnt BE-${2}.XXX )
elif [ ${#} -eq 3 ]
then
TARGET=${3}
else
__usage
fi
__be_exist "${POOL}/${BEDS}/${2}"
if __be_mounted "${POOL}/${BEDS}/${2}"
then
MNT=$(__be_findmnt "${POOL}/${BEDS}/${2}")
echo "${MNT}"
exit 1
fi
if ! mkdir -p ${TARGET} 2> /dev/null
then
echo "ERROR: Cannot create '${TARGET}' mountpoint"
exit 1
fi
zfs set mountpoint=legacy ${POOL}/${BEDS}/${2}
if ! mount -t zfs ${POOL}/${BEDS}/${2} ${TARGET}
then
echo "ERROR: Cannot mount '${2}' at '${TARGET}' mountpoint"
zfs set mountpoint=/ ${POOL}/${BEDS}/${2}
exit 1
fi
zfs list -H -o name,mountpoint -r ${POOL}/${BEDS}/${2} \
| grep -v -E "[[:space:]](legacy|none)$" \
| sort -n \
| grep -E "^${POOL}/${BEDS}/${2}/" \
| while read FS MOUNTPOINT
do
if [ "{FS}" != "${POOL}/${BEDS}/${2}" ]
then
INHERIT=$( zfs get -H -o source mountpoint ${FS} )
if [ "${INHERIT}" = "local" ]
then
case ${MOUNTPOINT} in
(legacy|none)
continue
;;
(*)
MOUNTPOINT="/$( echo "${FS}" | sed s^"${POOL}/${BEDS}/${2}/"^^g )"
;;
esac
fi
fi
if ! mkdir -p ${TARGET}${MOUNTPOINT} 1> /dev/null 2> /dev/null
then
echo "ERROR: Cannot create '${TARGET}${MOUNTPOINT}' mountpoint"
exit 1
fi
if ! mount -t zfs ${FS} ${TARGET}${MOUNTPOINT} 1> /dev/null 2> /dev/null
then
echo "ERROR: Cannot mount '${FS}' at '${TARGET}${MOUNTPOINT}' mountpoint"
exit 1
fi
done
echo "${TARGET}"
;;
(bind) # -------------------------------------------------------------
if [ ${#} -gt 1 ]
then
TARGET=${2}
else
__usage
fi
shift 2
__be_exist "${POOL}/${BEDS}/${TARGET}"
if __be_mounted "${POOL}/${BEDS}/${TARGET}"
then
MNT=$(__be_findmnt "${POOL}/${BEDS}/${TARGET}")
if [ ${#} -eq 0 ]
then
set -- ${BIND_COMPS:-proc sys dev tmp}
fi
comp=${1}
while shift
do
case ${comp} in
(proc)
mount -t proc proc "${MNT}/proc"
;;
(sys|dev)
mount --rbind "/${comp}" "${MNT}/${comp}"
mount --make-rslave "${MNT}/${comp}"
;;
(boot)
if ! findmnt -f /boot > /dev/null
then
mount /boot
fi
mount --bind /boot "${MNT}/boot"
;;
(*)
mount --bind "/${comp}" "${MNT}/${comp}"
;;
esac
comp=${1}
done
else
echo "ERROR: '${TARGET}' is not mounted"
exit 1
fi
;;
(chroot) # -------------------------------------------------------------------
if [ ${#} -eq 2 ]
then
TARGET=${2}
else
__usage
fi
__be_exist "${POOL}/${BEDS}/${2}"
if __be_mounted "${POOL}/${BEDS}/${2}"
then
MNT=$(__be_findmnt "${POOL}/${BEDS}/${TARGET}")
TMPRC=$(mktemp --tmpdir=/tmp BE-${2}.XXX)
if [ -r "${RCFILE:=$HOME/.bashrc}" ]
then
cp "${RCFILE}" ${TMPRC}
else
echo "Warning: unable to read ${RCFILE}"
fi
cat >> ${TMPRC} << EOF
env-update
. /etc/profile
export PS1="[$TARGET] \$PS1"
EOF
ionice -c3 -p $$ | sed 's/^/ /'
renice -n 20 -p $$ | sed 's/^/ /'
/usr/bin/chroot "${MNT}" ${SHELL:-/bin/bash} --rcfile ${TMPRC}
rm ${TMPRC}
else
echo "ERROR: '${TARGET}' is not mounted"
exit 1
fi
;;
(ubind|unbind) # -------------------------------------------------------------
if [ ${#} -gt 1 ]
then
TARGET=${2}
else
__usage
fi
shift 2
__be_exist "${POOL}/${BEDS}/${TARGET}"
if __be_mounted "${POOL}/${BEDS}/${TARGET}"
then
MNT=$(__be_findmnt "${POOL}/${BEDS}/${TARGET}")
if [ ${#} -eq 0 ]
then
set -- ${BIND_COMPS:-proc sys dev tmp}
fi
comp=${1}
while shift
do
case ${comp} in
(proc|sys|dev|tmp)
umount -l "${MNT}/${comp}"
;;
(*)
umount "${MNT}/${comp}"
;;
esac
comp=${1}
done
else
echo "ERROR: '${TARGET}' is not mounted"
exit 1
fi
;;
(umount|unmount) # ----------------------------------------------------------
if [ ${#} -eq 2 ]
then
# we need this empty section for argument checking
:
elif [ ${#} -eq 3 -a "${2}" = "-f" ]
then
OPTS="-f"
shift
else
__usage
fi
__be_exist "${POOL}/${BEDS}/${2}"
if ! __be_mounted "${POOL}/${BEDS}/${2}"
then
echo "Boot environment '${2}' is not mounted"
exit 1
fi
MOUNT=$( mount )
MOUNTPOINT=$( echo "${MOUNT}" | grep -m 1 "^${POOL}/${BEDS}/${2} on " | awk '{print $3}' )
echo "${MOUNT}" \
| awk '{print $1}' \
| grep -E "^${POOL}/${BEDS}/${2}(/|$)" \
| sort -n -r \
| while read FS
do
if ! umount ${OPTS} ${FS} 1> /dev/null 2> /dev/null
then
echo "ERROR: Cannot umount '${FS}' dataset"
exit 1
else
zfs set mountpoint=/ ${POOL}/${BEDS}/${2}
fi
done
echo "Unmounted successfully"
# only delete the temporary mountpoint directory
if echo "${MOUNTPOINT}" | grep -q -E "/BE-${2}\.[a-zA-Z0-9]{3}" 1> /dev/null 2> /dev/null
then
# delete only when it is an empty directory
if [ $( find ${MOUNTPOINT} | head | wc -l | bc ) -eq 1 ]
then
rm -r ${MOUNTPOINT}
fi
fi
;;
(*) # -----------------------------------------------------------------------
__usage
;;
esac