1、自制linux系統(tǒng)
[root@centos6 app]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 5.8G 0 rom
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 48.8G 0 part /
├─sda3 8:3 0 48.8G 0 part /app
├─sda4 8:4 0 1K 0 part
└─sda5 8:5 0 4G 0 part
sdb 8:16 0 20G 0 disk ---準(zhǔn)備一個20G 的干凈的磁盤
[root@centos6 app]#fdisk /dev/sdb ---創(chuàng)建兩個分區(qū)
[root@centos6 app]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 5.8G 0 rom
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 48.8G 0 part /
├─sda3 8:3 0 48.8G 0 part /app
├─sda4 8:4 0 1K 0 part
└─sda5 8:5 0 4G 0 part
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 1G 0 part ---存放boot
└─sdb2 8:18 0 19G 0 part -----用于存放根
[root@centos6 app]#mkfs.ext4 /dev/sdb1
[root@centos6 app]#mkfs.ext4 /dev/sdb2
[root@centos6 app]#blkid ---在兩個分區(qū)上創(chuàng)建文件系統(tǒng)
/dev/sda2: UUID="6350f737-89cd-4f10-b687-13672099dae5" TYPE="ext4"
/dev/sda1: UUID="bdfd5835-10c1-4a83-ac25-710153002d1e" TYPE="ext4"
/dev/sda3: UUID="388cdbbd-3813-412d-8748-51924f774474" TYPE="ext4"
/dev/sda5: UUID="c8b816ab-a2f2-4d18-9f91-b50794a91cef" TYPE="swap"
/dev/sdb1: UUID="fc122ac3-61a2-44bc-b599-375cb739ace5" TYPE="ext4"
/dev/sdb2: UUID="9489a316-3cce-4787-a79d-c9f7cdeab3fe" TYPE="ext4"
[root@centos6 app]#mkdir /mnt/boot ---創(chuàng)建一個臨時目錄,注意子目錄必須為boot
[root@centos6 app]#mount /dev/sdb1 /mnt/boot/ ---掛載
[root@centos6 app]#df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 50264772 1035892 46668880 3% /
/dev/sda1 999320 34668 912224 4% /boot
/dev/sda3 50264772 53208 47651564 1% /app
/dev/sdb1 1010780 1304 956464 1% /mnt/boot
[root@centos6 app]#grub-install --root-directory=/mnt /dev/sdb
---安裝grub,安裝時要指明根所在的目錄,安裝grub時boot是根
[root@centos6 app]#cp /boot/vmlinuz-2.6.32-696.el6.x86_64 /boot/initramfs-2.6.32-696.el6.x86_64.img /mnt/boot/ ---將內(nèi)核文件和inintrd文件拷貝到/mnt/boot/目錄下
[root@centos6 app]#cd /mnt/boot/
[root@centos6 boot]#ls ---文件已經(jīng)拷貝過
grub lost+found
initramfs-2.6.32-696.el6.x86_64.img vmlinuz-2.6.32-696.el6.x86_64
[root@centos6 boot]#cd grub/
[root@centos6 grub]#ls ---grub文件已經(jīng)生成
device.map iso9660_stage1_5 stage1 xfs_stage1_5
e2fs_stage1_5 jfs_stage1_5 stage2
fat_stage1_5 minix_stage1_5 ufs2_stage1_5
ffs_stage1_5 reiserfs_stage1_5 vstafs_stage1_5
[root@centos6 grub]#vim grub.conf ---編輯grub.conf文件
default=0
timeout=5
title zhanglinux
root (hd0,0)
kernel /vmlinuz-2.6.32-696.el6.x86_64 root=/dev/sda2 selinux=0 init=/bin/bash ---定義根下的一個程序是/bin/bash
initrd /initramfs-2.6.32-696.el6.x86_64.img
[root@centos6 grub]#mkdir /mnt/sysroot ---創(chuàng)建一個根的臨時目錄
[root@centos6 grub]#mount /dev/sdb2 /mnt/sysroot/ ---將根掛載到這個目錄下
[root@centos6 grub]#df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 50264772 1035916 46668856 3% /
/dev/sda1 999320 34668 912224 4% /boot
/dev/sda3 50264772 53208 47651564 1% /app
/dev/sdb1 1010780 31360 926408 4% /mnt/boot
/dev/sdb2 19460816 44992 18420600 1% /mnt/sysroot
[root@centos6 grub]#cd /mnt/sysroot/ ---進(jìn)入根目錄
[root@centos6 sysroot]#ls ---現(xiàn)在里面什么都沒有
lost+found
[root@centos6 sysroot]#cat copycmd.sh ---下一步就是運(yùn)行這個腳
本,將一些外部命令的文件和所依賴的庫文件拷貝到/mnt/sysroot/目
錄下
#!/bin/bash
#
read -p "please input a excute cmd(eg:cat|bash|quit):" command
[ -a /mnt/sysroot ]||mkdir /mnt/sysroot &>/dev/null
cpdir () {
cmd=`which --skip-alias $command`
if [ -f /mnt/sysroot$cmd ];then
echo "the $cmd has been cp before"
else
local dir=`dirname $cmd`
mkdir -p /mnt/sysroot$dir
cp $cmd /mnt/sysroot$cmd
echo "cp $cmd finished"
fi
}
cplib () {
cmd=`which --skip-alias $command`
ldd $cmd |egrep -o "/lib64.* "|while read i;do
if [ ! -f /mnt/sysroot$i ];then
local dir=`dirname $i`
mkdir -p /mnt/sysroot$dir
cp $i /mnt/sysroot$i
echo "cp $i finished"
else
echo "$i has been cp before"
fi
done
}
type $command&>/dev/null||{ echo the command is not exsit;exit 100; }
until [ $command == quit ];do
type $command&>/dev/null||{ echo the command is not exsit;exit 100; }
cpdir
cplib
read -p "please input new excute cmd(eg:cat|bash|quit):" command
cmd=`which --skip-alias $command`
done
利用以上腳本將常用的外部命令的文件和所依賴的庫文件拷貝根目錄
下,如bash,ls,ifconfig,ping,hostname,cat,mount,
umount,ps,pstree,這樣自制的linux系統(tǒng)就可以在bash里面執(zhí)行
這行命令。
為了讓自制的linux系統(tǒng)具有網(wǎng)絡(luò)功能,需要利用腳本將安裝網(wǎng)卡驅(qū)動模塊的命令的文件和所依賴的庫文件拷貝到根目錄下,這個命令是
insmod
[root@centos6 sysroot]#modinfo e1000 ---查看驅(qū)動模塊信息
filename: /lib/modules/2.6.32-696.el6.x86_64/kernel/drivers/net/e1000/e1000.ko
可以知道這個模塊來自于哪個文件
[root@centos6 sysroot]#cp /lib/modules/2.6.32-696.el6.x86_64/kernel/drivers/net/e1000/e1000.ko lib64/
復(fù)制這個模塊文件到lib64目錄下
[root@centos6 sysroot]#tree --查看一下目錄樹,看看是否所有的
文件都已經(jīng)拷貝完成
.
├── bin
│ ├── bash
│ ├── cat
│ ├── hostname
│ ├── ls
│ ├── mount
│ ├── ping
│ ├── ps
│ └── umount
├── lib64
│ ├── e1000.ko
│ ├── ld-linux-x86-64.so.2
│ ├── libacl.so.1
│ ├── libattr.so.1
│ ├── libblkid.so.1
│ ├── libcap.so.2
│ ├── libcrypt.so.1
│ ├── libc.so.6
│ ├── libdl.so.2
│ ├── libfreebl3.so
│ ├── libidn.so.11
│ ├── libm.so.6
│ ├── libnsl.so.1
│ ├── libproc-3.2.8.so
│ ├── libpthread.so.0
│ ├── libresolv.so.2
│ ├── librt.so.1
│ ├── libselinux.so.1
│ ├── libsepol.so.1
│ ├── libtinfo.so.5
│ ├── libutil.so.1
│ ├── libuuid.so.1
│ └── perl5
│ └── CORE
├── lost+found
├── sbin
│ ├── ifconfig
│ └── insmod
└── usr
└── bin
├── pstree
└── vim
8 directories, 34 files
[root@centos6 sysroot]#mkdir {etc,dec,proc,sys,boot,home,var,root,mnt,tmp,lib} ----創(chuàng)建根下對應(yīng)
的目錄
[root@centos6 sysroot]#ls
bin dec home lib64 mnt root sys usr
boot etc lib lost+found proc sbin tmp var
[root@centos6 sysroot]#chroot /mnt/sysroot/ ---切換根看一下
[I have no name!@centos6 /]#ls ---看到根下的目錄都準(zhǔn)備好了
bin dec home lib64 mnt root sys usr
boot etc lib lost+found proc sbin tmp var
[I have no name!@centos6 /]#pwd
/
[I have no name!@centos6 /]#sync
bash: sync: command not found
[I have no name!@centos6 /]#exit --退出來
exit
[root@centos6 sysroot]#sync ---同步一下磁盤,確保剛才的操作都
寫到磁盤里了
[root@centos6 sysroot]#sync
[root@centos6 sysroot]#sync
[root@centos6 sysroot]#halt ---關(guān)機(jī)后把sdb這塊硬盤拆下來
在vmware里新創(chuàng)建一個虛擬機(jī),然后將這
塊硬盤裝載到這個虛擬機(jī)下,執(zhí)行情況如
下圖

