File System Development
This guide covers file system development for embedded Linux systems, focusing on the Rock 5B+ platform.
File System Overview
1. Linux File System Hierarchy
/
├── bin/ # Essential binaries
├── boot/ # Boot files
├── dev/ # Device files
├── etc/ # Configuration files
├── home/ # User directories
├── lib/ # Libraries
├── media/ # Removable media
├── mnt/ # Mount points
├── opt/ # Optional software
├── proc/ # Process information
├── root/ # Root user home
├── run/ # Runtime data
├── sbin/ # System binaries
├── sys/ # System information
├── tmp/ # Temporary files
├── usr/ # User programs
└── var/ # Variable data
2. File System Types
- ext4: Standard Linux file system
- Btrfs: Advanced file system with snapshots
- XFS: High-performance file system
- JFFS2: Journaling Flash File System
- UBIFS: Unsorted Block Image File System
- SquashFS: Compressed read-only file system
Creating Custom File Systems
1. Buildroot Configuration
# Install Buildroot
git clone https://git.buildroot.net/buildroot
cd buildroot
# Configure for Rock 5B+
make rock5b_defconfig
# Customize configuration
make menuconfig
# Build file system
make
2. Yocto Project File System
# Create custom image recipe
cat > recipes-core/images/custom-image.bb << EOF
SUMMARY = "Custom embedded image for Rock 5B+"
LICENSE = "MIT"
inherit core-image
IMAGE_FEATURES += "splash"
CORE_IMAGE_EXTRA_INSTALL += "packagegroup-core-ssh-openssh"
CORE_IMAGE_EXTRA_INSTALL += "packagegroup-core-tools-debug"
CORE_IMAGE_EXTRA_INSTALL += "packagegroup-core-tools-profile"
# Custom packages
IMAGE_INSTALL += "custom-package"
EOF
File System Optimization
1. Size Optimization
# Remove unnecessary packages
# Use busybox instead of full GNU tools
# Compress file system
# Use read-only root file system
# Minimize library dependencies
2. Performance Optimization
# Use appropriate file system for storage type
# Optimize mount options
# Use tmpfs for temporary files
# Implement proper caching
# Use SSD-optimized settings
Custom File System Implementation
1. Simple File System
#include <linux/fs.h>
#include <linux/module.h>
#include <linux/kernel.h>
// File system structure
struct simple_fs_sb_info {
struct super_block *sb;
unsigned long block_size;
unsigned long total_blocks;
unsigned long free_blocks;
};
// Inode operations
static struct inode_operations simple_fs_inode_ops = {
.lookup = simple_fs_lookup,
.create = simple_fs_create,
.unlink = simple_fs_unlink,
};
// File operations
static struct file_operations simple_fs_file_ops = {
.read = simple_fs_read,
.write = simple_fs_write,
.open = simple_fs_open,
.release = simple_fs_release,
};
// Directory operations
static struct file_operations simple_fs_dir_ops = {
.read = generic_read_dir,
.iterate = simple_fs_readdir,
};
// File system operations
static struct super_operations simple_fs_sops = {
.alloc_inode = simple_fs_alloc_inode,
.destroy_inode = simple_fs_destroy_inode,
.read_inode = simple_fs_read_inode,
.write_inode = simple_fs_write_inode,
.put_super = simple_fs_put_super,
.statfs = simple_fs_statfs,
};
2. File System Registration
// File system type
static struct file_system_type simple_fs_type = {
.owner = THIS_MODULE,
.name = "simplefs",
.mount = simple_fs_mount,
.kill_sb = kill_anon_super,
};
// Mount function
static struct dentry *simple_fs_mount(struct file_system_type *fs_type,
int flags, const char *dev_name,
void *data)
{
struct dentry *root;
struct super_block *sb;
sb = sget(fs_type, NULL, set_anon_super, flags, NULL);
if (IS_ERR(sb))
return ERR_CAST(sb);
if (!sb->s_root) {
sb->s_flags |= MS_ACTIVE;
root = simple_fs_fill_super(sb, data, flags & MS_SILENT ? 1 : 0);
if (IS_ERR(root)) {
deactivate_locked_super(sb);
return root;
}
sb->s_root = root;
}
return dget(sb->s_root);
}
Flash File Systems
1. JFFS2 Configuration
# Create JFFS2 file system
mkfs.jffs2 -r /path/to/rootfs -o rootfs.jffs2 -e 0x20000 -s 0x800
# Mount JFFS2
mount -t jffs2 /dev/mtdblock0 /mnt/jffs2
2. UBIFS Configuration
# Create UBIFS file system
mkfs.ubifs -r /path/to/rootfs -o rootfs.ubifs -m 2048 -e 126976 -c 2047
# Mount UBIFS
mount -t ubifs ubi0:rootfs /mnt/ubifs
File System Security
1. Access Control
# Set proper permissions
chmod 755 /bin
chmod 644 /etc/passwd
chmod 600 /etc/shadow
# Use ACLs for fine-grained control
setfacl -m u:user:rwx /path/to/file
getfacl /path/to/file
2. Encryption
# Encrypt file system
cryptsetup luksFormat /dev/sda1
cryptsetup luksOpen /dev/sda1 encrypted
mkfs.ext4 /dev/mapper/encrypted
File System Monitoring
1. Disk Usage Monitoring
#include <sys/statvfs.h>
// Get file system statistics
int get_fs_stats(const char *path, struct statvfs *stats) {
if (statvfs(path, stats) != 0) {
perror("statvfs");
return -1;
}
printf("Total blocks: %lu\n", stats->f_blocks);
printf("Free blocks: %lu\n", stats->f_bavail);
printf("Block size: %lu\n", stats->f_frsize);
return 0;
}
2. File System Health
# Check file system
fsck /dev/sda1
# Monitor disk I/O
iostat -x 1
# Check disk usage
df -h
du -sh /path/to/directory
Performance Tuning
1. Mount Options
# Optimize mount options
mount -o noatime,nodiratime /dev/sda1 /mnt
# Use tmpfs for temporary files
mount -t tmpfs -o size=100M tmpfs /tmp
2. File System Parameters
# Tune ext4 parameters
tune2fs -o journal_data_writeback /dev/sda1
tune2fs -O ^has_journal /dev/sda1
# Set read-ahead
echo 1024 > /sys/block/sda/queue/read_ahead_kb
Backup and Recovery
1. File System Backup
# Create file system image
dd if=/dev/sda1 of=backup.img bs=4M
# Compress backup
gzip backup.img
# Create incremental backup
rsync -av --link-dest=/backup/previous /source/ /backup/current/
2. Recovery Procedures
# Restore from backup
dd if=backup.img of=/dev/sda1 bs=4M
# Recover deleted files
extundelete /dev/sda1 --restore-all
# Repair corrupted file system
fsck -y /dev/sda1
Best Practices
1. File System Design
- Choose appropriate file system for storage type
- Implement proper backup strategies
- Use read-only root file system when possible
- Implement proper logging and monitoring
- Use encryption for sensitive data
2. Performance Optimization
- Use appropriate mount options
- Implement proper caching
- Monitor disk I/O performance
- Use SSD-optimized settings
- Implement proper cleanup procedures
3. Security Considerations
- Implement proper access control
- Use encryption for sensitive data
- Regular security updates
- Monitor file system access
- Implement audit logging