Changing the Raspberry Pi image filesystem to F2FS

Intro

The F2FS filesystem was developed specifically for flash memory like SD-cards. This means that the memory of a Raspberry Pi can last longer, which is a win. There are already tutorials out there of how to change your working Raspberry Pi file system use F2FS, but since there aren’t any official Raspbian images using F2FS, I’ll explain the steps of changing the official image to use F2FS.

Setup

Make sure you have an empty micro SD-card of at least 8GB (I’ll use one of 32GB). Next, download the newest Raspbian image from the official download page.

Changing Ext4 to F2FS

Now we can start to change the filesystem. To start, we need to mount the image, but since it contains multiple partitions we’ll create some loop devices first. As root execute the following

  kpartx -av /home/user/Downloads/raspbian.img

Note down the loop device and execute the following commands to mount the root filesystem of the pi image (we use loop0 here, make sure to change it to your situation)

  mkdir /media/user/rootfs
  mount /dev/mapper/loop0p2 /media/user/rootfs

Next up we create a temporary file based filesystem of about 1.8Gb to store the data so we can change the root partition filesystem. This is enough for the light image, but make sure to change the count if you have a larger image.

dd if=/dev/zero of=tmp.fs bs=1024 count=1722368
mkfs.f2fs tmp.fs
mkdir /media/user/tmp
mount tmp.fs /media/user/tmp/
sudo rsync -axv /media/user/rootfs /media/user/tmp

Now we need to change the filesystem entry in fstab as well. So we open /media/user/tmp/etc/fstab using vim and change this line

/dev/sda1     /          ext4     defaults,noatime     0     1

to the following

/dev/sda1     /          f2fs     defaults,noatime,discard     0     1

If your SD-card doesn’t support TRIM, you can leave out the discard option, but most SD-cards have it, so most of the times you can keep it.

Now we can change the file system of the Raspbian image and copy the files back. Make sure to use the right loop number (same as before) since you are removing all data on it!

umount /media/user/rootfs
wipefs -a /dev/mapper/loop0p2
mkfs.f2fs /dev/mapper/loop0p2
mount /dev/mapper/loop0p2 /media/user/rootfs
rsync -axv /media/user/tmp /media/user/rootfs

Now we can unmount these filesystems and remove the temporary file we created before

umount /media/user/rootfs
umount /media/user/tmp
rm /media/user/tmp.fs

We’re almost done, but before the image is ready, we also need to change something on the boot partition, namely the cmdline.txt. To do so, we first mount the boot partition

mkdir /media/user/boot
mount /dev/mapper/loop0p1 /media/user/boot

Using vim we open /media/user/boot/cmdline.txt to edit ‘rootfstype=ext4’ to ‘rootfstype=f2fs’. The result should look similar to this

console=serial0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=f2fs elevator=deadline fsck.repair=yes rootwait

The image is ready for installation, yay! Now we only need to close everything that’s still open

umount /media/user/boot
kpartx -dv /home/user/Downloads/raspbian.img

And that’s it, you’ve successfully changed your raspbian image to use the F2FS filesystem.

Final steps

If you want to actually install the modified image, you only need to enter one more command. It’s pretty much copied from the official Raspberry Pi documentation, so make sure to look there for more information. Anyways, make sure that the SD-card isn’t mounted and then type the following as root, where ‘device’ is the identifier for the SD-card (most likely sdX or mmcblkX)

dd if=raspbian.img of=/dev/device bs=4M conv=fsync status=progress

Enjoy your F2FS Raspberry Pi installation!

Sources

Trivial statements are trivial.

Rick Dullaart
email: dullaart[at]strw