linux-裁剪Linux功能,编译/bin/login, busybox编译linux

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux-裁剪Linux功能,编译/bin/login, busybox编译linux相关的知识,希望对你有一定的参考价值。

linux功能:终端提示信息

[[email protected] ~]# cat /etc/issue
Red Hat Enterprise Linux Server release 5.8 (Tikanga)
Kernel \r on an \m


[[email protected] ~]# uname -r
2.6.18-308.el5
[[email protected] ~]# uname -m
x86_64
[[email protected] ~]#




linux功能:用户

nsswitch: Network Service Switch (网络服务转换)
框架:/etc/passwd, /etc/shadow, /etc/group
 库:libnss_file.so, libnss_nis.so, libnsss_ldap.so
 配置文件: /etc/nsswitch.conf


[[email protected] ~]# cat /etc/nsswitch.conf
#
# /etc/nsswitch.conf
#
# An example Name Service Switch config file. This file should be
# sorted with the most-used services at the beginning.
#
# The entry ‘[NOTFOUND=return]‘ means that the search for an
# entry should stop if the search in the previous entry turned
# up nothing. Note that if the search failed due to some other reason
# (like no NIS server responding) then the search continues with the
# next entry.
#
# Legal entries are:
#
# nisplus or nis+  Use NIS+ (NIS version 3)
# nis or yp  Use NIS (NIS version 2), also called YP
# dns   Use DNS (Domain Name Service)
# files   Use the local files
# db   Use the local database (.db) files
# compat   Use NIS on compat mode
# hesiod   Use Hesiod for user lookups
# [NOTFOUND=return] Stop searching if not found so far
#


# To use db, put the "db" in front of "files" for entries you want to be
# looked up first in the databases
#
# Example:
#passwd:    db files nisplus nis
#shadow:    db files nisplus nis
#group:     db files nisplus nis


passwd:     files
shadow:     files
group:      files


#hosts:     db files nisplus nis dns
hosts:      files dns


# Example - obey only what nisplus tells us...
#services:   nisplus [NOTFOUND=return] files
#networks:   nisplus [NOTFOUND=return] files
#protocols:  nisplus [NOTFOUND=return] files
#rpc:        nisplus [NOTFOUND=return] files
#ethers:     nisplus [NOTFOUND=return] files
#netmasks:   nisplus [NOTFOUND=return] files    


bootparams: nisplus [NOTFOUND=return] files


ethers:     files
netmasks:   files
networks:   files
protocols:  files
rpc:        files
services:   files


netgroup:   nisplus


publickey:  nisplus


automount:  files nisplus
aliases:    files nisplus


sudoers: files ldap
 # Do not remove the following line, or various programs
  2 # that require network functionality will fail.
  3 127.0.0.1               localhost.localdomain localhost
  4 ::1             localhost6.localdomain6 localhost6
~                                                                                                                                                         
~            

[[email protected] ~]# ll /lib/libnss*  (库文件)
-rwxr-xr-x 1 root root   36416 Jan 19  2012 /lib/libnss_compat-2.5.so
lrwxrwxrwx 1 root root      20 Mar 13 10:30 /lib/libnss_compat.so.2 -> libnss_compat-2.5.so
-rwxr-xr-x 1 root root  825028 Apr  8  2010 /lib/libnss_db-2.2.so
lrwxrwxrwx 1 root root      16 Mar 13 10:31 /lib/libnss_db.so.2 -> libnss_db-2.2.so
-rwxr-xr-x 1 root root   21948 Jan 19  2012 /lib/libnss_dns-2.5.so
lrwxrwxrwx 1 root root      17 Mar 13 10:30 /lib/libnss_dns.so.2 -> libnss_dns-2.5.so
-rwxr-xr-x 1 root root   50848 Jan 19  2012 /lib/libnss_files-2.5.so
lrwxrwxrwx 1 root root      19 Mar 13 10:30 /lib/libnss_files.so.2 -> libnss_files-2.5.so

-rwxr-xr-x 1 root root   22764 Jan 19  2012 /lib/libnss_hesiod-2.5.so
lrwxrwxrwx 1 root root      20 Mar 13 10:30 /lib/libnss_hesiod.so.2 -> libnss_hesiod-2.5.so
-rwxr-xr-x 1 root root 3208576 Dec  4  2011 /lib/libnss_ldap-2.5.so
lrwxrwxrwx 1 root root      18 Mar 13 10:31 /lib/libnss_ldap.so.2 -> libnss_ldap-2.5.so
-rwxr-xr-x 1 root root   46568 Jan 19  2012 /lib/libnss_nis-2.5.so
-rwxr-xr-x 1 root root   55804 Jan 19  2012 /lib/libnss_nisplus-2.5.so
lrwxrwxrwx 1 root root      21 Mar 13 10:30 /lib/libnss_nisplus.so.2 -> libnss_nisplus-2.5.so
lrwxrwxrwx 1 root root      17 Mar 13 10:30 /lib/libnss_nis.so.2 -> libnss_nis-2.5.so
[[email protected] ~]# ll /usr/lib/libnss*
-rwxr-xr-x 1 root root 1205444 Jan 13  2012 /usr/lib/libnss3.so
-rwxr-xr-x 1 root root  435184 Jan 13  2012 /usr/lib/libnssckbi.so
lrwxrwxrwx 1 root root      24 Mar 13 10:31 /usr/lib/libnss_db.so -> ../../lib/libnss_db.so.2
lrwxrwxrwx 1 root root      26 Mar 13 10:31 /usr/lib/libnss_ldap.so -> ../../lib/libnss_ldap.so.2
-rwxr-xr-x 1 root root  102788 Jan 13  2012 /usr/lib/libnssutil3.so
[[email protected] ~]#


