您的位置:首页 > 运维架构 > Linux

linux-内核编译、安装grub、安装initrd及内核,screen,组装Linux

2017-03-15 15:18 711 查看
用户空间用于访问、监控内核的目录
/proc, /sys
通过修改/proc,/sys中的文件控制内核参数(伪文件系统)

/proc/sys: 此目录中的文件很多是可读写的,可修改其中内容(重定向的方式修改)
/sys/: 某些文件可写

[root@localhost ~]# ll /proc/sys
total 0
dr-xr-xr-x 0 root root 0 Mar 4 22:48 abi
dr-xr-xr-x 0 root root 0 Mar 4 22:47 crypto
dr-xr-xr-x 0 root root 0 Mar 4 22:48 debug
dr-xr-xr-x 0 root root 0 Mar 4 22:48 dev
dr-xr-xr-x 0 root root 0 Mar 4 22:47 fs
dr-xr-xr-x 0 root root 0 Mar 4 22:47 kernel
dr-xr-xr-x 0 root root 0 Mar 4 22:47 net
dr-xr-xr-x 0 root root 0 Mar 4 22:48 vm
[root@localhost ~]# ll /proc/sys/vm/
total 0
-rw-r--r-- 1 root root 0 Mar 4 22:48 block_dump
--w------- 1 root root 0 Mar 4 22:48 compact_memory
-rw-r--r-- 1 root root 0 Mar 4 22:48 dirty_background_bytes
-rw-r--r-- 1 root root 0 Mar 4 22:48 dirty_background_ratio
-rw-r--r-- 1 root root 0 Mar 4 22:48 dirty_bytes
-rw-r--r-- 1 root root 0 Mar 4 22:48 dirty_expire_centisecs
-rw-r--r-- 1 root root 0 Mar 4 22:48 dirty_ratio
-rw-r--r-- 1 root root 0 Mar 4 22:48 dirty_writeback_centisecs
-rw-r--r-- 1 root root 0 Mar 4 22:48 drop_caches
-rw-r--r-- 1 root root 0 Mar 4 22:48 extfrag_threshold
-rw-r--r-- 1 root root 0 Mar 4 22:48 extra_free_kbytes
-rw-r--r-- 1 root root 0 Mar 4 22:48 hugepages_treat_as_movable
-rw-r--r-- 1 root root 0 Mar 4 22:48 hugetlb_shm_group
-rw-r--r-- 1 root root 0 Mar 4 22:48 laptop_mode
-rw-r--r-- 1 root root 0 Mar 4 22:48 legacy_va_layout
-rw-r--r-- 1 root root 0 Mar 4 22:48 lowmem_reserve_ratio
-rw-r--r-- 1 root root 0 Mar 4 22:48 max_map_count
-rw-r--r-- 1 root root 0 Mar 4 22:48 meminfo_legacy_layout
-rw-r--r-- 1 root root 0 Mar 4 22:48 memory_failure_early_kill
-rw-r--r-- 1 root root 0 Mar 4 22:48 memory_failure_recovery
-rw-r--r-- 1 root root 0 Mar 4 22:48 min_free_kbytes
-rw-r--r-- 1 root root 0 Mar 4 22:48 min_slab_ratio
-rw-r--r-- 1 root root 0 Mar 4 22:48 min_unmapped_ratio
-rw-r--r-- 1 root root 0 Mar 4 22:48 mmap_min_addr
-rw-r--r-- 1 root root 0 Mar 4 22:48 nr_hugepages
-rw-r--r-- 1 root root 0 Mar 4 22:48 nr_hugepages_mempolicy
-rw-r--r-- 1 root root 0 Mar 4 22:48 nr_overcommit_hugepages
-r--r--r-- 1 root root 0 Mar 4 22:48 nr_pdflush_threads
-rw-r--r-- 1 root root 0 Mar 4 22:48 numa_zonelist_order
-rw-r--r-- 1 root root 0 Mar 4 22:48 oom_dump_tasks
-rw-r--r-- 1 root root 0 Mar 4 22:48 oom_kill_allocating_task
-rw-r--r-- 1 root root 0 Mar 4 22:48 overcommit_kbytes
-rw-r--r-- 1 root root 0 Mar 4 22:48 overcommit_memory
-rw-r--r-- 1 root root 0 Mar 4 22:48 overcommit_ratio
-rw-r--r-- 1 root root 0 Mar 4 22:48 page-cluster
-rw-r--r-- 1 root root 0 Mar 4 22:48 panic_on_oom
-rw-r--r-- 1 root root 0 Mar 4 22:48 percpu_pagelist_fraction
-rw-r--r-- 1 root root 0 Mar 4 22:48 scan_unevictable_pages
-rw-r--r-- 1 root root 0 Mar 4 22:48 stat_interval
-rw-r--r-- 1 root root 0 Mar 4 22:48 swappiness
-rw-r--r-- 1 root root 0 Mar 4 22:48 unmap_area_factor
-rw-r--r-- 1 root root 0 Mar 4 22:48 vfs_cache_pressure
-rw-r--r-- 1 root root 0 Mar 4 22:48 would_have_oomkilled
-rw-r--r-- 1 root root 0 Mar 4 22:48 zone_reclaim_mode
[root@localhost ~]#

