Giter VIP home page Giter VIP logo

learn-linux's Introduction

A note on building simple linux system

This repository is an expansion version of this gist

The computer have several stage on booting process, the way to achieve or perform the operation are variant but the overall general stage are same:

  • System startup
  • BIOS and Bootloader stage
  • Kernel
  • Process init

see the wiki for more information.

The requirement

  • busybox for userspace program
  • latest stable linux Kernel
  • runnning linux operating system on the host
  • USB flash drive or QEMU

Partition layout and File system

  • 200 MB for root filesystem with ext4 format

Outline

  1. Create disk image file instead of formatting physical drive
  2. Prepare the linux system on mounted drive
  3. Test the newly created linux system on QEMU emulator
  4. Transfer the newly created linux system to USB flash drive

Create the disk image

dd if=/dev/zero of=linux.img bs=200M count=1 status=progress

Setup partition

enable boot flag and create a partition with whole disk image size.

fdisk ./linux.img

after adding partition on the disk image:

  • map the partition to loop-back device
  • format the partition
  • mount the partition
sudo losetup -v -P -f linux.img
sudo mkfs.ext4 /dev/loop<n>p<n>
sudo mkdir -v /mnt/newsystem
sudo mount -v /dev/loop<n>p<m> /mnt/newsystem

replace <n> with the proper device number, and <m> with partition number.

check if it's successfuly mounted by findmnt -T /mnt/newsystem.

later after you've done with the disk image, detach it by:

sudo umount -v /mnt/newsystem
sudo losetup -dv /dev/loop<n>

in case you want to mount the image, mount the partition by specifying the offset:

sudo mount linux.img /mnt/newsystem -o loop,offset=$((512*2048))

Prepare the linux system

We'll populate the disk image with minimum requirement for building a linux system.

install the busybox, see the gist above.

make sure the owner of /mnt/newsystem are root (though by default it's already root), because when chroot, the only user that available was root

Preparing directory for Virtual Kernel File Systems

sudo mkdir -pv /mnt/newsystem/{dev/{shm,pts},proc,sys,run,tmp}

Preparing standard directory

sudo mkdir -pv /mnt/newsystem/{etc,home,root,boot/syslinux}

Bash is really cool and badass! You can create multiple directories in all sorts of ways, even with just a single line of code.

Configure the /etc/fstab

sudo tee /mnt/newsystem/etc/fstab << "EOF"
proc           /proc          proc     nosuid,noexec,nodev 0     0
sysfs          /sys           sysfs    nosuid,noexec,nodev 0     0
tmpfs          /run           tmpfs    defaults            0     0
devtmpfs       /dev           devtmpfs mode=0755,nosuid    0     0
tmpfs          /dev/shm       tmpfs    nosuid,nodev        0     0
devpts         /dev/pts       devpts   gid=5,mode=620      0     0
EOF

add mount -a later in your init program?

Install the syslinux bootloader

extlinux -i /mnt/newsystem/boot/syslinux --device=/dev/loop<n>

write MBR Bootstrap code:

dd bs=440 count=1 conv=notrunc if=/usr/lib/syslinux/bios/mbr.bin of=linux.img

syslinux cannot be loaded without the bootstrapping code, see the MBR bootstrap code creation for further reading.

Configure syslinux

sudo tee /mnt/newsystem/boot/syslinux/extlinux.conf << "EOF"
TIMEOUT 300
ONTIMEOUT limnux

UI vesamenu.c32
MENU TITLE Boot

LABEL limnux
	MENU LABEL Limnux ayooo
	LINUX /boot/bzImage
	APPEND root=/dev/sda1 console=ttyS0 rw
EOF
sudo cp -v /usr/lib/syslinux/bios/{vesamenu.c32,libutil.c32,libcom32.c32}  /mnt/newsystem/boot/syslinux

replace the root kernel parameter with PARTUUID if you want to use it with usb flash drive

and the last thing, copy the compiled kernel:

cp -v ./bzImage /mnt/newsystem/boot

the bootloader will loop forever if it can't find the kernel location XD

Run it on QEMU

qemu-system-x86_64 \
-drive format=raw,file=./linux.img,index=0,media=disk \
-nographic -enable-kvm

if you use tigervnc, remove the -nographic and console kernel parameter, otherwise if kernel panic occurs the kernel log will not shown.

You also able to using physical device instead of disk image by -hdb <device> option

Misc

chrooted the disk image

if you want to perform chroot, we need to prepare the virtual kernel file systems so the kernel will be able to communicate with the kernel itself, see the LFS chapter 7.3 for the rest of instruction.

make a backup using disk image format

transfer device to disk image file:

dd if=<device> of=<file.img> oflag=direct conv=fsync bs=4M status=progress

to mount, use offset to specify which partition to be mounted.

you can also run the image on QEMU! if the disk img using partition, don't forget to use /dev/sda1 instead of /dev/sda for root kernel parameter

Useful command for debugging

findmnt -A
mount
losetup -a
df -h

Link and references

  • linuxfromscratch - LFS Guide
  • tldp - HOWTO create rescue disk
  • wiki.archlinux - installation guide
  • Manual Page
    • man mount
    • cat /proc/filesystems or ls /lib/modules/$(uname -r)/kernel/fs
  • wiki.archlinux - BIOS System
  • wiki.syslinux - Syslinux modules working dependencies
  • unix - Who provide the UUID in the root kernel parameter? initramfs - PARTUUID is a good alternative too!
  • stackoverflow - initramfs vs initrd
  • askubuntu - create partition on disk image
  • unix - install bootloader on unpartitioned disk
  • joe-bergeron - what is bootloader? tiny bootloader
  • superuser - auto-mount partition on loop back device

Question

is it true that transfering the kernel directly without bootloader no longer possible?

learn-linux's People

Contributors

realyukisan avatar

Stargazers

Memet Zx avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.