Skip to content

FreeBSD & ZFS Notes to self

ZFS Datarecovery:

If you have a ZFS pool with expired redundancy, you can try this in FreeBSD
Recover data of a zpool with I/O errors without redundancy

zpool import -F -R /mnt -N -f -o readonly=on *yourpool*

And what can be saved will be saved.
Remember to change back the sysctl’s after data recovery.

(the solution is for FreeBSD. Linux/Solaris uses other ways to set libzfs options, mdb for Solaris, different systl-s for Linux)

Add drives to FreeBSD ZFS System

How to add drives to existing ZFS pool with swap space and boot space.

Add the new drive to the system’s SATA or SAS bus. Write down the serialnumber of the drive for future reference. Keep it on a note inside the enclosure.
When the drive do not have any partition on it, it will not show up in /dev/
Use camcontrol to get the dev address, here we have added a Seagate 8TB drive to the system.

# camcontrol devlist

<ST8000AS0002-1NA17Z AR17>         at scbus1 target 0 lun 0 (pass0,ada0)
<SAMSUNG MZ7LN256HCHP-000L7 EMT03L6Q>  at scbus2 target 0 lun 0 (pass1,ada1)
<AHCI SGPIO Enclosure 2.00 0001>   at scbus6 target 0 lun 0 (pass2,ses0)
<Generic- Multi-Card 1.00>         at scbus7 target 0 lun 0 (da0,pass3)

First destroy existing partition on drive:

gpart destroy -F ada0

Create GPT on drive:

gpart create -s GPT ada0

Add partition table:

gpart add -t freebsd-boot -s 512K ada0 
gpart add -t freebsd-swap -b 2048 -s 2G ada0 
gpart add -t freebsd-zfs -b 4196352 ada0

Verify the partitions:

# gpart show
=>       40  500118112  ada1  GPT  (238G)
         40     532480     1  efi  (260M)
     532520       1024     2  freebsd-boot  (512K)
     533544        984        - free -  (492K)
     534528    4194304     3  freebsd-swap  (2.0G)
    4728832  495388672     4  freebsd-zfs  (236G)
  500117504        648        - free -  (324K)

=>         40  15628053088  ada0  GPT  (7.3T)
           40         1024     1  freebsd-boot  (512K)
         1064          984        - free -  (492K)
         2048      4194304     2  freebsd-swap  (2.0G)
      4196352  15623856776     3  freebsd-zfs  (7.3T)

Add the GPT partition “freebsd-zfs” as a zpool to the system.

# zpool create tank ada0p3

Use zfs create to make appropriate dataset’s for your usecase.

Resize bhyve vm image:

So you have a vm running low on disk space. Run the following commands to resize the image file.

pkg install e2fsprogs
cd /zroot/bhyve/<vm-name>

# Use truncate to allocate diskspace for the image
truncate -s<newsize> disk0.img

# Use mdconfig to be able to make changes to image
mdconfig disk0.img

# Reveal the partion scheme with gpart
gpart show md0
gpart recover md0      # Run if you get "gpart: table 'md0' is corrupt: Operation not permitted" This is sign of GPT scheme

gpart resize -iN md0   # N - the partition index

# Run fsck on the partition to be sure that everything is good
e2fsck -f /dev/md0pN   # N - the partition index

# The actual resize command, this will take a while depending on size
resize2fs /dev/md0pN   # could be md0pN in case of GPT scheme

# Close the image.
mdconfig -d -u0

If you are using Proxmox as HyperVisor, you can run FreeBSD inside that. But sometimes you need to expand your zfs root disk. The steps are almost like above.

pkg install e2fsprogs

gpart show
# Your zroot is usually something like ada0 and the last partition.
# Repair the partion with:
gpart recover ada0

# Resize the last partition into the free space
gpart resize -i N ada0 # N - the partition index

# now comes the zfs part. To have zfs to expand in to the free space. You need to online the partition with the -e argument.

zpool online -e zroot /dev/ada0pN # N - the partition index

# You should now have expanded your partition to the new size. Be aware that you can not undo this change.
Published inBSD

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.