设定内核参数值的方法(能立即生效,但无法永久有效):
1、重定向:echo VALUE > /proc/sys/TO/SOMEFILE
2、sysctl -w kernel.hostname= (sysctl专用于设定内核参数)


[root@localhost ~]# free -m
total used free shared buffers cached
Mem: 1861 329 1532 1 31 111
-/+ buffers/cache: 185 1676
Swap: 2047 0 2047
[root@localhost ~]# echo 1 > /proc/sys/vm/drop_caches
[root@localhost ~]# free -m
total used free shared buffers cached
Mem: 1861 217 1644 1 0 32
-/+ buffers/cache: 184 1677
Swap: 2047 0 2047
[root@localhost ~]#
[root@localhost ~]# cat /proc/sys/kernel/hostname
localhost
[root@localhost ~]# echo cxiong > /proc/sys/kernel/hostname
[root@localhost ~]# hostname
cxiong
[root@localhost ~]#

[root@localhost ~]# sysctl -w kernel.hostname="localhost"
kernel.hostname = localhost
[root@localhost ~]# hostname
localhost
[root@localhost ~]#

设定内核参数值的方法(永久有效,但无法立即生效)
修改配置文件:/etc/sysctl.conf
修改文件完成之后,执行如下命令可立即生效:
sysctl -p
sysctl -a: 显示所有内核参数及其值


[root@localhost ~]# vim /etc/sysctl.conf
[root@localhost ~]# cat /etc/sysctl.conf
# Kernel sysctl configuration file for Red Hat Linux
#
# For binary values, 0 is disabled, 1 is enabled. See sysctl(8) and
# sysctl.conf(5) for more details.

# Controls IP packet forwarding
net.ipv4.ip_forward = 1 (修改参数为1)

# Controls source route verification
net.ipv4.conf.default.rp_filter = 1

# Do not accept source routing
net.ipv4.conf.default.accept_source_route = 0

# Controls the System Request debugging functionality of the kernel
kernel.sysrq = 0

# Controls whether core dumps will append the PID to the core filename.
# Useful for debugging multi-threaded applications.
kernel.core_uses_pid = 1

# Controls the use of TCP syncookies
net.ipv4.tcp_syncookies = 1

# Disable netfilter on bridges.
net.bridge.bridge-nf-call-ip6tables = 0
net.bridge.bridge-nf-call-iptables = 0
net.bridge.bridge-nf-call-arptables = 0

# Controls the default maxmimum size of a mesage queue
kernel.msgmnb = 65536

# Controls the maximum size of a message, in bytes
kernel.msgmax = 65536

# Controls the maximum shared segment size, in bytes
kernel.shmmax = 68719476736

# Controls the maximum number of shared memory segments, in pages
kernel.shmall = 4294967296
[root@localhost ~]# cat /proc/sys/net/ipv4/ip_forward
0

[root@localhost ~]# sysctl -p
net.ipv4.ip_forward = 1
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
error: "net.bridge.bridge-nf-call-ip6tables" is an unknown key
error: "net.bridge.bridge-nf-call-iptables" is an unknown key
error: "net.bridge.bridge-nf-call-arptables" is an unknown key
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
[root@localhost ~]#

内核模块管理(内核模块必须与内核版本完全一致):
lsmod: 查看

modprobe MOD_NAME:装载某模块
modprobe -r MOD_NAME: 卸载某模块

modinfo MOD_NAME: 查看模块的具体信息

insmod /PATH/TO/MODULE_FILE: 根据路径,[b]装载模块
rmmod MOD_NAME[/b]
depmod /PATH/TO/MODILES_DIR

[root@localhost ~]# lsmod
Module Size Used by
8021q 25527 0
garp 7152 1 8021q
stp 2218 1 garp
llc 5578 2 garp,stp
ipt_REJECT 2351 2
nf_conntrack_ipv4 9506 2
nf_defrag_ipv4 1483 1 nf_conntrack_ipv4
iptable_filter 2793 1
ip_tables 17831 1 iptable_filter
ip6t_REJECT 4628 2
nf_conntrack_ipv6 8337 3
nf_defrag_ipv6 27236 1 nf_conntrack_ipv6
xt_state 1492 5
nf_conntrack 80390 3 nf_conntrack_ipv4,nf_conntrack_ipv6,xt_state
ip6table_filter 2889 1
ip6_tables 18732 1 ip6table_filter
ipv6 334932 275 ip6t_REJECT,nf_conntrack_ipv6,nf_defrag_ipv6
ext3 240580 1
jbd 80950 1 ext3
uinput 8120 0
microcode 112653 0
vmware_balloon 7199 0
raid0 12417 0
snd_ens1371 21587 2
snd_rawmidi 23017 1 snd_ens1371
snd_ac97_codec 124967 1 snd_ens1371
ac97_bus 1452 1 snd_ac97_codec
snd_seq 55727 0
snd_seq_device 6500 2 snd_rawmidi,snd_seq
snd_pcm 87790 2 snd_ens1371,snd_ac97_codec
snd_timer 22443 2 snd_seq,snd_pcm
snd 70953 11 snd_ens1371,snd_rawmidi,snd_ac97_codec,snd_seq,snd_seq_device,snd_pcm,snd_timer
soundcore 7958 1 snd
snd_page_alloc 8856 1 snd_pcm
e1000 160643 0
sg 29318 0
i2c_piix4 11776 0
i2c_core 29964 1 i2c_piix4
shpchp 29130 0
ext4 378476 2
jbd2 93427 1 ext4
mbcache 8193 2 ext3,ext4
sr_mod 15049 0
cdrom 39085 1 sr_mod
sd_mod 36998 9
crc_t10dif 1305 1 sd_mod
ahci 41208 0
mptspi 16411 7
mptscsih 36636 1 mptspi
mptbase 93615 2 mptspi,mptscsih
scsi_transport_spi 25447 1 mptspi
pata_acpi 3701 0
ata_generic 3837 0
ata_piix 24409 0
dm_mirror 14384 0
dm_region_hash 12085 1 dm_mirror
dm_log 9930 2 dm_mirror,dm_region_hash
dm_mod 95622 5 dm_mirror,dm_log

