Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Prototype for systemd and fstab integration #4943

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
SUBDIRS = zfs zpool zdb zhack zinject zstreamdump ztest zpios
SUBDIRS += mount_zfs fsck_zfs zvol_id vdev_id arcstat dbufstat zed
SUBDIRS += arc_summary raidz_test
SUBDIRS += arc_summary raidz_test zready
12 changes: 11 additions & 1 deletion cmd/zpool/zpool_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2191,6 +2191,7 @@ zpool_do_import(int argc, char **argv)
boolean_t do_rewind = B_FALSE;
boolean_t xtreme_rewind = B_FALSE;
boolean_t do_scan = B_FALSE;
boolean_t do_imported = B_FALSE;
uint64_t pool_state, txg = -1ULL;
char *cachefile = NULL;
importargs_t idata = { 0 };
Expand Down Expand Up @@ -2221,6 +2222,9 @@ zpool_do_import(int argc, char **argv)
case 'D':
do_destroyed = B_TRUE;
break;
case 'E':
do_imported = B_TRUE;
break;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll want to add this option to the man page and comment above. I'm surprised to see E was already part of the getopt string above.

case 'f':
flags |= ZFS_IMPORT_ANY_HOST;
break;
Expand Down Expand Up @@ -2420,6 +2424,11 @@ zpool_do_import(int argc, char **argv)

if (pools != NULL && idata.exists &&
(argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
if (do_imported) {
err = 0;
log_history = B_FALSE;
goto exit_early;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about the other error cases below?

(void) fprintf(stderr, gettext("cannot import '%s': "
"a pool with that name already exists\n"),
argv[0]);
Expand All @@ -2443,12 +2452,13 @@ zpool_do_import(int argc, char **argv)
}

if (err == 1) {
exit_early:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like memleak here?

if (searchdirs != NULL)
free(searchdirs);
if (envdup != NULL)
free(envdup);
nvlist_free(policy);
return (1);
return (err);
}

/*
Expand Down
1 change: 1 addition & 0 deletions cmd/zready/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/zready
19 changes: 19 additions & 0 deletions cmd/zready/Makefile.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
include $(top_srcdir)/config/Rules.am

AM_CPPFLAGS += -DDEBUG

DEFAULT_INCLUDES += \
-I$(top_srcdir)/include \
-I$(top_srcdir)/lib/libspl/include

sbin_PROGRAMS = zready

zready_SOURCES = \
zready.c

zready_LDADD = \
$(top_builddir)/lib/libnvpair/libnvpair.la \
$(top_builddir)/lib/libuutil/libuutil.la \
$(top_builddir)/lib/libzpool/libzpool.la \
$(top_builddir)/lib/libzfs/libzfs.la \
$(top_builddir)/lib/libzfs_core/libzfs_core.la
272 changes: 272 additions & 0 deletions cmd/zready/zready.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,272 @@
/*
* 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
*/

#include <stdio.h>
#include <unistd.h>
#include <stdio_ext.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/zfs_context.h>
#include <sys/vdev.h>
#include <sys/vdev_impl.h>
#include <sys/stat.h>

#define ZPOOL_PATH "/dev/zpool"

static nvlist_t *
get_config(const char *dev)
Copy link
Contributor

@behlendorf behlendorf Sep 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The existing zpool_read_label() function already does most of this and is part of libzfs. Rather than have two versions of this function I'd suggest extending zpool_read_label() as needed. The interfaces are slightly different but not really in a significant way.

{
int fd;
vdev_label_t label;
char *path, *buf = label.vl_vdev_phys.vp_nvlist;
size_t buflen = sizeof (label.vl_vdev_phys.vp_nvlist);
struct stat64 statbuf;
uint64_t psize;
int l;
int found = 0;
nvlist_t *best = NULL;
uint64_t best_txg = 0;
uint64_t state;
uint64_t pguid = 0;

path = strdup(dev);

if ((fd = open64(path, O_RDONLY)) < 0) {
(void) printf("cannot open '%s': %s\n", path, strerror(errno));
free(path);
exit(1);
}

if (fstat64_blk(fd, &statbuf) != 0) {
(void) printf("failed to stat '%s': %s\n", path,
strerror(errno));
free(path);
(void) close(fd);
exit(1);
}

psize = statbuf.st_size;
psize = P2ALIGN(psize, (uint64_t)sizeof (vdev_label_t));

for (l = 0; l < VDEV_LABELS; l++) {
uint64_t g, txg;
nvlist_t *config = NULL;

if (pread64(fd, &label, sizeof (label),
vdev_label_offset(psize, l, 0)) != sizeof (label)) {
(void) fprintf(stderr, "failed to read label %d\n", l);
continue;
}

if (nvlist_unpack(buf, buflen, &config, 0) != 0) {
(void) fprintf(stderr, "failed to unpack label %d\n", l);
} else {
if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) != 0)
continue;
if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &g) != 0)
continue;
if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, &state) != 0)
continue;

