Can i shrink and partition a zfs disk?

Hello, i have FreeBSD installed in a zfs disk, and i want to shrink the filesystem and make a new partition to install some non-systemd Linux distro for gaming.
Also any suggestions of bootloader (i wave windowns 10 (or 11 dont remember) in a disk and want to boot FreeBSD and Linux from the other zfs disk. Thanks.
 
Yes, you can, but it's complex. Nothing in zfs commands is designed for that (conversely, it's easy to make the filesystem grow).

I did that once for a Virtualbox VM. I used zfs send and receive, IIRC. I added a receiver disk to the VM, so I don't know if it's applicable in your case. If noone propose a solution, I will post my procedure after my dayjob, if this can inspire you.
 
Yes, you can, but it's complex. Nothing in zfs commands is designed for that (conversely, it's easy to make the filesystem grow).

I did that once for a Virtualbox VM. I used zfs send and receive, IIRC. I added a receiver disk to the VM, so I don't know if it's applicable in your case. If noone propose a solution, I will post my procedure after my dayjob, if this can inspire you.
Thanks, if no responses post it if you find the time. Thank you.
 
Hello, i have FreeBSD installed in a zfs disk, and i want to shrink the filesystem and make a new partition to install some non-systemd Linux distro for gaming.
Also any suggestions of bootloader (i wave windowns 10 (or 11 dont remember) in a disk and want to boot FreeBSD and Linux from the other zfs disk. Thanks.
zfs send & receive to backup device , then delete freebsd-zfs partition.
 
zfs send & receive to backup device , then delete freebsd-zfs partition.
I second that.
Shrinking a partition is always a tricky and risky job. Enlarge is almost trivial, but shrinking is not, since the program do the shrinking needs to discriminate used from unused blocks.
I did it a lot in my old Windows days with GParted Live (cannot handle ZFS), but those were NTFS and FAT32 partitions in a MBR scheme, so way less complex than ZFS in GPT.

I'd say - like Alain De Vos already said - above and first at all do a fresh full and clean BU anyway before you start any stunt, and "just" create a new partition scheme:
You either - what I would prefer - create a new disk with your wanted partition scheme, copy your stuff to that drive, and exchange drives.
Or if you backupped all your system configs, have a list of all packages installed pkg prime-list > /path/to/packageslist.txt and /home/ and others safely backupped (of course you do that anyway), it can be less complicated, less risky, and way quicker than fumbling with shrinking to do a fresh installation onto a clean wiped drive with your wanted partiton scheme, and copy your stuff to that.
 
The procedure to transfert a FreeBSD standard installation to a new disk and reducing the size of the pool. It's not exactly what you need, but it can help. In your case, just focus on the freebsd-zfs partition. You can use an USB key as receiver and transfert back the pool on a reduced freebsd-zfs partition on your disk. It's not without risk.
Code:
- Add the new disk and start the machine (nda1).

# gpart show 
=>       40  104857520  nda0  GPT  (50G)
         40     532480     1  efi  (260M)
     532520       1024     2  freebsd-boot  (512K)
     533544        984        - free -  (492K)
     534528    4194304     3  freebsd-swap  (2.0G)
    4728832  100126720     4  freebsd-zfs  (48G)
  104855552       2008        - free -  (1.0M)

- Disk settings
ls /dev/nda*
/dev/nda0    /dev/nda0p2    /dev/nda0p4
/dev/nda0p1    /dev/nda0p3    /dev/nda1

# gpart create -s GPT /dev/nda1
nda1 created
# gpart show -l
=>       40  104857520  nda0  GPT  (50G)
         40     532480     1  efiboot0  (260M)
     532520       1024     2  gptboot0  (512K)
     533544        984        - free -  (492K)
     534528    4194304     3  swap0  (2.0G)
    4728832  100126720     4  zfs0  (48G)
  104855552       2008        - free -  (1.0M)

=>      40  41942960  nda1  GPT  (20G)
        40  41942960        - free -  (20G)
# gpart add -t efi -l efiboot0 -s 260m nda1
nda1p1 added
# gpart add -t freebsd-boot -l gptboot0 -s 512k nda1
nda1p2 added
# gpart add -t freebsd-swap -l swap0 -b 534528 -s 2g nda1
nda1p3 added
# gpart add -t freebsd-zfs -l zfs0 nda1
nda1p4 added

- EFI boot settings
# newfs_msdos nda1p1
/dev/nda1p1: 532288 sectors in 16634 FAT16 clusters (16384 bytes/cluster)
BytesPerSec=512 SecPerClust=32 ResSectors=1 FATs=2 RootDirEnts=512 Media=0xf0 FATsecs=65 SecPerTrack=63 Heads=255 HiddenSecs=0 HugeSectors=532480
# mount -t msdosfs /dev/nda1p1 /mnt
# mkdir -p /mnt/efi/boot
# mkdir /mnt/efi/freebsd
Note and check: the efi partion must be mounted on /boot/efi
# cp /boot/efi/efi/boot/* /mnt/efi/boot/
# cp /boot/efi/efi/freebsd/* /mnt/efi/freebsd/
# umount /mnt

- freebsd-boot settings
# gpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i2 nda1
partcode written to nda1p2
bootcode written to nda1

- Datasets transfert
# zfs list
NAME                 USED  AVAIL  REFER  MOUNTPOINT
zroot               5.29G  40.7G    96K  /zroot
zroot/ROOT          3.55G  40.7G    96K  none
zroot/ROOT/default  3.55G  40.7G  3.55G  /
zroot/home          65.8M  40.7G  61.7M  /home
zroot/tmp            292K  40.7G   200K  /tmp
zroot/usr           1.67G  40.7G    96K  /usr
zroot/usr/ports      899M  40.7G   899M  /usr/ports
zroot/usr/src        816M  40.7G   816M  /usr/src
zroot/var           1.03M  40.7G    96K  /var
zroot/var/audit       96K  40.7G    96K  /var/audit
zroot/var/crash       96K  40.7G    96K  /var/crash
zroot/var/log        544K  40.7G   356K  /var/log
zroot/var/mail       128K  40.7G   128K  /var/mail
zroot/var/tmp         96K  40.7G    96K  /var/tmp
# zfs snapshot -r zroot@tr
# zpool create -o altroot=/mnt aroot nda1p4 
# zfs send -R zroot@tr | zfs receive -F aroot
# zfs set mountpoint=/aroot aroot
# zpool bootfs=aroot/ROOT/default aroot
# zfs destroy -r aroot@tr
# zfs destroy -r zroot@tr
# zfs mount aroot/ROOT/default
# rm /mnt/boot/zfs/zpool.cache 
# rm /mnt/etc/zfs/zpool.cache
# rmdir /mnt/zroot

- poweroff, then remove the old disk (nda0)
- power on
Note and check: the efi partion must be mounted on /boot/efi
# efibootmgr -a -c -l /boot/efi/efi/freebsd/loader.efi -L freebsd-newdisk
 
If you know the low level layout of zfs, in theory you can write a "compacting" algorithm - IIRC DOS had such a tool for compacting FAT filesystem and you could see a visualization (diff color for free and used blocks) and you could see as blocks were moved. In zfs/zpool you'd use diff. color for different categories of blocks... The goal would be to move stuff from the end of the storage pool toward the start, filling in free blocks and fixing up various data structures.

Looks like Martin Minkus has already done a bunch of work to explore low level details of zfs & even written a GUI for it! https://github.com/mminkus/zfs-explorer - he even uses the HPLOD (Hot Programming Language Of the Day) rust for it! Also check out https://github.com/mminkus/zfs-explorer for zfs on disk format.
 
😂 That's right.
Those beans are yours: 🫘
1775241288269.png
 
Back
Top