[root@localhost ~]# modprobe -r sg
[root@localhost ~]# lsmod | grep sg
[root@localhost ~]# modprobe sg
[root@localhost ~]# lsmod | grep sg
sg 29318 0
[root@localhost ~]#

[root@localhost ~]# modinfo sg
filename: /lib/modules/2.6.32-504.el6.x86_64/kernel/drivers/scsi/sg.ko
alias: char-major-21-*
version: 3.5.34
license: GPL
description: SCSI generic (sg) driver
author: Douglas Gilbert
srcversion: DAF3F1D4974BB261E473A85
depends: (包依赖关系)
vermagic: 2.6.32-504.el6.x86_64 SMP mod_unload modversions
parm: scatter_elem_sz:scatter gather element size (default: max(SG_SCATTER_SZ, PAGE_SIZE)) (int)
parm: def_reserved_size:size of buffer reserved for each fd (int)
parm: allow_dio:allow direct I/O (default: 0 (disallow)) (int)
[root@localhost ~]#

[root@localhost ~]# modprobe -r mii
[root@localhost ~]# insmod /lib/modules/2.6.32-504.el6.x86_64/kernel/drivers/net/mii.ko
[root@localhost ~]# lsmod | grep mii
mii 5376 0
[root@localhost ~]#

大多数内核中的功能除了核心功能之外,在编译时,大多功能都有三种选择:
1、不使用此功能;
2、编译成内核模块;
3、编译进内核;

如何手动编译内核:(develop tool/develop library)
内核的编译与rpm包不同,因为内核的参数多;
make gconfig: Gnome桌面环境使用,需要安装图形开发库组:GNOME Software Development
make kconfig: KDE桌面环境使用,需要安装图形开发库

make menuconfig:

make
make modules_install
make install

[root@localhost ~]# uname -r
2.6.32-504.el6.x86_64 (rhel6上的主流内核版本号一直为2.6.32;rhel5的主流内核版本号为2.6.18)

任务:收集rhel内核源码包
https://www.kernel.org/pub/linux/kernel/v2.6/

通过make menuconfig编译内核

[root@localhost ~]# ll /usr/src/
total 12
drwxr-xr-x. 2 root root 4096 Jun 28 2011 debug
drwxr-xr-x. 3 root root 4096 Jun 21 2015 kernels
drwxr-xr-x. 22 root root 4096 Mar 4 23:53 linux-2.6.28.10
[root@localhost linux]# ll
total 380
drwxr-xr-x. 24 root root 4096 Mar 4 23:53 arch(平台架构)
drwxr-xr-x. 2 root root 4096 Mar 4 23:53 block
-rw-r--r--. 1 root root 18693 Mar 4 23:53 COPYING
-rw-r--r--. 1 root root 93212 Mar 4 23:53 CREDITS
drwxr-xr-x. 3 root root 4096 Mar 4 23:53 crypto(加密解密)
drwxr-xr-x. 82 root root 4096 Mar 4 23:53 Documentation
drwxr-xr-x. 83 root root 4096 Mar 4 23:53 drivers(驱动)
drwxr-xr-x. 18 root root 4096 Mar 4 23:53 firmware
drwxr-xr-x. 62 root root 4096 Mar 4 23:53 fs (文件系统)
drwxr-xr-x. 27 root root 4096 Mar 4 23:53 include
drwxr-xr-x. 2 root root 4096 Mar 4 23:53 init
drwxr-xr-x. 2 root root 4096 Mar 4 23:53 ipc(进程间通信)
-rw-r--r--. 1 root root 2430 Mar 4 23:53 Kbuild
drwxr-xr-x. 6 root root 4096 Mar 4 23:53 kernel
drwxr-xr-x. 6 root root 4096 Mar 4 23:53 lib
-rw-r--r--. 1 root root 106036 Mar 4 23:53 MAINTAINERS
-rw-r--r--. 1 root root 57314 Mar 4 23:53 Makefile
drwxr-xr-x. 2 root root 4096 Mar 4 23:53 mm(内存管理)
drwxr-xr-x. 45 root root 4096 Mar 4 23:53 net(网络)
-rw-r--r--. 1 root root 16930 Mar 4 23:53 README
-rw-r--r--. 1 root root 3172 Mar 4 23:53 REPORTING-BUGS
drwxr-xr-x. 7 root root 4096 Mar 4 23:53 samples
drwxr-xr-x. 10 root root 4096 Mar 4 23:53 scripts
drwxr-xr-x. 5 root root 4096 Mar 4 23:53 security
drwxr-xr-x. 20 root root 4096 Mar 4 23:53 sound
drwxr-xr-x. 2 root root 4096 Mar 4 23:53 usr
drwxr-xr-x. 3 root root 4096 Mar 4 23:53 virt(虚拟化)
[root@localhost linux]#

