菜鳥學Linux 第034篇筆記 vmlinuz, initrd, modules, script
內核兩部分
核心 /boot/vmlinuz-version
內核模塊 /lib/modules/version
內核設計風格
單內核
模塊化設計
微內核
裝載模塊命令
insmod
modprobe
查看Linux內核最新版本
www.kernel.org
查詢所安裝的發行版linux的內核版本 uname -r
用戶空間訪問、監控內核的方式
/proc, /sys 僞文件系統
/proc/sys 此目錄中的文件很多是可讀寫的
/sys/ 某些文件可寫
設定內核參數值的方法
會立即生效,但無法永久有效
echo value > /proc/sys/TO/somefile
e.g. echo 1 > /proc/sys/vm/drop-caches
sysctl -w kernel.hostname=msky
configure kernel parameters at runtime
-p Load in sysctl settings from the file specified or
/etc/sysctl.conf if none given. Specifying - as filename
means reading data from standard input.
-a Display all values currently available.
永久有效,但無法立即生效
修改/etc/sysctl.conf 使用sysctl -p 來讓內核重讀該配置文件
修改是否通話雙網卡流量傳送 net.ipv4.ip_forward = 0
0 is disabled, 1 is enabled.
內核模塊管理
lsmod program to show the status of modules in the Linux Kernel
modinfo program to show information about a Linux Kernel module
modprobe program to add and remove modules from the Linux Kernel
modprobe MOD_NAME 裝載模塊(不需要指路徑)
modprobe -r MOD_NAME 卸載模塊
insmod /PATH/TO/MOD_NAME 需要指路徑
rmmod MOD_NAME
depmod program to generate modules.dep and map files.
depmod /PATH/TO/MODULES_DIR
內核中的功能 除了核心功能之外,在編譯時,大多功能都有三種選擇
1、不使用此功能
2、編譯成內核模塊
3、編譯進內核
如何手動編譯內核
make gconfig 需要GNOME Desktop Environment
圖形開發庫 GNOME Software Development
make kconfig 需要KDE (K Desktop Environment)
圖形開發庫 KDE Software Development
make menuconfig
下載Linux內核後解壓
1. tar xf linux-4.8.11.tar -C /usr/src/
2. ln -sv linux-4.8.11 linux
3. make menuconfig 選擇特性注意遠程連接的需要顯示的窗口夠大纔可以顯示
如不知道要刪除或配置某個模塊可以導入先前的配置文件
即/boot/config-2.6.18-308.el5 將該文件mv到編譯的文件夾下改爲.config
4. make 注意可能會需要非常長的時間0.5-2小時不等,不建議遠程操作
因爲時間長可能連接中斷,會導致編譯中斷
如果非要遠程,建議安裝軟件screen yum install screen
編譯執行前使用screen
然後執行make
screen命令
不加任何參數建立screen
-ls 顯示已經建立的屏幕
Ctrl+a 放開 按d 拆除屏幕
-r ID 還原回某屏幕
6. make modules_install
7. make install
二次編譯時清理 清理之前如果有需要請備份.config文件
make clean
make mrproper
grub-->kernel-->initrd-->ROOTFS(/sbin/init, /bin/bash)
克隆迷你小linux (目錄未測試成功,老報錯,目前正在研究中。。。)
建立硬盤分區sdc1 sdc2
創建兩個目錄/mnt/boot /mnt/sysroot
將sdb1做成grub分區, 前章節有介紹,這裏不作詳細介紹
拷貝內核到/mnt/boot目錄下
cp /boot/vmlinuz-2.6.18-308.el5 /mnt/boot/vmlinuz-2.6.18
做initrd
cp /boot/initrd-2.6.18-308.el5.img /root/
file initrd-2.6.18-308.el5.img
mv initrd-2.6.18-308.el5.img initrd-2.6.18-308.el5.img.gz
mkdir initrd
cd initrd
cpio -id < ../initrd-2.6.18-308.el5.img
此上命令可以用zcat /boot/initrd-2.6.18-308.el5.img | cpio -id
vim init
切換到最後一行找到
mkrootdev -t ext3 -o defaults,ro /dev/sda1 將sda1 更改爲sdc2
打包成init放到/mnt/boot目錄裏 只可以解壓的那個目錄裏執行該命令並
find . | cpio -H newc --quiet -o | gzip -9 > /mnt/boot/initrd.gz
最後創建grub.conf上一章也講過了,這裏不再重複
掛載sdc2到 /mnt/sysroot目錄下創建基本的linux 根目錄
/bin /boot /dev /etc /home /lib /proc /sys /root /sbin /tmp
/usr /var/log
cp /sbin/init /mnt/sysroot/sbin/
cp /bin/bash /mnt/sysroot/bin/
將以上兩個文件所依賴的庫文件拷貝到lib目錄下
ldd /sbin/init
cp /lib/libsepol.so.1 /mnt/sysroot/lib/
cp /lib/libselinux.so.1 /mnt/sysroot/lib/
cp /lib/libc.so.6 /mnt/sysroot/lib/
cp /lib/libdl.so.2 /mnt/sysroot/lib/
cp /lib/ld-linux.so.2 /mnt/sysroot/lib/
ld /bin/bash
cp /lib/libtermcap.so.2 /mnt/sysroot/
在/mnt/sysroot/etc/創建inittab文件添加如下兩行
id:3:initdefault:
si::sysinit:/etc/rc.d/rc.sysinit
在/mnt/sysroot/etc/rc.d/創建rc.sysinit文件添加如下兩行
#!/bin/bash
#
echo -e "\tWelcome to \033[31mMySky lab\033[0m Linux."
/bin/bash
編寫腳本自動拷貝某命令的庫到某個目錄下
學習兩個bash 參數擴展
FILE=/usr/local/src
${parameter#word} echo ${FILE#*/} usr/local/src
${parameter##word} echo ${FILE##*/} src
${parameter%word} echo ${FILE%/*} /usr/local
${parameter%%word} echo ${FILE%%/*} 空
#!/bin/bash
#This scrip is used to copy lib to other directory.
DESTDIR=/mnt/sysroot
cplib() {
LIBPATH=${1%/*}
[ ! -d $DESTDIR$LIBPATH ] && mkdir -p $DESTDIR$LIBPATH
[ ! -e $DESTDIR${1} ] && cp $1 $DESTDIR$LIBPATH && echo "copy $DESTDIR${1} done"
}
cpbin() {
CMDPATH=${1%/*}
[ ! -d $DESTDIR$CMDPATH ] && mkdir -p $DESTDIR$CMDPATH
[ ! -e $DESTDIR${1} ] && cp $1 $DESTDIR$CMDPATH && echo "copy $DESTDIR${1} done"
for LIB in `ldd $1 | grep -o "/.*lib\(64\)\{0,1\}/[^[:space:]]\{1,\}"`; do
cplib $LIB
done
}
read -p "Input Command:" CMD
until [ $CMD == "q" ]; do
! which $CMD &> /dev/null && echo "Wrong command" && read -p "input again:" CMD && continue
COMMAND=`which $CMD | grep -v "alias" | grep -o "[^[:space:]]\{1,\}"`
cpbin $COMMAND
echo "copy $COMMAND finished."
read -p "Continue command:" CMD
done