4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 * Functions to convert between a list of vdevs and an nvlist representing the
29 * configuration. Each entry in the list can be one of:
32 * disk=(path=..., devid=...)
41 * While the underlying implementation supports it, group vdevs cannot contain
42 * other group vdevs. All userland verification of devices is contained within
43 * this file. If successful, the nvlist returned can be passed directly to the
44 * kernel; we've done as much verification as possible in userland.
46 * Hot spares are a special case, and passed down as an array of disk vdevs, at
47 * the same level as the root of the vdev tree.
49 * The only function exported by this file is 'make_root_vdev'. The
50 * function performs several passes:
52 * 1. Construct the vdev specification. Performs syntax validation and
53 * makes sure each device is valid.
54 * 2. Check for devices in use. Using libdiskmgt, makes sure that no
55 * devices are also in use. Some can be overridden using the 'force'
56 * flag, others cannot.
57 * 3. Check for replication errors if the 'force' flag is not specified.
58 * validates that the replication level is consistent across the
60 * 4. Call libzfs to label any whole disks with an EFI label.
68 #include <libnvpair.h>
72 #include <sys/efi_partition.h>
75 #include <sys/mntent.h>
77 #include "zpool_util.h"
79 #define DISK_ROOT "/dev/dsk"
80 #define RDISK_ROOT "/dev/rdsk"
81 #define BACKUP_SLICE "s2"
84 * For any given vdev specification, we can have multiple errors. The
85 * vdev_error() function keeps track of whether we have seen an error yet, and
86 * prints out a header if its the first error we've seen.
93 vdev_error(const char *fmt
, ...)
98 (void) fprintf(stderr
, gettext("invalid vdev specification\n"));
100 (void) fprintf(stderr
, gettext("use '-f' to override "
101 "the following errors:\n"));
103 (void) fprintf(stderr
, gettext("the following errors "
104 "must be manually repaired:\n"));
109 (void) vfprintf(stderr
, fmt
, ap
);
114 libdiskmgt_error(int error
)
117 * ENXIO/ENODEV is a valid error message if the device doesn't live in
118 * /dev/dsk. Don't bother printing an error message in this case.
120 if (error
== ENXIO
|| error
== ENODEV
)
123 (void) fprintf(stderr
, gettext("warning: device in use checking "
124 "failed: %s\n"), strerror(error
));
128 * Check that a file is valid. All we can do in this case is check that it's
129 * not in use by another pool, and not in use by swap.
132 check_file(const char *file
, boolean_t force
, boolean_t isspare
)
142 if (dm_inuse_swap(file
, &err
)) {
144 libdiskmgt_error(err
);
146 vdev_error(gettext("%s is currently used by swap. "
147 "Please see swap(1M).\n"), file
);
152 if ((fd
= open(file
, O_RDONLY
)) < 0)
155 if (zpool_in_use(g_zfs
, fd
, &state
, &name
, &inuse
) == 0 && inuse
) {
159 case POOL_STATE_ACTIVE
:
160 desc
= gettext("active");
163 case POOL_STATE_EXPORTED
:
164 desc
= gettext("exported");
167 case POOL_STATE_POTENTIALLY_ACTIVE
:
168 desc
= gettext("potentially active");
172 desc
= gettext("unknown");
177 * Allow hot spares to be shared between pools.
179 if (state
== POOL_STATE_SPARE
&& isspare
)
182 if (state
== POOL_STATE_ACTIVE
||
183 state
== POOL_STATE_SPARE
|| !force
) {
185 case POOL_STATE_SPARE
:
186 vdev_error(gettext("%s is reserved as a hot "
187 "spare for pool %s\n"), file
, name
);
190 vdev_error(gettext("%s is part of %s pool "
191 "'%s'\n"), file
, desc
, name
);
206 * By "whole disk" we mean an entire physical disk (something we can
207 * label, toggle the write cache on, etc.) as opposed to the full
208 * capacity of a pseudo-device such as lofi or did. We act as if we
209 * are labeling the disk, which should be a pretty good test of whether
210 * it's a viable device or not. Returns B_TRUE if it is and B_FALSE if
214 is_whole_disk(const char *arg
)
216 struct dk_gpt
*label
;
218 char path
[MAXPATHLEN
];
220 (void) snprintf(path
, sizeof (path
), "%s%s%s",
221 RDISK_ROOT
, strrchr(arg
, '/'), BACKUP_SLICE
);
222 if ((fd
= open(path
, O_RDWR
| O_NDELAY
)) < 0)
224 if (efi_alloc_and_init(fd
, EFI_NUMPAR
, &label
) != 0) {
234 * Create a leaf vdev. Determine if this is a file or a device. If it's a
235 * device, fill in the device id to make a complete nvlist. Valid forms for a
238 * /dev/dsk/xxx Complete disk path
239 * /xxx Full path to file
240 * xxx Shorthand for /dev/dsk/xxx
243 make_leaf_vdev(const char *arg
, uint64_t is_log
)
245 char path
[MAXPATHLEN
];
246 struct stat64 statbuf
;
247 nvlist_t
*vdev
= NULL
;
249 boolean_t wholedisk
= B_FALSE
;
252 * Determine what type of vdev this is, and put the full path into
253 * 'path'. We detect whether this is a device of file afterwards by
254 * checking the st_mode of the file.
258 * Complete device or file path. Exact type is determined by
259 * examining the file descriptor afterwards.
261 wholedisk
= is_whole_disk(arg
);
262 if (!wholedisk
&& (stat64(arg
, &statbuf
) != 0)) {
263 (void) fprintf(stderr
,
264 gettext("cannot open '%s': %s\n"),
265 arg
, strerror(errno
));
269 (void) strlcpy(path
, arg
, sizeof (path
));
272 * This may be a short path for a device, or it could be total
273 * gibberish. Check to see if it's a known device in
274 * /dev/dsk/. As part of this check, see if we've been given a
275 * an entire disk (minus the slice number).
277 (void) snprintf(path
, sizeof (path
), "%s/%s", DISK_ROOT
,
279 wholedisk
= is_whole_disk(path
);
280 if (!wholedisk
&& (stat64(path
, &statbuf
) != 0)) {
282 * If we got ENOENT, then the user gave us
283 * gibberish, so try to direct them with a
284 * reasonable error message. Otherwise,
285 * regurgitate strerror() since it's the best we
288 if (errno
== ENOENT
) {
289 (void) fprintf(stderr
,
290 gettext("cannot open '%s': no such "
291 "device in %s\n"), arg
, DISK_ROOT
);
292 (void) fprintf(stderr
,
293 gettext("must be a full path or "
294 "shorthand device name\n"));
297 (void) fprintf(stderr
,
298 gettext("cannot open '%s': %s\n"),
299 path
, strerror(errno
));
306 * Determine whether this is a device or a file.
308 if (wholedisk
|| S_ISBLK(statbuf
.st_mode
)) {
309 type
= VDEV_TYPE_DISK
;
310 } else if (S_ISREG(statbuf
.st_mode
)) {
311 type
= VDEV_TYPE_FILE
;
313 (void) fprintf(stderr
, gettext("cannot use '%s': must be a "
314 "block device or regular file\n"), path
);
319 * Finally, we have the complete device or file, and we know that it is
320 * acceptable to use. Construct the nvlist to describe this vdev. All
321 * vdevs have a 'path' element, and devices also have a 'devid' element.
323 verify(nvlist_alloc(&vdev
, NV_UNIQUE_NAME
, 0) == 0);
324 verify(nvlist_add_string(vdev
, ZPOOL_CONFIG_PATH
, path
) == 0);
325 verify(nvlist_add_string(vdev
, ZPOOL_CONFIG_TYPE
, type
) == 0);
326 verify(nvlist_add_uint64(vdev
, ZPOOL_CONFIG_IS_LOG
, is_log
) == 0);
327 if (strcmp(type
, VDEV_TYPE_DISK
) == 0)
328 verify(nvlist_add_uint64(vdev
, ZPOOL_CONFIG_WHOLE_DISK
,
329 (uint64_t)wholedisk
) == 0);
332 * For a whole disk, defer getting its devid until after labeling it.
334 if (S_ISBLK(statbuf
.st_mode
) && !wholedisk
) {
336 * Get the devid for the device.
340 char *minor
= NULL
, *devid_str
= NULL
;
342 if ((fd
= open(path
, O_RDONLY
)) < 0) {
343 (void) fprintf(stderr
, gettext("cannot open '%s': "
344 "%s\n"), path
, strerror(errno
));
349 if (devid_get(fd
, &devid
) == 0) {
350 if (devid_get_minor_name(fd
, &minor
) == 0 &&
351 (devid_str
= devid_str_encode(devid
, minor
)) !=
353 verify(nvlist_add_string(vdev
,
354 ZPOOL_CONFIG_DEVID
, devid_str
) == 0);
356 if (devid_str
!= NULL
)
357 devid_str_free(devid_str
);
359 devid_str_free(minor
);
370 * Go through and verify the replication level of the pool is consistent.
371 * Performs the following checks:
373 * For the new spec, verifies that devices in mirrors and raidz are the
376 * If the current configuration already has inconsistent replication
377 * levels, ignore any other potential problems in the new spec.
379 * Otherwise, make sure that the current spec (if there is one) and the new
380 * spec have consistent replication levels.
382 typedef struct replication_level
{
384 uint64_t zprl_children
;
385 uint64_t zprl_parity
;
386 } replication_level_t
;
388 #define ZPOOL_FUZZ (16 * 1024 * 1024)
391 * Given a list of toplevel vdevs, return the current replication level. If
392 * the config is inconsistent, then NULL is returned. If 'fatal' is set, then
393 * an error message will be displayed for each self-inconsistent vdev.
395 static replication_level_t
*
396 get_replication(nvlist_t
*nvroot
, boolean_t fatal
)
404 replication_level_t lastrep
, rep
, *ret
;
405 boolean_t dontreport
;
407 ret
= safe_malloc(sizeof (replication_level_t
));
409 verify(nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
410 &top
, &toplevels
) == 0);
412 lastrep
.zprl_type
= NULL
;
413 for (t
= 0; t
< toplevels
; t
++) {
414 uint64_t is_log
= B_FALSE
;
419 * For separate logs we ignore the top level vdev replication
422 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_LOG
, &is_log
);
426 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
,
428 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
429 &child
, &children
) != 0) {
431 * This is a 'file' or 'disk' vdev.
433 rep
.zprl_type
= type
;
434 rep
.zprl_children
= 1;
440 * This is a mirror or RAID-Z vdev. Go through and make
441 * sure the contents are all the same (files vs. disks),
442 * keeping track of the number of elements in the
445 * We also check that the size of each vdev (if it can
446 * be determined) is the same.
448 rep
.zprl_type
= type
;
449 rep
.zprl_children
= 0;
451 if (strcmp(type
, VDEV_TYPE_RAIDZ
) == 0) {
452 verify(nvlist_lookup_uint64(nv
,
453 ZPOOL_CONFIG_NPARITY
,
454 &rep
.zprl_parity
) == 0);
455 assert(rep
.zprl_parity
!= 0);
461 * The 'dontreport' variable indicates that we've
462 * already reported an error for this spec, so don't
463 * bother doing it again.
468 for (c
= 0; c
< children
; c
++) {
469 nvlist_t
*cnv
= child
[c
];
471 struct stat64 statbuf
;
472 uint64_t size
= -1ULL;
478 verify(nvlist_lookup_string(cnv
,
479 ZPOOL_CONFIG_TYPE
, &childtype
) == 0);
482 * If this is a replacing or spare vdev, then
483 * get the real first child of the vdev.
485 if (strcmp(childtype
,
486 VDEV_TYPE_REPLACING
) == 0 ||
487 strcmp(childtype
, VDEV_TYPE_SPARE
) == 0) {
491 verify(nvlist_lookup_nvlist_array(cnv
,
492 ZPOOL_CONFIG_CHILDREN
, &rchild
,
494 assert(rchildren
== 2);
497 verify(nvlist_lookup_string(cnv
,
502 verify(nvlist_lookup_string(cnv
,
503 ZPOOL_CONFIG_PATH
, &path
) == 0);
506 * If we have a raidz/mirror that combines disks
507 * with files, report it as an error.
509 if (!dontreport
&& type
!= NULL
&&
510 strcmp(type
, childtype
) != 0) {
516 "mismatched replication "
517 "level: %s contains both "
518 "files and devices\n"),
526 * According to stat(2), the value of 'st_size'
527 * is undefined for block devices and character
528 * devices. But there is no effective way to
529 * determine the real size in userland.
531 * Instead, we'll take advantage of an
532 * implementation detail of spec_size(). If the
533 * device is currently open, then we (should)
534 * return a valid size.
536 * If we still don't get a valid size (indicated
537 * by a size of 0 or MAXOFFSET_T), then ignore
538 * this device altogether.
540 if ((fd
= open(path
, O_RDONLY
)) >= 0) {
541 err
= fstat64(fd
, &statbuf
);
544 err
= stat64(path
, &statbuf
);
548 statbuf
.st_size
== 0 ||
549 statbuf
.st_size
== MAXOFFSET_T
)
552 size
= statbuf
.st_size
;
555 * Also make sure that devices and
556 * slices have a consistent size. If
557 * they differ by a significant amount
558 * (~16MB) then report an error.
561 (vdev_size
!= -1ULL &&
562 (labs(size
- vdev_size
) >
569 "%s contains devices of "
570 "different sizes\n"),
583 * At this point, we have the replication of the last toplevel
584 * vdev in 'rep'. Compare it to 'lastrep' to see if its
587 if (lastrep
.zprl_type
!= NULL
) {
588 if (strcmp(lastrep
.zprl_type
, rep
.zprl_type
) != 0) {
594 "mismatched replication level: "
595 "both %s and %s vdevs are "
597 lastrep
.zprl_type
, rep
.zprl_type
);
600 } else if (lastrep
.zprl_parity
!= rep
.zprl_parity
) {
606 "mismatched replication level: "
607 "both %llu and %llu device parity "
608 "%s vdevs are present\n"),
614 } else if (lastrep
.zprl_children
!= rep
.zprl_children
) {
620 "mismatched replication level: "
621 "both %llu-way and %llu-way %s "
622 "vdevs are present\n"),
623 lastrep
.zprl_children
,
640 * Check the replication level of the vdev spec against the current pool. Calls
641 * get_replication() to make sure the new spec is self-consistent. If the pool
642 * has a consistent replication level, then we ignore any errors. Otherwise,
643 * report any difference between the two.
646 check_replication(nvlist_t
*config
, nvlist_t
*newroot
)
650 replication_level_t
*current
= NULL
, *new;
654 * If we have a current pool configuration, check to see if it's
655 * self-consistent. If not, simply return success.
657 if (config
!= NULL
) {
660 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
662 if ((current
= get_replication(nvroot
, B_FALSE
)) == NULL
)
666 * for spares there may be no children, and therefore no
667 * replication level to check
669 if ((nvlist_lookup_nvlist_array(newroot
, ZPOOL_CONFIG_CHILDREN
,
670 &child
, &children
) != 0) || (children
== 0)) {
676 * If all we have is logs then there's no replication level to check.
678 if (num_logs(newroot
) == children
) {
684 * Get the replication level of the new vdev spec, reporting any
685 * inconsistencies found.
687 if ((new = get_replication(newroot
, B_TRUE
)) == NULL
) {
693 * Check to see if the new vdev spec matches the replication level of
697 if (current
!= NULL
) {
698 if (strcmp(current
->zprl_type
, new->zprl_type
) != 0) {
700 "mismatched replication level: pool uses %s "
701 "and new vdev is %s\n"),
702 current
->zprl_type
, new->zprl_type
);
704 } else if (current
->zprl_parity
!= new->zprl_parity
) {
706 "mismatched replication level: pool uses %llu "
707 "device parity and new vdev uses %llu\n"),
708 current
->zprl_parity
, new->zprl_parity
);
710 } else if (current
->zprl_children
!= new->zprl_children
) {
712 "mismatched replication level: pool uses %llu-way "
713 "%s and new vdev uses %llu-way %s\n"),
714 current
->zprl_children
, current
->zprl_type
,
715 new->zprl_children
, new->zprl_type
);
728 * Go through and find any whole disks in the vdev specification, labelling them
729 * as appropriate. When constructing the vdev spec, we were unable to open this
730 * device in order to provide a devid. Now that we have labelled the disk and
731 * know that slice 0 is valid, we can construct the devid now.
733 * If the disk was already labeled with an EFI label, we will have gotten the
734 * devid already (because we were able to open the whole disk). Otherwise, we
735 * need to get the devid after we label the disk.
738 make_disks(zpool_handle_t
*zhp
, nvlist_t
*nv
)
742 char *type
, *path
, *diskname
;
743 char buf
[MAXPATHLEN
];
748 char *minor
= NULL
, *devid_str
= NULL
;
750 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
752 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
753 &child
, &children
) != 0) {
755 if (strcmp(type
, VDEV_TYPE_DISK
) != 0)
759 * We have a disk device. Get the path to the device
760 * and see if it's a whole disk by appending the backup
761 * slice and stat()ing the device.
763 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0);
764 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
,
765 &wholedisk
) != 0 || !wholedisk
)
768 diskname
= strrchr(path
, '/');
769 assert(diskname
!= NULL
);
771 if (zpool_label_disk(g_zfs
, zhp
, diskname
) == -1)
775 * Fill in the devid, now that we've labeled the disk.
777 (void) snprintf(buf
, sizeof (buf
), "%ss0", path
);
778 if ((fd
= open(buf
, O_RDONLY
)) < 0) {
779 (void) fprintf(stderr
,
780 gettext("cannot open '%s': %s\n"),
781 buf
, strerror(errno
));
785 if (devid_get(fd
, &devid
) == 0) {
786 if (devid_get_minor_name(fd
, &minor
) == 0 &&
787 (devid_str
= devid_str_encode(devid
, minor
)) !=
789 verify(nvlist_add_string(nv
,
790 ZPOOL_CONFIG_DEVID
, devid_str
) == 0);
792 if (devid_str
!= NULL
)
793 devid_str_free(devid_str
);
795 devid_str_free(minor
);
800 * Update the path to refer to the 's0' slice. The presence of
801 * the 'whole_disk' field indicates to the CLI that we should
802 * chop off the slice number when displaying the device in
805 verify(nvlist_add_string(nv
, ZPOOL_CONFIG_PATH
, buf
) == 0);
812 for (c
= 0; c
< children
; c
++)
813 if ((ret
= make_disks(zhp
, child
[c
])) != 0)
816 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
817 &child
, &children
) == 0)
818 for (c
= 0; c
< children
; c
++)
819 if ((ret
= make_disks(zhp
, child
[c
])) != 0)
822 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
823 &child
, &children
) == 0)
824 for (c
= 0; c
< children
; c
++)
825 if ((ret
= make_disks(zhp
, child
[c
])) != 0)
832 * Determine if the given path is a hot spare within the given configuration.
835 is_spare(nvlist_t
*config
, const char *path
)
841 uint64_t guid
, spareguid
;
847 if ((fd
= open(path
, O_RDONLY
)) < 0)
850 if (zpool_in_use(g_zfs
, fd
, &state
, &name
, &inuse
) != 0 ||
852 state
!= POOL_STATE_SPARE
||
853 zpool_read_label(fd
, &label
) != 0) {
861 verify(nvlist_lookup_uint64(label
, ZPOOL_CONFIG_GUID
, &guid
) == 0);
864 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
866 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
867 &spares
, &nspares
) == 0) {
868 for (i
= 0; i
< nspares
; i
++) {
869 verify(nvlist_lookup_uint64(spares
[i
],
870 ZPOOL_CONFIG_GUID
, &spareguid
) == 0);
871 if (spareguid
== guid
)
880 * Go through and find any devices that are in use. We rely on libdiskmgt for
881 * the majority of this task.
884 check_in_use(nvlist_t
*config
, nvlist_t
*nv
, int force
, int isreplacing
,
891 char buf
[MAXPATHLEN
];
894 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
896 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
897 &child
, &children
) != 0) {
899 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0);
902 * As a generic check, we look to see if this is a replace of a
903 * hot spare within the same pool. If so, we allow it
904 * regardless of what libdiskmgt or zpool_in_use() says.
907 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
,
908 &wholedisk
) == 0 && wholedisk
)
909 (void) snprintf(buf
, sizeof (buf
), "%ss0",
912 (void) strlcpy(buf
, path
, sizeof (buf
));
913 if (is_spare(config
, buf
))
917 if (strcmp(type
, VDEV_TYPE_DISK
) == 0 ||
918 strcmp(type
, VDEV_TYPE_FILE
) == 0)
919 ret
= check_file(path
, force
, isspare
);
924 for (c
= 0; c
< children
; c
++)
925 if ((ret
= check_in_use(config
, child
[c
], force
,
926 isreplacing
, B_FALSE
)) != 0)
929 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
930 &child
, &children
) == 0)
931 for (c
= 0; c
< children
; c
++)
932 if ((ret
= check_in_use(config
, child
[c
], force
,
933 isreplacing
, B_TRUE
)) != 0)
936 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
937 &child
, &children
) == 0)
938 for (c
= 0; c
< children
; c
++)
939 if ((ret
= check_in_use(config
, child
[c
], force
,
940 isreplacing
, B_FALSE
)) != 0)
947 is_grouping(const char *type
, int *mindev
)
949 if (strcmp(type
, "raidz") == 0 || strcmp(type
, "raidz1") == 0) {
952 return (VDEV_TYPE_RAIDZ
);
955 if (strcmp(type
, "raidz2") == 0) {
958 return (VDEV_TYPE_RAIDZ
);
961 if (strcmp(type
, "mirror") == 0) {
964 return (VDEV_TYPE_MIRROR
);
967 if (strcmp(type
, "spare") == 0) {
970 return (VDEV_TYPE_SPARE
);
973 if (strcmp(type
, "log") == 0) {
976 return (VDEV_TYPE_LOG
);
979 if (strcmp(type
, "cache") == 0) {
982 return (VDEV_TYPE_L2CACHE
);
989 * Construct a syntactically valid vdev specification,
990 * and ensure that all devices and files exist and can be opened.
991 * Note: we don't bother freeing anything in the error paths
992 * because the program is just going to exit anyway.
995 construct_spec(int argc
, char **argv
)
997 nvlist_t
*nvroot
, *nv
, **top
, **spares
, **l2cache
;
998 int t
, toplevels
, mindev
, nspares
, nlogs
, nl2cache
;
1001 boolean_t seen_logs
;
1011 seen_logs
= B_FALSE
;
1017 * If it's a mirror or raidz, the subsequent arguments are
1018 * its leaves -- until we encounter the next mirror or raidz.
1020 if ((type
= is_grouping(argv
[0], &mindev
)) != NULL
) {
1021 nvlist_t
**child
= NULL
;
1022 int c
, children
= 0;
1024 if (strcmp(type
, VDEV_TYPE_SPARE
) == 0) {
1025 if (spares
!= NULL
) {
1026 (void) fprintf(stderr
,
1027 gettext("invalid vdev "
1028 "specification: 'spare' can be "
1029 "specified only once\n"));
1035 if (strcmp(type
, VDEV_TYPE_LOG
) == 0) {
1037 (void) fprintf(stderr
,
1038 gettext("invalid vdev "
1039 "specification: 'log' can be "
1040 "specified only once\n"));
1048 * A log is not a real grouping device.
1049 * We just set is_log and continue.
1054 if (strcmp(type
, VDEV_TYPE_L2CACHE
) == 0) {
1055 if (l2cache
!= NULL
) {
1056 (void) fprintf(stderr
,
1057 gettext("invalid vdev "
1058 "specification: 'cache' can be "
1059 "specified only once\n"));
1066 if (strcmp(type
, VDEV_TYPE_MIRROR
) != 0) {
1067 (void) fprintf(stderr
,
1068 gettext("invalid vdev "
1069 "specification: unsupported 'log' "
1070 "device: %s\n"), type
);
1076 for (c
= 1; c
< argc
; c
++) {
1077 if (is_grouping(argv
[c
], NULL
) != NULL
)
1080 child
= realloc(child
,
1081 children
* sizeof (nvlist_t
*));
1084 if ((nv
= make_leaf_vdev(argv
[c
], B_FALSE
))
1087 child
[children
- 1] = nv
;
1090 if (children
< mindev
) {
1091 (void) fprintf(stderr
, gettext("invalid vdev "
1092 "specification: %s requires at least %d "
1093 "devices\n"), argv
[0], mindev
);
1100 if (strcmp(type
, VDEV_TYPE_SPARE
) == 0) {
1104 } else if (strcmp(type
, VDEV_TYPE_L2CACHE
) == 0) {
1106 nl2cache
= children
;
1109 verify(nvlist_alloc(&nv
, NV_UNIQUE_NAME
,
1111 verify(nvlist_add_string(nv
, ZPOOL_CONFIG_TYPE
,
1113 verify(nvlist_add_uint64(nv
,
1114 ZPOOL_CONFIG_IS_LOG
, is_log
) == 0);
1115 if (strcmp(type
, VDEV_TYPE_RAIDZ
) == 0) {
1116 verify(nvlist_add_uint64(nv
,
1117 ZPOOL_CONFIG_NPARITY
,
1120 verify(nvlist_add_nvlist_array(nv
,
1121 ZPOOL_CONFIG_CHILDREN
, child
,
1124 for (c
= 0; c
< children
; c
++)
1125 nvlist_free(child
[c
]);
1130 * We have a device. Pass off to make_leaf_vdev() to
1131 * construct the appropriate nvlist describing the vdev.
1133 if ((nv
= make_leaf_vdev(argv
[0], is_log
)) == NULL
)
1142 top
= realloc(top
, toplevels
* sizeof (nvlist_t
*));
1145 top
[toplevels
- 1] = nv
;
1148 if (toplevels
== 0 && nspares
== 0 && nl2cache
== 0) {
1149 (void) fprintf(stderr
, gettext("invalid vdev "
1150 "specification: at least one toplevel vdev must be "
1155 if (seen_logs
&& nlogs
== 0) {
1156 (void) fprintf(stderr
, gettext("invalid vdev specification: "
1157 "log requires at least 1 device\n"));
1162 * Finally, create nvroot and add all top-level vdevs to it.
1164 verify(nvlist_alloc(&nvroot
, NV_UNIQUE_NAME
, 0) == 0);
1165 verify(nvlist_add_string(nvroot
, ZPOOL_CONFIG_TYPE
,
1166 VDEV_TYPE_ROOT
) == 0);
1167 verify(nvlist_add_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
1168 top
, toplevels
) == 0);
1170 verify(nvlist_add_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
1171 spares
, nspares
) == 0);
1173 verify(nvlist_add_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
1174 l2cache
, nl2cache
) == 0);
1176 for (t
= 0; t
< toplevels
; t
++)
1177 nvlist_free(top
[t
]);
1178 for (t
= 0; t
< nspares
; t
++)
1179 nvlist_free(spares
[t
]);
1180 for (t
= 0; t
< nl2cache
; t
++)
1181 nvlist_free(l2cache
[t
]);
1193 * Get and validate the contents of the given vdev specification. This ensures
1194 * that the nvlist returned is well-formed, that all the devices exist, and that
1195 * they are not currently in use by any other known consumer. The 'poolconfig'
1196 * parameter is the current configuration of the pool when adding devices
1197 * existing pool, and is used to perform additional checks, such as changing the
1198 * replication level of the pool. It can be 'NULL' to indicate that this is a
1199 * new pool. The 'force' flag controls whether devices should be forcefully
1200 * added, even if they appear in use.
1203 make_root_vdev(zpool_handle_t
*zhp
, int force
, int check_rep
,
1204 boolean_t isreplacing
, boolean_t dryrun
, int argc
, char **argv
)
1207 nvlist_t
*poolconfig
= NULL
;
1211 * Construct the vdev specification. If this is successful, we know
1212 * that we have a valid specification, and that all devices can be
1215 if ((newroot
= construct_spec(argc
, argv
)) == NULL
)
1218 if (zhp
&& ((poolconfig
= zpool_get_config(zhp
, NULL
)) == NULL
))
1222 * Validate each device to make sure that its not shared with another
1223 * subsystem. We do this even if 'force' is set, because there are some
1224 * uses (such as a dedicated dump device) that even '-f' cannot
1227 if (check_in_use(poolconfig
, newroot
, force
, isreplacing
,
1229 nvlist_free(newroot
);
1234 * Check the replication level of the given vdevs and report any errors
1235 * found. We include the existing pool spec, if any, as we need to
1236 * catch changes against the existing replication level.
1238 if (check_rep
&& check_replication(poolconfig
, newroot
) != 0) {
1239 nvlist_free(newroot
);
1244 * Run through the vdev specification and label any whole disks found.
1246 if (!dryrun
&& make_disks(zhp
, newroot
) != 0) {
1247 nvlist_free(newroot
);