移植库文件

[[email protected] ~]# cp -d /lib64/libnss_files* /mnt/sysroot/lib64/
[[email protected] ~]# ls /mnt/sysroot/lib64/
ld-linux-x86-64.so.2  libblkid.so.1         libdl.so.2           libnss_files.so.2  librt.so.1       libtermcap.so.2
libacl.so.1           libc.so.6             libm.so.6            libpthread.so.0    libselinux.so.1  libuuid.so.1
libattr.so.1          libdevmapper.so.1.02  libnss_files-2.5.so  libresolv.so.2     libsepol.so.1    modules
[[email protected] ~]# ll /mnt/sysroot/lib64/
total 3444
-rwxr-xr-x 1 root root  144776 Mar 19 07:20 ld-linux-x86-64.so.2
-rwxr-xr-x 1 root root   27920 Mar 19 07:20 libacl.so.1
-rwxr-xr-x 1 root root   18176 Mar 19 07:20 libattr.so.1
-rwxr-xr-x 1 root root   43808 Mar 19 07:20 libblkid.so.1
-rwxr-xr-x 1 root root 1722328 Mar 19 07:20 libc.so.6
-r-xr-xr-x 1 root root  154640 Mar 19 07:20 libdevmapper.so.1.02
-rwxr-xr-x 1 root root   23360 Mar 19 07:20 libdl.so.2
-rwxr-xr-x 1 root root  615136 Mar 19 07:20 libm.so.6
-rwxr-xr-x 1 root root   53880 Mar 19 07:43 libnss_files-2.5.so
lrwxrwxrwx 1 root root      19 Mar 19 07:43 libnss_files.so.2 -> libnss_files-2.5.so
-rwxr-xr-x 1 root root  145872 Mar 19 07:20 libpthread.so.0
-rwxr-xr-x 1 root root   92816 Mar 19 07:20 libresolv.so.2
-rwxr-xr-x 1 root root   53448 Mar 19 07:20 librt.so.1
-rwxr-xr-x 1 root root   95464 Mar 19 07:20 libselinux.so.1
-rwxr-xr-x 1 root root  247496 Mar 19 07:20 libsepol.so.1
-rwxr-xr-x 1 root root   15840 Mar 19 07:20 libtermcap.so.2
-rwxr-xr-x 1 root root   17936 Mar 19 07:20 libuuid.so.1
drwxr-xr-x 2 root root    1024 Mar 19 07:20 modules
[[email protected] ~]# ls /mnt/sysroot/usr/
bin  lib64  local  sbin
[[email protected] ~]# cp -d /usr/lib64/libnss_files.so /mnt/sysroot/usr/lib64/
[[email protected] ~]# cp -d /usr/lib64/libnss3.so /mnt/sysroot/usr/lib64/
[[email protected] ~]# cp -d /usr/lib64/libnssutil3.so /mnt/sysroot/usr/lib64/
[[email protected] ~]# ll /mnt/sysroot/usr/lib64/
total 1716
-rwxr-xr-x 1 root root  380464 Mar 19 07:20 libncurses.so.5
-rwxr-xr-x 1 root root 1237944 Mar 19 07:44 libnss3.so
lrwxrwxrwx 1 root root      29 Mar 19 07:44 libnss_files.so -> ../../lib64/libnss_files.so.2
-rwxr-xr-x 1 root root  123152 Mar 19 07:44 libnssutil3.so
[[email protected] ~]#

[[email protected] ~]# cat /mnt/sysroot/etc/nsswitch.conf
passwd:     files
shadow:     files
group:      files
#hosts:     db files nisplus nis dns
hosts:      files dns
[[email protected] ~]#

[[email protected] ~]# grep -E "^root|hadoop" /etc/shadow
root:$1$TjFLtg.N$BtOWZtw9.UBGwxTE07PK81:17238:0:99999:7:::
hadoop:$1$zYoVuMh.$ySUuGs9FGRe1UFdaxWQ8B.:17244:0:99999:7:::
[[email protected] ~]# grep -E "^root|hadoop" /etc/shadow > /mnt/sysroot/shadow

[[email protected] ~]#  grep -E "^root|hadoop" /etc/shadow > /mnt/sysroot/etc/shadow
[[email protected] ~]# grep -E "^root|hadoop" /etc/group > /mnt/sysroot/etc/group