如何手动编译内核(内核参数非常多):
make gconfig: Gnome桌面环境使用,需要安装图形开发库组:GNOME Software Development
make kconfig: KDE桌面环境使用,需要安装图形开发库
make menuconfig(经常使用):

lftp root@192.168.1.18:/pub> get ncurses-devel-5.5-24.20060715.x86_64.rpm (make menuconfig依赖包)
1791882 bytes transferred
lftp root@192.168.1.18:/pub> bye
[root@localhost linux]#
[root@localhost linux]# ls
arch COPYING crypto drivers fs init Kbuild lib Makefile ncurses-devel-5.5-24.20060715.x86_64.rpm README samples security usr
block CREDITS Documentation firmware include ipc kernel MAINTAINERS mm net REPORTING-BUGS scripts sound virt
[root@localhost linux]# rpm -ivh ncurses-devel-5.5-24.20060715.x86_64.rpm
warning: ncurses-devel-5.5-24.20060715.x86_64.rpm: Header V3 DSA signature: NOKEY, key ID e8562897
Preparing... ########################################### [100%]
1:ncurses-devel ########################################### [100%]
[root@localhost linux]# make menuconfig (make menuconfig修改内核参数)
HOSTCC scripts/kconfig/conf.o
HOSTCC scripts/kconfig/kxgettext.o
HOSTCC scripts/kconfig/lxdialog/checklist.o
HOSTCC scripts/kconfig/lxdialog/inputbox.o
HOSTCC scripts/kconfig/lxdialog/menubox.o
HOSTCC scripts/kconfig/lxdialog/textbox.o
HOSTCC scripts/kconfig/lxdialog/util.o
HOSTCC scripts/kconfig/lxdialog/yesno.o
HOSTCC scripts/kconfig/mconf.o
SHIPPED scripts/kconfig/zconf.tab.c
SHIPPED scripts/kconfig/lex.zconf.c
SHIPPED scripts/kconfig/zconf.hash.c
HOSTCC scripts/kconfig/zconf.tab.o
HOSTLD scripts/kconfig/mconf
scripts/kconfig/mconf arch/x86/Kconfig
#
# using defaults found in /boot/config-2.6.18-308.el5
#
/boot/config-2.6.18-308.el5:1365:warning: symbol value 'm' invalid for FIXED_PHY
/boot/config-2.6.18-308.el5:1671:warning: symbol value 'm' invalid for ISDN
/boot/config-2.6.18-308.el5:2789:warning: symbol value 'm' invalid for RTC_INTF_SYSFS
/boot/config-2.6.18-308.el5:2790:warning: symbol value 'm' invalid for RTC_INTF_PROC
/boot/config-2.6.18-308.el5:2791:warning: symbol value 'm' invalid for RTC_INTF_DEV
/boot/config-2.6.18-308.el5:2813:warning: symbol value 'm' invalid for DMA_ENGINE
#
# configuration written to .config
#

*** End of Linux kernel configuration.
*** Execute 'make' to build the kernel or try 'make help'.

[root@localhost linux]# pwd
/usr/src/linux
[root@localhost linux]# ls -a (编译完成后会生成.config文件)
. block CREDITS drivers .gitignore ipc lib Makefile net samples sound
.. .config crypto firmware include Kbuild .mailmap mm README scripts usr
arch COPYING Documentation fs init kernel MAINTAINERS ncurses-devel-5.5-24.20060715.x86_64.rpm REPORTING-BUGS security virt
[root@localhost linux]# make (编译时间实际30分钟)
[root@localhost linux]#

