实战自制Linux操作系统
自制linux系统
步骤概述:
1、新建一个硬盘2、在该新硬盘上新建两个分区,一个当boot分区,一个当/分区3、格式化并且挂载两个分区4、安装grub至目标磁盘5、为grub提供配置文件6、复制内核文件和initrd文件7、创建目标主机根文件系统8、移植bash命令和其库文件到根文件系统9、装载模块,实现网络功能10、启动测试
具体操作步骤如下:
1)为CentOS 6系统添加一块新的硬盘,然后启动,演示如下:
2)查看新添加的硬盘,并为添加好的磁盘分区,创建文件系统;
[[email protected] ~]# fdisk -l /dev/sd[a-z] Disk /dev/sda: 85.9 GB, 85899345920 bytes 255 heads, 63 sectors/track, 10443 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: 0x0007eba7 Device Boot Start End Blocks Id System /dev/sda1 * 1 26 204800 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 26 7859 62914560 8e Linux LVM /dev/sda3 7860 9165 10490445 83 Linux /dev/sda4 9166 10443 10265535 5 Extended /dev/sda5 9166 9819 5253223+ 83 Linux Disk /dev/sdb: 21.5 GB, 21474836480 bytes #可以看到新添加的硬盘 sdb 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: 0x00000000
分三个主分区,分别作为为根分区,swap分区和boot分区,并创建对应的文件系统,操作如下:
[[email protected] ~]# fdisk /dev/sdb #创建分区, #创建文件系统如下命令: [[email protected] ~]# mkfs.ext4 /dev/sdb1 [[email protected] ~]# mkswap /dev/sdb2 [[email protected] ~]# mkfs.ext4 /dev/sdb3 #查看创建好的分区 [[email protected] ~]# blkid /dev/sdb1: UUID="f4921d76-f089-4cb6-8f47-aff34711cb7a" TYPE="ext4" /dev/sdb2: UUID="3eaa649e-a5c0-4f9c-8887-453368419238" TYPE="swap" /dev/sdb3: UUID="286c857d-8f52-465f-9ed6-8eeaa3363743" TYPE="ext4"
3)创建一个boot目录,并把boot分区(sdb1)挂载上;
[[email protected] ~]# mkdir /mnt/boot [[email protected] ~]# mount /dev/sdb1 /mnt/boot [[email protected] ~]# df Filesystem 1K-blocks Used Available Use% Mounted on /dev/mapper/vg0-root 20511356 1112808 18349972 6% / tmpfs 502068 0 502068 0% /dev/shm /dev/sda1 194241 34199 149802 19% /boot /dev/mapper/vg0-usr 10190136 2473060 7192788 26% /usr /dev/mapper/vg0-var 20511356 489148 18973632 3% /var /dev/sda5 5039592 10352 4766580 1% /home /dev/sdb1 104769 1550 97598 2% /mnt/boot # 挂载的设备sdb1
4)创建grub,并查看;
[[email protected] ~]# ls /mnt/boot/ lost+found [[email protected] ~]# grub-install --root-directory=/mnt /dev/sdb #创建grub 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 [[email protected] ~]# ls /mnt/boot/ grub lost+found [[email protected]entOS6 ~]# ls /mnt/boot/grub/ device.map fat_stage1_5 iso9660_stage1_5 minix_stage1_5 stage1 ufs2_stage1_5 xfs_stage1_5 e2fs_stage1_5 ffs_stage1_5 jfs_stage1_5 reiserfs_stage1_5 stage2 vstafs_stage1_5
5)要想成为一个真正的系统,还需要内核文件,initrd以及grub/grub.conf这些文件;
[[email protected] ~]# cp /boot/vmlinuz-2.6.32-642.el6.x86_64 /mnt/boot/vmlinuz [[email protected] ~]# cp /boot/initramfs-2.6.32-642.el6.x86_64.img /mnt/boot/initramfs.img [[email protected] ~]# vim /mnt/boot/grub/grub.conf #手动编写配置文件 default=0 timeout=5 title CentOS (Express) root (hd0,0) # 在当前下为第二块磁盘应为(hd1,0),但是如果自其它设备上使用为(hd0,0) kernel /vmlinuz ro root=/dev/sda3 #加载的根目录所在的分区,因为在其他设备上为第一块磁盘所以为 /dev/sda3 initrd /initramfs.img #内核匹配的ramfs文件
6)要把/dev/sda3当做根文件系统,就需要挂载,并创建根文件系统下想对应的子目录:
[[email protected] ~]# mkdir /mnt/sysroot [[email protected] ~]# mount /dev/sdb3 /mnt/sysroot/ [[email protected] ~]# cd /mnt/sysroot/ [[email protected] sysroot]# ls lost+found [[email protected] sysroot]# mkdir -pv etc bin sbin lib lib64 dev proc sys mnt var usr home root tmp media mkdir: created directory `etc‘ mkdir: created directory `bin‘ mkdir: created directory `sbin‘ mkdir: created directory `lib‘ mkdir: created directory `lib64‘ mkdir: created directory `dev‘ mkdir: created directory `proc‘ mkdir: created directory `sys‘ mkdir: created directory `mnt‘ mkdir: created directory `var‘ mkdir: created directory `usr‘ mkdir: created directory `home‘ mkdir: created directory `root‘ mkdir: created directory `tmp‘ mkdir: created directory `media‘ [[email protected] sysroot]# ls bin dev etc home lib lib64 lost+found media mnt proc root sbin sys tmp usr var
7)根文件系统的目录有了,但是要想运行还需要程序,还需要bash,以及基于动态编译的共享库文件
[[email protected] sysroot]# cp /bin/bash /mnt/sysroot/bin #复制bash [[email protected] sysroot]# ldd /bin/bash # 查看库文件 linux-vdso.so.1 => (0x00007ffd127d9000) libtinfo.so.5 => /lib64/libtinfo.so.5 (0x0000003278e00000) libdl.so.2 => /lib64/libdl.so.2 (0x000000326f200000) libc.so.6 => /lib64/libc.so.6 (0x000000326f600000) /lib64/ld-linux-x86-64.so.2 (0x000000326ee00000) # 复制共享库中的文件到创建的根目录中的lib64下 [[email protected] sysroot]# cp /lib64/libtinfo.so.5 /mnt/sysroot/lib64 [[email protected] sysroot]# cp /lib64/libdl.so.2 /mnt/sysroot/lib64 [[email protected] sysroot]# cp /lib64/libc.so.6 /mnt/sysroot/lib64 [[email protected]tOS6 sysroot]# cp /lib64/ld-linux-x86-64.so.2 /mnt/sysroot/lib64
8)如上,基本的操作已经完成,使用 chroot 切换根,进行测试,如下:
[[email protected] sysroot]# chroot /mnt/sysroot/ # 切换根 bash-4.1# bash-4.1# bash-4.1# ls # 因为没有复制命令,所以只能使用内建命令,如果需要使用外部命令,就把命令以及以依赖的库复制过去就可以了。 bash: ls: command not found bash-4.1# pwd / bash-4.1# exit exit
9)最后,我们在编辑一下配置文件/mnt/boot/grub/grub.conf
[[email protected] ~]# vim /mnt/boot/grub/grub.conf default=0 timeout=5 title CentOS (Express) root (hd0,0) kernel /vmlinuz ro root=/dev/sda3 init=/bin/bash # 表示要求限定运行的的是/bin/bash 而不是 /sbin/init 这样局直接把bash当做用户空间的第一个进程来使用了 initrd /initramfs.img
10)最后同步一下
[[email protected] ~]# sync
11)我们新建一个虚拟机,把硬盘改为新创建好的磁盘,测试系统能否启动?
启动此虚拟机,如下图:按e键可以看到我们创建的系统文件,选定内核,按b键启动
选定内核,按b键启动,发现系统报错,什么原因呢?可能是没有添加selinux策略
选定内核,按e键,进入编辑模式,添加selinux=0 ,重新启动,发现可以正常启动
到此,一个迷你版的Linux诞生了,可以装在自己移动U盘等设备上面,这里不做过多解释
时间: 2024-10-17 03:45:06