[[email protected] ~]# ll /mnt/sysroot/etc/
total 18
-rw-r--r-- 1 root root  125 Mar 19 07:20 fstab
-rw-r--r-- 1 root root   28 Mar 19 07:53 group
-rw-r--r-- 1 root root  239 Mar 19 07:20 inittab
-rw-r--r-- 1 root root   74 Mar 19 07:30 issue
-rw-r--r-- 1 root root  113 Mar 19 07:47 nsswitch.conf
-rw-r--r-- 1 root root   73 Mar 19 07:52 passwd
drwxr-xr-x 6 root root 1024 Mar 19 07:20 rc.d
-rw-r--r-- 1 root root  120 Mar 19 07:53 shadow
drwxr-xr-x 3 root root 1024 Mar 19 07:20 sysconfig
[[email protected] ~]#


添加

[[email protected] ~]# bash copylib.sh
Your command: mingetty
copy /sbin/mingetty finished.
Continue: passwd
copy lib /usr/lib64/libuser.so.1 finished.
copy lib /lib64/libcrypt.so.1 finished.
copy lib /lib64/libgobject-2.0.so.0 finished.
copy lib /lib64/libgmodule-2.0.so.0 finished.
copy lib /lib64/libglib-2.0.so.0 finished.
copy lib /usr/lib64/libpopt.so.0 finished.
copy lib /lib64/libpam_misc.so.0 finished.
copy lib /lib64/libaudit.so.0 finished.
copy lib /lib64/libpam.so.0 finished.
copy /usr/bin/passwd finished.
Continue: useradd
copy /usr/sbin/useradd finished.
Continue: userdel
copy /usr/sbin/userdel finished.
Continue: usermod
copy /usr/sbin/usermod finished.
Continue: groupadd
copy /usr/sbin/groupadd finished.
Continue:


[[email protected] sysroot]# cat etc/inittab
id:3:initdefault:
si::sysinit:/etc/rc.d/rc.sysinit

l0:0:wait:/etc/rc.d/rc 0
l3:3:wait:/etc/rc.d/rc 3
l6:6:wait:/etc/rc.d/rc 6


1:2345:respawn:/sbin/agetty -n -l /bin/bash 38400 tty1
2:2345:respawn:/sbin/agetty -n -l /bin/bash 38400 tty2
[[email protected] sysroot]#

[[email protected] sysroot]# cat etc/inittab
id:3:initdefault:
si::sysinit:/etc/rc.d/rc.sysinit


l0:0:wait:/etc/rc.d/rc 0
l3:3:wait:/etc/rc.d/rc 3
l6:6:wait:/etc/rc.d/rc 6



1:2345:respawn:/sbin/mingetty tty1
2:2345:respawn:/sbin/mingetty tty2
[[email protected] sysroot]#





编译/bin/login程序,不需要密码认证

http://www.cnblogs.com/xianyunhe/archive/2011/08/08/2124342.html


(1) 查看/bin/login所属的软件包
[[email protected] ~]# rpm -qf /bin/login
util-linux-2.13-0.47.el5
(2)下载login对应的源码
util-linux-2.13-0.47.e15.src.rpm
(3)解压源码包
[[email protected] Desktop]# rpm -ivh util-linux-2.13-0.47.e15.src.rp  

解压后的源码包所在的目录为: /usr/src/redhat/SOURCES/
查看解压的文件:

[[email protected] ~]# ls /usr/src/redhat/SOURCES/
floppy-0.12-locale.patch                     util-linux-2.13-blockdev-rmparts.patch      util-linux-2.13-mount-comment.patch
floppy-0.12.tar.gz                           util-linux-2.13-cal-3.patch                 util-linux-2.13-mount-context.patch
nologin.8                                    util-linux-2.13-cal-wide.patch              util-linux-2.13-mount-fake.patch
nologin.c                                    util-linux-2.13-cramfs-maxentries.patch     util-linux-2.13-mount-fsname.patch
raw-handle-nonpresent-devs.patch             util-linux-2.13-cramfs-zerofiles.patch      util-linux-2.13-mount-man-bind.patch
util-linux-2.11f-rawman.patch                util-linux-2.13-ctrlaltdel-man.patch        util-linux-2.13-mount-man-bugs.patch
util-linux-2.11y-chsh-103004.patch           util-linux-2.13-ctty3.patch                 util-linux-2.13-mount-man-ext.patch
util-linux-2.11y-fdisksegv-103954.patch      util-linux-2.13-fdisk-1TB.patch             util-linux-2.13-mount-man-nfs4.patch
util-linux-2.11y-multibyte.patch             util-linux-2.13-fdisk-b-4096.patch          util-linux-2.13-mount-man-nfs.patch
util-linux-2.11y-procpartitions-37436.patch  util-linux-2.13-fdisk-dm.patch              util-linux-2.13-mount-man-vfat.patch
util-linux-2.11y-skipraid2.patch             util-linux-2.13-fdisk-dos.patch             util-linux-2.13-mount-move.patch
util-linux-2.12a-126572-fdiskman.patch       util-linux-2.13-fdisk-gpt.patch             util-linux-2.13-mount-mtabfake.patch
util-linux-2.12a-16415-rdevman.patch         util-linux-2.13-fdisk-isfull.patch          util-linux-2.13-mount-mtab.patch
util-linux-2.12a-ipcs-84243-86285.patch      util-linux-2.13-fdisk-limit.patch           util-linux-2.13-mount-nonfs.patch
util-linux-2.12a-mountbylabel-dm.patch       util-linux-2.13-fdisk-man.patch             util-linux-2.13-mount-setuid.patch
util-linux-2.12a-mount-man-cifs.patch        util-linux-2.13-fdisk-sectors.patch         util-linux-2.13-mount-sloppy.patch
util-linux-2.12a-pamsession.patch            util-linux-2.13-fdisk-sleep.patch           util-linux-2.13-mount-subtree.patch
util-linux-2.12a-pamstart.patch              util-linux-2.13-fdisk-vmware.patch          util-linux-2.13-mount-twiceloop.patch
util-linux-2.12a-partlimit.patch             util-linux-2.13-flock-segfaults.patch       util-linux-2.13-mount-uhelper.patch
util-linux-2.12a-raw-man-dd.patch            util-linux-2.13-fsfreeze.patch              util-linux-2.13-mount-uuid.patch
util-linux-2.12j-113790-hotkeys.patch        util-linux-2.13-fstab-man.patch             util-linux-2.13-partx-man.patch
util-linux-2.12j-managed.patch               util-linux-2.13-hexdump-gcc.patch           util-linux-2.13-pre7.tar.bz2
util-linux-2.12j-pamconsole.patch            util-linux-2.13-ipcs-32bit.patch            util-linux-2.13-raw-raw0.patch
util-linux-2.12p-cal-wide.patch              util-linux-2.13-ipcs-rc.patch               util-linux-2.13-rnetdev.patch
util-linux-2.12p-col-EILSEQ.patch            util-linux-2.13-ipcs-shmax.patch            util-linux-2.13-schedutils-man.patch
util-linux-2.12p-execl.patch                 util-linux-2.13-localedir.patch             util-linux-2.13-schedutils-SCHED_BATCH.patch
util-linux-2.12p-fdformat-ide.patch          util-linux-2.13-login-hang.patch            util-linux-2.13-script-races.patch
util-linux-2.12p-floppy-generic.patch        util-linux-2.13-login-hush.patch            util-linux-2.13-script-utempter.patch
util-linux-2.12p-fstab-man.patch             util-linux-2.13-login-ipv6.patch            util-linux-2.13-sfdisk-geo.patch
util-linux-2.12p-ipcs-typo.patch             util-linux-2.13-login-man-userswitch.patch  util-linux-2.13-sfdisk-sectors.patch
util-linux-2.12p-login-lastlog.patch         util-linux-2.13-login-pam-acct.patch        util-linux-2.13-sfdisk-write.patch
util-linux-2.12p-look-separator.patch        util-linux-2.13-login-timeval.patch         util-linux-2.13-swapon-ignore.patch
util-linux-2.12p-lvm2dupes-76467.patch       util-linux-2.13-login-utmp.patch            util-linux-2.13-swapon-suspend.patch
util-linux-2.12p-mount-ocfs2.patch           util-linux-2.13-losetup-all.patch           util-linux-2.13-swap-page.patch
util-linux-2.12p-mtab-lock.patch             util-linux-2.13-losetup-deprecated.patch    util-linux-2.13-umount-sysfs.patch
util-linux-2.12p-swaponsymlink-57300.patch   util-linux-2.13-losetup-rdonly.patch        util-linux-60-raw.rules
util-linux-2.12p-vipw-perm.patch             util-linux-2.13-mkfs-man.patch              util-linux-chsh-chfn.pamd
util-linux-2.13-arch.patch                   util-linux-2.13-mkswap-mounted.patch        util-linux-login.pamd
util-linux-2.13-audit-hwclock.patch          util-linux-2.13-mkswap-selinux.patch        util-linux-remote.pamd
util-linux-2.13-audit-login.patch            util-linux-2.13-mkswap-v0.patch
util-linux-2.13-blockdev-man.patch           util-linux-2.13-moretc.patch
[[email protected] ~]# ls /usr/src/redhat/SOURCES/util-linux-2.13*tar*
util-linux-2.13-pre7.tar.bz2
继续解压文件
[[email protected] ~]# tar xf /usr/src/redhat/SOURCES/util-linux-2.13-pre7.tar.bz2
解压后的文件夹为util-linux-2.13-pre7。

[[email protected] ~]# ls
anaconda-ks.cfg                                          fuse-libs-2.7.4-8_12.el5.x86_64.rpm  reload.sh            util-linux-2.13-0.59.el5.src.rpm
boot.gz                                                  initrd-2.6.18-308.el5.img.gz         screen-4.0.3         util-linux-2.13-0.59.el5.x86_64.rpm
copylib.sh                                               install.log                          screen-4.0.3.tar.gz  util-linux-2.13-pre7
Desktop                                                  install.log.syslog                   sysroot3.gz          vim74
fuse-2.7.4                                               iso                                  sysroot4.gz          vim-7.4.tar.bz2
fuse-2.7.4-8_12.el5.x86_64.rpm                           linux-2.6.28.10.tar.gz               sysroot6.gz
fuse-2.7.4.tar.gz                                        ntfs-3g-2009.4.4                     sysroot.gz
fuse-kmdl-2.6.18-308.24.1.el5-2.7.4-8_12.el5.x86_64.rpm  ntfs-3g-2009.4.4.gz                  test
[[email protected] ~]#


其中login的源码的目录为/usr/src/redhat/SOURCES/util-linux-2.13-pre7/login-utils
login-utils文件中不存在make文件,因此,需要自己编写编译命令来生成login。

[[email protected] util-linux-2.13-pre7]# ls
ABOUT-NLS   config       configure.ac  example.files  hwclock  lib          m4           misc-utils  partx   schedutils
aclocal.m4  config.h.in  COPYING       fdisk          include  licenses     Makefile.am  mount       po      sys-utils
AUTHORS     configure    disk-utils    getopt         INSTALL  login-utils  Makefile.in  NEWS        README  text-utils
[[email protected] util-linux-2.13-pre7]# ls login-utils/
agetty.8    chsh.1      initctl.8  last.c       Makefile.in  newgrp.c                   selinux_utils.c  shutdown.c    ttymsg.h  wall.c
agetty.c    chsh.c      initctl.c  login.1      mesg.1       README.getty               selinux_utils.h  simpleinit.8  vigr.8
checktty.c  fastboot.8  islocal.c  login.c      mesg.c       README.modems-with-agetty  setpwnam.c       simpleinit.c  vipw.8
chfn.1      fasthalt.8  islocal.h  login.h      my_crypt.h   README.poeigl              setpwnam.h       simpleinit.h  vipw.c
chfn.c      halt.8      last.1     Makefile.am  newgrp.1     reboot.8                   shutdown.8       ttymsg.c      wall.1
[[email protected] util-linux-2.13-pre7]#


(4)修改login.c中的代码
把344行的passwd_req = 1;
更改为passwd_req = 0;
在文件中添加locale.h头文件
#include <locale.h>
(5)编译生成login
[[email protected] login-utils]# gcc -o login login.c ../lib/setproctitle.c checktty.c -Wall -lcrypt -I ../include/
(6)更新login
[[email protected] login-utils]# cp login /bin/login

重启,即可在文本模式中以root账户登录。




修改输入提示符