if (state == POOL_STATE_DESTROYED)
continue;
if (pguid == 0)
pguid = g;
else if (pguid != g) {
fprintf(stderr, "pool guid mismatch between labels\n");
exit(1);
}
if (txg > best_txg) {
if (best)
nvlist_free(best);
best = config;
best_txg = txg;
} else {
nvlist_free(config);
}
found++;
}
}

if (found < 3)
exit(1);

free(path);
(void) close(fd);
return best;
}

static int
build_vdev(nvlist_t *vdev, uint64_t vguid, uint64_t dguid)
{
char dir[64];
char *type;
uint64_t cguid;
nvlist_t **child;
uint_t nc;
int i, ready = 0;
int ret = 0;

sprintf(dir, "%llu", (u_longlong_t)vguid);
if (mkdir(dir, 0755) < 0 && errno != EEXIST) {
fprintf(stderr, "failed to mkdir %s: %s\n", dir, strerror(errno));
exit(1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it's a prototype but best to return (1); and return error from main().

}
if (chdir(dir) < 0) {
fprintf(stderr, "failed to enter %s: %s\n", dir, strerror(errno));
exit(1);
}

if (nvlist_lookup_string(vdev, ZPOOL_CONFIG_TYPE, &type) != 0) {
fprintf(stderr, "fail to get vdev type\n");
exit(1);
}

if (dguid == vguid) {
ready = 1;
ret = 1;
} else if (strcmp(type, "disk") != 0 && strcmp(type, "file") != 0) {
int found = 0;

if (nvlist_lookup_nvlist_array(vdev, ZPOOL_CONFIG_CHILDREN, &child, &nc) != 0) {
fprintf(stderr, "fail to get child vdev\n");
exit(1);
}
for (i = 0; i < nc; i++) {
if (nvlist_lookup_uint64(child[i], ZPOOL_CONFIG_GUID, &cguid) != 0) {
fprintf(stderr, "failed to get child guid\n");
exit(1);
}
if ((ret = build_vdev(child[i], cguid, dguid)))
break;
}
if (ret) {
for (i = 0; i < nc; i++) {
struct stat sbuf;
char path[64];
if (nvlist_lookup_uint64(child[i], ZPOOL_CONFIG_GUID, &cguid) != 0) {
fprintf(stderr, "failed to get child guid\n");
exit(1);
}
sprintf(path, "%llu/ready", (u_longlong_t)cguid);
if (stat(path, &sbuf) == 0)
found++;
}
if (found == nc)
ready = 1;
}
}

if (ready && close(open("ready", O_WRONLY|O_CREAT, 0644)) < 0) {
fprintf(stderr, "failed to create \"ready\"\n");
exit(1);
}
chdir("..");
return ret;
}

static void
build_tree(nvlist_t *config)
{
DIR *dfd;
struct dirent *dirent;
nvlist_t *vdev;
uint64_t pguid, vguid, dguid, nc;
char *name;
int found = 0;
int fd;
if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &vdev) != 0 ||
nvlist_lookup_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN, &nc) != 0 ||
nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pguid) != 0 ||
nvlist_lookup_uint64(config, ZPOOL_CONFIG_TOP_GUID, &vguid) != 0 ||
nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, &dguid) != 0 ||
nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, &name) != 0) {
fprintf(stderr, "fail to parse pool config\n");
exit(1);
}

