Bug 204622 - [zfs] [patch] Improve 'zpool labelclear' command
Summary: [zfs] [patch] Improve 'zpool labelclear' command
Status: New
Alias: None
Product: Base System
Classification: Unclassified
Component: kern (show other bugs)
Version: CURRENT
Hardware: Any Any
: --- Affects Some People
Assignee: freebsd-fs
URL:
Keywords: feature, patch
Depends on:
Blocks:
 
Reported: 2015-11-17 08:39 UTC by Ganael LAPLANCHE
Modified: 2016-11-23 13:31 UTC (History)
2 users (show)

See Also:


Attachments
Patch for 10.2 (8.05 KB, text/plain)
2015-11-17 08:39 UTC, Ganael LAPLANCHE
no flags Details
Patch for -CURRENT (8.08 KB, patch)
2015-11-17 08:40 UTC, Ganael LAPLANCHE
no flags Details | Diff
Patch for 12-CURRENT (r303655) (8.13 KB, patch)
2016-08-03 12:36 UTC, Ganael LAPLANCHE
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Ganael LAPLANCHE 2015-11-17 08:39:21 UTC
Created attachment 163238 [details]
Patch for 10.2

Hi,

Here is a proposal to improve the "zpool labelclear" command. Currently, this command blindly overwrites both the 2 first and last labels of a disk (i.e. 2 x 256 kB at the beginning and end of a disk).

This can lead to tricky situations where labels are still seen while another filesystem has taken place over the original zpool. In such a case, still being able to import the zpool can lead to destroying the filesystem that has been created over the pool.

Here is a simple example :

# zpool create test ada1
[... work ...]
# zpool export test

then, one decides to re-format the drive (without clearing the labels, damn!) :

# newfs /dev/ada1
# mount /dev/ada1 /mnt
# touch /mnt/foo
# umount /mnt

Unfortunately, ZFS labels are still there as newfs did not overwrite them :

# zpool import
   pool: test
     id: 17834878187816880903
  state: ONLINE
 action: The pool can be imported using its name or numeric identifier.
 config:

        test        ONLINE
          ada1      ONLINE

If we import/export the pool, labels a re-written and the UFS filesystem is broken :

# zpool import test
# zpool export test
# mount /dev/ada1 /mnt
mount: /dev/ada1: Invalid argument

To minimize the risks of overwriting important data, I have looked for a way to invalidate a label by writing the *minimal* amount of data. I have found a simple way to invalidate a whole label by just modifying 1 byte: setting an invalid encoding for its nvlist is enough to make it considered as broken and ignored. The attached patch adds a -m (minimal) option to the 'labelclear' command that exploits this hack. With this option, we are now able to clear labels (to avoid a future import/export) without breaking a UFS filesystem :

(after having unmounted UFS mountpoint /mnt)
# zpool labelclear -m -f /dev/ada1
# zpool import
<nothing!>
# mount /dev/ada1 /mnt
# ls /mnt
.snap   foo

Of course, if the 4 bytes (4 * 1 byte) which are overwritten here are important for the new filesystem, it will be broken too. Anyway, erasing 4 bytes will always be less risky than erasing 4 * 256 kB!

There are other situations where one would like to clear beginning or ending labels only ; the patch adds two options for that purpose : -b (beginning) and -e (ending). It also adds the ability to invalidate a single label using the -i (index) option. Here is an example with a msdosfs filesystem :

# zpool create test ada1
# zpool export test
# newfs_msdos /dev/ada1
# mount -t msdosfs -o large /dev/ada1  /mnt
# touch /mnt/foo
# umount /mnt
# zdb -l /dev/ada1 
--------------------------------------------
LABEL 0
--------------------------------------------
failed to unpack label 0
--------------------------------------------
LABEL 1
--------------------------------------------
failed to unpack label 1
--------------------------------------------
LABEL 2
--------------------------------------------
    version: 5000
    name: 'test'
    [...]
--------------------------------------------
LABEL 3
--------------------------------------------
    version: 5000
    name: 'test'
    [...]

Ending labels remain valid :

# zpool import
   pool: test
     id: 693883832785598877
  state: ONLINE
 action: The pool can be imported using its name or numeric identifier.
 config:

        test        ONLINE
          ada1      ONLINE

If we import that filesystem, we will be breaking our msdosfs. To invalidate those two ending labels (only) without breaking our filesystem, we can safely perform the following :

# zpool labelclear -e -m -f /dev/ada1
# zpool import
<nothing!>
# mount -t msdosfs -o large /dev/ada1 /mnt
# ls /mnt
foo

Finally, the original command did a blind invalidation, without even trying to check that there is a valid label to erase. I have added a '-c' (check) option that tries to validate a label before doing anything on the disk. It will exit if no label is detected, avoiding to erase important data. This feature will try to unpack the pool configuration (nvlist) to see if it is valid or not ; this should be enough in most cases :

# zpool create test ada1
# zpool export test
# zpool labelclear -c -f /dev/ada1
# zpool labelclear -c -f /dev/ada1
Label clear failed on vdev /dev/ada1

Here, the second call did nothing as the label has already been invalidated.

Find attached two patches: one for 10.2 and another one for -CURRENT (r290928). It keeps full compatibility with the zpool command interface, as well as libzfs and libnvpair ABIs.

I am submitting this patch here as it's been developped and tested on FreeBSD, please tell me if it would be better posted on Illumos Gate.

Best regards,
Ganael.
Comment 1 Ganael LAPLANCHE 2015-11-17 08:40:01 UTC
Created attachment 163239 [details]
Patch for -CURRENT
Comment 2 Ganael LAPLANCHE 2016-03-11 14:53:14 UTC
Has anyone tested that patch ? I would be pleased to get feedbacks on that :)
Comment 3 Ganael LAPLANCHE 2016-08-03 12:36:10 UTC
Created attachment 173223 [details]
Patch for 12-CURRENT (r303655)

Hi,

Here is an updated patch following r297763 and the import of upstream labelclear command from Illumos gate (https://www.illumos.org/issues/6418).

It applies to 12-CURRENT (r303655).

Regards,

Ganael.
Comment 4 Ganael LAPLANCHE 2016-11-15 08:52:39 UTC
Patch reported upstream at IllumOS gate, see:

https://www.illumos.org/issues/7584