Skip to content

Commit

Permalink
Make 'zpool labelclear -f' work on offlined disks
Browse files Browse the repository at this point in the history
This patch allows you to clear the label on offlined disks in an active
pool with `-f`.  Previously, labelclear wouldn't let you do that.

Signed-off-by: Tony Hutter <hutter2@llnl.gov>
  • Loading branch information
tonyhutter committed Aug 26, 2021
1 parent 6bc61d2 commit e8be733
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion cmd/zpool/zpool_main.c
Expand Up @@ -1214,6 +1214,26 @@ zpool_do_remove(int argc, char **argv)
return (ret);
}

/*
* Return 1 if a vdev is active (being used in a pool)
* Return 0 if a vdev is inactive (offlined or faulted, or not in active pool)
*
* This is useful for checking if a disk in an active pool is offlined or
* faulted.
*/
static int
vdev_is_active(char *vdev_path)
{
int fd;
fd = open(vdev_path, O_EXCL);
if (fd < 0) {
return (1); /* cant open O_EXCL - disk is active */
}

close(fd);
return (0); /* disk is inactive in the pool */
}

/*
* zpool labelclear [-f] <vdev>
*
Expand Down Expand Up @@ -1323,9 +1343,19 @@ zpool_do_labelclear(int argc, char **argv)
case POOL_STATE_ACTIVE:
case POOL_STATE_SPARE:
case POOL_STATE_L2CACHE:
if (force && !vdev_is_active(vdev)) {
break;
}

(void) fprintf(stderr, gettext(
"%s is a member (%s) of pool \"%s\"\n"),
"%s is a member (%s) of pool \"%s\""),
vdev, zpool_pool_state_to_name(state), name);

if (force) {
(void) fprintf(stderr, gettext(
". Offline the disk first to clear its label."));
}
printf("\n");
ret = 1;
goto errout;

Expand Down

0 comments on commit e8be733

Please sign in to comment.