一、编译一个简单的linux步骤如下:

# yum groupinstall ""Development Tools" "Server Platform Development" -y    //安装开发包组
# tar xf linux-3.10.65.tar.xz  //下载内核这里就不介绍了
# cd /usr/src
# ln -sv linux-3.10.15 linux  
# cd linux

# make help
# make allnoconfig
# make menuconfig
这里简单介绍下这次编译到的选项:
内核核心模块相关的:
[*]64-bit kernel  //内核编译成64位的
[*]enable loadable module support --> [*]Module uploading   //允许装卸载模块,这里都是编译进内核的,不借助ramdisk装载模块,先选中吧
   Processor type and features  --->  [*]Symmetric multi-processing support   //支持多处理器,linux上多处理器和多核心是一个意思
                                      Processor family (Generic-x86-64)  ---> (X) Generic-x86-64  //选通用的就可以     
   Bus options (PCI etc.)  --->  [*] PCI support    //PCI相关的
   Device Drivers  --->   SCSI device support  ----> [*]SCSI device support  //SCSI设备的支持
                                                     [*]SCSI disk support  //SCSI硬盘支持
                          [*] Fusion MPT device support  --->  [*] Fusion MPT ScsiHost drivers for SPI  //lspci命令可以查看到本机pci的信息    
                                                               (*) Fusion MPT misc device (ioctl) drivers 
文件系统相关的:
File systems  ---> <*> Second extended fs support    
                   <*> Ext3 journalling file system support                                     
                             [*]   Default to 'data=ordered' in ext3 (NEW)                               
                             [*]   Ext3 extended attributes (NEW)  
                             <*> The Extended 4 (ext4) filesystem         

Executable file formats / Emulations  ---> [*] Kernel support for ELF binaries
                                           [*] Write ELF core dumps with partial segments (NEW)
                                           <*> Kernel support for scripts starting with #!

键盘鼠标usb相关的

Device Drivers  ---> Input device support  --->  [*]Keyboards  ---> 
                                                 [*]   Mice  ---> 
                                                 <*>   Mouse interface  
                     [*] USB support  ---> <*>   Support for Host-side USB 
                                                 <*>     xHCI HCD (USB 3.0) support
                                                 <*>     EHCI HCD (USB 2.0) support    
                                                 <*>     OHCI HCD support 
                                                 <*>     UHCI HCD (most Intel and VIA) Support

udev相关的:

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
Device Drivers --> Generic Driver Options --> Mintain a devtmpfs filesystem to mount at /dev  //识别到的设备都挂载到/dev目录
                           [*]   Automount devtmpfs at /dev, after the kernel mounted the rootfs  
# make -j 4 bzImage //只编译内核文件
# fdisk /dev/sdb  //在现有系统上添加一块盘,分两个区,一个用来做boot,一个用来做根目录

# mke2fs -t ext4 /dev/sdb1
# mke2fs -t ext4 /dev/sdb2
# mkdir /mnt/{boot,sysroot}
# mkdir -pv etc dev proc sys bin usr/{lib,bin,sbin,lin64} lib64 lib/modules home var/{log,run,lock} tmp mnt media root    # mount /dev/sdb1 /mnt/boot/ # mount /dev/sdb2 /mnt/sysroot
# grub-install --root-directory=/mnt /dev/sdb    
# cd /usr/src/linux
# cp arch/x86/boot/bzImage /mnt/boot/bzImage
# file /mnt/boot/bzImage
# vim /mnt/boot/grub/grub.conf
    default=0
    timeout=5
    title Mini Linux (3.10.67)
    root (hd0,0)
    kernel /bzImage ro root=/dev/sda2 init=/bin/bash
写一个脚本bincp复制命令及依赖库到sysroot下
    #!/bin/bash
    #
    target=/mnt/sysroot
    [ -d $target ] || mkdir /mnt/sysroot

    read -p "A command:" command

    libcp(){
      for lib in $(ldd $1 | grep -o "[^[:space:]]*/lib[^[:space:]]*"); do
        libdir=$(dirname $lib)
        [ -d $target$libdir ] || mkdir -p $target$libdir
        [ -f $target$lib ] || cp $lib $target$lib
      done
    }

    while [ "$command" != 'q' -a "$command" != 'quit' ]; do
      if !which $command &> /dev/null;then
        read -p "No such command,enter again:" command
        continue
      fi
      command=$(which --skip-alias $command)
      cmnddir=$(dirname $command)

       [ -d $target$cmnddir ] || mkdir -p $target$cmnddir
       [ -f $target$command ] || cp $command $target$command
        libcp $command
        read -p "Another command(quit):" command
      done

 

