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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2013, 2015 by Delphix. All rights reserved.
25 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
29 * Functions to convert between a list of vdevs and an nvlist representing the
30 * configuration. Each entry in the list can be one of:
33 * disk=(path=..., devid=...)
42 * While the underlying implementation supports it, group vdevs cannot contain
43 * other group vdevs. All userland verification of devices is contained within
44 * this file. If successful, the nvlist returned can be passed directly to the
45 * kernel; we've done as much verification as possible in userland.
47 * Hot spares are a special case, and passed down as an array of disk vdevs, at
48 * the same level as the root of the vdev tree.
50 * The only function exported by this file is 'make_root_vdev'. The
51 * function performs several passes:
53 * 1. Construct the vdev specification. Performs syntax validation and
54 * makes sure each device is valid.
55 * 2. Check for devices in use. Using libdiskmgt, makes sure that no
56 * devices are also in use. Some can be overridden using the 'force'
57 * flag, others cannot.
58 * 3. Check for replication errors if the 'force' flag is not specified.
59 * validates that the replication level is consistent across the
61 * 4. Call libzfs to label any whole disks with an EFI label.
68 #include <libdiskmgt.h>
70 #include <libnvpair.h>
75 #include <sys/efi_partition.h>
78 #include <sys/mntent.h>
80 #include "zpool_util.h"
82 #define BACKUP_SLICE "s2"
85 * For any given vdev specification, we can have multiple errors. The
86 * vdev_error() function keeps track of whether we have seen an error yet, and
87 * prints out a header if its the first error we've seen.
94 vdev_error(const char *fmt
, ...)
99 (void) fprintf(stderr
, gettext("invalid vdev specification\n"));
101 (void) fprintf(stderr
, gettext("use '-f' to override "
102 "the following errors:\n"));
104 (void) fprintf(stderr
, gettext("the following errors "
105 "must be manually repaired:\n"));
110 (void) vfprintf(stderr
, fmt
, ap
);
115 libdiskmgt_error(int error
)
118 * ENXIO/ENODEV is a valid error message if the device doesn't live in
119 * /dev/dsk. Don't bother printing an error message in this case.
121 if (error
== ENXIO
|| error
== ENODEV
)
124 (void) fprintf(stderr
, gettext("warning: device in use checking "
125 "failed: %s\n"), strerror(error
));
129 * Validate a device, passing the bulk of the work off to libdiskmgt.
132 check_slice(const char *path
, int force
, boolean_t wholedisk
, boolean_t isspare
)
139 who
= DM_WHO_ZPOOL_FORCE
;
141 who
= DM_WHO_ZPOOL_SPARE
;
145 if (dm_inuse((char *)path
, &msg
, who
, &error
) || error
) {
147 libdiskmgt_error(error
);
150 vdev_error("%s", msg
);
157 * If we're given a whole disk, ignore overlapping slices since we're
158 * about to label it anyway.
161 if (!wholedisk
&& !force
&&
162 (dm_isoverlapping((char *)path
, &msg
, &error
) || error
)) {
164 /* dm_isoverlapping returned -1 */
165 vdev_error(gettext("%s overlaps with %s\n"), path
, msg
);
168 } else if (error
!= ENODEV
) {
169 /* libdiskmgt's devcache only handles physical drives */
170 libdiskmgt_error(error
);
180 * Validate a whole disk. Iterate over all slices on the disk and make sure
181 * that none is in use by calling check_slice().
184 check_disk(const char *name
, dm_descriptor_t disk
, int force
, int isspare
)
186 dm_descriptor_t
*drive
, *media
, *slice
;
192 * Get the drive associated with this disk. This should never fail,
193 * because we already have an alias handle open for the device.
195 if ((drive
= dm_get_associated_descriptors(disk
, DM_DRIVE
,
196 &err
)) == NULL
|| *drive
== NULL
) {
198 libdiskmgt_error(err
);
202 if ((media
= dm_get_associated_descriptors(*drive
, DM_MEDIA
,
204 dm_free_descriptors(drive
);
206 libdiskmgt_error(err
);
210 dm_free_descriptors(drive
);
213 * It is possible that the user has specified a removable media drive,
214 * and the media is not present.
216 if (*media
== NULL
) {
217 dm_free_descriptors(media
);
218 vdev_error(gettext("'%s' has no media in drive\n"), name
);
222 if ((slice
= dm_get_associated_descriptors(*media
, DM_SLICE
,
224 dm_free_descriptors(media
);
226 libdiskmgt_error(err
);
230 dm_free_descriptors(media
);
235 * Iterate over all slices and report any errors. We don't care about
236 * overlapping slices because we are using the whole disk.
238 for (i
= 0; slice
[i
] != NULL
; i
++) {
239 char *name
= dm_get_name(slice
[i
], &err
);
241 if (check_slice(name
, force
, B_TRUE
, isspare
) != 0)
247 dm_free_descriptors(slice
);
255 check_device(const char *path
, boolean_t force
, boolean_t isspare
)
257 dm_descriptor_t desc
;
262 * For whole disks, libdiskmgt does not include the leading dev path.
264 dev
= strrchr(path
, '/');
267 if ((desc
= dm_get_descriptor_by_name(DM_ALIAS
, dev
, &err
)) != NULL
) {
268 err
= check_disk(path
, desc
, force
, isspare
);
269 dm_free_descriptor(desc
);
273 return (check_slice(path
, force
, B_FALSE
, isspare
));
277 * Check that a file is valid. All we can do in this case is check that it's
278 * not in use by another pool, and not in use by swap.
281 check_file(const char *file
, boolean_t force
, boolean_t isspare
)
290 if (dm_inuse_swap(file
, &err
)) {
292 libdiskmgt_error(err
);
294 vdev_error(gettext("%s is currently used by swap. "
295 "Please see swap(1M).\n"), file
);
299 if ((fd
= open(file
, O_RDONLY
)) < 0)
302 if (zpool_in_use(g_zfs
, fd
, &state
, &name
, &inuse
) == 0 && inuse
) {
306 case POOL_STATE_ACTIVE
:
307 desc
= gettext("active");
310 case POOL_STATE_EXPORTED
:
311 desc
= gettext("exported");
314 case POOL_STATE_POTENTIALLY_ACTIVE
:
315 desc
= gettext("potentially active");
319 desc
= gettext("unknown");
324 * Allow hot spares to be shared between pools.
326 if (state
== POOL_STATE_SPARE
&& isspare
)
329 if (state
== POOL_STATE_ACTIVE
||
330 state
== POOL_STATE_SPARE
|| !force
) {
332 case POOL_STATE_SPARE
:
333 vdev_error(gettext("%s is reserved as a hot "
334 "spare for pool %s\n"), file
, name
);
337 vdev_error(gettext("%s is part of %s pool "
338 "'%s'\n"), file
, desc
, name
);
353 * By "whole disk" we mean an entire physical disk (something we can
354 * label, toggle the write cache on, etc.) as opposed to the full
355 * capacity of a pseudo-device such as lofi or did. We act as if we
356 * are labeling the disk, which should be a pretty good test of whether
357 * it's a viable device or not. Returns B_TRUE if it is and B_FALSE if
361 is_whole_disk(const char *arg
)
363 struct dk_gpt
*label
;
365 char path
[MAXPATHLEN
];
367 (void) snprintf(path
, sizeof (path
), "%s%s%s",
368 ZFS_RDISK_ROOT
, strrchr(arg
, '/'), BACKUP_SLICE
);
369 if ((fd
= open(path
, O_RDWR
| O_NDELAY
)) < 0)
371 if (efi_alloc_and_init(fd
, EFI_NUMPAR
, &label
) != 0) {
381 * Create a leaf vdev. Determine if this is a file or a device. If it's a
382 * device, fill in the device id to make a complete nvlist. Valid forms for a
385 * /dev/dsk/xxx Complete disk path
386 * /xxx Full path to file
387 * xxx Shorthand for /dev/dsk/xxx
390 make_leaf_vdev(const char *arg
, uint64_t is_log
)
392 char path
[MAXPATHLEN
];
393 struct stat64 statbuf
;
394 nvlist_t
*vdev
= NULL
;
396 boolean_t wholedisk
= B_FALSE
;
399 * Determine what type of vdev this is, and put the full path into
400 * 'path'. We detect whether this is a device of file afterwards by
401 * checking the st_mode of the file.
405 * Complete device or file path. Exact type is determined by
406 * examining the file descriptor afterwards.
408 wholedisk
= is_whole_disk(arg
);
409 if (!wholedisk
&& (stat64(arg
, &statbuf
) != 0)) {
410 (void) fprintf(stderr
,
411 gettext("cannot open '%s': %s\n"),
412 arg
, strerror(errno
));
416 (void) strlcpy(path
, arg
, sizeof (path
));
419 * This may be a short path for a device, or it could be total
420 * gibberish. Check to see if it's a known device in
421 * /dev/dsk/. As part of this check, see if we've been given a
422 * an entire disk (minus the slice number).
424 (void) snprintf(path
, sizeof (path
), "%s/%s", ZFS_DISK_ROOT
,
426 wholedisk
= is_whole_disk(path
);
427 if (!wholedisk
&& (stat64(path
, &statbuf
) != 0)) {
429 * If we got ENOENT, then the user gave us
430 * gibberish, so try to direct them with a
431 * reasonable error message. Otherwise,
432 * regurgitate strerror() since it's the best we
435 if (errno
== ENOENT
) {
436 (void) fprintf(stderr
,
437 gettext("cannot open '%s': no such "
438 "device in %s\n"), arg
, ZFS_DISK_ROOT
);
439 (void) fprintf(stderr
,
440 gettext("must be a full path or "
441 "shorthand device name\n"));
444 (void) fprintf(stderr
,
445 gettext("cannot open '%s': %s\n"),
446 path
, strerror(errno
));
453 * Determine whether this is a device or a file.
455 if (wholedisk
|| S_ISBLK(statbuf
.st_mode
)) {
456 type
= VDEV_TYPE_DISK
;
457 } else if (S_ISREG(statbuf
.st_mode
)) {
458 type
= VDEV_TYPE_FILE
;
460 (void) fprintf(stderr
, gettext("cannot use '%s': must be a "
461 "block device or regular file\n"), path
);
466 * Finally, we have the complete device or file, and we know that it is
467 * acceptable to use. Construct the nvlist to describe this vdev. All
468 * vdevs have a 'path' element, and devices also have a 'devid' element.
470 verify(nvlist_alloc(&vdev
, NV_UNIQUE_NAME
, 0) == 0);
471 verify(nvlist_add_string(vdev
, ZPOOL_CONFIG_PATH
, path
) == 0);
472 verify(nvlist_add_string(vdev
, ZPOOL_CONFIG_TYPE
, type
) == 0);
473 verify(nvlist_add_uint64(vdev
, ZPOOL_CONFIG_IS_LOG
, is_log
) == 0);
474 if (strcmp(type
, VDEV_TYPE_DISK
) == 0)
475 verify(nvlist_add_uint64(vdev
, ZPOOL_CONFIG_WHOLE_DISK
,
476 (uint64_t)wholedisk
) == 0);
479 * For a whole disk, defer getting its devid until after labeling it.
481 if (S_ISBLK(statbuf
.st_mode
) && !wholedisk
) {
483 * Get the devid for the device.
487 char *minor
= NULL
, *devid_str
= NULL
;
489 if ((fd
= open(path
, O_RDONLY
)) < 0) {
490 (void) fprintf(stderr
, gettext("cannot open '%s': "
491 "%s\n"), path
, strerror(errno
));
496 if (devid_get(fd
, &devid
) == 0) {
497 if (devid_get_minor_name(fd
, &minor
) == 0 &&
498 (devid_str
= devid_str_encode(devid
, minor
)) !=
500 verify(nvlist_add_string(vdev
,
501 ZPOOL_CONFIG_DEVID
, devid_str
) == 0);
503 if (devid_str
!= NULL
)
504 devid_str_free(devid_str
);
506 devid_str_free(minor
);
517 * Go through and verify the replication level of the pool is consistent.
518 * Performs the following checks:
520 * For the new spec, verifies that devices in mirrors and raidz are the
523 * If the current configuration already has inconsistent replication
524 * levels, ignore any other potential problems in the new spec.
526 * Otherwise, make sure that the current spec (if there is one) and the new
527 * spec have consistent replication levels.
529 typedef struct replication_level
{
531 uint64_t zprl_children
;
532 uint64_t zprl_parity
;
533 } replication_level_t
;
535 #define ZPOOL_FUZZ (16 * 1024 * 1024)
538 * Given a list of toplevel vdevs, return the current replication level. If
539 * the config is inconsistent, then NULL is returned. If 'fatal' is set, then
540 * an error message will be displayed for each self-inconsistent vdev.
542 static replication_level_t
*
543 get_replication(nvlist_t
*nvroot
, boolean_t fatal
)
551 replication_level_t lastrep
= {0};
552 replication_level_t rep
;
553 replication_level_t
*ret
;
554 boolean_t dontreport
;
556 ret
= safe_malloc(sizeof (replication_level_t
));
558 verify(nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
559 &top
, &toplevels
) == 0);
561 for (t
= 0; t
< toplevels
; t
++) {
562 uint64_t is_log
= B_FALSE
;
567 * For separate logs we ignore the top level vdev replication
570 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_LOG
, &is_log
);
574 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
,
576 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
577 &child
, &children
) != 0) {
579 * This is a 'file' or 'disk' vdev.
581 rep
.zprl_type
= type
;
582 rep
.zprl_children
= 1;
588 * This is a mirror or RAID-Z vdev. Go through and make
589 * sure the contents are all the same (files vs. disks),
590 * keeping track of the number of elements in the
593 * We also check that the size of each vdev (if it can
594 * be determined) is the same.
596 rep
.zprl_type
= type
;
597 rep
.zprl_children
= 0;
599 if (strcmp(type
, VDEV_TYPE_RAIDZ
) == 0) {
600 verify(nvlist_lookup_uint64(nv
,
601 ZPOOL_CONFIG_NPARITY
,
602 &rep
.zprl_parity
) == 0);
603 assert(rep
.zprl_parity
!= 0);
609 * The 'dontreport' variable indicates that we've
610 * already reported an error for this spec, so don't
611 * bother doing it again.
616 for (c
= 0; c
< children
; c
++) {
617 nvlist_t
*cnv
= child
[c
];
619 struct stat64 statbuf
;
620 uint64_t size
= -1ULL;
626 verify(nvlist_lookup_string(cnv
,
627 ZPOOL_CONFIG_TYPE
, &childtype
) == 0);
630 * If this is a replacing or spare vdev, then
631 * get the real first child of the vdev.
633 if (strcmp(childtype
,
634 VDEV_TYPE_REPLACING
) == 0 ||
635 strcmp(childtype
, VDEV_TYPE_SPARE
) == 0) {
639 verify(nvlist_lookup_nvlist_array(cnv
,
640 ZPOOL_CONFIG_CHILDREN
, &rchild
,
642 assert(rchildren
== 2);
645 verify(nvlist_lookup_string(cnv
,
650 verify(nvlist_lookup_string(cnv
,
651 ZPOOL_CONFIG_PATH
, &path
) == 0);
654 * If we have a raidz/mirror that combines disks
655 * with files, report it as an error.
657 if (!dontreport
&& type
!= NULL
&&
658 strcmp(type
, childtype
) != 0) {
664 "mismatched replication "
665 "level: %s contains both "
666 "files and devices\n"),
674 * According to stat(2), the value of 'st_size'
675 * is undefined for block devices and character
676 * devices. But there is no effective way to
677 * determine the real size in userland.
679 * Instead, we'll take advantage of an
680 * implementation detail of spec_size(). If the
681 * device is currently open, then we (should)
682 * return a valid size.
684 * If we still don't get a valid size (indicated
685 * by a size of 0 or MAXOFFSET_T), then ignore
686 * this device altogether.
688 if ((fd
= open(path
, O_RDONLY
)) >= 0) {
689 err
= fstat64(fd
, &statbuf
);
692 err
= stat64(path
, &statbuf
);
696 statbuf
.st_size
== 0 ||
697 statbuf
.st_size
== MAXOFFSET_T
)
700 size
= statbuf
.st_size
;
703 * Also make sure that devices and
704 * slices have a consistent size. If
705 * they differ by a significant amount
706 * (~16MB) then report an error.
709 (vdev_size
!= -1ULL &&
710 (labs(size
- vdev_size
) >
717 "%s contains devices of "
718 "different sizes\n"),
731 * At this point, we have the replication of the last toplevel
732 * vdev in 'rep'. Compare it to 'lastrep' to see if its
735 if (lastrep
.zprl_type
!= NULL
) {
736 if (strcmp(lastrep
.zprl_type
, rep
.zprl_type
) != 0) {
742 "mismatched replication level: "
743 "both %s and %s vdevs are "
745 lastrep
.zprl_type
, rep
.zprl_type
);
748 } else if (lastrep
.zprl_parity
!= rep
.zprl_parity
) {
754 "mismatched replication level: "
755 "both %llu and %llu device parity "
756 "%s vdevs are present\n"),
762 } else if (lastrep
.zprl_children
!= rep
.zprl_children
) {
768 "mismatched replication level: "
769 "both %llu-way and %llu-way %s "
770 "vdevs are present\n"),
771 lastrep
.zprl_children
,
788 * Check the replication level of the vdev spec against the current pool. Calls
789 * get_replication() to make sure the new spec is self-consistent. If the pool
790 * has a consistent replication level, then we ignore any errors. Otherwise,
791 * report any difference between the two.
794 check_replication(nvlist_t
*config
, nvlist_t
*newroot
)
798 replication_level_t
*current
= NULL
, *new;
802 * If we have a current pool configuration, check to see if it's
803 * self-consistent. If not, simply return success.
805 if (config
!= NULL
) {
808 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
810 if ((current
= get_replication(nvroot
, B_FALSE
)) == NULL
)
814 * for spares there may be no children, and therefore no
815 * replication level to check
817 if ((nvlist_lookup_nvlist_array(newroot
, ZPOOL_CONFIG_CHILDREN
,
818 &child
, &children
) != 0) || (children
== 0)) {
824 * If all we have is logs then there's no replication level to check.
826 if (num_logs(newroot
) == children
) {
832 * Get the replication level of the new vdev spec, reporting any
833 * inconsistencies found.
835 if ((new = get_replication(newroot
, B_TRUE
)) == NULL
) {
841 * Check to see if the new vdev spec matches the replication level of
845 if (current
!= NULL
) {
846 if (strcmp(current
->zprl_type
, new->zprl_type
) != 0) {
848 "mismatched replication level: pool uses %s "
849 "and new vdev is %s\n"),
850 current
->zprl_type
, new->zprl_type
);
852 } else if (current
->zprl_parity
!= new->zprl_parity
) {
854 "mismatched replication level: pool uses %llu "
855 "device parity and new vdev uses %llu\n"),
856 current
->zprl_parity
, new->zprl_parity
);
858 } else if (current
->zprl_children
!= new->zprl_children
) {
860 "mismatched replication level: pool uses %llu-way "
861 "%s and new vdev uses %llu-way %s\n"),
862 current
->zprl_children
, current
->zprl_type
,
863 new->zprl_children
, new->zprl_type
);
876 * Go through and find any whole disks in the vdev specification, labelling them
877 * as appropriate. When constructing the vdev spec, we were unable to open this
878 * device in order to provide a devid. Now that we have labelled the disk and
879 * know the pool slice is valid, we can construct the devid now.
881 * If the disk was already labeled with an EFI label, we will have gotten the
882 * devid already (because we were able to open the whole disk). Otherwise, we
883 * need to get the devid after we label the disk.
886 make_disks(zpool_handle_t
*zhp
, nvlist_t
*nv
, zpool_boot_label_t boot_type
,
891 char *type
, *path
, *diskname
;
892 char buf
[MAXPATHLEN
];
898 char *minor
= NULL
, *devid_str
= NULL
;
900 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
902 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
903 &child
, &children
) != 0) {
905 if (strcmp(type
, VDEV_TYPE_DISK
) != 0)
909 * We have a disk device. Get the path to the device
910 * and see if it's a whole disk by appending the backup
911 * slice and stat()ing the device.
913 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0);
915 diskname
= strrchr(path
, '/');
916 assert(diskname
!= NULL
);
919 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
,
920 &wholedisk
) != 0 || !wholedisk
) {
922 * This is not whole disk, return error if
923 * boot partition creation was requested
925 if (boot_type
== ZPOOL_CREATE_BOOT_LABEL
) {
926 (void) fprintf(stderr
,
927 gettext("creating boot partition is only "
928 "supported on whole disk vdevs: %s\n"),
935 ret
= zpool_label_disk(g_zfs
, zhp
, diskname
, boot_type
,
941 * Fill in the devid, now that we've labeled the disk.
943 (void) snprintf(buf
, sizeof (buf
), "%ss%d", path
, slice
);
944 if ((fd
= open(buf
, O_RDONLY
)) < 0) {
945 (void) fprintf(stderr
,
946 gettext("cannot open '%s': %s\n"),
947 buf
, strerror(errno
));
951 if (devid_get(fd
, &devid
) == 0) {
952 if (devid_get_minor_name(fd
, &minor
) == 0 &&
953 (devid_str
= devid_str_encode(devid
, minor
)) !=
955 verify(nvlist_add_string(nv
,
956 ZPOOL_CONFIG_DEVID
, devid_str
) == 0);
958 if (devid_str
!= NULL
)
959 devid_str_free(devid_str
);
961 devid_str_free(minor
);
966 * Update the path to refer to the pool slice. The presence of
967 * the 'whole_disk' field indicates to the CLI that we should
968 * chop off the slice number when displaying the device in
971 verify(nvlist_add_string(nv
, ZPOOL_CONFIG_PATH
, buf
) == 0);
978 /* illumos kernel does not support booting from multi-vdev pools. */
979 if ((boot_type
== ZPOOL_CREATE_BOOT_LABEL
)) {
980 if ((strcmp(type
, VDEV_TYPE_ROOT
) == 0) && children
> 1) {
981 (void) fprintf(stderr
, gettext("boot pool "
982 "can not have more than one vdev\n"));
987 for (c
= 0; c
< children
; c
++) {
988 ret
= make_disks(zhp
, child
[c
], boot_type
, boot_size
);
993 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
994 &child
, &children
) == 0)
995 for (c
= 0; c
< children
; c
++) {
996 ret
= make_disks(zhp
, child
[c
], boot_type
, boot_size
);
1001 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
1002 &child
, &children
) == 0)
1003 for (c
= 0; c
< children
; c
++) {
1004 ret
= make_disks(zhp
, child
[c
], boot_type
, boot_size
);
1013 * Determine if the given path is a hot spare within the given configuration.
1016 is_spare(nvlist_t
*config
, const char *path
)
1022 uint64_t guid
, spareguid
;
1028 if ((fd
= open(path
, O_RDONLY
)) < 0)
1031 if (zpool_in_use(g_zfs
, fd
, &state
, &name
, &inuse
) != 0 ||
1033 state
!= POOL_STATE_SPARE
||
1034 zpool_read_label(fd
, &label
) != 0) {
1042 verify(nvlist_lookup_uint64(label
, ZPOOL_CONFIG_GUID
, &guid
) == 0);
1045 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
1047 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
1048 &spares
, &nspares
) == 0) {
1049 for (i
= 0; i
< nspares
; i
++) {
1050 verify(nvlist_lookup_uint64(spares
[i
],
1051 ZPOOL_CONFIG_GUID
, &spareguid
) == 0);
1052 if (spareguid
== guid
)
1061 * Go through and find any devices that are in use. We rely on libdiskmgt for
1062 * the majority of this task.
1065 is_device_in_use(nvlist_t
*config
, nvlist_t
*nv
, boolean_t force
,
1066 boolean_t replacing
, boolean_t isspare
)
1072 char buf
[MAXPATHLEN
];
1074 boolean_t anyinuse
= B_FALSE
;
1076 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
1078 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1079 &child
, &children
) != 0) {
1081 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0);
1084 * As a generic check, we look to see if this is a replace of a
1085 * hot spare within the same pool. If so, we allow it
1086 * regardless of what libdiskmgt or zpool_in_use() says.
1089 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
,
1090 &wholedisk
) == 0 && wholedisk
)
1091 (void) snprintf(buf
, sizeof (buf
), "%ss0",
1094 (void) strlcpy(buf
, path
, sizeof (buf
));
1096 if (is_spare(config
, buf
))
1100 if (strcmp(type
, VDEV_TYPE_DISK
) == 0)
1101 ret
= check_device(path
, force
, isspare
);
1102 else if (strcmp(type
, VDEV_TYPE_FILE
) == 0)
1103 ret
= check_file(path
, force
, isspare
);
1108 for (c
= 0; c
< children
; c
++)
1109 if (is_device_in_use(config
, child
[c
], force
, replacing
,
1113 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
1114 &child
, &children
) == 0)
1115 for (c
= 0; c
< children
; c
++)
1116 if (is_device_in_use(config
, child
[c
], force
, replacing
,
1120 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
1121 &child
, &children
) == 0)
1122 for (c
= 0; c
< children
; c
++)
1123 if (is_device_in_use(config
, child
[c
], force
, replacing
,
1131 is_grouping(const char *type
, int *mindev
, int *maxdev
)
1133 if (strncmp(type
, "raidz", 5) == 0) {
1134 const char *p
= type
+ 5;
1140 } else if (*p
== '0') {
1141 return (NULL
); /* no zero prefixes allowed */
1144 nparity
= strtol(p
, &end
, 10);
1145 if (errno
!= 0 || nparity
< 1 || nparity
>= 255 ||
1151 *mindev
= nparity
+ 1;
1154 return (VDEV_TYPE_RAIDZ
);
1160 if (strcmp(type
, "mirror") == 0) {
1163 return (VDEV_TYPE_MIRROR
);
1166 if (strcmp(type
, "spare") == 0) {
1169 return (VDEV_TYPE_SPARE
);
1172 if (strcmp(type
, "log") == 0) {
1175 return (VDEV_TYPE_LOG
);
1178 if (strcmp(type
, "cache") == 0) {
1181 return (VDEV_TYPE_L2CACHE
);
1188 * Construct a syntactically valid vdev specification,
1189 * and ensure that all devices and files exist and can be opened.
1190 * Note: we don't bother freeing anything in the error paths
1191 * because the program is just going to exit anyway.
1194 construct_spec(int argc
, char **argv
)
1196 nvlist_t
*nvroot
, *nv
, **top
, **spares
, **l2cache
;
1197 int t
, toplevels
, mindev
, maxdev
, nspares
, nlogs
, nl2cache
;
1200 boolean_t seen_logs
;
1210 seen_logs
= B_FALSE
;
1216 * If it's a mirror or raidz, the subsequent arguments are
1217 * its leaves -- until we encounter the next mirror or raidz.
1219 if ((type
= is_grouping(argv
[0], &mindev
, &maxdev
)) != NULL
) {
1220 nvlist_t
**child
= NULL
;
1221 int c
, children
= 0;
1223 if (strcmp(type
, VDEV_TYPE_SPARE
) == 0) {
1224 if (spares
!= NULL
) {
1225 (void) fprintf(stderr
,
1226 gettext("invalid vdev "
1227 "specification: 'spare' can be "
1228 "specified only once\n"));
1234 if (strcmp(type
, VDEV_TYPE_LOG
) == 0) {
1236 (void) fprintf(stderr
,
1237 gettext("invalid vdev "
1238 "specification: 'log' can be "
1239 "specified only once\n"));
1247 * A log is not a real grouping device.
1248 * We just set is_log and continue.
1253 if (strcmp(type
, VDEV_TYPE_L2CACHE
) == 0) {
1254 if (l2cache
!= NULL
) {
1255 (void) fprintf(stderr
,
1256 gettext("invalid vdev "
1257 "specification: 'cache' can be "
1258 "specified only once\n"));
1265 if (strcmp(type
, VDEV_TYPE_MIRROR
) != 0) {
1266 (void) fprintf(stderr
,
1267 gettext("invalid vdev "
1268 "specification: unsupported 'log' "
1269 "device: %s\n"), type
);
1275 for (c
= 1; c
< argc
; c
++) {
1276 if (is_grouping(argv
[c
], NULL
, NULL
) != NULL
)
1279 child
= realloc(child
,
1280 children
* sizeof (nvlist_t
*));
1283 if ((nv
= make_leaf_vdev(argv
[c
], B_FALSE
))
1286 child
[children
- 1] = nv
;
1289 if (children
< mindev
) {
1290 (void) fprintf(stderr
, gettext("invalid vdev "
1291 "specification: %s requires at least %d "
1292 "devices\n"), argv
[0], mindev
);
1296 if (children
> maxdev
) {
1297 (void) fprintf(stderr
, gettext("invalid vdev "
1298 "specification: %s supports no more than "
1299 "%d devices\n"), argv
[0], maxdev
);
1306 if (strcmp(type
, VDEV_TYPE_SPARE
) == 0) {
1310 } else if (strcmp(type
, VDEV_TYPE_L2CACHE
) == 0) {
1312 nl2cache
= children
;
1315 verify(nvlist_alloc(&nv
, NV_UNIQUE_NAME
,
1317 verify(nvlist_add_string(nv
, ZPOOL_CONFIG_TYPE
,
1319 verify(nvlist_add_uint64(nv
,
1320 ZPOOL_CONFIG_IS_LOG
, is_log
) == 0);
1321 if (strcmp(type
, VDEV_TYPE_RAIDZ
) == 0) {
1322 verify(nvlist_add_uint64(nv
,
1323 ZPOOL_CONFIG_NPARITY
,
1326 verify(nvlist_add_nvlist_array(nv
,
1327 ZPOOL_CONFIG_CHILDREN
, child
,
1330 for (c
= 0; c
< children
; c
++)
1331 nvlist_free(child
[c
]);
1336 * We have a device. Pass off to make_leaf_vdev() to
1337 * construct the appropriate nvlist describing the vdev.
1339 if ((nv
= make_leaf_vdev(argv
[0], is_log
)) == NULL
)
1348 top
= realloc(top
, toplevels
* sizeof (nvlist_t
*));
1351 top
[toplevels
- 1] = nv
;
1354 if (toplevels
== 0 && nspares
== 0 && nl2cache
== 0) {
1355 (void) fprintf(stderr
, gettext("invalid vdev "
1356 "specification: at least one toplevel vdev must be "
1361 if (seen_logs
&& nlogs
== 0) {
1362 (void) fprintf(stderr
, gettext("invalid vdev specification: "
1363 "log requires at least 1 device\n"));
1368 * Finally, create nvroot and add all top-level vdevs to it.
1370 verify(nvlist_alloc(&nvroot
, NV_UNIQUE_NAME
, 0) == 0);
1371 verify(nvlist_add_string(nvroot
, ZPOOL_CONFIG_TYPE
,
1372 VDEV_TYPE_ROOT
) == 0);
1373 verify(nvlist_add_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
1374 top
, toplevels
) == 0);
1376 verify(nvlist_add_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
1377 spares
, nspares
) == 0);
1379 verify(nvlist_add_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
1380 l2cache
, nl2cache
) == 0);
1382 for (t
= 0; t
< toplevels
; t
++)
1383 nvlist_free(top
[t
]);
1384 for (t
= 0; t
< nspares
; t
++)
1385 nvlist_free(spares
[t
]);
1386 for (t
= 0; t
< nl2cache
; t
++)
1387 nvlist_free(l2cache
[t
]);
1398 split_mirror_vdev(zpool_handle_t
*zhp
, char *newname
, nvlist_t
*props
,
1399 splitflags_t flags
, int argc
, char **argv
)
1401 nvlist_t
*newroot
= NULL
, **child
;
1403 zpool_boot_label_t boot_type
;
1406 if ((newroot
= construct_spec(argc
, argv
)) == NULL
) {
1407 (void) fprintf(stderr
, gettext("Unable to build a "
1408 "pool from the specified devices\n"));
1412 if (zpool_is_bootable(zhp
))
1413 boot_type
= ZPOOL_COPY_BOOT_LABEL
;
1415 boot_type
= ZPOOL_NO_BOOT_LABEL
;
1417 if (!flags
.dryrun
&&
1418 make_disks(zhp
, newroot
, boot_type
, 0) != 0) {
1419 nvlist_free(newroot
);
1423 /* avoid any tricks in the spec */
1424 verify(nvlist_lookup_nvlist_array(newroot
,
1425 ZPOOL_CONFIG_CHILDREN
, &child
, &children
) == 0);
1426 for (c
= 0; c
< children
; c
++) {
1431 verify(nvlist_lookup_string(child
[c
],
1432 ZPOOL_CONFIG_PATH
, &path
) == 0);
1433 if ((type
= is_grouping(path
, &min
, &max
)) != NULL
) {
1434 (void) fprintf(stderr
, gettext("Cannot use "
1435 "'%s' as a device for splitting\n"), type
);
1436 nvlist_free(newroot
);
1442 if (zpool_vdev_split(zhp
, newname
, &newroot
, props
, flags
) != 0) {
1443 nvlist_free(newroot
);
1451 * Get and validate the contents of the given vdev specification. This ensures
1452 * that the nvlist returned is well-formed, that all the devices exist, and that
1453 * they are not currently in use by any other known consumer. The 'poolconfig'
1454 * parameter is the current configuration of the pool when adding devices
1455 * existing pool, and is used to perform additional checks, such as changing the
1456 * replication level of the pool. It can be 'NULL' to indicate that this is a
1457 * new pool. The 'force' flag controls whether devices should be forcefully
1458 * added, even if they appear in use.
1461 make_root_vdev(zpool_handle_t
*zhp
, int force
, int check_rep
,
1462 boolean_t replacing
, boolean_t dryrun
, zpool_boot_label_t boot_type
,
1463 uint64_t boot_size
, int argc
, char **argv
)
1466 nvlist_t
*poolconfig
= NULL
;
1470 * Construct the vdev specification. If this is successful, we know
1471 * that we have a valid specification, and that all devices can be
1474 if ((newroot
= construct_spec(argc
, argv
)) == NULL
)
1477 if (zhp
&& ((poolconfig
= zpool_get_config(zhp
, NULL
)) == NULL
))
1481 * Validate each device to make sure that its not shared with another
1482 * subsystem. We do this even if 'force' is set, because there are some
1483 * uses (such as a dedicated dump device) that even '-f' cannot
1486 if (is_device_in_use(poolconfig
, newroot
, force
, replacing
, B_FALSE
)) {
1487 nvlist_free(newroot
);
1492 * Check the replication level of the given vdevs and report any errors
1493 * found. We include the existing pool spec, if any, as we need to
1494 * catch changes against the existing replication level.
1496 if (check_rep
&& check_replication(poolconfig
, newroot
) != 0) {
1497 nvlist_free(newroot
);
1502 * Run through the vdev specification and label any whole disks found.
1504 if (!dryrun
&& make_disks(zhp
, newroot
, boot_type
, boot_size
) != 0) {
1505 nvlist_free(newroot
);