mirror of
https://github.com/psankar/simplefs.git
synced 2025-07-22 21:44:30 +02:00
mkfs-simplefs: An utility to create simplefs on a device.
This commit is contained in:
parent
f050b4f8e7
commit
5b5062b46e
8
Makefile
8
Makefile
@ -1,8 +1,14 @@
|
||||
obj-m := simplefs.o
|
||||
simplefs-objs := simple.o
|
||||
|
||||
all:
|
||||
all: ko mkfs-simplefs
|
||||
|
||||
ko:
|
||||
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
|
||||
|
||||
mkfs-simplefs_SOURCES:
|
||||
mkfs-simplefs.c simple.h
|
||||
|
||||
clean:
|
||||
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
|
||||
rm mkfs-simplefs
|
||||
|
25
README
25
README
@ -15,7 +15,7 @@ To test:
|
||||
|
||||
One-time-activity:
|
||||
|
||||
linux-okb0:/home/psankar/src/simplefs > dd bs=1M count=100 if=/dev/zero of=image
|
||||
linux-okb0:/home/psankar/src/simplefs > dd bs=4096 count=100 if=/dev/zero of=image
|
||||
linux-okb0:/home/psankar/src/simplefs > mkdir mount
|
||||
|
||||
Repeat everytime after making changes to the source:
|
||||
@ -23,17 +23,14 @@ Repeat everytime after making changes to the source:
|
||||
linux-okb0:/home/psankar/src/simplefs > make
|
||||
linux-okb0:/home/psankar/src/simplefs > su - ; # switch to root user if not already, or use sudo
|
||||
|
||||
linux-okb0:/home/psankar/src/simplefs # dmesg -c
|
||||
linux-okb0:/home/psankar/src/simplefs # insmod simplefs.ko ; mount -o loop -t simplefs image /home/psankar/src/simplefs/mount/ ; dmesg
|
||||
[ 5788.269663] Sucessfully registered simplefs
|
||||
[ 5788.274803] simplefs is succesfully mounted on [/dev/loop2]
|
||||
linux-okb0:/home/psankar/src/simplefs # cd mount/
|
||||
linux-okb0:/home/psankar/src/simplefs/mount # ls
|
||||
linux-okb0:/home/psankar/src/simplefs/mount # cd ..
|
||||
linux-okb0:/home/psankar/src/simplefs # umount mount ; rmmod simplefs.ko
|
||||
linux-okb0:/home/psankar/src/simplefs # dmesg
|
||||
[ 5788.269663] Sucessfully registered simplefs
|
||||
[ 5788.274803] simplefs is succesfully mounted on [/dev/loop2]
|
||||
[ 5814.008604] simplefs superblock is destroyed. Unmount succesful.
|
||||
[ 5814.014065] Sucessfully unregistered simplefs
|
||||
# Creating a loop back device and formatting it with our shiny, brand new simplefs
|
||||
|
||||
linux-okb0:/home/psankar/src/simplefs # losetup /dev/loop0
|
||||
losetup: /dev/loop0: No such file or directory
|
||||
linux-okb0:/home/psankar/src/simplefs # losetup /dev/loop0 image
|
||||
linux-okb0:/home/psankar/src/simplefs # ./mkfs-simplefs /dev/loop0
|
||||
Super block written succesfully
|
||||
linux-okb0:/home/psankar/src/simplefs # losetup -d /dev/loop0
|
||||
linux-okb0:/home/psankar/src/simplefs # losetup /dev/loop0
|
||||
losetup: /dev/loop0: No such file or directory
|
||||
linux-okb0:/home/psankar/src/simplefs #
|
||||
|
44
mkfs-simplefs.c
Normal file
44
mkfs-simplefs.c
Normal file
@ -0,0 +1,44 @@
|
||||
#include "simple.h"
|
||||
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int fd;
|
||||
ssize_t ret;
|
||||
struct simplefs_super_block sb;
|
||||
|
||||
if (argc != 2) {
|
||||
printf("Usage: mkfs-simplefs <device>\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
fd = open(argv[1], O_RDWR);
|
||||
if (fd == -1) {
|
||||
perror("Error opening the device");
|
||||
return -1;
|
||||
}
|
||||
|
||||
sb.version = 1;
|
||||
sb.magic = SIMPLEFS_MAGIC;
|
||||
sb.block_size = SIMPLEFS_DEFAULT_BLOCK_SIZE;
|
||||
sb.free_blocks = ~0;
|
||||
|
||||
ret = write(fd, (char *)&sb, sizeof(sb));
|
||||
|
||||
/* Just a redundant check. Not required ideally. */
|
||||
if (ret != SIMPLEFS_DEFAULT_BLOCK_SIZE)
|
||||
printf
|
||||
("bytes written [%d] are not equal to the default block size",
|
||||
(int)ret);
|
||||
else
|
||||
printf("Super block written succesfully");
|
||||
|
||||
close(fd);
|
||||
|
||||
return 0;
|
||||
}
|
4
simple.c
4
simple.c
@ -9,6 +9,8 @@
|
||||
#include <linux/module.h>
|
||||
#include <linux/fs.h>
|
||||
|
||||
#include "simple.h"
|
||||
|
||||
static int simplefs_readdir(struct file *filp, void *dirent, filldir_t filldir)
|
||||
{
|
||||
/* ls will list nothing as of now.
|
||||
@ -76,7 +78,7 @@ int simplefs_fill_super(struct super_block *sb, void *data, int silent)
|
||||
struct inode *inode;
|
||||
|
||||
/* A magic number that uniquely identifies our filesystem type */
|
||||
sb->s_magic = 0x10032013;
|
||||
sb->s_magic = SIMPLEFS_MAGIC;
|
||||
|
||||
inode = simplefs_get_inode(sb, NULL, S_IFDIR, 0);
|
||||
inode->i_op = &simplefs_inode_ops;
|
||||
|
11
simple.h
Normal file
11
simple.h
Normal file
@ -0,0 +1,11 @@
|
||||
const int SIMPLEFS_MAGIC = 0x10032013;
|
||||
const int SIMPLEFS_DEFAULT_BLOCK_SIZE = 4 * 1024;
|
||||
|
||||
struct simplefs_super_block {
|
||||
unsigned int version;
|
||||
unsigned int magic;
|
||||
unsigned int block_size;
|
||||
unsigned int free_blocks;
|
||||
|
||||
char padding[ (4 * 1024) - (4 * sizeof(unsigned int))];
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user