Skip to content

Instantly share code, notes, and snippets.

@kandeshvari
Created September 7, 2020 11:49
Show Gist options
  • Save kandeshvari/6d6a267263585123009c98120957131f to your computer and use it in GitHub Desktop.
Save kandeshvari/6d6a267263585123009c98120957131f to your computer and use it in GitHub Desktop.
ZFS: make new pool from disk detached from mirror
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (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 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Copyright 2013 Saso Kiselkov. All rights reserved.
* Copyright 2015 Toomas Soome <tsoome@me.com>
*/
/*
* SHA-256 and SHA-512/256 hashes, as specified in FIPS 180-4, available at:
* http://csrc.nist.gov/cryptval
*
* This is a very compact implementation of SHA-256 and SHA-512/256.
* It is designed to be simple and portable, not to be fast.
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/fs/zfs.h>
#include <libzutil.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stddef.h>
#include <sys/zio.h>
#include <sys/vdev_impl.h>
#include <libzfs/libnvpair.h>
/*
* The literal definitions according to FIPS180-4 would be:
*
* Ch(x, y, z) (((x) & (y)) ^ ((~(x)) & (z)))
* Maj(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
*
* We use logical equivalents which require one less op.
*/
#include <stdint.h>
#include <sys/spa_checksum.h>
#define __unused __attribute__((unused))
#define Ch(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
#define Maj(x, y, z) (((x) & (y)) ^ ((z) & ((x) ^ (y))))
#define ROTR(x, n) (((x) >> (n)) | ((x) << ((sizeof (x) * NBBY)-(n))))
/* SHA-224/256 operations */
#define BIGSIGMA0_256(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22))
#define BIGSIGMA1_256(x) (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25))
#define SIGMA0_256(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ ((x) >> 3))
#define SIGMA1_256(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ ((x) >> 10))
/* SHA-256 round constants */
static const uint32_t SHA256_K[64] = {
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
};
static void
SHA256Transform(uint32_t *H, const uint8_t *cp) {
uint32_t a, b, c, d, e, f, g, h, t, T1, T2, W[64];
/* copy chunk into the first 16 words of the message schedule */
for (t = 0; t < 16; t++, cp += sizeof(uint32_t))
W[t] = (cp[0] << 24) | (cp[1] << 16) | (cp[2] << 8) | cp[3];
/* extend the first 16 words into the remaining 48 words */
for (t = 16; t < 64; t++)
W[t] = SIGMA1_256(W[t - 2]) + W[t - 7] +
SIGMA0_256(W[t - 15]) + W[t - 16];
/* init working variables to the current hash value */
a = H[0];
b = H[1];
c = H[2];
d = H[3];
e = H[4];
f = H[5];
g = H[6];
h = H[7];
/* iterate the compression function for all rounds of the hash */
for (t = 0; t < 64; t++) {
T1 = h + BIGSIGMA1_256(e) + Ch(e, f, g) + SHA256_K[t] + W[t];
T2 = BIGSIGMA0_256(a) + Maj(a, b, c);
h = g;
g = f;
f = e;
e = d + T1;
d = c;
c = b;
b = a;
a = T1 + T2;
}
/* add the compressed chunk to the current hash value */
H[0] += a;
H[1] += b;
H[2] += c;
H[3] += d;
H[4] += e;
H[5] += f;
H[6] += g;
H[7] += h;
}
/*
* Implements the SHA-224 and SHA-256 hash algos - to select between them
* pass the appropriate initial values of 'H' and truncate the last 32 bits
* in case of SHA-224.
*/
static void
SHA256(uint32_t *H, const void *buf, uint64_t size, zio_cksum_t *zcp) {
uint8_t pad[128];
unsigned padsize = size & 63;
unsigned i, k;
/* process all blocks up to the last one */
for (i = 0; i < size - padsize; i += 64)
SHA256Transform(H, (uint8_t *) buf + i);
/* process the last block and padding */
for (k = 0; k < padsize; k++)
pad[k] = ((uint8_t *) buf)[k + i];
for (pad[padsize++] = 0x80; (padsize & 63) != 56; padsize++)
pad[padsize] = 0;
for (i = 0; i < 8; i++)
pad[padsize++] = (size << 3) >> (56 - 8 * i);
for (i = 0; i < padsize; i += 64)
SHA256Transform(H, pad + i);
ZIO_SET_CHECKSUM(zcp,
(uint64_t) H[0] << 32 | H[1],
(uint64_t) H[2] << 32 | H[3],
(uint64_t) H[4] << 32 | H[5],
(uint64_t) H[6] << 32 | H[7]);
}
static void
zio_checksum_SHA256(const void *buf, uint64_t size, zio_cksum_t *zcp) {
/* SHA-256 as per FIPS 180-4. */
uint32_t H[] = {
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
};
SHA256(H, buf, size, zcp);
}
static void label_write(int fd, uint64_t offset, uint64_t size, void *buf) {
zio_eck_t *zbt, zbt_orig;
zbt = (zio_eck_t *) ((char *) buf + size) - 1;
zbt_orig = *zbt;
zio_cksum_t zc;
ZIO_SET_CHECKSUM(&zbt->zec_cksum, offset, 0, 0, 0);
zio_checksum_SHA256(buf, size, &zc);
zbt->zec_cksum = zc;
printf("writing new label... ");
VERIFY(pwrite(fd, buf, size, offset) == size);
printf("Ok\n");
*zbt = zbt_orig;
}
void usage(void) {
fprintf(stderr, "error: no device specified\n");
printf("Read/write label from detached zfs vdev\n");
printf("Usage: zhack [-s] [-w] device\n");
printf(" -w write new label\n");
printf(" -s show pool config in text format\n");
printf(" device path to device or file with pool data\n\n");
printf("Examples:\n");
printf(" check and show label config from file with label copy (made with dd)\n");
printf(" zhack -s ada2p4.eli.256K.copy\n\n");
printf(" write new label in file\n");
printf(" zhack -w ada2p4.eli.256K.copy\n\n");
printf(" write new label directly to device\n");
printf(" zhack -w /dev/ada2p4.eli\n\n");
exit(1);
}
// based on https://gist.github.com/jjwhitney/baaa63144da89726e482
int main(int argc, char **argv) {
int fd;
vdev_label_t vl;
nvlist_t *config;
uberblock_t *ub = (uberblock_t *) vl.vl_uberblock;
uint64_t txg;
char *buf;
size_t buflen;
int c;
int write_flag = 0;
int show_config = 0;
while ((c = getopt(argc, argv, "ws")) != -1) {
switch (c) {
case 'w':
write_flag = 1;
break;
case 's':
show_config = 1;
break;
default:
usage();
break;
}
}
argc -= optind;
argv += optind;
optind = 1;
if (argc == 0) {
usage();
}
// open device
VERIFY((fd = open(argv[0], O_RDWR)) != -1);
// read 256K as vdev label `vl`
VERIFY(pread(fd, &vl, sizeof(vdev_label_t), 0) == sizeof(vdev_label_t));
// unpack config
config = fnvlist_unpack(vl.vl_vdev_phys.vp_nvlist, sizeof(vl.vl_vdev_phys.vp_nvlist));
if (show_config) {
// print current pool config
printf("Current pool config:\n");
nvlist_print(stdout, config);
}
// get current txg
txg = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG);
printf("current txg: %lu (==0)\n", txg);
/* check this pool is detached */
VERIFY(txg == 0);
VERIFY(ub->ub_txg == 0);
VERIFY(ub->ub_rootbp.blk_birth != 0); // we will make this value new txg
printf("blk_birth: %lu\n", ub->ub_rootbp.blk_birth);
// set new txg in uberblock
txg = ub->ub_rootbp.blk_birth;
ub->ub_txg = txg;
printf("new txg set in uberblock: %lu\n", ub->ub_txg);
// set current timestamp
ub->ub_timestamp = gethrestime_sec();
time_t timestamp = ub->ub_timestamp;
printf("new timestamp: %llu UTC: %s", (u_longlong_t) ub->ub_timestamp, asctime(localtime(&timestamp)));
// remove old txg value...
fnvlist_remove(config, ZPOOL_CONFIG_POOL_TXG);
// ...and set new one
fnvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG, txg);
// get current ashift
nvlist_t *vdev_tree = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE);
uint64_t ashift = fnvlist_lookup_uint64(vdev_tree, ZPOOL_CONFIG_ASHIFT);
printf("ashift: %lu\n", ashift);
VERIFY(ashift != 0);
// pack config
buf = vl.vl_vdev_phys.vp_nvlist;
buflen = sizeof(vl.vl_vdev_phys.vp_nvlist);
VERIFY(nvlist_pack(config, &buf, &buflen, NV_ENCODE_XDR, 0) == 0);
if (show_config) {
// print changed pool config
printf("Changed pool config:\n");
nvlist_print(stdout, config);
}
if (write_flag) {
// write new label
label_write(fd, offsetof(vdev_label_t, vl_uberblock), 1ULL << (ashift), ub);
label_write(fd, offsetof(vdev_label_t, vl_vdev_phys), VDEV_PHYS_SIZE, &vl.vl_vdev_phys);
fsync(fd);
}
return (0);
}
@kandeshvari
Copy link
Author

kandeshvari commented Sep 7, 2020

works with zfs 0.8.3 source tree and FreeBSD 12.1 zfs labels.

replace zhack utility in zfs source tree with this file and run make.

based on https://gist.github.com/jjwhitney/baaa63144da89726e482
more info:

This utility works with label0 (first 256K of the vdev disk), so it's better to create a copy of the label with:

dd if=/dev/ada2p4.eli of=./ada2p4.256K.orig bs=256K count=1

and restore it with

dd if=./ada2p4.256K.orig of=/dev/ada2p4.eli bs=256K count=1 conv=notrunc

tip: import this pool with -N option to avoid mount filesystems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment