定制属于自己的Linux操作系统

1、新建一个硬盘
2、在该新硬盘上新建两个分区,一个当boot分区,一个当/分区
3、格式化并且挂载两个分区
4、安装grub至目标磁盘
5、为grub提供配置文件
6、复制内核文件和initrd文件
7、创建目标主机根文件系统
8、移植bash命令和其库文件到根文件系统
9、装载模块,实现网络功能
10、启动测试

特别提醒
如果在vmvare上做此实验,在新建虚拟机创建新磁盘的时候,一定要选“Store virtual disk as a single file”,否则,也会出现内核恐慌kennel panic。

定制属于自己的Linux操作系统

步骤演示详解过程:

1、新建一个硬盘

2、在该新硬盘上新建两个分区,一个当boot分区,一个当/分区
首先,我们要在目标磁盘上分两个区,并进行格式化。第一个分区100M,用来装引导程序;第二个分区19G,用来装根文件系统。然后再进行挂载操作,将/dev/sdb1挂载到/mnt/boot下,将/dev/sdb2挂载到/mnt/root下。

sdb                  8:16  0    20G  0 disk
├─sdb1                8:17  0 109.8M  0 part
└─sdb2                8:18  0  19.9G  0 part
[root@localhost ~]# fdisk -l /dev/sdb

Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xdd8058c2

Device Boot      Start        End      Blocks  Id  System
/dev/sdb1              1          14      112423+  83  Linux
/dev/sdb2              15        2610    20852370  83  Linux

3、格式化并且挂载两个分区
注意,其中boot分区的挂载目录一定是boot名字的目录

[root@localhost ~]# mkfs.ext4 /dev/sdb1
[root@localhost ~]# mkfs.ext4 /dev/sdb2
[root@localhost ~]# mkdir -p /mnt/{root,boot}
[root@localhost ~]# ll /mnt/
total 8
drwxr-xr-x 2 root root 4096 Jul 25 08:52 boot
drwxr-xr-x 2 root root 4096 Jul 25 08:52 root
[root@localhost ~]# mount /dev/sdb1 /mnt/boot/
[root@localhost ~]# mount /dev/sdb2 /mnt/root/
[root@localhost ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sdb1            103M  1.6M  96M  2% /mnt/boot
/dev/sdb2              20G  44M  19G  1% /mnt/root

4、安装grub至目标磁盘
一个系统能启动,就需要引导,所以我们首先要安装一个grub引导程序到我们的新磁盘上,安装grub引导程序主要有两个命令,一个是grub-install,另一个是setup,这里最好使用grub-install来安装。因为:

①grub-install会安装grub引导第二阶段的文件
②setup不会安装第二阶段的引导程序,是安装引导信息到MBR
//第二个需要注意的地方就是--root-directory=后面接的路径应该是boot目录所在的地方,而不是/mnt/boot,因为boot目录在mnt下;目标磁盘是/dev/sdb
[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/device.map.
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
[root@localhost ~]# cd /mnt/boot/
[root@localhost boot]# ll
total 13
drwxr-xr-x 2 root root  1024 Jul 25 08:58 grub
drwx------ 2 root root 12288 Jul 25 08:51 lost+found
安装完grub后,进入grub目录,会发现没有grub.conf配置文件,这样就导致我们的引导程序是不健全的,所以我们需要手动写一个配置文件在里边。

5、为grub提供配置文件
上面移植了内核和initrd文件,我们就可以根据内核版本和initrd版本来编写grub.conf配置文件了:

default=0
timeout=5
title CentOS to ZhangHe Soft-Linux
root (hd0,0)
kernel /vmlinz-soft ro root=/dev/sda2 quiet selinux=0 init=/bin/bash
initrd /initramfs-soft.img
quiet是静默安装,不再显示安装时的一大堆信息。后面要把selinux关掉,而且init要使用/bin/bash,告诉内核不要再去找init程序了。如果不指定这一步,在启动过程中就会报kernel panic(内核恐慌),以为系统就它一个了,没有init进程,恐慌的不行。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/14850.html