新建虚拟机Mini Linux使用上面的sdb硬盘
重启Mini Linux,可以在bash命令行进行一些命令的操作,比如cd,ls,mount等

也可以新建init程序让系统开机运行

新建脚本init: /mnt/sysroot/sbin/init
                #!/bin/bash
                #
                echo -e "\twelcome to \033[32mMini\033[0m linux"
                mount -n -t proc proc /proc
                mount -n -t sysfs /sys
                mount -n -o remount,rw /dev/sda2 /
                /bin/bash
        # chmod +x /mnt/sysroot/sbin/init

二、结合busybox+dropbear制作linux

静态编译busybox:

下载busybox:https://busybox.net/downloads/ 

# wget https://busybox.net/downloads/
# tar xf busybox-1.22.1.tar.bz2 # cd busybox-1.22.1 # yum install glibc-static //静态编译busybox依赖到的库
# make menuconfig  //配置下面两项,其他默认就可以了
    Busybox Settings ---> Build Options ---> [*] Build BusyBox as a static binary (no shared libs)   
    Installation Options ("make install" behavior) ---> What kind of applet links to install (as soft-links) ---> (X) as soft-links //安装完成后文件链接类型
                                    (./_install) BusyBox installation prefix (NEW) //安装完成在什么位置,默认当前编译路径下,默认就好
# make && make install  //安装busybox
# cd /mnt/sysroot
# rm -rf ./*    //删掉之前在sysroot中创建的目录,busybox会自动创建
# cd busybox-1.22.1  
# tree _install  //可以看一下busybox安装后生成了哪些文件
# cp -a _install/* /mnt/sysroot/  //复制_install中所有文件到sysroot中
# cd /mnt/sysroot
# rm linuxrc  //删掉busybox模拟的init程序,"ls /sbin | grep init"中的init就够用了 
修改grub配置文件:
default=0
timeout=3
title Mini Linux (3.10.67)
        root (hd0,0)
        kernel /bzImage ro root=/dev/sda2 init=/sbin/init  //修改init就好
# mkdir -pv etc lib lib64 proc sys dev root home boot mnt media tmp var  //在sysroot下创建其他缺少的目录

  busybox默认模拟的是CentOS5的init程序,它会去读取inittab文件,所以我们创建一个/sysroot/etc/inittab,内容如下:

# vim /sysroot/etc/inittab
  ::sysinit:/etc/rc.d/rc.sysinit  //完成系统初始化 tty1::askfirst:/bin/sh        //启动几个终端
tty2::askfirst:/bin/sh
tty3::askfirst:/bin/sh
tty4::askfirst:/bin/sh   ::ctrlaltdel:/sbin/reboot  //ctrl+alt+del组合键就重启   ::shutdown:/bin/umount -a -r  //关机前卸载所有文件系统

接下来创建初始化脚本:/sysroot/etc/rc.d/rc.sysinit

  #!/bin/sh
  #
  echo -e "\tWelcome to \033[32mMini\033[0m Linux"
  mount -t proc porc /proc    //如果不能挂载,换成绝对路径
  mount -t sysfs sysfs /sys  

  echo "scan /sys and to populatte to /dev..."
  mdev -s            //系统启动时自动输出所有文件到/dev目录下

  mount -o remount,rw /dev/sda2 /

  echo "mouting all filesystems.."
  mount -a          //挂载剩余的所有文件系统

# chmod +x etc/rc.d/rc.sysinit 

 将网络相关功能编译进内核:

# cd /usr/src/linux
# make menuconfig

  -*- Networking support ---> Networking options ---> [*] TCP/IP networking
                                  [*] IP: multicasting
                                  [*] IP: advanced router
                                  [*] IP: kernel level autoconfiguration
  Device Drivers --->[*] Network device support ---> [*] Ethernet driver support (NEW) --->[*] Intel devices (NEW)
                                                         <*> Intel(R) PRO/1000 Gigabit Ethernet support

# make  bzImage
# cp arch/x86/boot/bzImage /mnt/boot

重启Mini Linux测试网络功能

# ifconfig -a    //查看网卡
# ifconfig eth0 192.168.2.20 up
# ifconfig lo 127.0.0.1 up
# ping 192.168.2.1  

 假设把上面的网络功能编译为模块