[root@localhost linux]# ls /boot/ (当前系统的.config文件)
config-2.6.18-308.el5 grub initrd-2.6.18-308.el5.img lost+found symvers-2.6.18-308.el5.gz System.map-2.6.18-308.el5 vmlinuz-2.6.18-308.el5
[root@localhost linux]#
[root@localhost linux]# ls /boot/config-2.6.18-308.el5 grub initrd-2.6.18-308.el5.img lost+found symvers-2.6.18-308.el5.gz System.map-2.6.18-308.el5 vmlinuz-2.6.18-308.el5
[root@localhost linux]# cp /boot/config-2.6.18-308.el5 .config (将当前系统.config文件作为编译新内核的.config文件)
cp: overwrite `.config'?
[root@localhost linux]# make menuconfig
scripts/kconfig/mconf arch/x86/Kconfig
#
# configuration written to .config
#

*** End of Linux kernel configuration.
*** Execute 'make' to build the kernel or try 'make help'.

[root@localhost linux]# make (编译)
[root@localhost linux]# make modules_install
[root@localhost linux]# make install
编译完成后,重启操作系统,在grub中会生成新title,选择新内核即可;




二次编译时清理,清理前,如果有需要,请备份配置文件.config:
make clean
make mrproper

screen命令:
screen -ls: 显示已经建立的屏幕
screen: 直接打开一个新的屏幕
Ctrl+a, d: 拆除屏幕
screen -r ID: 还原回某屏幕
exit: 退出
[root@localhost screen-4.0.3]# find / -name screen
'/usr/share/terminfo/s/screen
/usr/src/kernels/2.6.18-308.el5-x86_64/include/config/input/mousedev/screen
/usr/local/share/screen
/usr/local/bin/screen
/root/screen-4.0.3/screen
/root/screen-4.0.3/share/screen
/root/screen-4.0.3/bin/screen
[root@localhost screen-4.0.3]# cd
[root@localhost ~]# screen
[detached]
[root@localhost ~]# screen -l
[detached]
[root@localhost ~]# screen -ls
There are screens on:
27474.pts-1.localhost (Detached)
27492.pts-1.localhost (Detached)
2 Sockets in /tmp/screens/S-root.

[root@localhost ~]# screen -r 27474
[detached]
[root@localhost ~]#

在宿主机上创建新系统,分区情况

/mnt/boot
/mnt/sysroot/bin,sbin,etc,var,tmp,proc,sys,dev,lib,usr,home

[root@localhost ~]# fdisk -l

Disk /dev/hda: 21.4 GB, 21474836480 bytes
15 heads, 63 sectors/track, 44384 cylinders
Units = cylinders of 945 * 512 = 483840 bytes

Disk /dev/hda doesn't contain a valid partition table

Disk /dev/sda: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 1 38 305203+ 83 Linux
/dev/sda2 39 2484 19647495 83 Linux
/dev/sda3 2485 2610 1012095 82 Linux swap / Solaris
[root@localhost ~]# fdisk /dev/hda (指定新boot及新root分区)
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won't be recoverable.

The number of cylinders for this disk is set to 44384.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-44384, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-44384, default 44384): +20M

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (43-44384, default 43):
Using default value 43
Last cylinder or +size or +sizeM or +sizeK (43-44384, default 44384): +512M

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# fdisk -l

Disk /dev/hda: 21.4 GB, 21474836480 bytes
15 heads, 63 sectors/track, 44384 cylinders
Units = cylinders of 945 * 512 = 483840 bytes

Device Boot Start End Blocks Id System
/dev/hda1 1 42 19813+ 83 Linux (boot分区)
/dev/hda2 43 1101 500377+ 83 Linux (root分区)


Disk /dev/sda: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 1 38 305203+ 83 Linux
/dev/sda2 39 2484 19647495 83 Linux
/dev/sda3 2485 2610 1012095 82 Linux swap / Solaris
[root@localhost ~]# mke2fs -j /dev/hda1
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
4968 inodes, 19812 blocks
990 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=20447232
3 block groups
8192 blocks per group, 8192 fragments per group
1656 inodes per group
Superblock backups stored on blocks:
8193

Writing inode tables: done
Creating journal (1024 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 29 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@localhost ~]# mke2fs -j /dev/hda2
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
125488 inodes, 500376 blocks
25018 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67633152
62 block groups
8192 blocks per group, 8192 fragments per group
2024 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409

Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 25 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@localhost ~]# mkdir /mnt/{boot,sysroot}
[root@localhost ~]# mount /dev/hda1 /mnt/boot/
[root@localhost ~]# mount /dev/hda2 /mnt/sysroot/
[root@localhost ~]# mount
/dev/sda2 on / type ext3 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/sda1 on /boot type ext3 (rw)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
none on /proc/fs/vmblock/mountPoint type vmblock (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
/dev/hda1 on /mnt/boot type ext3 (rw)
/dev/hda2 on /mnt/sysroot type ext3 (rw)

[root@localhost ~]# grub-install --root-directory=/mnt /dev/hda (在新boot分区中安装grub相关1、1.5、2阶段)
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/hda
(hd1) /dev/sda
[root@localhost ~]# ls /mnt/boot/
grub lost+found
[root@localhost ~]# cp /boot/vmlinuz-2.6.18-308.el5 /mnt/boot/vmlinuz (新系统内核)
[root@localhost ~]# mkinitrd /boot/initrd-`uname -r`.img `uname-r` (此命令用于在当前系统中创建initrd,不适合为其他硬盘创建initrd)
[root@localhost ~]# uname -r
2.6.18-308.el5
[root@localhost ~]# mount
/dev/sda2 on / type ext3 (rw) (当前系统rootfs所在目录)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/sda1 on /boot type ext3 (rw)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
none on /proc/fs/vmblock/mountPoint type vmblock (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
/dev/hda1 on /mnt/boot type ext3 (rw)
/dev/hda2 on /mnt/sysroot type ext3 (rw)

[root@localhost ~]# file /boot/initrd-`uname -r` (查看initrd文件类型,通过展开现有initrd文件、编辑、再压缩的方法创建initrd文件)
/boot/initrd-2.6.18-308.el5: ERROR: cannot open `/boot/initrd-2.6.18-308.el5' (No such file or directory)
[root@localhost ~]# file /boot/initrd-`uname -r`.img
/boot/initrd-2.6.18-308.el5.img: gzip compressed data, from Unix, last modified: Mon Mar 13 10:35:32 2017, max compression
[root@localhost ~]# cp /boot/initrd-`uname -r`.img /root
[root@localhost ~]# ls
initrd-2.6.18-308.el5.img screen-4.0.3 screen-4.0.3.tar.gz linux-2.6.28.10.tar.gz Desktop anaconda-ks.cfg install.log install.log.syslog
[root@localhost ~]# mv initrd-2.6.18-308.el5.img initrd-2.6.18-308.el5.img.gz (解压initrd文件)
[root@localhost ~]# gzip -d initrd-2.6.18-308.el5.img.gz
[root@localhost ~]# file initrd-2.6.18-308.el5.img
initrd-2.6.18-308.el5.img: ASCII cpio archive (SVR4 with no CRC) (cpio文件)
[root@localhost ~]# mkdir test
[root@localhost ~]# cd test/
[root@localhost test]# cd
[root@localhost ~]# cd test/
[root@localhost test]# cd
[root@localhost ~]# ls
test initrd-2.6.18-308.el5.img screen-4.0.3 screen-4.0.3.tar.gz linux-2.6.28.10.tar.gz Desktop anaconda-ks.cfg install.log install.log.syslog
[root@localhost ~]# cd
[root@localhost ~]# cd test/
[root@localhost test]# cpio -id < ../ (解压cpio文件)
anaconda-ks.cfg .config/ .gconf/ .gstreamer-0.10/ install.log.syslog .nautilus/ test/
.bash_history .cshrc .gconfd/ .gtkrc-1.2-gnome2 .lftp/ .redhat/ .Trash/
.bash_logout Desktop/ .gnome/ .ICEauthority linux-2.6.28.10.tar.gz screen-4.0.3/ .xsession-errors
.bash_profile .dmrc .gnome2/ initrd-2.6.18-308.el5.img .metacity/ screen-4.0.3.tar.gz
.bashrc .eggcups/ .gnome2_private/ install.log .mozilla/ .tcshrc
[root@localhost test]# cpio -id < ../initrd-2.6.18-308.el5.img
14779 blocks
[root@localhost test]# ls (initrd也是一个Linux)
bin dev etc init lib proc sbin sys sysroot