[[email protected] sysroot]# cat .bash_profile 
PS1=‘[\[email protected]\h \w]\$‘  (用户名、主机名、工作目录名的基名、#)
export PS1
[[email protected] sysroot]#

技术分享




查看本机硬件设备信息:
1、cat /proc/cpuinfo


2、lsusb


3、lspci


4、hal-device
 Hardware Abstract Layer


[[email protected] ~]# lsusb
Protocol spec without prior Class and Subclass spec at line 4297
Bus 001 Device 001: ID 0000:0000 
Bus 002 Device 001: ID 0000:0000 
Bus 002 Device 002: ID 0e0f:0003 VMware, Inc. Virtual Mouse
Bus 002 Device 003: ID 0e0f:0002 VMware, Inc. Virtual USB Hub
[[email protected] ~]# lspci
00:00.0 Host bridge: Intel Corporation 440BX/ZX/DX - 82443BX/ZX/DX Host bridge (rev 01)
00:01.0 PCI bridge: Intel Corporation 440BX/ZX/DX - 82443BX/ZX/DX AGP bridge (rev 01)
00:07.0 ISA bridge: Intel Corporation 82371AB/EB/MB PIIX4 ISA (rev 08)
00:07.1 IDE interface: Intel Corporation 82371AB/EB/MB PIIX4 IDE (rev 01)
00:07.3 Bridge: Intel Corporation 82371AB/EB/MB PIIX4 ACPI (rev 08)
00:07.7 System peripheral: VMware Virtual Machine Communication Interface (rev 10)
00:0f.0 VGA compatible controller: VMware SVGA II Adapter
00:10.0 SCSI storage controller: LSI Logic / Symbios Logic 53c1030 PCI-X Fusion-MPT Dual Ultra320 SCSI (rev 01)
00:11.0 PCI bridge: VMware PCI bridge (rev 02)
00:15.0 PCI bridge: VMware PCI Express Root Port (rev 01)
00:15.1 PCI bridge: VMware PCI Express Root Port (rev 01)
00:15.2 PCI bridge: VMware PCI Express Root Port (rev 01)
00:15.3 PCI bridge: VMware PCI Express Root Port (rev 01)
00:15.4 PCI bridge: VMware PCI Express Root Port (rev 01)
00:15.5 PCI bridge: VMware PCI Express Root Port (rev 01)
00:15.6 PCI bridge: VMware PCI Express Root Port (rev 01)
00:15.7 PCI bridge: VMware PCI Express Root Port (rev 01)
00:16.0 PCI bridge: VMware PCI Express Root Port (rev 01)
00:16.1 PCI bridge: VMware PCI Express Root Port (rev 01)
00:16.2 PCI bridge: VMware PCI Express Root Port (rev 01)
00:16.3 PCI bridge: VMware PCI Express Root Port (rev 01)
00:16.4 PCI bridge: VMware PCI Express Root Port (rev 01)
00:16.5 PCI bridge: VMware PCI Express Root Port (rev 01)
00:16.6 PCI bridge: VMware PCI Express Root Port (rev 01)
00:16.7 PCI bridge: VMware PCI Express Root Port (rev 01)
00:17.0 PCI bridge: VMware PCI Express Root Port (rev 01)
00:17.1 PCI bridge: VMware PCI Express Root Port (rev 01)
00:17.2 PCI bridge: VMware PCI Express Root Port (rev 01)
00:17.3 PCI bridge: VMware PCI Express Root Port (rev 01)
00:17.4 PCI bridge: VMware PCI Express Root Port (rev 01)
00:17.5 PCI bridge: VMware PCI Express Root Port (rev 01)
00:17.6 PCI bridge: VMware PCI Express Root Port (rev 01)
00:17.7 PCI bridge: VMware PCI Express Root Port (rev 01)
00:18.0 PCI bridge: VMware PCI Express Root Port (rev 01)
00:18.1 PCI bridge: VMware PCI Express Root Port (rev 01)
00:18.2 PCI bridge: VMware PCI Express Root Port (rev 01)
00:18.3 PCI bridge: VMware PCI Express Root Port (rev 01)
00:18.4 PCI bridge: VMware PCI Express Root Port (rev 01)
00:18.5 PCI bridge: VMware PCI Express Root Port (rev 01)
00:18.6 PCI bridge: VMware PCI Express Root Port (rev 01)
00:18.7 PCI bridge: VMware PCI Express Root Port (rev 01)
02:00.0 USB controller: VMware USB1.1 UHCI Controller
02:01.0 Ethernet controller: Intel Corporation 82545EM Gigabit Ethernet Controller (Copper) (rev 01)
02:02.0 Multimedia audio controller: Ensoniq ES1371 [AudioPCI-97] (rev 02)
02:03.0 USB controller: VMware USB2 EHCI Controller
02:05.0 SATA controller: VMware Device 07e0
[[email protected] ~]#


编译内核:
1、配置
make menuconfig
make gconfig
make kconfig
make oldconfig
make config

保存为.config

2、编译
make
make modules_install
make install

模块安装位置:/lib/modules/KERNEL_VERSION/ 


如何实现部分编译:
1、只编译某子目录下的相关代码:
make dir/


make arch/
make drivers/net/


2、只编译部分模块
make M=drivers/net/


3、只编译某一模块
make drivers/net/pcnet32.ko


4、将编译完成的结果放置于别的目录中
make O=/tmp/kernel


5、交叉编译
make ARCH=



www.busybox.net

busybox可以模拟大部分linux命令;kernel+busybox可以模拟linux

initrd也是一个linux,busybox也可以模拟initrd

rootfs


如何编译busybox:(静态编译)


[[email protected] ~]# cd busybox-1.20.2
[[email protected] busybox-1.20.2]# make menuconfig

技术分享

技术分享


技术分享



准备分区IDE:
/dev/hdb1: ext3  /boot
/dev/hdb2: ext3 /

[[email protected] ~]# 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
/dev/hda2              43        1101      500377+  83  Linux


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


Disk /dev/hdb 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
[[email protected] ~]# fdisk /dev/hdb
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): p


Disk /dev/hdb: 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/hdb1               1          42       19813+  83  Linux
/dev/hdb2              43        1101      500377+  83  Linux


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


Calling ioctl() to re-read partition table.
Syncing disks.
[[email protected] ~]# mke2fs -j /dev/hdb1
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 28 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[[email protected] ~]# mke2fs -j /dev/hdb2
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 28 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[[email protected] ~]#




busybox编译过程失败

内核头版本较低,缺少头文件,导致busybox编译失败

解决办法:将内核linux-2.6.38.5/include/mtd/ubi-user.h复制到/root/busybox-1.20.2/mtd/ 下