Device Drivers --->[*] Network device support ---> [*] Ethernet driver support (NEW) --->  <M> Intel(R) PRO/1000 Gigabit Ethernet support 
# cp arch/x86/boot/bzImage /mnt/boot
# sync  

 编译网卡e1000,将.ko文件复制到/mnt/sysroot/lib/modules下

# cd /usr/src/linux
# make M=drivers/net/ethernet/intel/e1000
# mkdir /mnt/sysroot/lib/modules -pv
# cp drivers/net/ethernet/intel/e1000/e1000.ko /mnt/sysroot/lib/modules/

重启Mini Linux,装载进e1000网卡模块

# insmod /lib/modules/e1000.ko

设置系统初始化的时候加载网卡模块,并初始化

# vim /etc/rc.d/rc.sysinit
    echo "Load driver for e1000..."     insmod /lib/modules/e1000.ko     echo "Initializing ethernet card..."     ifconfig eth0 192.168.2.20 up     ifconfig lo 127.0.0.1 up     [ -f /etc/sysconfig/network ] && . /etc/sysconfig/network     [ -z "$HOSTNAME" -o "$HOSTNAME" == '(none)' ] && HOSTNAME='lcoalhost'      hostname $HOSTNAME 

设置主机名

# mkdir etc/sysconfig
# vim etc/sysconfig/network
    HOSTNAME=mini.alen.com

给Mini Linux添加账号和密码

# vim etc/passwd
  root:x:0:0::/root:/bin/bash
# vim etc/group
  root:x:0:
# vim etc/shadow
或者直接追加宿主机上的:# head -1 /etc/shadow > etc/shadow
# chmod 400 etc/shadow

sha密码可能不支持,换成md5的吧

# openssl passwd -1 -salt $(openssl rand -hex 4)

添加模拟终端:

修改etc/initab文件:

# vim etc/insttab

    ::sysinit:/etc/rc.d/rc.sysinit
    ::respawn:/sbin/getty 9600 tty1  //修改这几个模拟终端,getty开机时会自动调用login程序
    ::respawn:/sbin/getty 9600 tty2  //
    ::respawn:/sbin/getty 9600 tty3  //
    ::ctrlaltdel:/sbin/reboot
    ::shutdown:/bin/umount -a -r

添加issue文件,修改终端显示信息

# vim etc/issue
     Welcome to alen Linux
     Kernel \r        

添加ssh服务 

下载dropbear源码:https://matt.ucc.asn.au/dropbear/releases/dropbear-2019.78.tar.bz2

默认编译安装就可以了

复制dropbear命令到sysroot下

# bash bincp.sh 
        dropbear,dropbearkey,dbclient  //添加这几个就可以了

添加存放远程终端文件的目录dev/pts

# mkdir dev/pts
# vim etc/fstab
devpts /dev/pts    devpts  mode=620  0 0   //挂载pts

 为dropbear生成密钥文件

# mkdir etc/dropbear
# cd etc/dropbear
# dropbearkey -t rsa -s 2048 -f dropbear_rsa_host_key
# dropbearkey -t dss -f dropbear_dss_host_key

添加安全shell

# vim etc/shells
    比如: /bin/sh /bin/ash /bin/hush /bin/bash /sbin/nologin

添加nsswitch服务:网络服务转换

# vim etc/nsswitch.conf
      passwd: files
      group: files
      shadow: files
      hosts: files dns

复制nsswitch的库文件到sysroot下

# mkdir usr/lib64
# cp -d /lib64/libnss_files* lib64/  
# cp -d /usr/lib64/libnss3.so usr/lib64/
# cp -d /usr/lib64/libnssutil3.so usr/lib64/
# cp -d /usr/lib64/libnss_files* usr/lib64/

重启Mini Linux,测试

# vim .bash_profile
设置PS1环境变量   export PS1='[\u@\h \w]\$'
  export PATH=$PATH:/usr/local/bin:/usr/local/sbin  
添加dropbear的pid存放路径
# mkdir /var/run
# dropbear -F -E  //让dropbear工作于前台
# mkdir /dev/pts
# mount -t devpts devpts /dev/pts

# vi /etc/profile
[ $UID -eq 0 ] && export PATH=/bin:/sbin:/usr/bin:/usr/sbin

至此,一个迷你型的linux就编译好了

 

 

 

 

 

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