The time now is Thu 21 Jan 2021, 12:56
All times are UTC - 4 |
Author |
Message |
NickYa
Joined: 01 Dec 2011 Posts: 1
|
Posted: Mon 05 Dec 2011, 16:56 Post subject:
Remake initrd Subject description: Embed SFS into initrd.cpio |
|
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
|
Back to top
|
|
 |
technosaurus

Joined: 18 May 2008 Posts: 4878 Location: Blue Springs, MO
|
Posted: Wed 21 Dec 2011, 06:25 Post subject:
|
|
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/viewtopic.php?p=590822#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 github repositories. I may eventually get around to updating my blogspot.
|
Back to top
|
|
 |
technosaurus

Joined: 18 May 2008 Posts: 4878 Location: Blue Springs, MO
|
Posted: Wed 01 Feb 2012, 15:50 Post subject:
|
|
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[i]).
if init is a shell script the "env[]" is already set as variables and the argv[] is $1 $2 $3 ...
_________________ Check out my github repositories. I may eventually get around to updating my blogspot.
|
Back to top
|
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|