Search This Blog

2025-12-21

FreeNAS, TrueNAS, FreeBSD - Shrink ZFS File System

Product: Debian distribution, TrueNAS, FreeNAS

Version: Any version which uses ZFS File System

Overview

ZFS always create mount point as volume group equivalent volume even you assigns 1 partition to a mount point.  In Debian, TrueNAS or FreeNAS, it always creates as volume group (ZDEV), regardless you assigns entire hard drive (1 device) or 1 partition to it.

ZFS does not has build-in functionality to shrink partition, but we can remove partition after adding another one.  So the strategy to shrink partition is below:

  1. In the first disk, which contains the partition that we would like to shrink, finds out how much space is used by the mount point or partition.  For example, let's assume 8GB is used in a partition which is 500GB
  2. Locates another disk or partition which can hold all the data used by above partition.  We will later leverage ZFS feature to auto-copy its content over to this new partition.  For simplicity, let's call this as 2nd disk
  3. In the 2nd disk, creates a partition which is bigger than 8GB.  For example, creates a new 16GB partition
  4. Unmount the mount point to avoid any user copying any data into the partition which we are going to shrink
  5. Adds the new 16GB partition from 2nd disk into the same partition (volume, or ZDEV) which we would like to shrink.  Don't worry that we are making the volume 500GB + 16GB larger.  ZFS will not copy anything into this volume since we unmount it
  6. Detach the partition we would like to shrink from the volume (ZDEV) to force ZFS to copy all the used data from this partition into 2nd disk's new partition (16GB partition size)
  7. Now, you can delete and re-create the partition with smaller size in 1st disk
  8. Adds back this partition into the volume or ZDEV.  Do not mount the mount point yet
  9. Detach the 2nd disk's 16GB partition from the volume, so what ZFS will copy all the content to the newly added but smaller partition
  10. This complete the partition shrink

Procedure

Assumes the partition we would like to shrink is called boot-pool (FreeNAS' name for boot volume). It contains partition sda3 inside disk 1 (device name called sda).  The 2nd partition name that going to temporary add is called vg00/zvol0.

  1. Checks how much space is used in boot-pool
    1. Command: zpool list -v boot-pool

% zpool list -v boot-pool
NAME        SIZE  ALLOC   FREE  CKPOINT  EXPANDSZ   FRAG    CAP  DEDUP    HEALTH  ALTROOT
boot-pool   928G  3.18G   925G        -         -     0%     0%  1.00x    ONLINE  -
  sda3      931G  3.18G   925G        -         -     0%  0.34%      -    ONLINE
  1.  Above shown 3.18GB is used.  It contains only 1 partition called sda3 in disk sda
  2. Locates a device which is bigger than 3.18GB.  Let's say 8GB
    1. Option 1: If TrueNAS, then create a new Zvol from any of the VDEV (storage pool)
    2. Option 2: Creates a new partition sdb1, sdb2, or sdb3 from 2nd disk (or 3rd, 4th, etc)
  3. For this post, I will use TrueNAS to create new Zvol called vg00/zvol0 (storage pool vg00, Zvol/dataset zvol0)
  4. Adds vg00/zvol0 into boot-pool
    1. Command: zpool add boot-pool vg00/zvol0
  5. Detach sda3 from boot-pool
    1. Command: zpool remove boot-pool sda3
    2. This command will take some time as it is going to copy 3.18GB into vg00/zvol0
  6. Re-create partition sda3 to smaller size, e.g. 100GB
    1. Command: parted sda
    2. Refers to parted command on how to delete partition using menu driven command
    3. Refers to parted command on how to create partition using menu driven command.  It must be larger than 3.18GB to hold all the data
  7. Adds sda3 back into boot-pool
    1. Command: zpool add boot-pool sda3
  8. Detach vg00/zvol0 from boot-pool
    1. Command: zpool remove boot-pool vg00/zvol0
    2. This command will take some time as it is going to copy 3.18GB into sda3


No comments: