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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
*/
/*
* Copyright 2015 Nexenta Systems, Inc. All rights reserved.
* Copyright 2016 Toomas Soome <tsoome@me.com>
* Copyright 2022 OmniOS Community Edition (OmniOSce) Association.
*/
#include <assert.h>
#include <libintl.h>
#include <libnvpair.h>
#include <libzfs.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <sys/mnttab.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/efi_partition.h>
#include <libbe.h>
#include <libbe_priv.h>
#include <libzfsbootenv.h>
char *mnttab = MNTTAB;
/*
* Private function prototypes
*/
static int set_bootfs(char *boot_rpool, char *be_root_ds);
static int set_canmount(be_node_list_t *, char *);
static boolean_t be_do_install_mbr(char *, nvlist_t *);
static int be_do_installboot_helper(zpool_handle_t *, nvlist_t *, char *,
char *, uint16_t);
static int be_do_installboot(be_transaction_data_t *, uint16_t);
static int be_get_grub_vers(be_transaction_data_t *, char **, char **);
static int get_ver_from_capfile(char *, char **);
static int be_promote_zone_ds(char *, char *);
static int be_promote_ds_callback(zfs_handle_t *, void *);
/* ******************************************************************** */
/* Public Functions */
/* ******************************************************************** */
/*
* Function: be_activate
* Description: Calls _be_activate which activates the BE named in the
* attributes passed in through be_attrs. The process of
* activation sets the bootfs property of the root pool, resets
* the canmount property to noauto, and sets the default in the
* grub menu to the entry corresponding to the entry for the named
* BE.
* Parameters:
* be_attrs - pointer to nvlist_t of attributes being passed in.
* The follow attribute values are used by this function:
*
* BE_ATTR_ORIG_BE_NAME *required
* Return:
* BE_SUCCESS - Success
* be_errno_t - Failure
* Scope:
* Public
*/
int
be_activate(nvlist_t *be_attrs)
{
int ret = BE_SUCCESS;
char *be_name = NULL;
be_nextboot_state_t nextboot;
boolean_t next_boot;
/* Initialize libzfs handle */
if (!be_zfs_init())
return (BE_ERR_INIT);
/* Get the BE name to activate */
if (nvlist_lookup_string(be_attrs, BE_ATTR_ORIG_BE_NAME, &be_name)
!= 0) {
be_print_err(gettext("be_activate: failed to "
"lookup BE_ATTR_ORIG_BE_NAME attribute\n"));
be_zfs_fini();
return (BE_ERR_INVAL);
}
/* Validate BE name */
if (!be_valid_be_name(be_name)) {
be_print_err(gettext("be_activate: invalid BE name %s\n"),
be_name);
be_zfs_fini();
return (BE_ERR_INVAL);
}
if (nvlist_lookup_boolean_value(be_attrs, BE_ATTR_ACTIVE_NEXTBOOT,
&next_boot) == 0) {
if (next_boot)
nextboot = BE_NEXTBOOT_SET;
else
nextboot = BE_NEXTBOOT_UNSET;
} else {
nextboot = BE_NEXTBOOT_IGNORE;
}
ret = _be_activate(be_name, nextboot);
be_zfs_fini();
return (ret);
}
/*
* Function: be_installboot
* Description: Calls be_do_installboot to install/update bootloader on
* pool passed in through be_attrs. The primary consumer is
* bootadm command to avoid duplication of the code.
* Parameters:
* be_attrs - pointer to nvlist_t of attributes being passed in.
* The following attribute values are used:
*
* BE_ATTR_ORIG_BE_NAME *required
* BE_ATTR_ORIG_BE_POOL *required
* BE_ATTR_ORIG_BE_ROOT *required
* BE_ATTR_INSTALL_FLAGS optional
*
* Return:
* BE_SUCCESS - Success
* be_errno_t - Failure
* Scope:
* Public
*/
int
be_installboot(nvlist_t *be_attrs)
{
int ret = BE_SUCCESS;
uint16_t flags = 0;
uint16_t verbose;
be_transaction_data_t bt = { 0 };
/* Get flags */
if (nvlist_lookup_pairs(be_attrs, NV_FLAG_NOENTOK,
BE_ATTR_INSTALL_FLAGS, DATA_TYPE_UINT16, &flags, NULL) != 0) {
be_print_err(gettext("be_installboot: failed to lookup "
"BE_ATTR_INSTALL_FLAGS attribute\n"));
return (BE_ERR_INVAL);
}
/* Set verbose early, so we get all messages */
verbose = flags & BE_INSTALLBOOT_FLAG_VERBOSE;
if (verbose == BE_INSTALLBOOT_FLAG_VERBOSE)
libbe_print_errors(B_TRUE);
ret = nvlist_lookup_string(be_attrs, BE_ATTR_ORIG_BE_NAME,
&bt.obe_name);
if (ret != 0) {
be_print_err(gettext("be_installboot: failed to "
"lookup BE_ATTR_ORIG_BE_NAME attribute\n"));
return (BE_ERR_INVAL);
}
ret = nvlist_lookup_string(be_attrs, BE_ATTR_ORIG_BE_POOL,
&bt.obe_zpool);
if (ret != 0) {
be_print_err(gettext("be_installboot: failed to "
"lookup BE_ATTR_ORIG_BE_POOL attribute\n"));
return (BE_ERR_INVAL);
}
ret = nvlist_lookup_string(be_attrs, BE_ATTR_ORIG_BE_ROOT,
&bt.obe_root_ds);
if (ret != 0) {
be_print_err(gettext("be_installboot: failed to "
"lookup BE_ATTR_ORIG_BE_ROOT attribute\n"));
return (BE_ERR_INVAL);
}
/* Initialize libzfs handle */
if (!be_zfs_init())
return (BE_ERR_INIT);
ret = be_do_installboot(&bt, flags);
be_zfs_fini();
return (ret);
}
/* ******************************************************************** */
/* Semi Private Functions */
/* ******************************************************************** */
/*
* Function: _be_activate
* Description: This does the actual work described in be_activate.
* Parameters:
* be_name - pointer to the name of BE to activate.
* nextboot - flag to ignore, set or unset nextboot
*
* Return:
* BE_SUCCESS - Success
* be_errnot_t - Failure
* Scope:
* Public
*/
int
_be_activate(char *be_name, be_nextboot_state_t nextboot)
{
be_transaction_data_t cb = { 0 };
zfs_handle_t *zhp = NULL;
char root_ds[MAXPATHLEN];
char active_ds[MAXPATHLEN];
be_node_list_t *be_nodes = NULL;
uuid_t uu = {0};
int entry, ret = BE_SUCCESS;
int zret = 0;
/*
* TODO: The BE needs to be validated to make sure that it is actually
* a bootable BE.
*/
if (be_name == NULL)
return (BE_ERR_INVAL);
if (nextboot == BE_NEXTBOOT_SET && getzoneid() != GLOBAL_ZONEID)
return (BE_ERR_INVAL);
/* Set obe_name to be_name in the cb structure */
cb.obe_name = be_name;
/* find which zpool the be is in */
if ((zret = zpool_iter(g_zfs, be_find_zpool_callback, &cb)) == 0) {
be_print_err(gettext("be_activate: failed to "
"find zpool for BE (%s)\n"), cb.obe_name);
return (BE_ERR_BE_NOENT);
} else if (zret < 0) {
be_print_err(gettext("be_activate: "
"zpool_iter failed: %s\n"),
libzfs_error_description(g_zfs));
ret = zfs_err_to_be_err(g_zfs);
return (ret);
}
if ((ret = be_make_root_ds(cb.obe_zpool, cb.obe_name, root_ds,
sizeof (root_ds))) != BE_SUCCESS) {
be_print_err(gettext("%s: failed to get BE container dataset "
"for %s/%s\n"), __func__, cb.obe_zpool, cb.obe_name);
return (ret);
}
cb.obe_root_ds = strdup(root_ds);
if (getzoneid() == GLOBAL_ZONEID) {
ret = be_do_installboot(&cb, BE_INSTALLBOOT_FLAG_NULL);
if (ret != BE_SUCCESS)
return (ret);
if (!be_has_menu_entry(root_ds, cb.obe_zpool, &entry)) {
if ((ret = be_append_menu(cb.obe_name, cb.obe_zpool,
NULL, NULL, NULL)) != BE_SUCCESS) {
be_print_err(gettext("be_activate: Failed to "
"add BE (%s) to the menu\n"),
cb.obe_name);
goto done;
}
}
if (be_has_grub()) {
if ((ret = be_change_grub_default(cb.obe_name,
cb.obe_zpool)) != BE_SUCCESS) {
be_print_err(gettext("be_activate: failed to "
"change the default entry in menu.lst\n"));
goto done;
}
}
}
if ((ret = _be_list(cb.obe_name, &be_nodes, BE_LIST_DEFAULT))
!= BE_SUCCESS) {
return (ret);
}
if ((ret = set_canmount(be_nodes, "noauto")) != BE_SUCCESS) {
be_print_err(gettext("be_activate: failed to set "
"canmount dataset property\n"));
goto done;
}
if (getzoneid() == GLOBAL_ZONEID) {
switch (nextboot) {
case BE_NEXTBOOT_SET:
if ((ret = lzbe_set_boot_device(be_nodes->be_rpool,
lzbe_add, root_ds)) != 0) {
be_print_err(gettext("be_activate: failed to "
"set nextboot for %s\n"), root_ds);
goto done;
}
break;
case BE_NEXTBOOT_UNSET:
if ((ret = lzbe_set_boot_device(be_nodes->be_rpool,
lzbe_add, "")) != 0) {
be_print_err(gettext("be_activate: failed to "
"clear nextboot for %s\n"), root_ds);
goto done;
}
break;
default:
if ((ret = set_bootfs(be_nodes->be_rpool,
root_ds)) != BE_SUCCESS) {
be_print_err(gettext("be_activate: failed to "
"set bootfs pool property for %s\n"),
root_ds);
goto done;
}
}
}
if (nextboot == BE_NEXTBOOT_IGNORE) {
if ((zhp = zfs_open(g_zfs, root_ds, ZFS_TYPE_FILESYSTEM)) !=
NULL) {
/*
* We don't need to close the zfs handle at this
* point because The callback funtion
* be_promote_ds_callback() will close it for us.
*/
if (be_promote_ds_callback(zhp, NULL) != 0) {
be_print_err(gettext("be_activate: "
"failed to activate the "
"datasets for %s: %s\n"),
root_ds,
libzfs_error_description(g_zfs));
ret = BE_ERR_PROMOTE;
goto done;
}
} else {
be_print_err(gettext("be_activate: failed to open "
"dataset (%s): %s\n"), root_ds,
libzfs_error_description(g_zfs));
ret = zfs_err_to_be_err(g_zfs);
goto done;
}
if (getzoneid() == GLOBAL_ZONEID &&
be_get_uuid(cb.obe_root_ds, &uu) == BE_SUCCESS &&
(ret = be_promote_zone_ds(cb.obe_name, cb.obe_root_ds))
!= BE_SUCCESS) {
be_print_err(gettext("be_activate: failed to promote "
"the active zonepath datasets for zones in BE "
"%s\n"), cb.obe_name);
}
}
if (getzoneid() != GLOBAL_ZONEID) {
if (!be_zone_compare_uuids(root_ds)) {
be_print_err(gettext("be_activate: activating zone "
"root dataset from non-active global BE is not "
"supported\n"));
ret = BE_ERR_NOTSUP;
goto done;
}
if ((zhp = zfs_open(g_zfs, root_ds,
ZFS_TYPE_FILESYSTEM)) == NULL) {
be_print_err(gettext("be_activate: failed to open "
"dataset (%s): %s\n"), root_ds,
libzfs_error_description(g_zfs));
ret = zfs_err_to_be_err(g_zfs);
goto done;
}
/* Find current active zone root dataset */
if ((ret = be_find_active_zone_root(zhp, cb.obe_zpool,
active_ds, sizeof (active_ds))) != BE_SUCCESS) {
be_print_err(gettext("be_activate: failed to find "
"active zone root dataset\n"));
ZFS_CLOSE(zhp);
goto done;
}
/* Do nothing if requested BE is already active */
if (strcmp(root_ds, active_ds) == 0) {
ret = BE_SUCCESS;
ZFS_CLOSE(zhp);
goto done;
}
/* Set active property for BE */
if (zfs_prop_set(zhp, BE_ZONE_ACTIVE_PROPERTY, "on") != 0) {
be_print_err(gettext("be_activate: failed to set "
"active property (%s): %s\n"), root_ds,
libzfs_error_description(g_zfs));
ret = zfs_err_to_be_err(g_zfs);
ZFS_CLOSE(zhp);
goto done;
}
ZFS_CLOSE(zhp);
/* Unset active property for old active root dataset */
if ((zhp = zfs_open(g_zfs, active_ds,
ZFS_TYPE_FILESYSTEM)) == NULL) {
be_print_err(gettext("be_activate: failed to open "
"dataset (%s): %s\n"), active_ds,
libzfs_error_description(g_zfs));
ret = zfs_err_to_be_err(g_zfs);
goto done;
}
if (zfs_prop_set(zhp, BE_ZONE_ACTIVE_PROPERTY, "off") != 0) {
be_print_err(gettext("be_activate: failed to unset "
"active property (%s): %s\n"), active_ds,
libzfs_error_description(g_zfs));
ret = zfs_err_to_be_err(g_zfs);
ZFS_CLOSE(zhp);
goto done;
}
ZFS_CLOSE(zhp);
}
done:
be_free_list(be_nodes);
return (ret);
}
/*
* Function: be_activate_current_be
* Description: Set the currently "active" BE to be "active on boot"
* Paramters:
* none
* Returns:
* BE_SUCCESS - Success
* be_errnot_t - Failure
* Scope:
* Semi-private (library wide use only)
*/
int
be_activate_current_be(void)
{
int ret = BE_SUCCESS;
be_transaction_data_t bt = { 0 };
if ((ret = be_find_current_be(&bt)) != BE_SUCCESS) {
return (ret);
}
ret = _be_activate(bt.obe_name, BE_NEXTBOOT_IGNORE);
if (ret != BE_SUCCESS) {
be_print_err(gettext("be_activate_current_be: failed to "
"activate %s\n"), bt.obe_name);
return (ret);
}
return (BE_SUCCESS);
}
/*
* Function: be_is_active_on_boot
* Description: Checks if the BE name passed in has the "active on boot"
* property set to B_TRUE.
* Paramters:
* be_name - the name of the BE to check
* Returns:
* B_TRUE - if active on boot.
* B_FALSE - if not active on boot.
* Scope:
* Semi-private (library wide use only)
*/
boolean_t
be_is_active_on_boot(char *be_name)
{
be_node_list_t *be_node = NULL;
if (be_name == NULL) {
be_print_err(gettext("be_is_active_on_boot: "
"be_name must not be NULL\n"));
return (B_FALSE);
}
if (_be_list(be_name, &be_node, BE_LIST_DEFAULT) != BE_SUCCESS) {
return (B_FALSE);
}
if (be_node == NULL) {
return (B_FALSE);
}
if (be_node->be_active_on_boot) {
be_free_list(be_node);
return (B_TRUE);
} else {
be_free_list(be_node);
return (B_FALSE);
}
}
/* ******************************************************************** */
/* Private Functions */
/* ******************************************************************** */
/*
* Function: set_bootfs
* Description: Sets the bootfs property on the boot pool to be the
* root dataset of the activated BE.
* Parameters:
* boot_pool - The pool we're setting bootfs in.
* be_root_ds - The main dataset for the BE.
* Return:
* BE_SUCCESS - Success
* be_errno_t - Failure
* Scope:
* Private
*/
static int
set_bootfs(char *boot_rpool, char *be_root_ds)
{
zpool_handle_t *zhp;
int err = BE_SUCCESS;
if ((zhp = zpool_open(g_zfs, boot_rpool)) == NULL) {
be_print_err(gettext("set_bootfs: failed to open pool "
"(%s): %s\n"), boot_rpool, libzfs_error_description(g_zfs));
err = zfs_err_to_be_err(g_zfs);
return (err);
}
err = zpool_set_prop(zhp, "bootfs", be_root_ds);
if (err) {
be_print_err(gettext("set_bootfs: failed to set "
"bootfs property for pool %s: %s\n"), boot_rpool,
libzfs_error_description(g_zfs));
err = zfs_err_to_be_err(g_zfs);
zpool_close(zhp);
return (err);
}
zpool_close(zhp);
return (BE_SUCCESS);
}
/*
* Function: set_canmount
* Description: Sets the canmount property on the datasets of the
* activated BE.
* Parameters:
* be_nodes - The be_node_t returned from be_list
* value - The value of canmount we setting, on|off|noauto.
* Return:
* BE_SUCCESS - Success
* be_errno_t - Failure
* Scope:
* Private
*/
static int
set_canmount(be_node_list_t *be_nodes, char *value)
{
char ds_path[MAXPATHLEN];
zfs_handle_t *zhp = NULL;
be_node_list_t *list = be_nodes;
int err = BE_SUCCESS;
while (list != NULL) {
be_dataset_list_t *datasets = list->be_node_datasets;
if ((err = be_make_root_ds(list->be_rpool, list->be_node_name,
ds_path, sizeof (ds_path))) != BE_SUCCESS) {
be_print_err(gettext("%s: failed to get BE container "
"dataset for %s/%s\n"), __func__,
list->be_rpool, list->be_node_name);
return (err);
}
if ((zhp = zfs_open(g_zfs, ds_path, ZFS_TYPE_DATASET)) ==
NULL) {
be_print_err(gettext("set_canmount: failed to open "
"dataset (%s): %s\n"), ds_path,
libzfs_error_description(g_zfs));
err = zfs_err_to_be_err(g_zfs);
return (err);
}
if (zfs_prop_get_int(zhp, ZFS_PROP_MOUNTED)) {
/*
* it's already mounted so we can't change the
* canmount property anyway.
*/
err = BE_SUCCESS;
} else {
err = zfs_prop_set(zhp,
zfs_prop_to_name(ZFS_PROP_CANMOUNT), value);
if (err) {
ZFS_CLOSE(zhp);
be_print_err(gettext("set_canmount: failed to "
"set dataset property (%s): %s\n"),
ds_path, libzfs_error_description(g_zfs));
err = zfs_err_to_be_err(g_zfs);
return (err);
}
}
ZFS_CLOSE(zhp);
while (datasets != NULL) {
if ((err = be_make_root_ds(list->be_rpool,
datasets->be_dataset_name, ds_path,
sizeof (ds_path))) != BE_SUCCESS) {
be_print_err(gettext("%s: failed to get BE "
"container dataset for %s/%s\n"), __func__,
list->be_rpool, datasets->be_dataset_name);
return (err);
}
if ((zhp = zfs_open(g_zfs, ds_path, ZFS_TYPE_DATASET))
== NULL) {
be_print_err(gettext("set_canmount: failed to "
"open dataset %s: %s\n"), ds_path,
libzfs_error_description(g_zfs));
err = zfs_err_to_be_err(g_zfs);
return (err);
}
if (zfs_prop_get_int(zhp, ZFS_PROP_MOUNTED)) {
/*
* it's already mounted so we can't change the
* canmount property anyway.
*/
err = BE_SUCCESS;
ZFS_CLOSE(zhp);
break;
}
err = zfs_prop_set(zhp,
zfs_prop_to_name(ZFS_PROP_CANMOUNT), value);
if (err) {
ZFS_CLOSE(zhp);
be_print_err(gettext("set_canmount: "
"Failed to set property value %s "
"for dataset %s: %s\n"), value, ds_path,
libzfs_error_description(g_zfs));
err = zfs_err_to_be_err(g_zfs);
return (err);
}
ZFS_CLOSE(zhp);
datasets = datasets->be_next_dataset;
}
list = list->be_next_node;
}
return (err);
}
/*
* Function: be_get_grub_vers
* Description: Gets the grub version number from /boot/grub/capability. If
* capability file doesn't exist NULL is returned.
* Parameters:
* bt - The transaction data for the BE we're getting the grub
* version for.
* cur_vers - used to return the current version of grub from
* the root pool.
* new_vers - used to return the grub version of the BE we're
* activating.
* Return:
* BE_SUCCESS - Success
* be_errno_t - Failed to find version
* Scope:
* Private
*/
static int
be_get_grub_vers(be_transaction_data_t *bt, char **cur_vers, char **new_vers)
{
zfs_handle_t *zhp = NULL;
zfs_handle_t *pool_zhp = NULL;
int ret = BE_SUCCESS;
char cap_file[MAXPATHLEN];
char *temp_mntpnt = NULL;
char *zpool_mntpt = NULL;
char *ptmp_mntpnt = NULL;
char *orig_mntpnt = NULL;
boolean_t be_mounted = B_FALSE;
boolean_t pool_mounted = B_FALSE;
if (!be_has_grub()) {
be_print_err(gettext("be_get_grub_vers: Not supported on "
"this architecture\n"));
return (BE_ERR_NOTSUP);
}
if (bt == NULL || bt->obe_name == NULL || bt->obe_zpool == NULL ||
bt->obe_root_ds == NULL) {
be_print_err(gettext("be_get_grub_vers: Invalid BE\n"));
return (BE_ERR_INVAL);
}
if ((pool_zhp = zfs_open(g_zfs, bt->obe_zpool, ZFS_TYPE_FILESYSTEM)) ==
NULL) {
be_print_err(gettext("be_get_grub_vers: zfs_open failed: %s\n"),
libzfs_error_description(g_zfs));
return (zfs_err_to_be_err(g_zfs));
}
/*
* Check to see if the pool's dataset is mounted. If it isn't we'll
* attempt to mount it.
*/
if ((ret = be_mount_pool(pool_zhp, &ptmp_mntpnt,
&orig_mntpnt, &pool_mounted)) != BE_SUCCESS) {
be_print_err(gettext("be_get_grub_vers: pool dataset "
"(%s) could not be mounted\n"), bt->obe_zpool);
ZFS_CLOSE(pool_zhp);
return (ret);
}
/*
* Get the mountpoint for the root pool dataset.
*/
if (!zfs_is_mounted(pool_zhp, &zpool_mntpt)) {
be_print_err(gettext("be_get_grub_vers: pool "
"dataset (%s) is not mounted. Can't read the "
"grub capability file.\n"), bt->obe_zpool);
ret = BE_ERR_NO_MENU;
goto cleanup;
}
/*
* get the version of the most recent grub update.
*/
(void) snprintf(cap_file, sizeof (cap_file), "%s%s",
zpool_mntpt, BE_CAP_FILE);
free(zpool_mntpt);
zpool_mntpt = NULL;
if ((ret = get_ver_from_capfile(cap_file, cur_vers)) != BE_SUCCESS)
goto cleanup;
if ((zhp = zfs_open(g_zfs, bt->obe_root_ds, ZFS_TYPE_FILESYSTEM)) ==
NULL) {
be_print_err(gettext("be_get_grub_vers: failed to "
"open BE root dataset (%s): %s\n"), bt->obe_root_ds,
libzfs_error_description(g_zfs));
free(cur_vers);
ret = zfs_err_to_be_err(g_zfs);
goto cleanup;
}
if (!zfs_is_mounted(zhp, &temp_mntpnt)) {
if ((ret = _be_mount(bt->obe_name, &temp_mntpnt,
BE_MOUNT_FLAG_NO_ZONES)) != BE_SUCCESS) {
be_print_err(gettext("be_get_grub_vers: failed to "
"mount BE (%s)\n"), bt->obe_name);
free(*cur_vers);
*cur_vers = NULL;
ZFS_CLOSE(zhp);
goto cleanup;
}
be_mounted = B_TRUE;
}
ZFS_CLOSE(zhp);
/*
* Now get the grub version for the BE being activated.
*/
(void) snprintf(cap_file, sizeof (cap_file), "%s%s", temp_mntpnt,
BE_CAP_FILE);
ret = get_ver_from_capfile(cap_file, new_vers);
if (ret != BE_SUCCESS) {
free(*cur_vers);
*cur_vers = NULL;
}
if (be_mounted)
(void) _be_unmount(bt->obe_name, 0);
cleanup:
if (pool_mounted) {
int iret = BE_SUCCESS;
iret = be_unmount_pool(pool_zhp, ptmp_mntpnt, orig_mntpnt);
if (ret == BE_SUCCESS)
ret = iret;
free(orig_mntpnt);
free(ptmp_mntpnt);
}
ZFS_CLOSE(pool_zhp);
free(temp_mntpnt);
return (ret);
}
/*
* Function: get_ver_from_capfile
* Description: Parses the capability file passed in looking for the VERSION
* line. If found the version is returned in vers, if not then
* NULL is returned in vers.
*
* Parameters:
* file - the path to the capability file we want to parse.
* vers - the version string that will be passed back.
* Return:
* BE_SUCCESS - Success
* be_errno_t - Failed to find version
* Scope:
* Private
*/
static int
get_ver_from_capfile(char *file, char **vers)
{
FILE *fp = NULL;
char line[BUFSIZ];
char *last = NULL;
int err = BE_SUCCESS;
errno = 0;
if (!be_has_grub()) {
be_print_err(gettext("get_ver_from_capfile: Not supported "
"on this architecture\n"));
return (BE_ERR_NOTSUP);
}
/*
* Set version string to NULL; the only case this shouldn't be set
* to be NULL is when we've actually found a version in the capability
* file, which is set below.
*/
*vers = NULL;
/*
* If the capability file doesn't exist, we're returning success
* because on older releases, the capability file did not exist
* so this is a valid scenario.
*/
if (access(file, F_OK) == 0) {
if ((fp = fopen(file, "r")) == NULL) {
err = errno;
be_print_err(gettext("get_ver_from_capfile: failed to "
"open file %s with error %s\n"), file,
strerror(err));
err = errno_to_be_err(err);
return (err);
}
while (fgets(line, BUFSIZ, fp)) {
char *tok = strtok_r(line, "=", &last);
if (tok == NULL || tok[0] == '#') {
continue;
} else if (strcmp(tok, "VERSION") == 0) {
*vers = strdup(last);
break;
}
}
(void) fclose(fp);
}
return (BE_SUCCESS);
}
/*
* To be able to boot EFI labeled disks, stage1 needs to be written
* into the MBR. We do not do this if we're on disks with a traditional
* fdisk partition table only, or if any foreign EFI partitions exist.
* In the trivial case of a whole-disk vdev we always write stage1 into
* the MBR.
*/
static boolean_t
be_do_install_mbr(char *diskname, nvlist_t *child)
{
struct uuid allowed_uuids[] = {
EFI_UNUSED,
EFI_RESV1,
EFI_BOOT,
EFI_ROOT,
EFI_SWAP,
EFI_USR,
EFI_BACKUP,
EFI_RESV2,
EFI_VAR,
EFI_HOME,
EFI_ALTSCTR,
EFI_RESERVED,
EFI_SYSTEM,
EFI_BIOS_BOOT,
EFI_SYMC_PUB,
EFI_SYMC_CDS
};
uint64_t whole;
struct dk_gpt *gpt;
struct uuid *u;
int fd, npart, i, j;
(void) nvlist_lookup_uint64(child, ZPOOL_CONFIG_WHOLE_DISK,
&whole);
if (whole)
return (B_TRUE);
if ((fd = open(diskname, O_RDONLY|O_NDELAY)) < 0)
return (B_FALSE);
if ((npart = efi_alloc_and_read(fd, &gpt)) <= 0)
return (B_FALSE);
for (i = 0; i != npart; i++) {
int match = 0;
u = &gpt->efi_parts[i].p_guid;
for (j = 0;
j != sizeof (allowed_uuids) / sizeof (struct uuid);
j++)
if (bcmp(u, &allowed_uuids[j],
sizeof (struct uuid)) == 0)
match++;
if (match == 0)
return (B_FALSE);
}
return (B_TRUE);
}
static int
be_do_installboot_helper(zpool_handle_t *zphp, nvlist_t *child, char *stage1,
char *stage2, uint16_t flags)
{
char install_cmd[MAXPATHLEN];
char be_run_cmd_errbuf[BUFSIZ];
char be_run_cmd_outbuf[BUFSIZ];
char diskname[MAXPATHLEN];
char *vname;
char *path, *type, *dsk_ptr;
char *flag = "";
int ret;
vdev_stat_t *vs;
uint_t vsc;
if (nvlist_lookup_string(child, ZPOOL_CONFIG_TYPE, &type) != 0) {
be_print_err(gettext("%s: failed to get device type\n"),
__func__);
return (BE_ERR_NODEV);
}
/* Skip indirect devices. */
if (strcmp(type, VDEV_TYPE_INDIRECT) == 0)
return (BE_ERR_NOTSUP);
if (nvlist_lookup_string(child, ZPOOL_CONFIG_PATH, &path) != 0) {
be_print_err(gettext("%s: failed to get device path\n"),
__func__);
return (BE_ERR_NODEV);
}
if ((nvlist_lookup_uint64_array(child, ZPOOL_CONFIG_VDEV_STATS,
(uint64_t **)&vs, &vsc) != 0) ||
vs->vs_state < VDEV_STATE_DEGRADED) {
/*
* Don't try to run installgrub on a vdev that is not ONLINE
* or DEGRADED. Try to print a warning for each such vdev.
*/
be_print_err(gettext("%s: vdev %s is %s, can't install "
"boot loader\n"), __func__, path,
zpool_state_to_name(vs->vs_state, vs->vs_aux));
return (BE_SUCCESS);
}
/*
* Modify the vdev path to point to the raw disk.
*/
path = strdup(path);
if (path == NULL)
return (BE_ERR_NOMEM);
dsk_ptr = strstr(path, "/dsk/");
if (dsk_ptr != NULL) {
*dsk_ptr = '\0';
dsk_ptr++;
} else {
dsk_ptr = "";
}
(void) snprintf(diskname, sizeof (diskname), "%s/r%s", path, dsk_ptr);
free(path);
vname = zpool_vdev_name(g_zfs, zphp, child, B_FALSE);
if (vname == NULL) {
be_print_err(gettext("%s: failed to get device name: %s\n"),
__func__, libzfs_error_description(g_zfs));
return (zfs_err_to_be_err(g_zfs));
}
if (be_is_isa("i386")) {
uint16_t force = flags & BE_INSTALLBOOT_FLAG_FORCE;
uint16_t mbr = flags & BE_INSTALLBOOT_FLAG_MBR;
if (force == BE_INSTALLBOOT_FLAG_FORCE) {
if (mbr == BE_INSTALLBOOT_FLAG_MBR ||
be_do_install_mbr(diskname, child))
flag = "-F -m -f";
else
flag = "-F";
} else {
if (mbr == BE_INSTALLBOOT_FLAG_MBR ||
be_do_install_mbr(diskname, child))
flag = "-m -f";
}
if (be_has_grub()) {
(void) snprintf(install_cmd, sizeof (install_cmd),
"%s %s %s %s %s", BE_INSTALL_GRUB, flag,
stage1, stage2, diskname);
} else {
/*
* With updated installboot, we only need boot
* directory.
*/
(void) snprintf(install_cmd, sizeof (install_cmd),
"%s %s -b %s %s", BE_INSTALL_BOOT, flag,
stage1, diskname);
}
} else if (be_is_isa("sparc")) {
if ((flags & BE_INSTALLBOOT_FLAG_FORCE) ==
BE_INSTALLBOOT_FLAG_FORCE)
flag = "-f -F zfs";
else
flag = "-F zfs";
(void) snprintf(install_cmd, sizeof (install_cmd),
"%s %s %s %s", BE_INSTALL_BOOT, flag, stage2, diskname);
} else {
be_print_err(gettext("%s: unsupported architecture.\n"),
__func__);
return (BE_ERR_BOOTFILE_INST);
}
*be_run_cmd_outbuf = '\0';
*be_run_cmd_errbuf = '\0';
ret = be_run_cmd(install_cmd, be_run_cmd_errbuf, BUFSIZ,
be_run_cmd_outbuf, BUFSIZ);
if (ret != BE_SUCCESS) {
be_print_err(gettext("%s: install failed for device %s.\n"),
__func__, vname);
ret = BE_ERR_BOOTFILE_INST;
}
be_print_err(gettext(" Command: \"%s\"\n"), install_cmd);
if (be_run_cmd_outbuf[0] != 0) {
be_print_err(gettext(" Output:\n"));
be_print_err("%s", be_run_cmd_outbuf);
}
if (be_run_cmd_errbuf[0] != 0) {
be_print_err(gettext(" Errors:\n"));
be_print_err("%s", be_run_cmd_errbuf);
}
free(vname);
return (ret);
}
/*
* Function: be_do_copy_grub_cap
* Description: This function will copy grub capability file to BE.
*
* Parameters:
* bt - The transaction data for the BE we're activating.
* Return:
* BE_SUCCESS - Success
* be_errno_t - Failure
*
* Scope:
* Private
*/
static int
be_do_copy_grub_cap(be_transaction_data_t *bt)
{
zfs_handle_t *zhp = NULL;
char cap_file[MAXPATHLEN];
char zpool_cap_file[MAXPATHLEN];
char line[BUFSIZ];
char *tmp_mntpnt = NULL;
char *orig_mntpnt = NULL;
char *pool_mntpnt = NULL;
FILE *cap_fp = NULL;
FILE *zpool_cap_fp = NULL;
int err = 0;
int ret = BE_SUCCESS;
boolean_t pool_mounted = B_FALSE;
boolean_t be_mounted = B_FALSE;
/*
* first get BE dataset mountpoint, we can free all the resources
* once cap_file is built, leaving only be unmount to be done.
*/
if ((zhp = zfs_open(g_zfs, bt->obe_root_ds, ZFS_TYPE_FILESYSTEM)) ==
NULL) {
be_print_err(gettext("%s: failed to "
"open BE root dataset (%s): %s\n"), __func__,
bt->obe_root_ds, libzfs_error_description(g_zfs));
return (zfs_err_to_be_err(g_zfs));
}
if (!zfs_is_mounted(zhp, &tmp_mntpnt)) {
if ((ret = _be_mount(bt->obe_name, &tmp_mntpnt,
BE_MOUNT_FLAG_NO_ZONES)) != BE_SUCCESS) {
be_print_err(gettext("%s: failed to "
"mount BE (%s)\n"), __func__, bt->obe_name);
ZFS_CLOSE(zhp);
goto done;
}
be_mounted = B_TRUE;
}
ZFS_CLOSE(zhp); /* BE dataset handle is not needed any more */
(void) snprintf(cap_file, sizeof (cap_file), "%s%s", tmp_mntpnt,
BE_CAP_FILE);
free(tmp_mntpnt);
/* get pool root dataset mountpoint */
zhp = zfs_open(g_zfs, bt->obe_zpool, ZFS_TYPE_FILESYSTEM);
if (zhp == NULL) {
be_print_err(gettext("%s: zfs_open failed: %s\n"),
__func__, libzfs_error_description(g_zfs));
ret = zfs_err_to_be_err(g_zfs);
goto done;
}
/*
* Check to see if the pool's dataset is mounted. If it isn't we'll
* attempt to mount it.
*/
if ((ret = be_mount_pool(zhp, &tmp_mntpnt,
&orig_mntpnt, &pool_mounted)) != BE_SUCCESS) {
be_print_err(gettext("%s: pool dataset "
"(%s) could not be mounted\n"), __func__, bt->obe_zpool);
ZFS_CLOSE(zhp);
goto done;
}
/*
* Get the mountpoint for the root pool dataset.
* NOTE: zhp must be kept for _be_unmount_pool()
*/
if (!zfs_is_mounted(zhp, &pool_mntpnt)) {
be_print_err(gettext("%s: pool "
"dataset (%s) is not mounted. Can't check the grub "
"version from the grub capability file.\n"), __func__,
bt->obe_zpool);
ret = BE_ERR_NO_MENU;
goto done;
}
(void) snprintf(zpool_cap_file, sizeof (zpool_cap_file), "%s%s",
pool_mntpnt, BE_CAP_FILE);
free(pool_mntpnt);
if ((cap_fp = fopen(cap_file, "r")) == NULL) {
err = errno;
be_print_err(gettext("%s: failed to open grub "
"capability file\n"), __func__);
ret = errno_to_be_err(err);
goto done;
}
if ((zpool_cap_fp = fopen(zpool_cap_file, "w")) == NULL) {
err = errno;
be_print_err(gettext("%s: failed to open new "
"grub capability file\n"), __func__);
ret = errno_to_be_err(err);
(void) fclose(cap_fp);
goto done;
}
while (fgets(line, BUFSIZ, cap_fp)) {
(void) fputs(line, zpool_cap_fp);
}
(void) fclose(zpool_cap_fp);
(void) fclose(cap_fp);
done:
if (be_mounted)
(void) _be_unmount(bt->obe_name, 0);
if (pool_mounted) {
err = be_unmount_pool(zhp, tmp_mntpnt, orig_mntpnt);
if (ret == BE_SUCCESS)
ret = err;
free(orig_mntpnt);
free(tmp_mntpnt);
zfs_close(zhp);
}
return (ret);
}
/*
* Function: be_is_install_needed
* Description: Check detached version files to detect if bootloader
* install/update is needed.
*
* Parameters:
* bt - The transaction data for the BE we're activating.
* update - set B_TRUE is update is needed.
* Return:
* BE_SUCCESS - Success
* be_errno_t - Failure
*
* Scope:
* Private
*/
static int
be_is_install_needed(be_transaction_data_t *bt, boolean_t *update)
{
int ret = BE_SUCCESS;
char *cur_vers = NULL, *new_vers = NULL;
assert(bt != NULL);
assert(update != NULL);
if (!be_has_grub()) {
/*
* no detached versioning, let installboot to manage
* versioning.
*/
*update = B_TRUE;
return (ret);
}
*update = B_FALSE; /* set default */
/*
* We need to check to see if the version number from
* the BE being activated is greater than the current
* one.
*/
ret = be_get_grub_vers(bt, &cur_vers, &new_vers);
if (ret != BE_SUCCESS) {
be_print_err(gettext("be_activate: failed to get grub "
"versions from capability files.\n"));
return (ret);
}
/* update if we have both versions and can compare */
if (cur_vers != NULL) {
if (new_vers != NULL) {
if (atof(cur_vers) < atof(new_vers))
*update = B_TRUE;
free(new_vers);
}
free(cur_vers);
} else if (new_vers != NULL) {
/* we only got new version - update */
*update = B_TRUE;
free(new_vers);
}
return (ret);
}
static int
be_do_installboot_walk(zpool_handle_t *zphp, nvlist_t *nv, char *stage1,
char *stage2, uint16_t flags)
{
boolean_t verbose = do_print;
nvlist_t **child;
uint_t children = 0;
int ret = -1;
/* It is OK to have no children. */
(void) nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
&children);
for (int c = 0; c < children; c++) {
char *vname;
int rv;
/* ensure update on child status */
vname = zpool_vdev_name(g_zfs, zphp, child[c], verbose);
if (vname == NULL) {
be_print_err(gettext("%s: "
"failed to get device name: %s\n"), __func__,
libzfs_error_description(g_zfs));
return (zfs_err_to_be_err(g_zfs));
} else {
be_print_err(gettext("%s: child %d of %d device %s\n"),
__func__, c, children, vname);
}
rv = be_do_installboot_walk(zphp, child[c], stage1, stage2,
flags);
switch (rv) {
case BE_ERR_NOTSUP:
/* ignore unsupported devices */
be_print_err(
gettext("%s: device %s is not supported\n"),
__func__, vname);
break;
case BE_SUCCESS:
/* catch at least one success */
ret = rv;
break;
default:
if (ret == -1)
ret = rv;
break;
}
free(vname);
}
if (children > 0)
return (ret == -1? BE_ERR_NOTSUP : ret);
return (be_do_installboot_helper(zphp, nv, stage1, stage2, flags));
}
/*
* Function: be_do_installboot
* Description: This function runs installgrub/installboot using the boot
* loader files from the BE we're activating and installing
* them on the pool the BE lives in.
*
* Parameters:
* bt - The transaction data for the BE we're activating.
* flags - flags for bootloader install
* Return:
* BE_SUCCESS - Success
* be_errno_t - Failure
*
* Scope:
* Private
*/
static int
be_do_installboot(be_transaction_data_t *bt, uint16_t flags)
{
zpool_handle_t *zphp = NULL;
zfs_handle_t *zhp = NULL;
nvlist_t *nv, *config;
char *tmp_mntpt = NULL;
char stage1[MAXPATHLEN];
char stage2[MAXPATHLEN];
int ret = BE_SUCCESS;
boolean_t be_mounted = B_FALSE;
boolean_t update = B_FALSE;
/*
* check versions. This call is to support detached
* version implementation like grub. Embedded versioning is
* checked by actual installer.
*/
if ((flags & BE_INSTALLBOOT_FLAG_FORCE) != BE_INSTALLBOOT_FLAG_FORCE) {
ret = be_is_install_needed(bt, &update);
if (ret != BE_SUCCESS || update == B_FALSE)
return (ret);
}
if ((zhp = zfs_open(g_zfs, bt->obe_root_ds, ZFS_TYPE_FILESYSTEM)) ==
NULL) {
be_print_err(gettext("%s: failed to "
"open BE root dataset (%s): %s\n"), __func__,
bt->obe_root_ds, libzfs_error_description(g_zfs));
ret = zfs_err_to_be_err(g_zfs);
return (ret);
}
if (!zfs_is_mounted(zhp, &tmp_mntpt)) {
if ((ret = _be_mount(bt->obe_name, &tmp_mntpt,
BE_MOUNT_FLAG_NO_ZONES)) != BE_SUCCESS) {
be_print_err(gettext("%s: failed to "
"mount BE (%s)\n"), __func__, bt->obe_name);
ZFS_CLOSE(zhp);
return (ret);
}
be_mounted = B_TRUE;
}
ZFS_CLOSE(zhp);
if (be_is_isa("i386")) {
if (be_has_grub()) {
(void) snprintf(stage1, sizeof (stage1), "%s%s",
tmp_mntpt, BE_GRUB_STAGE_1);
(void) snprintf(stage2, sizeof (stage2), "%s%s",
tmp_mntpt, BE_GRUB_STAGE_2);
} else {
(void) snprintf(stage1, sizeof (stage1), "%s%s",
tmp_mntpt, BE_LOADER_STAGES);
/* Skip stage2 */
}
} else if (be_is_isa("sparc")) {
char *platform = be_get_platform();
if (platform == NULL) {
be_print_err(gettext("%s: failed to detect system "
"platform name\n"), __func__);
if (be_mounted)
(void) _be_unmount(bt->obe_name, 0);
free(tmp_mntpt);
return (BE_ERR_BOOTFILE_INST);
}
stage1[0] = '\0'; /* sparc has no stage1 */
(void) snprintf(stage2, sizeof (stage2),
"%s/usr/platform/%s%s", tmp_mntpt,
platform, BE_SPARC_BOOTBLK);
} else {
be_print_err(gettext("%s: unsupported architecture.\n"),
__func__);
return (BE_ERR_BOOTFILE_INST);
}
if ((zphp = zpool_open(g_zfs, bt->obe_zpool)) == NULL) {
be_print_err(gettext("%s: failed to open "
"pool (%s): %s\n"), __func__, bt->obe_zpool,
libzfs_error_description(g_zfs));
ret = zfs_err_to_be_err(g_zfs);
if (be_mounted)
(void) _be_unmount(bt->obe_name, 0);
free(tmp_mntpt);
return (ret);
}
if ((config = zpool_get_config(zphp, NULL)) == NULL) {
be_print_err(gettext("%s: failed to get zpool "
"configuration information. %s\n"), __func__,
libzfs_error_description(g_zfs));
ret = zfs_err_to_be_err(g_zfs);
goto done;
}
/*
* Get the vdev tree
*/
if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nv) != 0) {
be_print_err(gettext("%s: failed to get vdev "
"tree: %s\n"), __func__, libzfs_error_description(g_zfs));
ret = zfs_err_to_be_err(g_zfs);
goto done;
}
ret = be_do_installboot_walk(zphp, nv, stage1, stage2, flags);
if (be_has_grub()) {
ret = be_do_copy_grub_cap(bt);
}
done:
ZFS_CLOSE(zhp);
if (be_mounted)
(void) _be_unmount(bt->obe_name, 0);
zpool_close(zphp);
free(tmp_mntpt);
return (ret);
}
/*
* Function: be_promote_zone_ds
* Description: This function finds the zones for the BE being activated
* and the active zonepath dataset for each zone. Then each
* active zonepath dataset is promoted.
*
* Parameters:
* be_name - the name of the global zone BE that we need to
* find the zones for.
* be_root_ds - the root dataset for be_name.
* Return:
* BE_SUCCESS - Success
* be_errno_t - Failure
*
* Scope:
* Private
*/
static int
be_promote_zone_ds(char *be_name, char *be_root_ds)
{
char *zone_ds = NULL;
char *temp_mntpt = NULL;
char origin[MAXPATHLEN];
char zoneroot_ds[MAXPATHLEN];
zfs_handle_t *zhp = NULL;
zfs_handle_t *z_zhp = NULL;
zoneList_t zone_list = NULL;
zoneBrandList_t *brands = NULL;
boolean_t be_mounted = B_FALSE;
int zone_index = 0;
int err = BE_SUCCESS;
/*
* Get the supported zone brands so we can pass that
* to z_get_nonglobal_zone_list_by_brand. Currently
* only the ipkg and labeled brand zones are supported
*
*/
if ((brands = be_get_supported_brandlist()) == NULL) {
be_print_err(gettext("be_promote_zone_ds: no supported "
"brands\n"));
return (BE_SUCCESS);
}
if ((zhp = zfs_open(g_zfs, be_root_ds,
ZFS_TYPE_FILESYSTEM)) == NULL) {
be_print_err(gettext("be_promote_zone_ds: Failed to open "
"dataset (%s): %s\n"), be_root_ds,
libzfs_error_description(g_zfs));
err = zfs_err_to_be_err(g_zfs);
z_free_brand_list(brands);
return (err);
}
if (!zfs_is_mounted(zhp, &temp_mntpt)) {
if ((err = _be_mount(be_name, &temp_mntpt,
BE_MOUNT_FLAG_NO_ZONES)) != BE_SUCCESS) {
be_print_err(gettext("be_promote_zone_ds: failed to "
"mount the BE for zones procesing.\n"));
ZFS_CLOSE(zhp);
z_free_brand_list(brands);
return (err);
}
be_mounted = B_TRUE;
}
/*
* Set the zone root to the temp mount point for the BE we just mounted.
*/
z_set_zone_root(temp_mntpt);
/*
* Get all the zones based on the brands we're looking for. If no zones
* are found that we're interested in unmount the BE and move on.
*/
if ((zone_list = z_get_nonglobal_zone_list_by_brand(brands)) == NULL) {
if (be_mounted)
(void) _be_unmount(be_name, 0);
ZFS_CLOSE(zhp);
z_free_brand_list(brands);
free(temp_mntpt);
return (BE_SUCCESS);
}
for (zone_index = 0; z_zlist_get_zonename(zone_list, zone_index)
!= NULL; zone_index++) {
char *zone_path = NULL;
/* Skip zones that aren't at least installed */
if (z_zlist_get_current_state(zone_list, zone_index) <
ZONE_STATE_INSTALLED)
continue;
if (((zone_path =
z_zlist_get_zonepath(zone_list, zone_index)) == NULL) ||
((zone_ds = be_get_ds_from_dir(zone_path)) == NULL) ||
!be_zone_supported(zone_ds))
continue;
if (be_find_active_zone_root(zhp, zone_ds,
zoneroot_ds, sizeof (zoneroot_ds)) != 0) {
be_print_err(gettext("be_promote_zone_ds: "
"Zone does not have an active root "
"dataset, skipping this zone.\n"));
continue;
}
if ((z_zhp = zfs_open(g_zfs, zoneroot_ds,
ZFS_TYPE_FILESYSTEM)) == NULL) {
be_print_err(gettext("be_promote_zone_ds: "
"Failed to open dataset "
"(%s): %s\n"), zoneroot_ds,
libzfs_error_description(g_zfs));
err = zfs_err_to_be_err(g_zfs);
goto done;
}
if (zfs_prop_get(z_zhp, ZFS_PROP_ORIGIN, origin,
sizeof (origin), NULL, NULL, 0, B_FALSE) != 0) {
ZFS_CLOSE(z_zhp);
continue;
}
/*
* We don't need to close the zfs handle at this
* point because the callback funtion
* be_promote_ds_callback() will close it for us.
*/
if (be_promote_ds_callback(z_zhp, NULL) != 0) {
be_print_err(gettext("be_promote_zone_ds: "
"failed to activate the "
"datasets for %s: %s\n"),
zoneroot_ds,
libzfs_error_description(g_zfs));
err = BE_ERR_PROMOTE;
goto done;
}
}
done:
if (be_mounted)
(void) _be_unmount(be_name, 0);
ZFS_CLOSE(zhp);
free(temp_mntpt);
z_free_brand_list(brands);
z_free_zone_list(zone_list);
return (err);
}
/*
* Function: be_promote_ds_callback
* Description: This function is used to promote the datasets for the BE
* being activated as well as the datasets for the zones BE
* being activated.
*
* Parameters:
* zhp - the zfs handle for zone BE being activated.
* data - not used.
* Return:
* 0 - Success
* be_errno_t - Failure
*
* Scope:
* Private
*/
static int
/* LINTED */
be_promote_ds_callback(zfs_handle_t *zhp, void *data)
{
char origin[MAXPATHLEN];
char *sub_dataset = NULL;
int ret = 0;
if (zhp != NULL) {
sub_dataset = strdup(zfs_get_name(zhp));
if (sub_dataset == NULL) {
ret = BE_ERR_NOMEM;
goto done;
}
} else {
be_print_err(gettext("be_promote_ds_callback: "
"Invalid zfs handle passed into function\n"));
ret = BE_ERR_INVAL;
goto done;
}
/*
* This loop makes sure that we promote the dataset to the
* top of the tree so that it is no longer a decendent of any
* dataset. The ZFS close and then open is used to make sure that
* the promotion is updated before we move on.
*/
while (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, origin,
sizeof (origin), NULL, NULL, 0, B_FALSE) == 0) {
if (zfs_promote(zhp) != 0) {
if (libzfs_errno(g_zfs) != EZFS_EXISTS) {
be_print_err(gettext("be_promote_ds_callback: "
"promote of %s failed: %s\n"),
zfs_get_name(zhp),
libzfs_error_description(g_zfs));
ret = zfs_err_to_be_err(g_zfs);
goto done;
} else {
/*
* If the call to zfs_promote returns the
* error EZFS_EXISTS we've hit a snapshot name
* collision. This means we're probably
* attemping to promote a zone dataset above a
* parent dataset that belongs to another zone
* which this zone was cloned from.
*
* TODO: If this is a zone dataset at some
* point we should skip this if the zone
* paths for the dataset and the snapshot
* don't match.
*/
be_print_err(gettext("be_promote_ds_callback: "
"promote of %s failed due to snapshot "
"name collision: %s\n"), zfs_get_name(zhp),
libzfs_error_description(g_zfs));
ret = zfs_err_to_be_err(g_zfs);
goto done;
}
}
ZFS_CLOSE(zhp);
if ((zhp = zfs_open(g_zfs, sub_dataset,
ZFS_TYPE_FILESYSTEM)) == NULL) {
be_print_err(gettext("be_promote_ds_callback: "
"Failed to open dataset (%s): %s\n"), sub_dataset,
libzfs_error_description(g_zfs));
ret = zfs_err_to_be_err(g_zfs);
goto done;
}
}
/* Iterate down this dataset's children and promote them */
ret = zfs_iter_filesystems(zhp, be_promote_ds_callback, NULL);
done:
free(sub_dataset);
ZFS_CLOSE(zhp);
return (ret);
}
|