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
|
/*
* 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 2013 Nexenta Systems, Inc. All rights reserved.
* Copyright 2022 OmniOS Community Edition (OmniOSce) Association.
*/
/*
* System includes
*/
#include <assert.h>
#include <errno.h>
#include <libintl.h>
#include <libnvpair.h>
#include <libzfs.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mntent.h>
#include <sys/mnttab.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/vfstab.h>
#include <unistd.h>
#include <libbe.h>
#include <libbe_priv.h>
typedef struct active_zone_root_data {
uuid_t parent_uuid;
char *zoneroot_ds;
} active_zone_root_data_t;
typedef struct mounted_zone_root_data {
char *zone_altroot;
char *zoneroot_ds;
} mounted_zone_root_data_t;
/* Private function prototypes */
static int be_find_active_zone_root_callback(zfs_handle_t *, void *);
static int be_find_mounted_zone_root_callback(zfs_handle_t *, void *);
static boolean_t be_zone_get_active(zfs_handle_t *);
/* ******************************************************************** */
/* Semi-Private Functions */
/* ******************************************************************** */
/*
* Function: be_make_zoneroot
* Description: Generate a string for a zone's zoneroot given the
* zone's zonepath.
* Parameters:
* zonepath - pointer to zonepath
* zoneroot - pointer to buffer to retrn zoneroot in.
* zoneroot_size - size of zoneroot
* Returns:
* None
* Scope:
* Semi-private (library wise use only)
*/
void
be_make_zoneroot(char *zonepath, char *zoneroot, int zoneroot_size)
{
(void) snprintf(zoneroot, zoneroot_size, "%s/root", zonepath);
}
/*
* Function: be_find_active_zone_root
* Description: This function will find the active zone root of a zone for
* a given global BE. It will iterate all of the zone roots
* under a zonepath, find the zone roots that belong to the
* specified global BE, and return the one that is active.
* Parameters:
* be_zhp - zfs handle to global BE root dataset.
* zonepath_ds - pointer to zone's zonepath dataset.
* zoneroot_ds - pointer to a buffer to store the dataset name of
* the zone's zoneroot that's currently active for this
* given global BE..
* zoneroot-ds_size - size of zoneroot_ds.
* Returns:
* BE_SUCCESS - Success
* be_errno_t - Failure
* Scope:
* Semi-private (library wide use only)
*/
int
be_find_active_zone_root(zfs_handle_t *be_zhp, char *zonepath_ds,
char *zoneroot_ds, int zoneroot_ds_size)
{
active_zone_root_data_t azr_data = { { 0 }, NULL };
zfs_handle_t *zhp;
char zone_container_ds[MAXPATHLEN];
int ret = BE_SUCCESS;
/* Get the uuid of the parent global BE */
if (getzoneid() == GLOBAL_ZONEID) {
if ((ret = be_get_uuid(zfs_get_name(be_zhp),
&azr_data.parent_uuid)) != BE_SUCCESS) {
be_print_err(gettext("be_find_active_zone_root: failed "
"to get uuid for BE root dataset %s\n"),
zfs_get_name(be_zhp));
return (ret);
}
} else {
if ((ret = be_zone_get_parent_uuid(zfs_get_name(be_zhp),
&azr_data.parent_uuid)) != BE_SUCCESS) {
be_print_err(gettext("be_find_active_zone_root: failed "
"to get parentbe uuid for zone root dataset %s\n"),
zfs_get_name(be_zhp));
return (ret);
}
}
/* Generate string for the root container dataset for this zone. */
if ((ret = be_make_container_ds(zonepath_ds, zone_container_ds,
sizeof (zone_container_ds))) != BE_SUCCESS) {
be_print_err(gettext("%s: failed to get BE container dataset "
"for %s\n"), __func__, zonepath_ds);
return (ret);
}
/* Get handle to this zone's root container dataset */
if ((zhp = zfs_open(g_zfs, zone_container_ds, ZFS_TYPE_FILESYSTEM))
== NULL) {
be_print_err(gettext("be_find_active_zone_root: failed to "
"open zone root container dataset (%s): %s\n"),
zone_container_ds, libzfs_error_description(g_zfs));
return (zfs_err_to_be_err(g_zfs));
}
/*
* Iterate through all of this zone's BEs, looking for ones
* that belong to the parent global BE, and finding the one
* that is marked active.
*/
if ((ret = zfs_iter_filesystems(zhp, be_find_active_zone_root_callback,
&azr_data)) != 0) {
be_print_err(gettext("be_find_active_zone_root: failed to "
"find active zone root in zonepath dataset %s: %s\n"),
zonepath_ds, be_err_to_str(ret));
goto done;
}
if (azr_data.zoneroot_ds != NULL) {
(void) strlcpy(zoneroot_ds, azr_data.zoneroot_ds,
zoneroot_ds_size);
free(azr_data.zoneroot_ds);
} else {
be_print_err(gettext("be_find_active_zone_root: failed to "
"find active zone root in zonepath dataset %s\n"),
zonepath_ds);
ret = BE_ERR_ZONE_NO_ACTIVE_ROOT;
}
done:
ZFS_CLOSE(zhp);
return (ret);
}
/*
* Function: be_find_mounted_zone_root
* Description: This function will find the dataset mounted as the zoneroot
* of a zone for a given mounted global BE.
* Parameters:
* zone_altroot - path of zoneroot wrt the mounted global BE.
* zonepath_ds - dataset of the zone's zonepath.
* zoneroot_ds - pointer to a buffer to store the dataset of
* the zoneroot that currently mounted for this zone
* in the mounted global BE.
* zoneroot_ds_size - size of zoneroot_ds
* Returns:
* BE_SUCCESS - Success
* be_errno_t - Failure
* Scope:
* Semi-private (library wide use only)
*/
int
be_find_mounted_zone_root(char *zone_altroot, char *zonepath_ds,
char *zoneroot_ds, int zoneroot_ds_size)
{
mounted_zone_root_data_t mzr_data = { 0 };
zfs_handle_t *zhp = NULL;
char zone_container_ds[MAXPATHLEN];
int ret = BE_SUCCESS;
int zret = 0;
/* Generate string for the root container dataset for this zone. */
if ((ret = be_make_container_ds(zonepath_ds, zone_container_ds,
sizeof (zone_container_ds))) != BE_SUCCESS) {
be_print_err(gettext("%s: failed to get BE container dataset "
"for %s\n"), __func__, zonepath_ds);
return (ret);
}
/* Get handle to this zone's root container dataset. */
if ((zhp = zfs_open(g_zfs, zone_container_ds, ZFS_TYPE_FILESYSTEM))
== NULL) {
be_print_err(gettext("be_find_mounted_zone_root: failed to "
"open zone root container dataset (%s): %s\n"),
zone_container_ds, libzfs_error_description(g_zfs));
return (zfs_err_to_be_err(g_zfs));
}
mzr_data.zone_altroot = zone_altroot;
/*
* Iterate through all of the zone's BEs, looking for the one
* that is currently mounted at the zone altroot in the mounted
* global BE.
*/
if ((zret = zfs_iter_filesystems(zhp,
be_find_mounted_zone_root_callback, &mzr_data)) == 0) {
be_print_err(gettext("be_find_mounted_zone_root: did not "
"find mounted zone under altroot zonepath %s\n"),
zonepath_ds);
ret = BE_ERR_NO_MOUNTED_ZONE;
goto done;
} else if (zret < 0) {
be_print_err(gettext("be_find_mounted_zone_root: "
"zfs_iter_filesystems failed: %s\n"),
libzfs_error_description(g_zfs));
ret = zfs_err_to_be_err(g_zfs);
goto done;
}
if (mzr_data.zoneroot_ds != NULL) {
(void) strlcpy(zoneroot_ds, mzr_data.zoneroot_ds,
zoneroot_ds_size);
free(mzr_data.zoneroot_ds);
}
done:
ZFS_CLOSE(zhp);
return (ret);
}
/*
* Function: be_zone_supported
* Description: This function will determine if a zone is supported
* based on its zonepath dataset. The zonepath dataset
* must:
* - not be under any global BE root dataset.
* - have a root container dataset underneath it.
*
* Parameters:
* zonepath_ds - name of dataset of the zonepath of the
* zone to check.
* Returns:
* B_TRUE - zone is supported
* B_FALSE - zone is not supported
* Scope:
* Semi-private (library wide use only)
*/
boolean_t
be_zone_supported(char *zonepath_ds)
{
char zone_container_ds[MAXPATHLEN];
int ret = 0;
/*
* Make sure the dataset for the zonepath is not hierarchically
* under any reserved BE root container dataset of any pool.
*/
if ((ret = zpool_iter(g_zfs, be_check_be_roots_callback,
zonepath_ds)) > 0) {
be_print_err(gettext("be_zone_supported: "
"zonepath dataset %s not supported\n"), zonepath_ds);
return (B_FALSE);
} else if (ret < 0) {
be_print_err(gettext("be_zone_supported: "
"zpool_iter failed: %s\n"),
libzfs_error_description(g_zfs));
return (B_FALSE);
}
/*
* Make sure the zonepath has a zone root container dataset
* underneath it.
*/
if ((ret = be_make_container_ds(zonepath_ds, zone_container_ds,
sizeof (zone_container_ds))) != BE_SUCCESS) {
be_print_err(gettext("%s: failed to get BE container dataset "
"for %s\n"), __func__, zonepath_ds);
return (B_FALSE);
}
if (!zfs_dataset_exists(g_zfs, zone_container_ds,
ZFS_TYPE_FILESYSTEM)) {
be_print_err(gettext("be_zone_supported: "
"zonepath dataset (%s) does not have a zone root container "
"dataset, zone is not supported, skipping ...\n"),
zonepath_ds);
return (B_FALSE);
}
return (B_TRUE);
}
/*
* Function: be_get_supported_brandlist
* Desciption: This functions retuns a list of supported brands in
* a zoneBrandList_t object.
* Parameters:
* None
* Returns:
* Failure - NULL if no supported brands found.
* Success - pointer to zoneBrandList structure.
* Scope:
* Semi-private (library wide use only)
*/
zoneBrandList_t *
be_get_supported_brandlist(void)
{
return (z_make_brand_list(BE_ZONE_SUPPORTED_BRANDS,
BE_ZONE_SUPPORTED_BRANDS_DELIM));
}
/*
* Function: be_zone_get_parent_uuid
* Description: This function gets the parentbe property of a zone root
* dataset, parsed it into internal uuid format, and returns
* it in the uuid_t reference pointer passed in.
* Parameters:
* root_ds - dataset name of a zone root dataset
* uu - pointer to a uuid_t to return the parentbe uuid in
* Returns:
* BE_SUCCESS - Success
* be_errno_t - Failure
* Scope:
* Private
*/
int
be_zone_get_parent_uuid(const char *root_ds, uuid_t *uu)
{
zfs_handle_t *zhp = NULL;
nvlist_t *userprops = NULL;
nvlist_t *propname = NULL;
char *uu_string = NULL;
int ret = BE_SUCCESS;
/* Get handle to zone root dataset */
if ((zhp = zfs_open(g_zfs, root_ds, ZFS_TYPE_FILESYSTEM)) == NULL) {
be_print_err(gettext("be_zone_get_parent_uuid: failed to "
"open zone root dataset (%s): %s\n"), root_ds,
libzfs_error_description(g_zfs));
return (zfs_err_to_be_err(g_zfs));
}
/* Get user properties for zone root dataset */
if ((userprops = zfs_get_user_props(zhp)) == NULL) {
be_print_err(gettext("be_zone_get_parent_uuid: "
"failed to get user properties for zone root "
"dataset (%s): %s\n"), root_ds,
libzfs_error_description(g_zfs));
ret = zfs_err_to_be_err(g_zfs);
goto done;
}
/* Get UUID string from zone's root dataset user properties */
if (nvlist_lookup_nvlist(userprops, BE_ZONE_PARENTBE_PROPERTY,
&propname) != 0 || nvlist_lookup_string(propname, ZPROP_VALUE,
&uu_string) != 0) {
be_print_err(gettext("be_zone_get_parent_uuid: failed to "
"get parent uuid property from zone root dataset user "
"properties.\n"));
ret = BE_ERR_ZONE_NO_PARENTBE;
goto done;
}
/* Parse the uuid string into internal format */
if (uuid_parse(uu_string, *uu) != 0 || uuid_is_null(*uu)) {
be_print_err(gettext("be_zone_get_parent_uuid: failed to "
"parse parentuuid\n"));
ret = BE_ERR_PARSE_UUID;
}
done:
ZFS_CLOSE(zhp);
return (ret);
}
/*
* Function: be_zone_set_parent_uuid
* Description: This function sets parentbe uuid into
* a zfs user property for a root zone dataset.
* Parameters:
* root_ds - Root zone dataset of the BE to set a uuid on.
* Return:
* be_errno_t - Failure
* BE_SUCCESS - Success
* Scope:
* Semi-private (library wide uses only)
*/
int
be_zone_set_parent_uuid(char *root_ds, uuid_t uu)
{
zfs_handle_t *zhp = NULL;
char uu_string[UUID_PRINTABLE_STRING_LENGTH];
int ret = BE_SUCCESS;
uuid_unparse(uu, uu_string);
/* Get handle to the root zone dataset. */
if ((zhp = zfs_open(g_zfs, root_ds, ZFS_TYPE_FILESYSTEM)) == NULL) {
be_print_err(gettext("be_zone_set_parent_uuid: failed to "
"open root zone dataset (%s): %s\n"), root_ds,
libzfs_error_description(g_zfs));
return (zfs_err_to_be_err(g_zfs));
}
/* Set parentbe uuid property for the root zone dataset */
if (zfs_prop_set(zhp, BE_ZONE_PARENTBE_PROPERTY, uu_string) != 0) {
be_print_err(gettext("be_zone_set_parent_uuid: failed to "
"set parentbe uuid property for root zone dataset: %s\n"),
libzfs_error_description(g_zfs));
ret = zfs_err_to_be_err(g_zfs);
}
ZFS_CLOSE(zhp);
return (ret);
}
/*
* Function: be_zone_compare_uuids
* Description: This function compare the parentbe uuid of the
* current running root zone dataset with the parentbe
* uuid of the given root zone dataset.
* Parameters:
* root_ds - Root zone dataset of the BE to compare.
* Return:
* B_TRUE - root dataset has right parentbe uuid
* B_FALSE - root dataset has wrong parentbe uuid
* Scope:
* Semi-private (library wide uses only)
*/
boolean_t
be_zone_compare_uuids(char *root_ds)
{
char *active_ds;
uuid_t parent_uuid = {0};
uuid_t cur_parent_uuid = {0};
/* Get parentbe uuid from given zone root dataset */
if ((be_zone_get_parent_uuid(root_ds,
&parent_uuid)) != BE_SUCCESS) {
be_print_err(gettext("be_zone_compare_uuids: failed to get "
"parentbe uuid from the given BE\n"));
return (B_FALSE);
}
/*
* Find current running zone root dataset and get it's parentbe
* uuid property.
*/
if ((active_ds = be_get_ds_from_dir("/")) != NULL) {
if ((be_zone_get_parent_uuid(active_ds,
&cur_parent_uuid)) != BE_SUCCESS) {
be_print_err(gettext("be_zone_compare_uuids: failed "
"to get parentbe uuid from the current running zone "
"root dataset\n"));
return (B_FALSE);
}
} else {
be_print_err(gettext("be_zone_compare_uuids: zone root dataset "
"is not mounted\n"));
return (B_FALSE);
}
if (uuid_compare(parent_uuid, cur_parent_uuid) != 0) {
return (B_FALSE);
}
return (B_TRUE);
}
/* ******************************************************************** */
/* Private Functions */
/* ******************************************************************** */
/*
* Function: be_find_active_zone_root_callback
* Description: This function is used as a callback to iterate over all of
* a zone's root datasets, finding the one that is marked active
* for the parent BE specified in the data passed in. The name
* of the zone's active root dataset is returned in heap storage
* in the active_zone_root_data_t structure passed in, so the
* caller is responsible for freeing it.
* Parameters:
* zhp - zfs_handle_t pointer to current dataset being processed
* data - active_zone_root_data_t pointer
* Returns:
* 0 - Success
* >0 - Failure
* Scope:
* Private
*/
static int
be_find_active_zone_root_callback(zfs_handle_t *zhp, void *data)
{
active_zone_root_data_t *azr_data = data;
uuid_t parent_uuid = { 0 };
int iret = 0;
int ret = 0;
if ((iret = be_zone_get_parent_uuid(zfs_get_name(zhp), &parent_uuid))
!= BE_SUCCESS) {
be_print_err(gettext("be_find_active_zone_root_callback: "
"skipping zone root dataset (%s): %s\n"),
zfs_get_name(zhp), be_err_to_str(iret));
goto done;
}
if (uuid_compare(azr_data->parent_uuid, parent_uuid) == 0) {
/*
* Found a zone root dataset belonging to the right parent,
* check if its active.
*/
if (be_zone_get_active(zhp)) {
/*
* Found active zone root dataset, if its already
* set in the callback data, that means this
* is the second one we've found. Return error.
*/
if (azr_data->zoneroot_ds != NULL) {
ret = BE_ERR_ZONE_MULTIPLE_ACTIVE;
goto done;
}
azr_data->zoneroot_ds = strdup(zfs_get_name(zhp));
if (azr_data->zoneroot_ds == NULL) {
ret = BE_ERR_NOMEM;
}
}
}
done:
ZFS_CLOSE(zhp);
return (ret);
}
/*
* Function: be_find_mounted_zone_root_callback
* Description: This function is used as a callback to iterate over all of
* a zone's root datasets, find the one that is currently
* mounted for the parent BE specified in the data passed in.
* The name of the zone's mounted root dataset is returned in
* heap storage the mounted_zone_data_t structure passed in,
* so the caller is responsible for freeing it.
* Parameters:
* zhp - zfs_handle_t pointer to the current dataset being
* processed
* data - mounted_zone_data_t pointer
* Returns:
* 0 - not mounted as zone's root
* 1 - this dataset is mounted as zone's root
* Scope:
* Private
*/
static int
be_find_mounted_zone_root_callback(zfs_handle_t *zhp, void *data)
{
mounted_zone_root_data_t *mzr_data = data;
char *mp = NULL;
if (zfs_is_mounted(zhp, &mp) && mp != NULL &&
strcmp(mp, mzr_data->zone_altroot) == 0) {
mzr_data->zoneroot_ds = strdup(zfs_get_name(zhp));
free(mp);
return (1);
}
free(mp);
return (0);
}
/*
* Function: be_zone_get_active
* Description: This function gets the active property of a zone root
* dataset, and returns true if active property is on.
* Parameters:
* zfs - zfs_handle_t pointer to zone root dataset to check
* Returns:
* B_TRUE - zone root dataset is active
* B_FALSE - zone root dataset is not active
* Scope:
* Private
*/
static boolean_t
be_zone_get_active(zfs_handle_t *zhp)
{
nvlist_t *userprops = NULL;
nvlist_t *propname = NULL;
char *active_str = NULL;
/* Get user properties for the zone root dataset */
if ((userprops = zfs_get_user_props(zhp)) == NULL) {
be_print_err(gettext("be_zone_get_active: "
"failed to get user properties for zone root "
"dataset (%s): %s\n"), zfs_get_name(zhp),
libzfs_error_description(g_zfs));
return (B_FALSE);
}
/* Get active property from the zone root dataset user properties */
if (nvlist_lookup_nvlist(userprops, BE_ZONE_ACTIVE_PROPERTY, &propname)
!= 0 || nvlist_lookup_string(propname, ZPROP_VALUE, &active_str)
!= 0) {
return (B_FALSE);
}
if (strcmp(active_str, "on") == 0)
return (B_TRUE);
return (B_FALSE);
}
|