[[email protected] ~]# tar xf busybox-1.20.2.tar.bz2
[[email protected] ~]# cd busybox-1.20.2
[[email protected] busybox-1.20.2]# ls
applets     AUTHORS        coreutils    editors    init       LICENSE     Makefile.custom  modutils    README   shell      TODO_unicode
applets_sh  Config.in      debianutils  examples   INSTALL    loginutils  Makefile.flags   networking  runit    sysklogd   util-linux
arch        configs        docs         findutils  libbb      mailutils   Makefile.help    printutils  scripts  testsuite
archival    console-tools  e2fsprogs    include    libpwdgrp  Makefile    miscutils        procps      selinux  TODO
[[email protected] busybox-1.20.2]# cd include/
[[email protected] include]# ls
applet_metadata.h  ar.h          bb_e2fs_defs.h  dump.h     grp_.h         libbb.h             platform.h  rtc_.h     unicode.h    volume_id.h  xregex.h
applets.src.h      bb_archive.h  busybox.h       fix_u32.h  inet_common.h  liblzo_interface.h  pwd_.h      shadow_.h  usage.src.h  xatonum.h
[[email protected] include]# mkdir mtd
[[email protected] include]#  cp /usr/src/linux-2.6.38.5/include/mtd/ubi-user.h mtd/
[[email protected] include]# ls mtd/
ubi-user.h
[[email protected] include]# cd ..
[[email protected] busybox-1.20.2]# pwd
/root/busybox-1.20.2
[[email protected] busybox-1.20.2]#
[[email protected] busybox-1.20.2]#
[[email protected] busybox-1.20.2]# make install


[[email protected] busybox-1.20.2]# ls
applets     busybox                 Config.in      docs       include   libpwdgrp   Makefile.custom  networking  scripts    TODO
applets_sh  busybox.links           configs        e2fsprogs  init      LICENSE     Makefile.flags   printutils  selinux    TODO_unicode
arch        busybox_unstripped      console-tools  editors    _install  loginutils  Makefile.help    procps      shell      util-linux
archival    busybox_unstripped.map  coreutils      examples   INSTALL   mailutils   miscutils        README      sysklogd
AUTHORS     busybox_unstripped.out  debianutils    findutils  libbb     Makefile    modutils         runit       testsuite
[[email protected] busybox-1.20.2]# ls _install/  (生成_install文件,作为模板

bin  linuxrc  sbin  usr
[[email protected] busybox-1.20.2]#


[[email protected] busybox-1.20.2]# cp _install /tmp/busybox -a
[[email protected] busybox-1.20.2]# cd /tmp/busybox/
[[email protected] busybox]# ls
bin  linuxrc  sbin  usr
[[email protected] busybox]# rm -f linuxrc
[[email protected] busybox]# ll
total 24
drwxr-xr-x 2 root root 4096 Mar 20 09:39 bin
drwxr-xr-x 2 root root 4096 Mar 20 09:39 sbin
drwxr-xr-x 4 root root 4096 Mar 20 09:39 usr
[[email protected] busybox]# mkdir porc sys etc dev sysroot lib /modules -pv  (以busybox生成initrd)
mkdir: created directory `porc‘
mkdir: created directory `sys‘
mkdir: created directory `etc‘
mkdir: created directory `dev‘
mkdir: created directory `sysroot‘
mkdir: created directory `lib‘
mkdir: created directory `/modules‘  
[[email protected] busybox]# modinfo ext3   (为kernel访问rootfs ext3提供模块)

filename:       /lib/modules/2.6.18-308.el5/kernel/fs/ext3/ext3.ko
license:        GPL
description:    Second Extended Filesystem with journaling extensions
author:         Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts‘o and others
srcversion:     26DC008FC415305C5F65313
depends:        jbd
vermagic:       2.6.18-308.el5 SMP mod_unload gcc-4.1
module_sig: 883f3504f2324332d3142b3e704051112141c09f45fe672e4db062f41923995d307c2a22213ac59609f5474bd4a6ca7665da553738c019557064a811c2
[[email protected] busybox]# modinfo jbd
filename:       /lib/modules/2.6.18-308.el5/kernel/fs/jbd/jbd.ko
license:        GPL
srcversion:     11842879E04FE2392B988CC
depends:       
vermagic:       2.6.18-308.el5 SMP mod_unload gcc-4.1
module_sig: 883f3504f2324332d3142b3e704051112263609f7a68e7433c9b3b9465cadb5165e78eb997ad1309e22e1a42144eb69c67e660a3d2f4ba94e2f12f42

[[email protected] busybox]# cp /lib/modules/2.6.18-308.el5/kernel/fs/ext3/ext3.ko lib/modules/
cp: overwrite `lib/modules/ext3.ko‘? y
[[email protected] busybox]# cp /lib/modules/2.6.18-308.el5/kernel/fs/jbd/jbd.ko lib/modules/
cp: overwrite `lib/modules/jbd.ko‘? y
[[email protected] busybox]#
[[email protected] busybox]# ls lib/modules/
ext3.ko  jbd.ko




[[email protected] ~]# 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/hdb1 on /opt/boot type ext3 (rw)
/dev/hdb2 on /opt/sysroot type ext3 (rw)
[[email protected] ~]# grub-install --root-directory=/opt /dev/hdb
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 /opt/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/hdb
(hd2) /dev/sda
[[email protected] ~]# ls /opt/boot/
grub  vmlinuz

[[email protected] busybox]# mknod dev/console c 5 1
[[email protected] busybox]# mknod dev/null c 1 3

[[email protected] busybox]# cat init
#!/bin/sh


mount -t proc proc /proc
mount -t sysfs sysfs /sys


insmod /lib/modules/jbd.ko
insmod /lib/modules/ext3.ko


mdev -s
mount -t ext3 /dev/hda2  /mnt/sysroot
exec switch_root /mnt/sysroot /sbin/init

[[email protected] busybox]# find . | cpio -H newc --quiet -o | gzip -9 > /opt/boot/initrd.gz

[[email protected] busybox]# cp /boot/vmlinuz-2.6.18-308.el5 /opt/boot/vmlinuz
cp: overwrite `/opt/boot/vmlinuz‘? y