[root@localhost test]#
[root@localhost test]#
[root@localhost test]# cd
[root@localhost ~]# mkdir iso
[root@localhost ~]# cd iso/
[root@localhost iso]# zcat /boot/initrd-2.6.18-308.el5.img | cpio -id (另外一种解压initrd文件方法,较简单,但只能展开在当前文件)
14779 blocks
[root@localhost iso]# ls
bin dev etc init lib proc sbin sys sysroot
[root@localhost iso]#
[root@localhost iso]# cat init (修改init文件,init为一个nash的脚本)
#!/bin/nash

mount -t proc /proc /proc
setquiet
echo Mounting proc filesystem
echo Mounting sysfs filesystem
mount -t sysfs /sys /sys
echo Creating /dev
mount -o mode=0755 -t tmpfs /dev /dev
mkdir /dev/pts
mount -t devpts -o gid=5,mode=620 /dev/pts /dev/pts
mkdir /dev/shm
mkdir /dev/mapper
echo Creating initial device nodes
mknod /dev/null c 1 3
mknod /dev/zero c 1 5
mknod /dev/urandom c 1 9
mknod /dev/systty c 4 0
mknod /dev/tty c 5 0
mknod /dev/console c 5 1
mknod /dev/ptmx c 5 2
mknod /dev/rtc c 10 135
mknod /dev/tty0 c 4 0
mknod /dev/tty1 c 4 1
mknod /dev/tty2 c 4 2
mknod /dev/tty3 c 4 3
mknod /dev/tty4 c 4 4
mknod /dev/tty5 c 4 5
mknod /dev/tty6 c 4 6
mknod /dev/tty7 c 4 7
mknod /dev/tty8 c 4 8
mknod /dev/tty9 c 4 9
mknod /dev/tty10 c 4 10
mknod /dev/tty11 c 4 11
mknod /dev/tty12 c 4 12
mknod /dev/ttyS0 c 4 64
mknod /dev/ttyS1 c 4 65
mknod /dev/ttyS2 c 4 66
mknod /dev/ttyS3 c 4 67
echo Setting up hotplug.
hotplug
echo Creating block device nodes.
mkblkdevs
echo "Loading ehci-hcd.ko module"
insmod /lib/ehci-hcd.ko
echo "Loading ohci-hcd.ko module"
insmod /lib/ohci-hcd.ko
echo "Loading uhci-hcd.ko module"
insmod /lib/uhci-hcd.ko
mount -t usbfs /proc/bus/usb /proc/bus/usb
echo "Loading jbd.ko module"
insmod /lib/jbd.ko
echo "Loading ext3.ko module"
insmod /lib/ext3.ko
echo "Loading scsi_mod.ko module"
insmod /lib/scsi_mod.ko
echo "Loading sd_mod.ko module"
insmod /lib/sd_mod.ko
echo "Loading scsi_transport_spi.ko module"
insmod /lib/scsi_transport_spi.ko
echo "Loading mptbase.ko module"
insmod /lib/mptbase.ko
echo "Loading mptscsih.ko module"
insmod /lib/mptscsih.ko
echo "Loading mptspi.ko module"
insmod /lib/mptspi.ko
echo "Loading libata.ko module"
insmod /lib/libata.ko
echo "Loading ata_piix.ko module"
insmod /lib/ata_piix.ko
echo "Loading ahci.ko module"
insmod /lib/ahci.ko
echo "Loading dm-mem-cache.ko module"
insmod /lib/dm-mem-cache.ko
echo "Loading dm-mod.ko module"
insmod /lib/dm-mod.ko
echo "Loading dm-log.ko module"
insmod /lib/dm-log.ko
echo "Loading dm-region_hash.ko module"
insmod /lib/dm-region_hash.ko
echo "Loading dm-message.ko module"
insmod /lib/dm-message.ko
echo "Loading dm-raid45.ko module"
insmod /lib/dm-raid45.ko
echo "Loading vmxnet3.ko module"
insmod /lib/vmxnet3.ko
echo "Loading pvscsi.ko module"
insmod /lib/pvscsi.ko
echo "Loading vmxnet.ko module"
insmod /lib/vmxnet.ko
echo Waiting for driver initialization.
stabilized --hash --interval 1000 /proc/scsi/scsi
mkblkdevs
echo Scanning and configuring dmraid supported devices
#resume LABEL=SWAP-sda3 (没有swap,可以省略)
echo Creating root device.
mkrootdev -t ext3 -o defaults,ro /dev/hda2 (修改为新的根设备/dev/hda2)
echo Mounting root filesystem.
mount /sysroot
echo Setting up other filesystems.
setuproot
echo Switching to new root and running init.
switchroot
[root@localhost iso]#