/* make pool root dir and cd into it */
if (mkdir(name, 0755) < 0 && errno != EEXIST) {
fprintf(stderr, "failed to mkdir %s: %s\n", name, strerror(errno));
exit(1);
}
if (chdir(name) < 0) {
fprintf(stderr, "failed to enter %s: %s\n", name, strerror(errno));
exit(1);
}

/* write pool_guid, TODO: we need to check pool_guid is correct */
if ((fd = open("pool_guid", O_WRONLY|O_CREAT|O_EXCL, 0644)) > 0) {
char u[64];
sprintf(u, "%llu", (u_longlong_t)pguid);
write(fd, u, strlen(u));
close(fd);
}

/* decend into top level vdev */
build_vdev(vdev, vguid, dguid);

dfd = opendir(".");
while ((dirent = readdir(dfd)) != NULL) {
struct stat sbuf;
char path[64];
if (strcmp(dirent->d_name, ".") == 0 || strcmp(dirent->d_name, "..") == 0)
continue;
if (snprintf(path, 64, "%s/ready", dirent->d_name) >= 64)
continue;
if (stat(path, &sbuf) == 0)
found++;
}
closedir(dfd);

if ((found == nc) && close(open("ready", O_WRONLY|O_CREAT, 0644)) < 0) {
fprintf(stderr, "failed to create \"ready\"\n");
exit(1);
}
}

int
main(int argc, char **argv)
{
nvlist_t *config;
if (argc < 2) {
fprintf(stderr, "Usage: %s <dev>\n", argv[0]);
exit(1);
}
config = get_config(argv[1]);
if (!config)
return (0);

if (mkdir(ZPOOL_PATH, 0755) < 0 && errno != EEXIST) {
fprintf(stderr, "failed to mkdir %s: %s\n", ZPOOL_PATH, strerror(errno));
exit(1);
}
if (chdir(ZPOOL_PATH) < 0) {
fprintf(stderr, "failed to enter %s: %s\n", ZPOOL_PATH, strerror(errno));
exit(1);
}
build_tree(config);
nvlist_free(config);
return (0);
}
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ AC_CONFIG_FILES([
cmd/arc_summary/Makefile
cmd/zed/Makefile
cmd/raidz_test/Makefile
cmd/zready/Makefile
contrib/Makefile
contrib/bash_completion.d/Makefile
contrib/dracut/Makefile
Expand Down
1 change: 1 addition & 0 deletions etc/systemd/system/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.service
*.target
*.preset
*.path
6 changes: 5 additions & 1 deletion etc/systemd/system/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ systemdunit_DATA = \
zfs-import-scan.service \
zfs-mount.service \
zfs-share.service \
zfs.target
zfs.target \
[email protected] \
[email protected]

EXTRA_DIST = \
$(top_srcdir)/etc/systemd/system/zfs-zed.service.in \
Expand All @@ -16,6 +18,8 @@ EXTRA_DIST = \
$(top_srcdir)/etc/systemd/system/zfs-mount.service.in \
$(top_srcdir)/etc/systemd/system/zfs-share.service.in \
$(top_srcdir)/etc/systemd/system/zfs.target.in \
$(top_srcdir)/etc/systemd/system/[email protected] \
$(top_srcdir)/etc/systemd/system/[email protected] \
$(top_srcdir)/etc/systemd/system/50-zfs.preset.in

$(systemdunit_DATA) $(systemdpreset_DATA):%:%.in
Expand Down
7 changes: 7 additions & 0 deletions etc/systemd/system/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[Unit]
Description=zpool ready path
DefaultDependencies=no

[Path]
PathExists=/dev/zpool/%i/ready
Unit=zpool@%i.service
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These templates are pretty clever.

Loading