How to shrink zvol used in ISCSI via CLI?

I have a pool and a zvol equal to the size of pool, this all is connected to PC through iSCSI. I wanted to shrink it to check performance. On a test virtual machine I can delete and recreate, but on my hardware NAS I already have some files.
I can’t shrink zvol via GUI. I searched FAQ and didn’t find commands to shrink zvol. Can you give me a link and write the commands to test it, to check if I get data loss or something?
Thank you.

Hey @Andrew_Zhitenev

Shrinking a zvol is an inherently dangerous operation, because ZFS isn’t able to coordinate with the guest filesystem inside. That’s why it’s not available from the GUI. There are commands to do this from the command-line, but because it effectively “truncates” the end of the zvol, any data inside the “cut-off” portion would be lost. You’d have to ensure that your guest OS moves all of your active data to the “start” of its partition first, and that’s something outside of ZFS purview.

Can I ask what you mean by “check performance” - is it because you’ve set the zvol to be equal to pool size, and you’re concerned about the pool being “100% full” so to speak? ZFS supports the UNMAP operation (equivalent to TRIM) against zvols, if your client operating system does - so files deleted from the guest should translate into “free space” on ZFS.

1 Like

I heard it is dangerous, that’s why I wanted to make some experiments in virtual environment.
Yes, I have 2 large disks in stripe and mirror pools, and for me the possibility to «lose» several TiB was a complete disaster. That is why I create zvol of maximum possible size, not sparse. It is connected to PC through iSCSI and on PC I made NTFS partition. Still sometimes I feel this solution doesn’t use all my network throughput (maybe it is misconfiguration of the router/NICs) but I wanted to test the difference between >80% and <80%. Now I think I wouldn’t. Besides if you say that free space on my NTFS partition is considered free on ZFS, that is wonderful, because I have about 50% of space free.

I’ve done this before. The key is to make sure you

  1. Shrink the partition in the guest VM first.
  2. Shrink the ZVOL, but Id make sure it’s at least several hundred MiB larger than the quoted size in the VM because you WILL lose data and possibly corrupt the VM if the volume ends up being smaller than the size required by the guest VM.

BTW, shrinking the ZVOL is very trivial. It’s as simple as setting the “volsize” property to the new size. Really, the more involved process is shrinking the partition in the guest VM.

1 Like

I can’t shrink it in GUI. And I can’t find a CLI command to do it.

The CLI command to do this should be:

You've backed up your data, right?
No, really.
You read the post above, where we talk about shrinking guest partitions and moving data to the "logical start" of the device?

zfs set volsize=32G poolname/zvolname
This sets the zvol poolname/zvolname to 32G in size. Adjust as necessary.