[root@localhost iso]# find . | cpio -H newc --quiet -o | gzip -9 > /mnt/boot/initrd.gz (当前目录下,使用此命令进行cpio归档,完成创建initrd文件;)
[root@localhost iso]# cd /mnt/boot/
[root@localhost boot]# ls (initrd文件创建成功)
initrd.gz vmlinuz grub lost+found
[root@localhost boot]# ll
total 4875
drwxr-xr-x 2 root root 1024 Mar 13 14:52 grub
-rw-r--r-- 1 root root 2835771 Mar 13 15:05 initrd.gz
drwx------ 2 root root 12288 Mar 13 14:50 lost+found
-rw-r--r-- 1 root root 2115644 Mar 13 14:53 vmlinuz
[root@localhost boot]# ll -h
total 4.8M
drwxr-xr-x 2 root root 1.0K Mar 13 14:52 grub
-rw-r--r-- 1 root root 2.8M Mar 13 15:05 initrd.gz (initrd只有2.8M)
drwx------ 2 root root 12K Mar 13 14:50 lost+found
-rw-r--r-- 1 root root 2.1M Mar 13 14:53 vmlinuz (内核vmlinux只有2.1M)
[root@localhost boot]# pwd
/mnt/boot
[root@localhost boot]# vi /mnt/boot/grub/grub.conf (vmlinuz、initrd已完成,还需为新系统创建grub.conf文件)
[root@localhost ~]# cat /mnt/boot/grub/grub.conf
default=0
timeout=5
title mylinux(cxiong)
root (hd0,0)
kernel /vmlinuz
initrd /initrd.gz

