简介
BusyBox 不多解释了
网上有大量教程来讲解busybox的安装和使用,要么有的太深奥,要么有的古老,参照网上的教程来做,总是编译不成功,或是安装失败,本人是做运维工作的,有些代码层次的东西,没必要了解的太深奥。所以借鉴了一些大牛的文章,和自己的亲手实验,成功的完成了物理机通过pxe启动(网络启动)进入自己定制的无盘环境中,在这个环境下可以做很多事情,例如排查硬件故障,拷贝数据,安装系统等
大家按照我的步骤来,只会成功,不会失败。我写的这篇文章,简单明了,如果想深入了解,请自行在网上查询
安装环境和测试环境
centos7.4、 VMware Workstation 14 Pro
安装过程
1、安装必要的软件
yum -y install gcc glibc-static ncurses-devel libgcrypt-devel zlib-devel
2、下载busybox安装包
mkdir /tmp/box && cd /tmp/box
wget https://busybox.net/downloads/busybox-1.27.2.tar.bz2
3、解压-配置-安装
tar -jxvf busybox-1.27.2.tar.bz2 && cd busybox-1.27.2/
make menuconfig
以下一项要选择上,其它默认即可
* Build BusyBox as a static binary (no shared libs)
make && make install
4、创建pxe系统的必要文件
1)创建系统必须的文件,并把busybox拷过来
mkdir -p /tmp/myOS/rootfs && cd /tmp/myOS/rootfs
mkdir etc usr var tmp proc home root dev lib lib64 sys
mkdir /tmp/myOS/rootfs/dev/shm
mkdir /tmp/myOS/rootfs/dev/pts
mkdir /tmp/myOS/rootfs/var/run
mkdir /tmp/myOS/rootfs/usr/lib64
cp -R /dev/{console,null,zero,raw,tty0,tty1,tty2,fb0,tty} /tmp/myOS/rootfs/dev/
cd /tmp/myOS/rootfs/dev/
mknod ram1 b 1 1
mknod ram0 b 1 0
mknod raw1 c 162 1
mknod hda b 3 0
mknod fd0 b 2 0
mknod loop0 b 7 0
mknod loop1 b 7 1
cp -R /tmp/box/busybox-1.27.2/_install/* /tmp/myOS/rootfs/
cp -R /tmp/box/busybox-1.27.2/examples/bootfloppy/etc/* /tmp/myOS/rootfs/etc
cp -R /etc/ld.so.conf* /tmp/myOS/rootfs/etc/
cp /etc/nsswitch.conf /tmp/myOS/rootfs/etc/
cp /etc/shells /tmp/myOS/rootfs/etc/
2)创建busybox开机启动脚本和登录脚本
rm -f /tmp/myOS/rootfs/etc/bashrc
rm -f /tmp/myOS/rootfs/etc/profile
cp /etc/bashrc /tmp/myOS/rootfs/etc/
cp /etc/profile /tmp/myOS/rootfs/etc/
cp /root/.bash_profile /tmp/myOS/rootfs/root/
cp /root/.bashrc /tmp/myOS/rootfs/root/
3)创建dhcpc服务需要的文件
mkdir -p /tmp/myOS/rootfs/usr/share/udhcpc/
cp /tmp/box/busybox-1.27.2/examples/udhcp/simple.script /tmp/myOS/rootfs/usr/share/udhcpc/default.script
chmod 775 /tmp/myOS/rootfs/usr/share/udhcpc/default.script
4)添加驱动
cp -R /lib/modules /tmp/myOS/rootfs/lib/
cp -R /lib/firmware/ /tmp/myOS/rootfs/lib/
cp -d /lib64/libnss* /tmp/myOS/rootfs/lib64/
cp -d /usr/lib64/libnss* /tmp/myOS/rootfs/usr/lib64/
5、添加额外的工具
使用脚本添加bash、curl、lspci、ldconfig等必要工具,脚本内容如下
[root@test01 rootfs]# cat /tmp/a.sh
#!/bin/bash
DEST_PATH=/tmp/myOS/rootfs
acp() {
CPATH=`echo $1 | grep -o "/.*/"`
[ ! -d $DEST_PATH$CPATH ] && mkdir -p $DEST_PATH$CPATH
[ ! -e $DEST_PATH$1 ] && cp $1 $DEST_PATH$CPATH
}
bin_cp() {
for i in `ldd $1 | grep -o "/[^[:space:]]*"`;
do acp $i
done
echo "lib copy complete"
acp $1 && echo "bin $1 copy complete"
}
read -p "please input your commad: " CMD
until [ $CMD == 'q' ] ;do
! which $CMD && read -p "commad err,please input again" CMD && continue
BIN_COMMAND=`which $CMD | grep -v "^alias" | grep -o "/.*"`
bin_cp $BIN_COMMAND
read -p "please input your commad" CMD
done
添加完脚本之后,需要将busybox的bash替换掉
cd /tmp/myOS/rootfs/bin
rm -f sh && cp ../usr/bin/bash bash && cp -s bash sh
cd /tmp/myOS/rootfs/usr/bin && cp -s ../../bin/busybox hostname
cd /tmp/myOS/rootfs
mv linuxrc init
6、修改必要的配置文件
[root@test01 rootfs]# cat /tmp/myOS/rootfs/etc/fstab
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
[root@test01 etc]# cat /tmp/myOS/rootfs/etc/init.d/rcS
#! /bin/sh
/bin/mount -a
/bin/sh /etc/boot.sh
[root@test01 rootfs]# cat /tmp/myOS/rootfs/etc/boot.sh
#!/bin/sh
function get_mod_name()
{
for mod_num in $(lspci -nn |egrep -o '\[.........\]' |tr -d '[|]' |sed s/:/.*/g)
do
grep -i "$mod_num" /lib/modules/$(uname -r)/modules.alias |awk '{print $NF}'
done
return 0
}
function insert_mod_name()
{
for mod_name in $(echo "$(get_mod_name)"|sort -u)
do
echo "modprobe $mod_name"
modprobe $mod_name
sleep 1
done
return 0
}
function insert_other_mod()
{
echo "modprobe sd_mod"
modprobe sd_mod
sleep 1
return 0
}
function set_hostname()
{
hostname PXE
return 0
}
function set_net()
{
udhcpc eth0
sleep 3
return 0
}
function set_dropbear()
{
mkdir /etc/dropbear
/usr/bin/dropbearkey -t rsa -f /etc/dropbear/dropbear_rsa_host_key
/usr/bin/dropbearkey -t dss -f /etc/dropbear/dropbear_dss_host_key
/usr/bin/dropbearkey -t ecdsa -f /etc/dropbear/dropbear_ecdsa_host_key
#/usr/bin/dropbear
return 0
}
function set_mdev()
{
echo /sbin/mdev > /proc/sys/kernel/hotplug
mdev -s
ldconfig
return 0
}
function main()
{
insert_mod_name
insert_other_mod
set_hostname
set_mdev
set_net
set_dropbear
return 0
}
#------------------
main
[root@test01 etc]# cat /tmp/myOS/rootfs/etc/inittab
::sysinit:/etc/init.d/rcS
tty1::respawn:/root/autologin.sh tty1
tty2::askfirst:-/bin/sh
::ctrlaltdel:/sbin/reboot
::shutdown:/bin/umount -a -r
[root@test01 rootfs]# cat /tmp/myOS/rootfs/root/autologin.sh
#!/bin/sh
/bin/login -f root
[root@test01 rootfs]# chmod 775 /tmp/myOS/rootfs/root/autologin.sh
[root@test01 rootfs]# cat /tmp/myOS/rootfs/etc/ld.so.conf
include ld.so.conf.d/*.conf
/lib
/lib64
/usr/lib
/usr/lib64
[root@test01 rootfs]# cat /tmp/myOS/rootfs/etc/passwd
root:x:0:0:root:/root:/bin/bash
[root@test rootfs]# cat /tmp/myOS/rootfs/etc/group
root:x:0:
[root@test rootfs]# cat /tmp/myOS/rootfs/etc/shadow
root:UO4IOJCCO.E6.:17538:0:99999:7:::
[root@test01 rootfs]# cat /tmp/myOS/rootfs/root/.bashrc
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias ll='ls -l'
alias vim='vi'
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
export PATH=$PATH:$HOME:/sbin
#exportPS1="[$USER@\h \W]\\$ "
/bin/sh /root/login.sh
[root@test01 rootfs]# cat /tmp/myOS/rootfs/root/login.sh
#!/bin/sh
echo "start login.sh.........."
[root@test01 rootfs]# chmod 775 /tmp/myOS/rootfs/root/login.sh
7、向busybox里安装dropbear
mkdir /tmp/dropbear && cd /tmp/dropbear
wget https://matt.ucc.asn.au/dropbear/releases/dropbear-2017.75.tar.bz2
tar -jxvf dropbear-2017.75.tar.bz2 &&cd dropbear-2017.75/
./configure --prefix=/tmp/myOS/rootfs/usr
make && make install
将dropbear的依赖拷贝过去,还要执行a.sh脚本
bash a.sh /tmp/myOS/rootfs/usr/sbin/dropbear
8、打包成initrd.img
cd /tmp/myOS/rootfs
find . | cpio -c -o | xz -9 --format=lzma > /var/www/html/tftpboot/initrd.img
cp /boot/vmlinuz-3.10.0-693.el7.x86_64 /var/www/html/tftpboot/vmlinuz
[root@test01 ~]# cat /var/www/html/tftpboot/pxelinux.cfg/default
default vesamenu.c32
#default 1
#prompt 1
timeout 100
#display boot.msg
menu background splash.jpg
menu title Welcome to OS INSTALL!
menu color border 0 #ffffffff #00000000
menu color sel 7 #ffffffff #ff000000
menu color title 0 #ffffffff #00000000
menu color tabmsg 0 #ffffffff #00000000
menu color unsel 0 #ffffffff #00000000
menu color hotsel 0 #ff000000 #ffffffff
menu color hotkey 7 #ffffffff #ff000000
menu color scrollbar 0 #ffffffff #00000000
label 0
menu label Boot from ^local drive
localboot 0xffff
label 1
menu label Auto Install
menu default
kernel http://10.0.0.50/tftpboot/vmlinuz
append initrd=http://10.0.0.50/tftpboot/initrd.img
ipappend 1
9、验证成功
ssh登录