You are not logged in.

#1 2015-10-31 01:35:29

unixman
Member
Registered: 2015-03-12
Posts: 64

ultimate initramfs

Hi. Since i dont like vanilla initramfs i decided to redesign/rewrite it.

Thus i wrote "init"  with C language. it was very thin. As below.

int main(int argc, char *argv[])
{
	int cfd;
	char* modules[] = {"scsi_mod.ko", "sd_mod.ko", "libata.ko" , "libahci.ko", "ahci.ko", 
                       "f2fs.ko", "serio.ko", "i8042.ko", "libps2.ko", "atkbd.ko",  0};  // ORDER is important
	
	for (char i = 0; modules[i] != 0; i++) {
		cfd = syscall(2, modules[i], 0);                    //open(modules[i], 0)
		syscall(313, cfd, "", 0);                                 //finit_module(cfd, "", 0)
		syscall(3, cfd);                                             //close(cfd)
	}

	syscall(83, "root", 0755);                                    //mkdir("root", 0755)
	syscall(133, "nod", 060755, 0x802);                   //mknod("nod", 060755, 0x802)
	syscall(165, "nod", "root", "f2fs", 1024, 0);         //mount("nod", "root", "f2fs", 1024, 0)
	syscall(80, "root");                                              //chdir("root")
	syscall(161, ".");                                                 //chroot(".");

	syscall(59, "/bin/init", (char*[2]) {"/bin/init", 0}, 0);     //execve("/bin/init", (char*[2]) {"/bin/init", 0}, 0)
}

So my initramfs consist of

[me@localhost ramfs]$ ls
ahci.ko  f2fs.ko  init	libahci.ko  libata.ko  scsi_mod.ko  sd_mod.ko
[me@localhost ramfs]$ 

There are no busybox, libs, aux and no garbage into it as you see.

To compile it i ran

gcc -O3 -static ramfs.c -o ramfs/init 

Then to create initramfs

[root@localhost ramfs]# find . -print0 | bsdcpio -o -0 -H newc > /boot/initramfs.img

i tested;  it works properly. if you adopt it may be you need to change "/dev/sda2" and "f2fs.ko" in above file.

If you use ext4 you must replace "f2fs.ko" with "mbcache.ko", "jbd2.ko", "crc16.ko", "ext4.ko"   ORDER is important.

Arch vanilla ramfs is 5.2MB when my homemade 2.2MB in size.(Both uncompressed)

i hope someone like it.

EDIT: Assembly version of above is below. works like a charm:)  1.59MB in size.

section .data
	cmd db "/bin/init", 0
	modules do 'scsi_mod.ko', 'sd_mod.ko', 'libata.ko' , 'libahci.ko', 'ahci.ko', 'f2fs.ko', 'serio.ko', 'i8042.ko', 'libps2.ko', 'atkbd.ko' 
	vars  dq "nod", "/r", "f2fs", ""

section .text
	global _start

_start:
	mov rax, 83
	mov rdi, vars+8
	mov rsi, 755o
	syscall

	mov rax, 133
	mov rdi, vars
	mov rsi, 60755o
	mov rdx, 2050
	syscall

	mov rcx, 10
	mov rbx, modules 
L1:
	push rcx

	mov rax, 2 
	mov rdi, rbx 
	mov rsi, 0
	syscall

	mov rdi, rax  
	mov rax, 313   
	mov rsi, vars+24
	mov rdx, 0
	syscall 

	mov rax, 3
	syscall

	add rbx, 16
	pop rcx
loop L1
	
	mov rax, 165 
	mov rdi, vars
	mov rsi, vars+8
	mov rdx, vars+16
	mov r10, 1024
	mov r8, 0
	syscall 

	mov rax, 80
	mov rdi, vars+8
	syscall 

	mov rax, 161
	mov rdi, vars+8
	syscall 

	mov rax, 59 
	mov rdi, cmd  
	lea rsi, [rsp + 8]
	mov rdx, 0
	syscall 

PS: nasm syntax

Last edited by unixman (2015-11-11 19:17:44)

Offline

#2 2015-10-31 10:47:58

moetunes
Member
From: A comfortable couch
Registered: 2010-10-09
Posts: 1,033

Re: ultimate initramfs

Using systemd kinda defeats the minimalism.
How do you shutdown or reboot ?


You're just jealous because the voices only talk to me.

Offline

#3 2015-10-31 12:49:34

unixman
Member
Registered: 2015-03-12
Posts: 64

Re: ultimate initramfs

Yes, i like minimalism so much. This is an initramfs just for mounting  the 'root'.

moetunes wrote:

Using systemd kinda defeats the minimalism.

It is hard to go away systemd. Almost no chance.

There is no support for shutdown or reboot or anything within it.

moetunes wrote:

How do you shutdown or reboot ?

i use 'systemctl reboot' or just 'reboot'  both.

i no like systemd nor hate. it is average not brillant. So i updated the code as below.

int main(int argc, char *argv[])
{
	char* modules[] = {"scsi_mod.ko", "sd_mod.ko", "libata.ko" , "libahci.ko", "ahci.ko", "f2fs.ko", 0};
	const char* args[] = {"/sbin/init", 0};
	int cfd;
	
	for (char i = 0; modules[i] != 0; i++) {
		cfd = syscall(2, modules[i], 0);
		syscall(313, cfd, "", 0);
		syscall(3, cfd);
	}

	syscall(165, "dev",  "/dev",  "devtmpfs", 2, 0);  
	syscall(83, "new_root", 755);
	syscall(165, "/dev/sda2", "/new_root", "f2fs", 1024, 0);
	syscall(165, "proc", "/new_root/proc", "proc",     14, 0);
	syscall(165, "sys",  "/new_root/sys",  "sysfs",    14, 0);
	syscall(165, "run",  "/new_root/run",  "tmpfs",    6, 0);   
	syscall(165, "dev",  "/new_root/dev", "devtmpfs", 8192, 0);    

	syscall(80, "/new_root");
	cfd = syscall(2, "/", 0);
	syscall(165, "new_root", "/", 0, 8192, 0);
	syscall(161, ".");
	syscall(3, cfd);

	syscall(59, args[0], args, 0);
}

Also tested.

Last edited by unixman (2015-10-31 12:51:16)

Offline

#4 2015-10-31 13:01:31

frostschutz
Member
Registered: 2013-11-15
Posts: 1,409

Re: ultimate initramfs

unixman wrote:

Yes, i like minimalism so much. This is an initramfs just for mounting  the 'root'.

What if I told you - you don't even need an initramfs for that? Kernel can mount unencrypted, regular partition root by itself, as long as driver for block device and filesystem and such are built-in.

moetunes wrote:

Using systemd kinda defeats the minimalism.

He's not using systemd (in the initramfs) just execing the one on the root...

I'll stick with busybox, myself. I find shell scripts easier to read and understand than C with ... magic syscall numbers.

Plus in my case it needs mdadm, luks, lvm anyways so you can't do this in C even if you wanted to, and if you start executing shell commands in C you can just as well stick to shell scripts.

Offline

Board footer

Powered by FluxBB