Linux以其開源思想和啟動速度快為廣大技術人員所喜愛,本文主要講述通過自己對內核的包裝以及對默認程式的設定,來實作自己定制一個自己需要的os系統,并能夠實作開機自動加載網卡,并為網卡配置ip地址,本文不涉及內核的編譯,內核編譯內容將在后續推出,敬請大家期待!
本文是通過宿主機——>目標機的形式來實作。
1、為虛擬機添加一個20G的硬碟,并將磁盤設定為單個檔案系統,并命名為smallcentos.vmdk
查看宿主機現在的硬碟資訊
[root@localhost ~]# fdisk -l /dev/sd[a-z]
Disk /dev/sda: 128.8GB, 128849018880bytes
255heads, 63sectors/track, 15665cylinders
Units = cylinders of 16065* 512= 8225280bytes
Sector size (logical/physical): 512bytes / 512bytes
I/O size (minimum/optimal): 512bytes / 512bytes
Disk identifier: 0x0001c38d
Device Boot Start End Blocks Id System
/dev/sda1 * 12620480083Linux
Partition 1does not end on cylinder boundary.
/dev/sda2 267859629145608e Linux LVM
Disk /dev/sdb: 21.5GB, 21474836480bytes
255heads, 63sectors/track, 2610cylinders
Units = cylinders of 16065* 512= 8225280bytes
Sector size (logical/physical): 512bytes / 512bytes
I/O size (minimum/optimal): 512bytes / 512bytes
Disk identifier: 0x00000000
新添加的硬碟被識別為sdb,目標主機將通過加載sdb來啟動
2、在sdb創建兩個基本100M、512M磁區,并將檔案系統格式化成ext4格式
1
[root@localhost ~]# echo -e "n\np\n1\n\n+100M\nn\np\n2\n\n+512M\nw"|fdisk /dev/sdb
格式化新建的磁區
1
2
[root@localhost ~]# mke2fs -t ext4 /dev/sdb1
[root@localhost ~]# mke2fs -t ext4 /dev/sdb2
3.將新創建的磁區分別掛載至/mnt/boot目錄和/mnt/sysroot
1
2
3
[root@localhost mnt]# mount
/dev/sdb1 on /mnt/boot type ext4 (rw)
/dev/sdb2 on /mnt/sysroot type ext4 (rw)
4.安裝grub至指定的磁區
[root@localhost mnt]# 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 isthe contents of the device map /mnt/boot/grub/device.map.
Check ifthisiscorrect or not. If any of the lines isincorrect,
fix it and re-run the script `grub-install'.
(fd0) /dev/fd0
(hd0) /dev/sda
(hd1) /dev/sdb
5.復制/boot目錄中的grub和initrd檔案至/mnt/boot目錄中(將啟動檔案復制到定制系統中)
1
2
[root@localhost grub]# cp /boot/vmlinuz-2.6.32-358.el6.x86_64 /mnt/boot/wangfengvmlinz
[root@localhost grub]# cp /boot/initramfs-2.6.32-358.el6.x86_64.img /mnt/boot/wangfenginitramfs.img
6.創建Linux需要的一些基本檔案(在定制系統上需要的)
1
[root@localhost grub]# mkdir -pv /mnt/sysroot/{etc/rc.d,usr,var,proc,sys,dev,lib,lib64,bin,sbin,boot,src,mnt,media,home,root}
7.在宿主機上移植一個可執行的二進制檔案和庫到目標機的硬碟上,如ls,cat,mkdir,mount,reboot,useradd,passwd,ifconfig,ip,ping等,
此處不再累贅,后面將會附上腳本實作方式
為了防止內核恐慌,需要為bash創建一個軟鏈接sh
8.在目標機的/boot/grub目錄中創建grub.conf,已實作開機自檢,內容如下
default=0
timeout=10
hiddenmenu
title wangfengLinux
root(hd0,0)
kernel /wangfengvmlinuz ro root=/dev/sda2 selinux=0 init=/sbin/init
initrd /wangfenginitramfs.img
9.為了能夠實作開機啟動網卡,需要將宿主機上的網卡組態檔復制到目標機上,可以通過lsmod查看當前系統的所有模塊,可以通過modinfo 模塊名稱來查看模塊的詳細資訊
1
2
[root@localhost ~]# lsmod |grep e1000 -->查看網卡的資訊
e1000 1706460
[root@localhost ~]# modinfo e1000
filename: /lib/modules/2.6.32-358.el6.x86_64/kernel/drivers/net/e1000/e1000.ko -->此處為網卡模塊的所在位置
version: 7.3.21-k8-NAPI
license: GPL
description: Intel(R) PRO/ 1000Network Driver
author: Intel Corporation, <[email protected]>
srcversion: 1D4F1E82BB99EA36D320B1B
alias: pci:v00008086d00002E6Esv*sd*bc*sc*i*
alias: pci:v00008086d000010B5sv*sd*bc*sc*i*
alias: pci:v00008086d00001099sv*sd*bc*sc*i*
alias: pci:v00008086d0000108Asv*sd*bc*sc*i*
alias: pci:v00008086d0000107Csv*sd*bc*sc*i*
alias: pci:v00008086d0000107Bsv*sd*bc*sc*i*
alias: pci:v00008086d0000107Asv*sd*bc*sc*i*
alias: pci:v00008086d00001079sv*sd*bc*sc*i*
alias: pci:v00008086d00001078sv*sd*bc*sc*i*
1
2
[root@localhost ~]# mkdir -p /mnt/sysroot/lib/modules
[root@localhost ~]# cp /lib/modules/2.6.32-358.el6.x86_64/kernel/drivers/net/e1000/e1000.ko /mnt/sysroot/lib/modules/e1000.ko
10.為了使系統能夠開機自動掛載一些檔案系統和初始化一些服務,需要在目標機上的/sbin/目錄下創建init檔案已實作需求,內容如下
#!/bin/bash
echo -e "Welcome to \033[32m Wangfeng\033[0m Linux"
mount -n -t proc /proc proc
mount -n -t sysfs sysfs /sys
insmod /lib/modules/e1000.ko
ifconfig lo 127.0.0.1/8
ifconfig eth0 192.168.1.200/24
route add -net 0.0.0.0 gw 192.168.1.253
/bin/bash
轉自https://blog.csdn.net/weixin_33747129/article/details/92648501?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-4.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-4.control
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/235727.html
標籤:專題技術討論區
上一篇:我已經在Ubuntu的docker上部署好了Nextcloud,用ip可以進入Nextcloud上。但是我想讓外網也可以登錄網盤,這個應該怎么實作呀?