QQ截圖20170902204848.png

QQ截圖20170902204922.png

QQ截圖20170902204944.png

QQ截圖20170902205010.png

QQ截圖20170902205034.png

QQ截圖20170902205044.png
開啟此虛擬機(jī)

QQ截圖20170902212056.png
到此一個小的自制的linux系統(tǒng)就完成了,也可以在bash下輸入一些命令使用了。
2、/proc目錄和/sys目錄
- /proc目錄:
內(nèi)核把自己內(nèi)部狀態(tài)信息及統(tǒng)計信息,以及可配置參數(shù)通
過proc偽文件系統(tǒng)加以輸出
/proc/sys
sysctl命令用于查看或設(shè)定此目錄中諸多參數(shù)
[root@centos6 ~]#cat /proc/sys/net/ipv4/icmp_echo_ignore_all
---查看是否禁ping,0是沒有開啟,1是開啟禁ping
0
[root@centos6 ~]#cat /proc/sys/net/ipv4/ip_forward ---查看是否開
啟路由功能,0表示沒有開啟,1是開啟
0
[root@centos6 ~]#echo 1 > /proc/sys/net/ipv4/ip_forward ---通過
echo重定向的方法可以開啟路由功能
[root@centos6 ~]#cat /proc/sys/net/ipv4/ip_forward
1
[root@centos6 ~]#sysctl -w net.ipv4.ip_forward=0---也可以用sysctl
命令去查看或設(shè)定此/proc/sys目錄中的諸多參數(shù),格式要注意,中間
是用點(diǎn)隔開,而不是/,并且前面的/proc/sys/也不用謝
net.ipv4.ip_forward = 0
[root@centos6 ~]#cat /proc/sys/net/ipv4/ip_forward
0
以上兩種方法設(shè)置/proc/sys目錄中的諸多參數(shù)都是臨時設(shè)定,重啟就
會丟失,要想不丟失,需要寫到配置文件里
配置文件是 /etc/sysctl.conf ---可以修改配置文件的內(nèi)容來修
改/proc/sys目錄中的諸多參數(shù),如果這個參數(shù)沒有,可以在里面增加
一行就可以了
[root@centos6 ~]#sysctl -p ---讀取配置文件設(shè)置的參數(shù),也即是讓
配置文件生效
[root@centos6 ~]#sysctl -a |grep net.ipv4.ip_forward ---查看所有生
效的參數(shù),可以用grep過濾一下,只看某個修改的參數(shù)
[root@centos6 ~]#free
total used free shared buffers cached
Mem: 1004112 141764 862348 252 312 8508
-/+ buffers/cache: 132944 871168
Swap: 0 0 0
[root@centos6 ~]#echo 1 > /proc/sys/vm/drop_caches ---0表示不
釋放緩存,可以用這種方式釋放緩存,只是釋放了一部分
[root@centos6 ~]#free -h
total used free shared buffers cached
Mem: 980M 138M 841M 252K 304K 8.3M
-/+ buffers/cache: 130M 850M
Swap: 0B 0B 0B
[root@centos6 ~]#sysctl -a |grep "fs.file"
fs.file-nr = 544 0 96220
fs.file-max = 96220 ---生產(chǎn)中有的時候也會修改這個參數(shù),表示系
統(tǒng)允許同時打開的文件個數(shù),這個文件在
[root@centos6 ~]#cat /proc/sys/fs/file-max
96220
[root@centos6 ~]#cat /etc/sysctl.conf ---配置文件中的這兩行有的時
候也需要更改
# Controls the maximum shared segment size, in bytes
kernel.shmmax = 68719476736 --表示系統(tǒng)允許利用的最大內(nèi)存,如
果企業(yè)里面用的是128G的內(nèi)存,就會造成內(nèi)存空間的浪費(fèi),空有硬
件而利用不起來,所以根據(jù)服務(wù)器的硬件配置更改這個參數(shù)
# Controls the maximum number of shared memory segments, in pages
kernel.shmall = 4294967296 ---表示允許利用的最大內(nèi)存的頁數(shù),
內(nèi)存空間是分頁的,一頁是4k大小。
- /sys目錄
此目錄用于輸出內(nèi)核識別出的各種硬件設(shè)備的相關(guān)屬性信息。
?/sys目錄:
sysfs:為用戶使用的偽文件系統(tǒng),輸出內(nèi)核識別出的各硬件設(shè)備的相關(guān)屬性信息,也有內(nèi)核對硬件特性的設(shè)定信息;有些參數(shù)是可以修改的,用于調(diào)整硬件工作特性
udev通過此路徑下輸出的信息動態(tài)為各設(shè)備創(chuàng)建所需要設(shè)備文件,udev是運(yùn)行用戶空間程序
專用工具:udevadmin, hotplug
udev為設(shè)備創(chuàng)建設(shè)備文件時,會讀取其事先定義好的規(guī)則文件,一般在/etc/udev/rules.d及/usr/lib/udev/rules.d目錄下
比如更改usb設(shè)備的名稱,系統(tǒng)在usb插入的時候會自動識別出設(shè)備名,我們可以定義一個規(guī)則文件,去修改usb設(shè)備的設(shè)備名
修改文件的格式如下圖

