Make a minimal entOS6 system

First, clarify the process of starting CentOS6

POST -> BootSequence (BIOS) -> Bootloader (MBR) -> Kernel (ramdisk) -> rootfs -> switchroot ->/sbin/init -> (/etc/inittab, /etc/init/*.conf) -> Set default run level -> System Initialization Script -> Shut down or start services under corresponding level -> Start Terminal

1. POST does not care about hardware

2. BootSequence, manually select a hard disk to boot

3. Bootloader, stage 1.0, is generated using grub-install

Stage 1.5 is also generated with grub-install

4. Stage 2.0, where the vmlinuz and ramdisk images of the kernel files are copied from the source system

Then you need a grub file, so CentOS6 can write it manually in the following format:

default=#: Set the default Startup menu item; title number starts from 0
timeout=#: Specify how long the menu will wait for options to be selected
splashimage=(hd#,#)/PATH/TO/XPM_PIC_FILE: Specify menu background picture
hiddenmenu: Hide Menu
title TITLE: Define Menu Title
    root(hd#,#) grub looks for the device partition where stage 2 and kernel files are located: root of grub
    kernel /PATH/TO/VMLINUZ_FILE:Started Kernel
    initrd /PATH/TO/INITRAMFS_FILE:Kernel Matched ramfs file
    password [--md5] STRING:Menu Edit Authentication

5. Copy several commands, do not start/sbin/init on, start/bin/bash directly

Script: copycmd-copy command and its dependent Libraries


1. CentOS6 Virtual Machine plus a Hard Disk

2. Partition hangs under temporary directory

Swap partition can not be done. I thought swap partition was missing at first, but later found that because of SELinux problem, which line of kernel s in grub.conf should add selinux=0

[root@localhost ~]# for i in `seq 0 2`;do echo - - - >/sys/class/scsi_host/host$i/scan;done
[root@localhost ~]# lsblk /dev/sdb -f
#One boot partition, one root partition
[root@localhost ~]# fdisk /dev/sdb
Command (m for help): n
Partition number (1-4): 1
First cylinder (1-130, default 1):
Last cylinder, +cylinders or +size{K,M,G} (1-130, default 130): +300M
Command (m for help): n
Partition number (1-4):
Partition number (1-4): 2
Last cylinder, +cylinders or +size{K,M,G} (40-130, default 130): +100M
Command (m for help): t
Partition number (1-4): 2
Hex code (type L to list codes): 82
Command (m for help): n
Partition number (1-4): 3
Last cylinder, +cylinders or +size{K,M,G} (54-130, default 130):
Command (m for help): w
The partition table has been altered!
#Create File System
[root@localhost ~]# mkfs.ext4 /dev/sdb1
[root@localhost ~]# mkswap /dev/sdb2
[root@localhost ~]# mkfs.ext4 /dev/sdb3
[root@localhost ~]# lsblk -f /dev/sdb
NAME   FSTYPE LABEL UUID                                 MOUNTPOINT
├─sdb1 ext4         54632ecc-7b84-400e-9c44-bc01edf500b5 
├─sdb2 swap         6ca8ef8e-3753-447e-ba89-569b6258ea25 
└─sdb3 ext4         a80dd4f2-d9f6-4eb0-909a-54d1ac06fd6b 
#Hang up temporarily
[root@localhost ~]# mkdir /mnt/{boot,sysroot}
[root@localhost ~]# mount /dev/sdb1 /mnt/boot/
[root@localhost ~]# mount /dev/sdb3 /mnt/sysroot/

3. Generate grubstage 1.0-stage 2.0 file

[root@localhost ~]# grub-install --root-directory=/mnt /dev/sdb
Probing devices to guess BIOS drives. This may take a long time.
Installation finished. No error reported.
This is the contents of the device map /mnt/boot/grub/
Check if this is correct or not. If any of the lines is incorrect,
fix it and re-run the script `grub-install'.

(fd0)   /dev/fd0
(hd0)   /dev/sda
(hd1)   /dev/sdb

4. Copy vmlinuz and ramfs images of kernel files

[root@localhost ~]# cp /boot/vmlinuz-2.6.32-754.el6.x86_64 /mnt/boot/vmlinuz
[root@localhost ~]# cp /boot/initramfs-2.6.32-754.el6.x86_64.img /mnt/boot/initramfs.img

Handwritten grub.conf, follow / boot/grub/grub.conf

Note: selinux=0 does not enable selinux, init=/bin/bash means boot use/bin/bash, not use/sbin/init

[root@localhost ~]# cat /mnt/boot/grub/grub.conf 
title CentOS6 test (hehehaha)
kernel /vmlinuz ro root=/dev/sda3 selinux=0 init=/bin/bash
initrd /initramfs.img

5. Create directories and copy commands

Script: copycmd-copy command and its dependent Libraries

[root@localhost sysroot]# mkdir home tmp root usr lib lib64 proc etc mnt bin sbin dev opt var sys

Cut the roots and try:

[root@localhost ~]# chroot /mnt/sysroot/
bash-4.1# ls
bin  etc   lib    mnt  proc  sbin  tmp  var
dev  home  lib64  opt  root  sys   usr
bash-4.1# pwd
bash-4.1# cd root
bash-4.1# pwd

Incidentally copy a network card driver

[root@localhost ~]# ethtool -i eth0
driver: e1000
version: 7.3.21-k8-NAPI
bus-info: 0000:02:01.0
supports-statistics: yes
supports-test: yes
supports-eeprom-access: yes
supports-register-dump: yes
supports-priv-flags: no
[root@localhost ~]# find / -name e1000.ko
[root@localhost ~]# cp /lib/modules/2.6.32-754.el6.x86_64/kernel/drivers/net/e1000/e1000.ko /mnt/sysroot/lib/

6. Shut down and use this disk to start a new virtual machine

Customize Create Virtual Machine

Magical grub interface:

Once in, load the network card, match the IP, ping other virtual machines

LFS: Build the smallest Linux document:

Tags: Linux SELinux network github

Posted on Sat, 31 Aug 2019 00:15:40 -0700 by Bozebo