[root@localhost ~]#
[root@localhost sysroot]# mkdir proc sys dev etc/rc.d lib bin sbin boot home var/log usr/{bin,sbin} root tmp -pv (为根文件系统创建目录结构)
mkdir: created directory `proc'
mkdir: created directory `sys'
mkdir: created directory `dev'
mkdir: created directory `etc'
mkdir: created directory `etc/rc.d'
mkdir: created directory `lib'
mkdir: created directory `bin'
mkdir: created directory `sbin'
mkdir: created directory `boot'
mkdir: created directory `home'
mkdir: created directory `var'
mkdir: created directory `var/log'
mkdir: created directory `usr'
mkdir: created directory `usr/bin'
mkdir: created directory `usr/sbin'
mkdir: created directory `root'
mkdir: created directory `tmp'
[root@localhost sysroot]# tree
.
|-- bin
|-- boot
|-- dev
|-- etc
| `-- rc.d
|-- home
|-- lib
|-- lost+found
|-- proc
|-- root
|-- sbin
|-- sys
|-- tmp
|-- usr
| |-- bin
| `-- sbin
`-- var
`-- log

18 directories, 0 files
[root@localhost sysroot]# cp /sbin/init /mnt/sysroot/sbin/ (init为系统第一进程)
[root@localhost sysroot]# cp /bin/bash /mnt/sysroot/bin/ (bash进程)
[root@localhost sysroot]# ldd /sbin/init (复制文件依赖库)
linux-vdso.so.1 => (0x00007fff1b9fd000)
libsepol.so.1 => /lib64/libsepol.so.1 (0x00000031e6400000)
libselinux.so.1 => /lib64/libselinux.so.1 (0x00000031e6000000)
libc.so.6 => /lib64/libc.so.6 (0x00000031e4800000)
libdl.so.2 => /lib64/libdl.so.2 (0x00000031e4c00000)
/lib64/ld-linux-x86-64.so.2 (0x00000031e4400000)

[root@localhost ~]# chroot /mnt/sysroot/ (chroot测试)
bash-3.2# ls
bash: ls: command not found
bash-3.2# exit
exit
[root@localhost ~]# sync
[root@localhost ~]# cd /mnt/sysroot/
[root@localhost sysroot]# vi etc/inittab
[root@localhost sysroot]#
[root@localhost ~]# cd /mnt/sysroot/
[root@localhost sysroot]# vi etc/inittab
[root@localhost sysroot]# ls
etc root lib64 lib libsepol.so.1 bin sbin boot dev home proc sys tmp usr var lost+found
[root@localhost sysroot]# vi etc/inittab
[root@localhost sysroot]# cat etc/inittab (编辑inittab文件)
id:3:initdefault:
si::sysinit:/etc/rc.d/rc.sysinit

[root@localhost sysroot]#
[root@localhost sysroot]# chmod +x etc/rc.d/rc.sysinit
[root@localhost sysroot]# sync
[root@localhost sysroot]# sync
[root@localhost sysroot]# vi etc/rc.d/sysinit
[root@localhost sysroot]# cat etc/rc.d/sysinit (开启sysinit服务)
#!/bin/bash
echo -e "\tWelcome to \033[31mCXIONG \033[0m linux."
/bin/bash

[root@localhost sysroot]# sync
[root@localhost sysroot]# sync
[root@localhost sysroot]# sync
[root@localhost sysroot]# sync
[root@localhost sysroot]# sync




文件错乱导致kernel panic解决办法:



打包/mnt/sysroot,并格式化后,再重新解压

find . | cpio -H newc --quiet -o | gzip -9 > /root/sysroot.gz
umount /dev/hda2
fuser -km /dev/hda2
umount /dev/hda2
mke2fs -j /dev/hda2
mount /dev/hda2 /mnt/sysroot
cd /mnt/sysroot
zcat /root/sysroot.gz | cpio -id
sync
sync

硬盘检查
umount /dev/hda2
fuser -km /dev/hda2
umount /dev/hda2
e2fsck -f /dev/hda2
mount /dev/hda2 /mnt/sysroot
sync
sync

${parameter#*word}
${parameter##*word}
The word is expanded to produce a pattern just as in pathname expansion. If the pattern matches the beginning of the value of
parameter, then the result of the expansion is the expanded value of parameter with the shortest matching pattern (the ?..?.
case) or the longest matching pattern (the ?..#?..case) deleted. If parameter is @ or *, the pattern removal operation is
applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable sub-
scripted with @ or *, the pattern removal operation is applied to each member of the array in turn, and the expansion is the
resultant list.

[root@localhost ~]# file=/usr/local/src
[root@localhost ~]# echo ${file#*/} (从左往右,第一次匹配到/,到最右边)
usr/local/src
[root@localhost ~]# echo ${file##*/} (从左往右,最后一次匹配到/,到[b]最右边)[/b]
src (文件名)
[root@localhost ~]#

[root@localhost ~]# file=hello/usr/local/src
[root@localhost ~]# echo ${file%%/*} (从右往左,第一次匹配到/,到最左边)
hello
[root@localhost ~]# echo ${file%/*}[b](从右往左,最后一次匹配到/,到最左边)[/b]
hello/usr/local (父目录)
[root@localhost ~]#

${parameter%word*}
${parameter%%word*}
The word is expanded to produce a pattern just as in pathname expansion. If the pattern matches a trailing portion of the
expanded value of parameter, then the result of the expansion is the expanded value of parameter with the shortest matching
pattern (the ?..?..case) or the longest matching pattern (the ?..%?..case) deleted. If parameter is @ or *, the pattern
removal operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is
an array variable subscripted with @ or *, the pattern removal operation is applied to each member of the array in turn, and
the expansion is the resultant list.

复制二进制程序及其依赖的库文件的脚本:
#!/bin/bash
#
DEST=/mnt/sysroot
libcp() {
LIBPATH=${1%/*}
[ ! -d $DEST$LIBPATH ] && mkdir -p $DEST$LIBPATH
[ ! -e $DEST${1} ] && cp $1 $DEST$LIBPATH && echo "copy lib $1 finished."
}

bincp() {
CMDPATH=${1%/*}
[ ! -d $DEST$CMDPATH ] && mkdir -p $DEST$CMDPATH
[ ! -e $DEST${1} ] && cp $1 $DEST$CMDPATH

for LIB in `ldd $1 | grep -o "/.*lib\(64\)\{0,1\}/[^[:space:]]\{1,\}"`; do
libcp $LIB
done
}

read -p "Your 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,\}"`
bincp $COMMAND
echo "copy $COMMAND finished."
read -p "Continue: " CMD
done

[root@localhost ~]# modinfo mii (开机装载网卡模块)
filename: /lib/modules/2.6.18-308.el5/kernel/drivers/net/mii.ko
license: GPL
description: MII hardware support library
author: Jeff Garzik <jgarzik@pobox.com>
srcversion: 16DCEDEE4B5629C222C352D
depends:
vermagic: 2.6.18-308.el5 SMP mod_unload gcc-4.1
module_sig: 883f3504f2324392d3142b3e704051112f4a809e30a7bade78b53fff21bc7ae7769423ed110f60a08aea9a8d3ad29725a95298edc5562ea33aedcc
[root@localhost ~]# modinfo pcnet32
filename: /lib/modules/2.6.18-308.el5/kernel/drivers/net/pcnet32.ko
license: GPL
description: Driver for PCnet32 and PCnetPCI based ethercards
author: Thomas Bogendoerfer
srcversion: F8144355
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  grub 内核编译 initrd