[[email protected] busybox]# cat /opt/boot/grub/grub.conf
default=0
timeout=3
title busybox linux 2.6.18
 root (hd0,0)
 kernel /vmlinuz ro root=/dev/hda2
 initrd /initrd.gz
[[email protected] busybox]# cd
[[email protected] ~]# cd busybox-1.20.2
[[email protected] busybox-1.20.2]# cp _install/* /opt/sysroot/ -a
[[email protected] busybox-1.20.2]# cd /opt/sysroot/
[[email protected] sysroot]# ls
bin  linuxrc  lost+found  sbin  usr
[[email protected] sysroot]# rm -rf linuxrc
[[email protected] sysroot]# mkdir -pv proc sys dev tmp var/{log,lock,run} lib/modules root boot mnt media etc/rc.d/init.d
mkdir: created directory `proc‘
mkdir: created directory `sys‘
mkdir: created directory `dev‘
mkdir: created directory `tmp‘
mkdir: created directory `var‘
mkdir: created directory `var/log‘
mkdir: created directory `var/lock‘
mkdir: created directory `var/run‘
mkdir: created directory `lib‘
mkdir: created directory `lib/modules‘
mkdir: created directory `root‘
mkdir: created directory `boot‘
mkdir: created directory `mnt‘
mkdir: created directory `media‘
mkdir: created directory `etc‘
mkdir: created directory `etc/rc.d‘
mkdir: created directory `etc/rc.d/init.d‘
[[email protected] sysroot]# ls sbin/
acpid     bootchartd  findfs       halt      ifenslave  loadkmap  man        mkfs.minix  nameif       rmmod       start-stop-daemon  sysctl   watchdog
adjtimex  depmod      freeramdisk  hdparm    ifup       logread   mdev       mkfs.vfat   pivot_root   route       sulogin            syslogd  zcip
arp       devmem      fsck         hwclock   init       losetup   mkdosfs    mkswap      poweroff     runlevel    swapoff            tunctl
blkid     fbsplash    fsck.minix   ifconfig  insmod     lsmod     mke2fs     modinfo     raidautorun  setconsole  swapon             udhcpc
blockdev  fdisk       getty        ifdown    klogd      makedevs  mkfs.ext2  modprobe    reboot       slattach    switch_root        vconfig
[[email protected] sysroot]#


[[email protected] sysroot]# vim etc/inittab
[[email protected] sysroot]# cat etc/inittab
::sysinit:/etc/rc.d/rc.sysinit
console::respawn:-/bin/sh
::ctrlaltdel:/sbin/reboot
::shutdown:/bin/umount -a -r
[[email protected] sysroot]# vim etc/fstab
[[email protected] sysroot]# cat etc/fstab
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
/dev/hda1               /boot                   ext3    defaults        0 0
/dev/hda2               /                       ext3    defaults        1 1


[[email protected] sysroot]# mknod dev/console c 5 1
[[email protected] sysroot]# mknod dev/null c 1 3


[[email protected] sysroot]# vim etc/rc.d/rc.sysinit
[[email protected] sysroot]# cat etc/rc.d/rc.sysinit
#!/bin/sh
echo -e "\tWelcome to  \033[31mbusybox \033[0m Linux"


echo -e "Remounting the root filesystem ..."
mount -t proc proc /proc
mount -t sysfs sysfs /sys
mount -o  remount,rw  /


echo -e "Creating the files of device ..."
mdev -s


echo -e "Mounting the filesystem ..."
mount -a
swapon -a


echo -e "Starting the log daemon ..."
syslogd
klogd


echo -e "Configuring loopback interface ..."
ifconfig  lo  127.0.0.1/24
[[email protected] sysroot]# chmod +x etc/rc.d/rc.sysinit
[[email protected] sysroot]#

技术分享


以上是关于linux-裁剪Linux功能,编译/bin/login, busybox编译linux的主要内容,如果未能解决你的问题,请参考以下文章

Linux内核编译及系统裁剪之一

Linux 裁剪之Little linux 搭建实录

Linux 系统裁剪笔记 软盘2

Linux 内核裁剪框架初探

Linux 内核裁剪框架初探

Linux 内核裁剪框架初探