Remake initrd

A home for all kinds of Puppy related projects
Post Reply
Message
Author
NickYa
Posts: 1
Joined: Thu 01 Dec 2011, 18:33

Remake initrd

#1 Post by NickYa »

Hi All

I have idea to substitute file init in initrd as (example)
// 2>/dev/null; diet -v -Os gcc -static init.c -o init -Wl,-s,-x,--gc-sections; exit
#include <stdio.h>
#include <unistd.h>
#include <sys/mount.h>
#include <sys/vfs.h>
#include <sys/stat.h>
#include <dirent.h>
#include <fcntl.h>
#include <linux/loop.h>

int main(int arvc, char *argv[], char *env[]){
char *devname="/Y.sfs";
char *size="size=64m";
int fd,fdev,lodev;
struct stat st;
struct statfs stfs;
dev_t rootdev;

while(*env){ if(env[0][0]=='s'&&env[0][1]=='i'&&env[0][2]=='z'&&env[0][3]=='e'&&env[0][4]=='=') size=env[0];env++;}

fdev = open(devname,O_RDWR);
if (fdev==-1) return 1;
lodev = open("/dev/loop0",O_RDWR);
if (lodev==-1) return 2;
ioctl(lodev, LOOP_SET_FD, fdev);
close(lodev);

if(mount("/dev/loop0","/d_root","squashfs",0,NULL)) return 3;
if(mount("none", "/d_rw","tmpfs",0,size)) return 4;
if(mount("aufs","/d_new","aufs",0,"br=/d_rw:/d_root")) return 5;

chdir("/d_new");
stat("/", &st);
rootdev = st.st_dev;
stat(".", &st);
statfs("/", &stfs);
if(mount(".", "/", NULL, 8192, NULL)) printf("error moving root\n");;
chroot(".");
chdir("/");

execl("/sbin/init", "init", (char*)NULL);

return 0;
}
//-----------------------------------------------------------------
and insert root squashfs into initrd (CPIO-archive).
In this case the kernel have to be compiled with squashfs and aufs.
I tested this idea with Fatdog and lucid and it works very quickly.
It'll be very good for NetBoot.

Cheers

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#2 Post by technosaurus »

very nice :) ... but why include an sfs rather than using it directly, you can even build it right into the kernel

I too recently wrote a little C init, but for a more specialized purpose
http://www.murga-linux.com/puppy/viewto ... 822#590822

thanks for the mount() example though - I didn't realize it was a sys command ... some comments would be even nicer though :)
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#3 Post by technosaurus »

I did a little looking into the kernel command line parameters, because I built a system without procfs or sysfs support, but still wanted to be able to get the contents of /proc/cmdline.

As NickYa demonstrates you can use env to get the parameters, using a standard main() construct. An alternative would be getenv("size")
That works for parameters matching the VAR=value construct because the kernel sets them, while the rest of the parameters (that don't match the kernel's inbuilt constructs) such as "nox" or "secret_squirrel" get passed as arguments to init (accessible via argv).

if init is a shell script the "env[]" is already set as variables and the argv[] is $1 $2 $3 ...
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

Post Reply