Monday, February 22, 2016

create your own minimal linux

I- *** create rootfs test with qemu ***
1- install busybox and prepare rootfs
$ sudo apt-get install busybox
$ mkdir test/rootfs/bin/ -p
$ cd test
$ sudo /bin/busybox --install rootfs/bin
$ cd rootfs
$ mkdir dev proc sys tmp
$ sudo mknod dev/console c 5 1
$ find . | cpio -H newc -o | gzip > ../rootfs.cpio.gz
$ cd ..
2- create init file in rootfs (rootfs/init) with content
#!/bin/ash
mount -t proc none /proc
mount -t sysfs none /sys
/bin/ash


3- change init file permission
$ chmod 775 rootfs/init

4- boot our footfs with qume and we use our kernel from /boot directory, but you can also compile the kernel from source, if you want.

$ sudo qemu-system-x86_64 -kernel /boot/vmlinuz-3.13.0-74-generic -initrd rootfs.cpio.gz 
qemu busybox

***finish ***


Now if you want to install it to disk and boot

II-  *** install to disk ***
follow the above instruction then
1- create sda sda1 for our disk
$ sudo mknod rootfs/dev/sda  b 8 0
$ sudo mknod rootfs//dev/sda1 b 8 1


2- create raw disk and prepare it
$ qqemu-img create -f raw -o size=4G test.raw
$ fdisk test.raw  (create one primary partition )
$ sudo losetup -f --show test.raw 
$ sudo  kpartx -a /dev/loop0
$ sudo mkfs.ext4 /dev/mapper/loop0p1

3- install grub to our disk
$ sudo mount /dev/mapper/loop0p1 /mnt
$ sudo grub-install --target=i386-pc --boot-directory=/mnt/grub /dev/loop0

4- copy all necessary file
$ sudo cp -r rootfs/* /mnt
$ sudo cp /boot/vmlinuz-3.13.0-74-generic /mnt/vmlinuz
$ sudo cp rootfs.cpio.gz  /mnt/

5- create grub menu in /mnt/grub/grub.cfg
 menuentry "mylinux" {
    set root=(hd0,1)
    linux /vmlinuz init=/bin/init
    initrd /rootfs.cpio.gz
 }


6- boot our disk with qemu
$ qemu-system-x86_64 -hda test.raw


No comments:

Post a Comment