BusyBox DIY Mini Linux

Source: Internet
Author: User
Tags dropbear

System: CentOS 5.8 64bit

Software version: busybox-1.21.1.tar.bz2

dropbear-2016.73.tar.bz2

Reference: http://os.51cto.com/art/201403/431943.htm


1. Compiling busybox-1.21.1.tar.bz2


Make Menuconfig

(Busybox settings-->build options-->build Busybox as a static binary (no shared libs)

Choose this static compilation, remove build with Large File support (for accessing files > 2 GB)


Make && make install


2. Making INITRD Files


mkdir INITRD

CP busybox-1.21.1/_install/* initrd/-RP

CD initrd/

MKDIR-PV proc SYS etc/init.d TMP dev mnt/sysroot

Mknod dev/console C 5 1

Mknod dev/null C 1 3

RM LINUXRC


To edit the init file:


[[email protected] initrd]# cat > Init << EOF

> #!/bin/sh

> MOUNT-T proc Proc/proc

> Mount-t SYSFS Sysfs/sys

> Insmod/lib/modules/jbd.ko

> Insmod/lib/modules/ext3.ko

> Insmod/lib/modules/scsi_mod.ko

> Insmod/lib/modules/sd_mod.ko

> Insmod/lib/modules/scsi_transport_spi.ko

> Insmod/lib/modules/mptbase.ko

> Insmod/lib/modules/mptscsih.ko

> Insmod/lib/modules/mptspi.ko

> Mdev-s

> Mount-t ext3/dev/sda2/mnt/sysroot

> Exec Switch_root/mnt/sysroot/sbin/init

> EOF

chmod +x Init


Because there is no ext3 compiled into the kernel, so it is necessary to mount the Ext3 module, and ext3 depends on the JBD module, so you need to load Jbd.ko, specifically through Modinfo ext3 view,

Because I use a SCSI hard disk in the virtual machine, so also to load SCSI driver, Scsi_mod.ko, Sd_mod.ko and so on is the corresponding module, the module must be copied to the corresponding path, the process is as follows:


mkdir LIB/MODULES-PV

Modinfo ext3

Cp/lib/modules/2.6.18-308.el5/kernel/fs/ext3/ext3.ko Lib/modules

Cp/lib/modules/2.6.18-308.el5/kernel/fs/jbd/jbd.ko lib/modules/

Cp/lib/modules/2.6.18-308.el5/kernel/drivers/scsi/scsi_mod.ko lib/modules/

Cp/lib/modules/2.6.18-308.el5/kernel/drivers/scsi/sd_mod.ko lib/modules/

Cp/lib/modules/2.6.18-308.el5/kernel/drivers/scsi/scsi_transport_spi.ko lib/modules/

Cp/lib/modules/2.6.18-308.el5/kernel/drivers/message/fusion/mptbase.ko lib/modules/

Cp/lib/modules/2.6.18-308.el5/kernel/drivers/message/fusion/mptscsih.ko lib/modules/

Cp/lib/modules/2.6.18-308.el5/kernel/drivers/message/fusion/mptspi.ko lib/modules/


Find. | Cpio--quiet-h Newc-o | Gzip-9-n >/mnt/boot/initrd.gz


Kernel compilation is cumbersome, next time another one


3. Creating a root file system


CP busybox-1.21.1/_install/*/mnt/sysroot/-RP

Cd/mnt/sysroot

MKDIR-PV proc SYS etc/rc.d/init.d tmp dev/pts boot Var/log usr/lib

Mknod dev/console C 5 1

Mknod dev/null C 1 3

mkdir LIB/MODULES-PV

Cp/lib/modules/2.6.18-308.el5/kernel/drivers/net/pcnet32.ko lib/modules/

Cp/lib/modules/2.6.18-308.el5/kernel/drivers/net/mii.ko lib/modules/


4. Provide initialization scripts:


Cat >etc/rc.d/rc.sysinit <<eof

> echo-e "\twelcome to \033[31mlinzb ' s\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 ..."

> Insmod/lib/modules/mii.ko

> Insmod/lib/modules/pcnet32.ko

> Ifconfig lo 127.0.0.1

> ifconfig eth0 192.168.2.227

> EOF


chmod +x Etc/rc.d/rc.sysinit

Rm-f LINUXRC


5. Provide Inittab and fstab

[email protected] sysroot]# cat > Etc/inittab <<eof

