Compile filesystem as module

Compile filesystem as module

1. Purpose

To avoid rebooting in filesystem development.

2. Environment

OS: Ubuntu 16.04.4 LTS

Kernel: 4.4.1

Target filesystem: EXT2

3. Procedure

  1. Run make menuconfig, mark EXT2 as module:

    1
    <M> Second extended fs support

    save this configuration.

  2. Compile && install kernel:

    1
    2
    3
    make -j$(nproc)
    make modules_install
    make install

    reboot and choose this kernel in grub.

  3. Create a directory called ‘modulefs’, and copy EXT2 source files to it:

    1
    2
    3
    cd ~
    mkdir modulefs
    cp -r <kernel_directory>/fs/ext2 modulefs
  4. Modify Makefile of EXT2, here is the diff result:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    5c5
    < obj-$(CONFIG_EXT2_FS) += ext2.o
    ---
    > obj-m += ext2.o
    9a10,12
    > KERNELDIR:=/home/feng/btrfs-test/linux-4.4.1/
    > PWD:=$(shell pwd)
    >
    12a16,20
    >
    > default:
    > make -C $(KERNELDIR) M=$(PWD) modules
    > clean:
    > rm -rf *.o *.mod.c *.ko *.symvers

    Replace KERNELDIR path with your kernel path.

  5. In ~/modulefs/ext2/, compile EXT2:

    1
    2
    make clean
    make -j$(nproc)
  6. Remove EXT2 module in system:

    1
    2
    modprobe ext2
    rmmod ext2
  7. Insert our EXT2 module into system:

    1
    insmod ext2.ko
  8. Add some printk in EXT2 source code to make sure verify this module.