site

files for beauhilton.com
git clone https://git.beauhilton.com/site.git
Log | Files | Refs

add-zfs-mirror.md (4416B)


      1 # How to add a mirror to a single ZFS disk
      2 
      3 <time id="post-date">2023-10-19</time>
      4 
      5 <p id="post-excerpt">
      6 tl;dr:&#x2005;<code>zpool&#x2005;attach&#x2005;data&#x2005;/dev/disk/by-partlabel/zfs-3a1xx&#x2005;/dev/sdx0</code>. Adjust for your own pool and disks.
      7 </p>
      8 
      9 
     10 ## A mirror is a Very Good Thing
     11 
     12 I got a second identical disk to add as a ZFS mirror to my existing setup.
     13 
     14 Redundancy FTW. 
     15 ZFS is king and queen. 
     16 Needs at least two disks to reap the full benefit
     17 (though a single-disk setup is still rad -
     18 CoW with snapshots and send/receive are worth it,
     19 and NixOS makes it easy to set up).
     20 
     21 ## It's all about the disk path
     22 
     23 The basic command is (as a super user, or using sudo):
     24 
     25 ```sh
     26 zpool attach name-of-the-pool disk-you-already-had disk-you-want-to-add
     27 ```
     28 
     29 On Linux, with a new drive, 
     30 no need for formatting or any other disk preparation. 
     31 Just plug it in.
     32 
     33 But how to specify which disks to use in the command? 
     34 I saw several guides that just used `/dev/sdx`, but it didn't work.
     35 
     36 I don't know what it is about my setup
     37 that differs from the guides I found online 
     38 (if you have a hint, [lmk](beauhilton.com/contact)),
     39 so YMMV,
     40 but below is some of what I tried and what eventually worked,
     41 mostly for my own reference.
     42 
     43 In my setup, `/dev/sdb` was my existing drive 
     44 and `/dev/sda` is the one I was trying to add 
     45 (these names are determined at boot time 
     46 and can vary from boot to boot - 
     47 ZFS takes care of finding things again on the back end 
     48 after you've added a drive, 
     49 regardless of the label you used to add it, 
     50 so using the `/dev/sdx` name is just fine in many situations).
     51 
     52 However, using
     53 ```sh
     54 zpool attach data /dev/sdb dev/sda
     55 ```
     56 
     57 gave me:
     58 
     59 ```sh
     60 cannot attach /dev/sda to /dev/sdb: no such device in pool
     61 ``````
     62 
     63 I tried a bunch of different things, from just the lsblk name with the partition ("sdb1"),
     64 to by-id ("ata-ST1400..."), to full path combinations, etc.
     65 
     66 What finally worked was the 
     67 by-partlabel identifier for the existing disk, 
     68 combined with the /dev/sdx0 name of the new one.
     69 
     70 A little `zdb -l` on the existing drive gave me the path I wanted:
     71 
     72 ```sh
     73 zdb -l /dev/sdb1
     74 ------------------------------------
     75 LABEL 0
     76 ------------------------------------
     77     version: 5000
     78     name: 'data'
     79     state: 0
     80     txg: 170113
     81     pool_guid: 8237228336559358688
     82     errata: 0
     83     hostid: 3236406100
     84     hostname: 'dell7050'
     85     top_guid: 11182661003591634341
     86     guid: 11182661003591634341
     87     vdev_children: 1
     88     vdev_tree:
     89         type: 'disk'
     90         id: 0
     91         guid: 11182661003591634341
     92         path: '/dev/disk/by-partlabel/zfs-3a1e459c75dc9b74'
     93         whole_disk: 1
     94         metaslab_array: 128
     95         metaslab_shift: 34
     96         ashift: 12
     97         asize: 14000504438784
     98         is_log: 0
     99         DTL: 3865
    100         create_txg: 4
    101     features_for_read:
    102         com.delphix:hole_birth
    103         com.delphix:embedded_data
    104     labels = 0 1 2 3
    105 ```
    106 
    107 So I tried:
    108 
    109 ```sh
    110 zpool attach data /dev/disk/by-partlabel/zfs-3a1e459c75dc9b74 /dev/sda1
    111 ```
    112 
    113 ## Great success
    114 
    115 ```conf
    116 ->> zpool status
    117   pool: data
    118  state: ONLINE
    119 status: One or more devices is currently being resilvered.  The pool will
    120         continue to function, possibly in a degraded state.
    121 action: Wait for the resilver to complete.
    122   scan: resilver in progress since Thu Oct 19 17:05:26 2023
    123         2.33T scanned at 1.90G/s, 252G issued at 205M/s, 3.77T total
    124         252G resilvered, 6.53% done, 04:59:42 to go
    125 config:
    126 
    127         NAME                      STATE     READ WRITE CKSUM
    128         data                      ONLINE       0     0     0
    129           mirror-0                ONLINE       0     0     0
    130             zfs-3a1e459c75dc9b74  ONLINE       0     0     0
    131             sda1                  ONLINE       0     0     0  (resilvering)
    132 
    133 errors: No known data errors
    134 ```
    135 
    136 ...and the next day, after resilvering completed
    137 (and a reboot to make sure the disk came online):
    138 
    139 ```conf
    140 ->> zpool status
    141   pool: data
    142  state: ONLINE
    143   scan: resilvered 3.77T in 05:13:53 with 0 errors on Thu Oct 19 22:19:19 2023
    144 config:
    145 
    146         NAME                      STATE     READ WRITE CKSUM
    147         data                      ONLINE       0     0     0
    148           mirror-0                ONLINE       0     0     0
    149             zfs-3a1e459c75dc9b74  ONLINE       0     0     0
    150             zfs-e2937e0fc8ebe95a  ONLINE       0     0     0
    151 
    152 errors: No known data errors
    153 ```
    154 
    155 (notice that what was `sda1` is now referred to by partlabel)