HOWTO: Replace the micro SD card's ext4 root partition by f2fs on the Raspberry PI


- Raspbian System running on a Raspberry Pi (this example based on Raspbian JESSIE)
- a separate second computer running Linux (Debian or Ubuntu by preference)

Make sure your working account has sudo privileges.

System check:

On the Raspberry Pi: type the following command in a Terminal:


   sda                57,6G              
   └─sda1      ntfs   57,6G /media/stick WEBDISC01
   mmcblk0            60,1G              
   ├─mmcblk0p1 vfat     60M /boot        boot
   └─mmcblk0p2 ext4     60G /      
The micro SD card on the Raspberry Pi is, as usual, named mmcblk0. It consists of two partitions:
mmcblk0p1 containing a vfat file system, mounted on /boot and
mmcblk0p2 containing an ext4 file system, mounted on / (root).

Our plan is now to replace the ext4 file system on mmcblk0p2, containing the root partition, by a f2fs file system, which is SD card friendly and is said to extend the card's durability.
Note that there is also an USB Stick sda, containing a NTFS file system on sda1, mounted on /media/stick - which is left untouched and doesn't affect the procedure.

Since the root partition of the SD card cannot be re-formatted on the running system, the system needs to be shut down, and, because the root partition will be on a f2fs file system when booted again, we need to

Ensure f2fs support is available on the Raspberry Pi

before shutdown. So, just run
   sudo apt-get install f2fs-tools
Then shutdown the Raspberry Pi.

Backup the SD card's root partition

Now we change to the second Linux computer, mentioned above, where a SD card drive is available. On this system, we need f2fs support as well, so login as a sudoer and - just like on the Pi - run
   sudo apt-get install f2fs-tools
Then remove the micro SD card from the Raspbrry Pi and insert it into the second Linux computer's SD card drive. Usually on common Linux desktop systems the card's file systems will be auto-mounted, and with
you'll probably find something like
   NAME   FSTYPE   SIZE MOUNTPOINT                                          LABEL
   . . .
   sdb            60,1G                                                     
   ├─sdb1 vfat      60M /media/user/boot                                 boot
   └─sdb2 ext4      60G /media/user/ad6203a1-ec50-4f44-a1c0-e6c3dd4c9202
   . . .
The long uid mount point name of the card's ext4 partition is kind of unhandy, so the following step is for better illustration only and thus does not bring us any further:
   sudo umount /dev/sdb1
   sudo umount /dev/sdb2
   sudo mkdir /media/boot_fs
   sudo mkdir /media/root_fs
   sudo mount -t vfat /dev/sdb1 /media/user/boot_fs
   sudo mount -t ext4 /dev/sdb2 /media/user/root_fs
Now, the SD card mount is much better readable:
   sdb            60,1G                                                     
   ├─sdb1 vfat      60M /media/user/boot_fs
   └─sdb2 ext4      60G /media/user/root_fs
which means:
what would be the boot partition on the Raspberry Pi, is /dev/sdb1 with a 60 MB vfat file system, mounted to the /media/user/boot_fs folder on the Linux desktop system
what would be the root ( / ) partition on the Raspberry Pi, is /dev/sdb2 with a 60 GB ext4 file system, mounted to the /media/user/root_fs folder on the Linux desktop system.

Time to proceed now.
Formatting the ext4 partition to another file system type will destroy all data, so it is neccessary to backup the contents of the partition:
   sudo cp -a /media/user/root_fs /home/user/backup_root_fs
Note that the -a option is important to ensure that really everything, including e.g. hidden/system files, is copied to the backup folder.

Create the f2fs partition

Now unmount the /dev/sdb2 partition
   sudo umount /dev/sdb2
and format it with the the f2fs file system
   sudo mkfs.f2fs /dev/sdb2
The partition has now a f2fs file system, but is empty, because by formatting all it's content got lost. Thus, we need to

Restore the root partition's content

Re-mount the partition to it's previous mount point
   sudo mount -t f2fs /dev/sdb2 /media/user/root_fs
now with the f2fs type option. The SD card mount will then look like this:
   sdb            60,1G                                                     
   ├─sdb1 vfat      60M /media/user/boot_fs
   └─sdb2 f2fs      60G /media/user/root_fs
We can now restore the root file system's content from the backup folder:
   sudo cp -a /home/user/backup_root_fs/* /media/user/root_fs
A little more to do to make the Raspberry Pi bootable with the SD card. First edit the SD card's /etc/fstab file and adapt the root file system's mount entry to f2fs:
   sudo vi /media/user/root_fs/etc/fstab
   . . .
   /dev/mmcblk0p1  /boot  vfat    defaults          0       2
   /dev/mmcblk0p2  /      f2fs    defaults,noatime,discard  0       1
   . . .
Then edit the cmdline.txt file on the SD card's boot partition and adapt the root file system type parameter to f2fs:
   sudo vi /media/user/boot_fs/cmdline.txt

   dwc_otg.lpm_enable=0 console= . . . root=/dev/mmcblk0p2 rootfstype=f2fs elevator=deadline . . .
That's it. You can now unmount the SD card's file systems, eject the card and insert it into the Raspberry Pi's card drive. The Pi will hopefully boot normally.