>:: Sysinit:/etc/rc.d/rc.sysinit

> console::respawn:-/bin/sh

>:: Ctrlaltdel:/sbin/reboot

>:: Shutdown:/bin/umount-a-R

> EOF


[Email protected] sysroot]# VI etc/fstab


Sysfs/sys Sysfs Defaults 0 0

PROC/PROC proc Defaults 0 0

/dev/sda1/boot ext3 defaults 0 0

/DEV/SDA2/EXT3 Defaults 1 1


Note: When doing experiments with SCSI drives, even if you mount the Sdb,minilinux identification is also SDA, this is the conclusion of their own experiments,

You can try it a bit more.


6, because the log process is started in the Rc.sysinit file, the system will generate a large number of logs in the run and display it in the console;

Frequent interruptions in the work in progress, in order to avoid this situation, we set up a configuration file for the log process, which specifies that the log is sent

to/var/log/messages file;


# Vim Etc/syslog.conf


Add the following line:

*.info/var/log/messages


7. Installing GRUB

Grub-install--root-directory=/mnt//dev/sdb

Note: The SDB here is the hard disk where Mini Linux resides


8. Provide grub.conf and kernel

[Email protected] sysroot]# vim/mnt/boot/grub/grub.conf


Default 0

Timeout 1

Color Light-green/black Light-magenta/black

Title My mini Linux

Root (hd0,0)

Kernel/vmlinuz ro root=/dev/sda2 Quiet

Initrd/initrd.gz


Cp/boot/vmlinuz-2.6.18-308.el5/mnt/boot/vmlinuz



######################################################


9. Enable the Virtual Console for the newly built Minilinux


This can be done with a host, or you can directly start a small Linux configuration that has just been built successfully. Here we use the host (restart host):

# Cd/mnt/sysroot


Change the Etc/inittab file to the following:

:: Sysinit:/etc/init.d/rc.sysinit

Tty1::askfirst:/bin/sh

Tty2::askfirst:/bin/sh

Tty3::askfirst:/bin/sh

Tty4::askfirst:/bin/sh

Tty5::askfirst:/bin/sh

Tty6::askfirst:/bin/sh

:: Ctrlaltdel:/sbin/reboot

:: Shutdown:/bin/umount-a-R



10. Create a login user for mini Linux


[Email protected] sysroot]# VI etc/passwd

The content is: root:x:0:0:root:/root:/bin/sh (here to modify the shell, because BusyBox no bash, or to create a soft connection to SH for bash)


[Email protected] sysroot]# VI Etc/shadow

Content: Root:$1$gm6zfnou$zpndu0pk3qcsitham8kjp.:16909:0:99999:7::: (This can be generated manually depending on your needs)


[Email protected] sysroot]# VI etc/group

Content is: Root:x:0:root


Change the Etc/inittab file to the following:

:: Sysinit:/etc/init.d/rc.sysinit (Remember to put RC.D Ln-s to init.d here first)

Tty1::respawn:/sbin/getty 9600 tty1

Tty2::respawn:/sbin/getty 9600 Tty2

Tty3::respawn:/sbin/getty 9600 Tty3

Tty4::respawn:/sbin/getty 9600 Tty4

Tty5::respawn:/sbin/getty 9600 Tty5

Tty6::respawn:/sbin/getty 9600 Tty6

:: Shutdown:/bin/umount-a-R

:: Ctrlaltdel:/sbin/reboot



11. SSH Remote Connection service to the system via Dropbear


<1> Compile and install Dropbear

Tar XF dropbear-2016.73.tar.bz2

CD dropbear-2016.73

./configure

Make

Make install


<2> Transplant Dropbear


Which dropbear find where the command is located

LDD dropbear view command-dependent repositories, and CP executables and libraries to the target directory