QQ截圖20170902223410.png
[root@centos6 ~]#udevadm trigger ---讓上面的文件生效
[root@centos6 ~]#ll /dev/wangusb ---可以看到軟鏈接已經(jīng)建好了
lrwxrwxrwx. 1 root root 5 Sep 2 21:04 /dev/wangusb -> myusb
此時如果插入一個usb設(shè)備,就會被識別為設(shè)備名為myusb。
3、源碼編譯安裝內(nèi)核
- 內(nèi)核編譯
?單內(nèi)核體系設(shè)計、但充分借鑒了微內(nèi)核設(shè)計體系的優(yōu)點(diǎn),為內(nèi)核引入模塊化機(jī)制
?內(nèi)核組成部分:
kernel: 內(nèi)核核心,一般為bzImage,通常在/boot目錄下,名稱為vmlinuz-VERSION-RELEASE
kernel object: 內(nèi)核對象,一般放置于
/lib/modules/VERSION-RELEASE/
也就是說用rpm方式安裝的內(nèi)核包,生成的文件一個是內(nèi)核文件,另一部分文件存放在/lib/modules/目錄下
make menuconfig 可以配置內(nèi)核選項(xiàng),哪些功能啟用,哪些功能不啟用
[ ]: N ---表示不啟用
[M]: M ---表示啟用的功能存放在/lib/modules/目錄下
[*]: Y --表示啟用的功能寫到內(nèi)核文件vmlinuz-uname-r中
/boot/config-2.6.32-696.el6.x86_64這個文件中規(guī)定了哪些內(nèi)核選項(xiàng)功能啟用,哪些不啟用,啟用時是到內(nèi)核文件中還是保存到/lib/modules/目錄下
CONFIG_CRC_T10DIF=m ---表示啟用并存放在/lib/modules/目錄下
CONFIG_CRC_ITU_T=m
CONFIG_CRC32=y ---啟用并寫在內(nèi)核文件中
# CONFIG_CRC32_SELFTEST is not set ---表示不啟用
CONFIG_CRC7=m
CONFIG_LIBCRC32C=m
CONFIG_ZLIB_INFLATE=y
CONFIG_ZLIB_DEFLATE=m
CONFIG_LZO_COMPRESS=m
輔助文件:ramdisk
initrd
initramfs
- 內(nèi)核模塊命令
lsmod命令:
顯示由核心已經(jīng)裝載的內(nèi)核模塊,顯示的是已經(jīng)加載的模塊
顯示的內(nèi)容來自于: /proc/modules文件
?modinfo命令:
顯示模塊的詳細(xì)描述信息
modinfo[ -k kernel ] [ modulename|filename... ]
-n: 只顯示模塊文件路徑
-p: 顯示模塊參數(shù)
-a: author
-d: description
-l: license
[root@centos6 boot]#lsmod ---顯示已經(jīng)加載的模塊
[root@centos6 boot]#modinfo e1000 ---顯示模塊信息,沒有加載的
也會顯示,只要有這個模塊
[root@centos6 boot]#modinfo -n e1000 ---顯示模塊文件的路徑
/lib/modules/2.6.32-696.el6.x86_64/kernel/drivers/net/e1000/e1000.ko
modprobe ---裝載模塊
moprobe -r ---卸載模塊
insmod命令:也可以裝載模塊,不自動解決依賴模塊,假如這個模塊
依賴于別的模塊,也只會安裝這一個模塊,不會安裝它依賴的模塊,
而modprobe不同,它會解決依賴性,如果發(fā)現(xiàn)要安裝的模塊依賴于
某個模塊,它會自動解決依賴性,去把依賴的模塊和這個模塊都安裝
了
命令的是后面跟模塊文件的路徑而不是模塊的名稱
lnsmod /lib/modules/2.6.32-696.el6.x86_64/kernel/drivers/net/e1000/e1000.ko
rmmod命令:也可以卸載模塊
rmmod e1000
- 編譯安裝過程
?安裝開發(fā)包組
?下載源碼文件
?.config:準(zhǔn)備文本配置文件
?make menuconfig:配置內(nèi)核選項(xiàng)
?make [-j #]
?make modules_install:安裝模塊
?make install :安裝內(nèi)核相關(guān)文件
安裝bzImage為/boot/vmlinuz-VERSION-RELEASE
生成initramfs文件
編輯grub的配置文件
實(shí)例
[root@redhat7 app]#ls
f1 linux-4.12.10.tar.xz reset.sh scp.sh script ssh1.exp
[root@redhat7 app]#tar xf linux-4.12.10.tar.xz ---解包
[root@redhat7 app]#ls
f1 linux-4.12.10 linux-4.12.10.tar.xz reset.sh scp.sh script ssh1.exp
[root@redhat7 app]#cd linux-4.12.10/ ---進(jìn)入這個目錄
[root@redhat7 linux-4.12.10]#ls
arch crypto include kernel net sound
block Documentation init lib README tools
certs drivers ipc MAINTAINERS samples usr
COPYING firmware Kbuild Makefile scripts virt
CREDITS fs Kconfig mm security
[root@redhat7 linux-4.12.10]#cp /boot/config-3.10.0-693.el7.x86_64 .config
---準(zhǔn)備.conf文件,可以從/boot目錄中拷
貝,也就是在原有的基礎(chǔ)上修改,也可以
不準(zhǔn)備這個文件,直接執(zhí)行make
menuconfig自己配置內(nèi)核選項(xiàng)
[root@redhat7 linux-4.12.10]#ls -a
. CREDITS .gitignore .mailmap security
.. crypto include MAINTAINERS sound
arch Documentation init Makefile tools
block drivers ipc mm usr
certs firmware Kbuild net virt
.cocciconfig fs Kconfig README
.config .get_maintainer.ignore kernel samples
COPYING .gitattributes lib scripts
[root@redhat7 linux-4.12.10]#yum groupinstall "Development Tools"
---安裝開發(fā)包組
[root@redhat7 linux-4.12.10]#yum install ncurses-devel ---安裝這個包
[root@redhat7 linux-4.12.10]#make menuconfig 配置內(nèi)核選項(xiàng)
進(jìn)入如下圖界面,進(jìn)行選擇內(nèi)核選項(xiàng)

QQ截圖20170903133047.png
設(shè)置內(nèi)核編譯者自己加的版本號

QQ截圖20170903133140.png

QQ截圖20170903133347.png
返回進(jìn)入修改文件系統(tǒng),我們是想讓編譯的新內(nèi)核也支持NTFS文件系統(tǒng),目前的內(nèi)核是不支持的

QQ截圖20170903133426.png

QQ截圖20170903133828.png
將這一項(xiàng)打成M,表示支持NTFS文件系統(tǒng),并保存至/lib/modules目錄下,這里選擇的時候按y、m、n進(jìn)行選擇

QQ截圖20170903134129.png

QQ截圖20170903134202.png
退出保存即可
[root@centos6 linux-4.12.10]#vim .config
搜索一下NTFS,看這一項(xiàng)是否已經(jīng)被修改
5205 CONFIG_NTFS_FS=m --發(fā)現(xiàn)已經(jīng)被修改
5206 # CONFIG_NTFS_DEBUG is not set
5207 CONFIG_NTFS_RW=y
5208
[root@centos6 linux-4.12.10]#make -j 4 && echo -e '\a' ---表示4顆
cpu并行運(yùn)行
安裝,并且安裝完后發(fā)出聲音
[root@centos6 linux-4.12.10]#yum install openssl-devel ---安裝的
時候會提示安裝
這個包,安裝后再執(zhí)行以下上面的命令即
可
[root@centos6 linux-4.12.10]#make -j 4 && echo -e '\a' ---發(fā)現(xiàn)可以
繼續(xù)安裝了,安裝過程比較長
[root@centos6 linux-4.12.10]#make modules_install ---安裝模塊
[root@centos6 linux-4.12.10]#make install ---安裝內(nèi)核相關(guān)文件
重啟后就可以用新安裝的內(nèi)核了。
- 在已經(jīng)執(zhí)行過編譯操作的內(nèi)核源碼樹做重新編譯
?需要事先清理操作:
make clean:清理大多數(shù)編譯生成的文件,但會保留config文件等
make mrproper: 清理所有編譯生成的文件、config及某些備份文件
make distclean:mrproper、patches以及編輯器備份文件 - 卸載內(nèi)核
?刪除/lib/modules/目錄下不需要的內(nèi)核庫文件
?刪除/app/ linux-4.12.10/這個目錄
?刪除/boot目錄下啟動的內(nèi)核和內(nèi)核映像文件
?更改grub的配置文件,刪除不需要的內(nèi)核啟動列表