implement heuristic random filesystem filling. for boot its random modules + kernel, for root its recreation of FreeBSD's file structure, for stress testing purposes. The Makefile containts proper target doing all required steps to recreate disk image with 1 test partition and 2 real-life example EXT2 partitions.

This commit is contained in:
mykola2312 2024-08-03 00:29:36 +03:00
parent d63936c4f5
commit 818b222f2a
4 changed files with 74 additions and 1 deletions

2
.gitignore vendored
View file

@ -1,2 +1,4 @@
.vscode/
obj/
bin/
mnt/

View file

@ -2,6 +2,9 @@ CC = gcc
CLFAGS = -g -Wall
LDFLAGS = -g
UID := $(shell id -u)
GID := $(shell id -g)
DISK = bin/disk.img
DISK_SIZE = 268435456
DISK_LOOP = /dev/loop690
@ -38,7 +41,10 @@ PART3_LOOP = /dev/loop693
.PHONY: clean
$(DISK):
mnt:
mkdir mnt
$(DISK): mnt
# create img file
truncate -s 256m $(DISK)
# format img file in MBR and create partitions
@ -60,6 +66,18 @@ $(DISK):
sudo mkfs.ext2 -t $(PART2_FSTYPE) -L $(PART2_LABEL) -U $(PART2_UUID) $(PART2_LOOP)
sudo mkfs.ext2 -t $(PART3_FSTYPE) -L $(PART3_LABEL) -U $(PART3_UUID) $(PART3_LOOP)
# fill EXT2 partitions
# mount and fill boot partition
sudo mount $(PART2_LOOP) mnt/ # mount
sudo chown -R ${UID}:${GID} mnt/ # because we ain't gonna run python with root, so own it by user
python gen/generate_files.py --mode=boot --dirs=gen/rootfs-dirs.gz --path=mnt
sudo chown -R 0:0 mnt/ # make it root
sudo umount mnt/ # done
# mount and fill root partition
sudo mount $(PART3_LOOP) mnt/ # mount
sudo chown -R ${UID}:${GID} mnt/ # because we ain't gonna run python with root, so own it by user
python gen/generate_files.py --mode=root --dirs=gen/rootfs-dirs.gz --path=mnt
sudo chown -R 0:0 mnt/ # make it root
sudo umount mnt/ # done
# detach loopback devices
sudo losetup -d $(PART1_LOOP)
sudo losetup -d $(PART2_LOOP)

53
gen/generate_files.py Normal file
View file

@ -0,0 +1,53 @@
import os
import os.path
import sys
import gzip
import string
import random
def get_arg(name):
return "".join(list(filter(lambda arg: arg.startswith(f"--{name}="), sys.argv))[0].split("=")[1:])
def rand_string(strlen):
return ''.join(random.choice(string.ascii_lowercase) for x in range(strlen))
def rand_file(fpath, size):
rand = open("/dev/urandom", "rb")
with open(fpath, "wb") as file:
file.write(rand.read(size))
rand.close()
def fill_boot(path):
# fill modules dir
os.mkdir(os.path.join(path, "modules"))
for _ in range(24):
rand_file(os.path.join(path, "modules", rand_string(14)), random.randint(1024*3, 1024*1024))
# generate some configs
for _ in range(10):
rand_file(os.path.join(path, rand_string(8)), random.randint(16, 1024))
# write stage2 and kernel
rand_file(os.path.join(path, "stage2"), 8192)
rand_file(os.path.join(path, "kernel"), 1024*1024*16)
def fill_root(path, dirs):
with gzip.open(dirs, "rt") as dirs:
for dir_path in dirs:
dir = dir_path[1:].strip()
if not dir: continue
os.mkdir(os.path.join(path, dir.strip()))
if __name__ == "__main__":
path = get_arg("path")
dirs = get_arg("dirs")
mode = get_arg("mode")
if not os.path.exists(path):
sys.stderr.write("the mnt dir doesn't exists. preventing disaster")
sys.exit(1)
if mode == "boot": fill_boot(path)
elif mode == "root": fill_root(path, dirs)
else:
sys.stderr.write("unkown mode\n")
sys.exit(1)

BIN
gen/rootfs-dirs.gz Normal file

Binary file not shown.