You can write a script yourself CP is more convenient, the content is as follows: (for reference only)

#!/bin/bash

#

function Cp_command () {

Command= ' which $1|grep-v "^alias\>" '

lib= ' ldd $COMMAND |cut-d ' ('-f1 |grep-o ' \/.*\/.*so.* '

Rootdir=/mnt/sysroot

For I in $LIB

Do

[!-e $ROOTDIR ${i%/*}]&& mkdir-p $ROOTDIR/${i%/*}

CP $i $ROOTDIR ${i%/*}-F && echo "CP $i Success" | | Echo-e "\033[31mfailed CP!\033[0m"

Done

[!-e $ROOTDIR ${command%/*}]&& mkdir-p $ROOTDIR/${command%/*}

CP $COMMAND $ROOTDIR ${command%/*}-F && echo "CP $COMMAND Success" | | Echo-e "\033[31mfailed CP!\033[0m"

}

While:; Do

Read-p "Please input a command (q means quit):" A

[$a = = "Q"]&& exit 0

Which $a && Cp_command $a | | echo "Usage: ' basename" CMD; Q means Quit "

Done


Need to copy dropbear, Dropbearkey, and dbclient three commands


<3> provide pseudo-terminal device files for remote users


To edit/mnt/sysroot/etc/fstab, add the following line:

Devpts/dev/ptsdevptsmode=6200 0


<4> generate a host key for the dropbear of the target system


By default, dropbear to the/etc/dropbear directory to find the RSA Format Master key (the default name is Dropbear_rsa_host_key) and the DSS format master key (the default name is Dropbear_dss_host_ Key). The RSA format can use different lengths of keys, but the DSS format uses only 1024-bit keys.


Mkdir/mnt/sysroot/etc/dropbear

Dropbearkey-t rsa-f/mnt/sysroot/etc/dropbear/dropbear_rsa_host_key-s 2048

Dropbearkey-t rsa-f/mnt/sysroot/etc/dropbear/dropbear_dss_host_key


When generating a key in RSA format, its length specifies that part-s 2048 can be omitted, or it can be specified as a different length, but the length needs to be an integer multiple of 8.


Note: This step can also be done on the target host, but the path will be modified accordingly.


<5> Define a secure shell


For security reasons, Dropbear only allows users whose default shell appears in the/etc/shells file to Telnet, so you'll need to create a/etc/shells file here, and add all the allowed shells.


# cat >>/mnt/sysroot/etc/shells << EOF

/bin/sh

/bin/ash

/bin/hush

/bin/bash

Eof


<6> provides Network service transformation mechanism for target host


Dropbear compiled with the default options on the host computer will rely on Nsswitch for user name resolution, so you will also need to provide NSS-related library files and configuration files for the target host.


Cat >>/mnt/sysroot/etc/nsswitch.conf << EOF

Passwd:files

Shadow:files

Group:files

Hosts:files DNS

Eof


To copy the required library files:

cp-d/lib/libnss_files*/mnt/sysroot/lib/

Cp-d/usr/lib/libnss3.so/usr/lib/libnss_files.so/mnt/sysroot/usr/lib/



7. Testing


Start the target host, set the network properties, use the following command to start the Dropbear service.

#/usr/local/sbin/dropbear





Debug

BusyBox Compile:

Error: Miscutils/ubi_tools.c:67:26:error:mtd/ubi-user.h:no such file or directory

This is a new supported Ubi file system under Linux and needs to be added manually. From the include\mtd\ubi-user.h copy of the Linux kernel source package to the INCLUDE\MTD under BusyBox,

If the include in BusyBox does not have a MTD folder, first create a MTD folder and then copy


Dropbear can go to the login interface, but not even, check the messages log, error: Login attempt for nonexistent user from 192.168.2.106:50307

Workaround: Done, copy library replication is wrong, because my system is 64-bit, so copy the Nsswitch library should be:

cp-d/lib64/libnss_*/mnt/sysroot/lib64/

Cp-d/usr/lib64/libnss3.so/mnt/sysroot/usr/lib64/



BusyBox DIY Mini Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.