Production process:
KERNEL->INITRD (BusyBox)->rootfs (BusyBox)
Kernel: Direct use of/boot/vmlinuz-' uname-r ' Kernel mirroring or manual compilation generation
INITRD: Compile BusyBox, build initrd on busybox basis
Rootfs: Also on the basis of BusyBox to establish ROOTFS
First, partition the new hard disk on the small system and mount the corresponding directory on the host to create the modified file.
/DEV/HDA1 100M boot partition mounted on/mnt/boot
/dev/hda2 512M root partition mounted on/mnt/sysroot
Second, compile the kernel source code, provide a required kernel for the new system (assuming the source code package is located in the/USR/SRC directory)
① Direct use of/boot/vmlinuz-' uname-r ' kernel mirroring
cp/boot/vmlinuz-' Uname-r '/mnt/boot/bzimage
② manual compilation generates kernel image
# CD/USR/SRC
# tar JXVF linux-2.6.38.5.tar.bz2
# CD linux-2.6.38.5
# Make Menuconfig
According to the actual and planning to choose the functions required; This example plans to make a network-capable micro-Linux and does not intend to use kernel modules, so here choose the corresponding network card driver directly compiled into the kernel. Because the VMware Workstation virtual machines are used, the required online drivers are pcnet32, and others can be selected as needed. After the selection is completed, it needs to be saved to the. config file in the current directory.
# Make subdir=arch/
# CP Arch/x86/boot/bzimage/mnt/boot
Note: In order to implement the following functions, be sure to compile the file system ext3 and network card driver directly into the kernel, otherwise, you need to manually load the modules of the relevant file system;
Third, compiling BusyBox
(assuming the BusyBox source code is in the/USR/SRC directory)
# CD/USR/SRC
# TAR-JXVF BUSYBOX-1.20.2.TAR.BZ2
# CD busybox-1.20.2
# mkdir INCLUDE/MTD
# Cp/usr/src/linux-2.6.38.5/include/mtd/ubi-user.h Include/mtd/
# Make Menuconfig
Description
1. Here you need to choose Busybox Settings---build options--build Busybox as a static binary (no shared libs), so you can compile Busybox into a Do not use the shared library's static binaries, which avoids the dependency on the host's shared library, but you can also not select this option, and then complete the compilation of its dependent shared library to the/lib directory on the target system, the latter method is used.
2, modify the installation location by: Busybox Settings---Installation Options---/_install Busybox installation prefix. The installed files are located in the selected installation location directory;
# make Install
A copy of the BusyBox to/mnt/sysroot, which was just installed under/usr/src/busybox-1.20.2/_install, is required for separate production of INITRD and Rootfs.
# cp/usr/src/busybox-1.20.2/_install/*/mnt/sysroot-a
Iv. Production of INITRD
# Cd/usr/src/busybox-1.20.2/_install
1, the establishment of INITRD in the Rootfs:
# MKDIR-PV proc SYS etc/init.d TMP dev mnt/sysroot
2. Create two necessary equipment files:
# Mknod Dev/console C 5 1
# Mknod Dev/null C 1 3
3, for INITRD to make the INIT program, the main task of this program is to implement the ROOTFS switch, so, can be scripted to achieve:
# RM LINUXRC
# VIM Init
Add the following content:
#!/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
To execute permissions on this script:
chmod +x Init
4. Making INITRD
# Find. | Cpio--quiet-h Newc-o | Gzip-9-n >/mnt/boot/initrd.gz
V. Create the required bootstrapper for the small system
# Grub-install--root-directory=/mnt/dev/hda
Note: The/dev/hda here is the new disk on which the target system resides;
Next, create a configuration file for grub:
# vim/mnt/boot/grub/grub.conf
Add something like the following:
Default 0
Timeout 3
Title Cqy ' Linux (2.6.38.5)
Root (hd0,0)
Kernel/bzimage ro root=/dev/hda2 Quiet
Initrd/initrd.gz
Vi. creating a true root file system
# Cd/mnt/sysroot
1, establish ROOTFS:
# MKDIR-PV proc SYS etc/rc.d/init.d tmp dev/pts boot Var/log usr/lib
2. Create two necessary equipment files:
# Mknod Dev/console C 5 1
# Mknod Dev/null C 1 3
3. Set up the system initialization script file
# Vim Etc/rc.d/rc.sysinit
Add the following content:
#!/bin/sh
Echo-e "\twelcome to \033[31mcqy ' 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 ..."
Ifconfig Lo 127.0.0.1/24
Ifconfig eth0 192.168.0.111/24
# END
Then let the script have execute permissions:
chmod +x Etc/init.d/rc.sysinit
4. Configuring Init and the required inittab files
# Cd/mnt/sysroot
# rm-f LINUXRC
To provide a configuration file for the init process:
# Vim Etc/inittab
Add the following content:
:: Sysinit:/etc/rc.d/rc.sysinit
Console::respawn:-/bin/sh
:: Ctrlaltdel:/sbin/reboot
:: Shutdown:/bin/umount-a-R
5. Prepare a File system table configuration file for the system/etc/fstab
# Vim Etc/fstab
Add the following content:
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
6, because the log process is started in the Rc.sysinit file, so the system will generate a large number of logs in the run and display it in the console, this will often interrupt the work in progress, in order to avoid this situation, we set up a configuration file for the log process, for which it is specified to send the log to/var/ Log/messages file;
# Vim Etc/syslog.conf
Add the following line:
*.info/var/log/messages
Vii. adding user accounts to the system (based on user information within the host)
1. Establish passwd account file for target host
# Cd/mnt/sysroot
# Vim ETC/PASSWD
Add the following content:
Root:x:0:0::/root:/bin/sh
Then create a "home" directory for the root user:
# mkdir Root
2. Set up group account file for target host
# Vim Etc/group
Add the following content:
root:x:0:
3, for the target host to establish shadow shadow password file, which is directly replicated in the host's shadow file on the root password line to achieve
# grep "^root"/etc/shadow > Etc/shadow
Note: When the target host starts, the root user's password is also the root user's password for the host. You can change the password of the root user after the target host is started.
4. Replace the Etc/inittab file with the following:
:: Sysinit:/etc/init.d/rc.sysinit
:: Respawn:/sbin/getty 9600 tty1
:: Respawn:/sbin/getty 9600 Tty2
:: Shutdown:/bin/umount-a-R
:: Ctrlaltdel:/sbin/reboot
Viii. provide banner information when the system is logged in
# vim/mnt/sysroot/etc/issue
Add the following content:
Welcome to Cqy ' s Linux ...
Kernel \ r
Ix. provide the host name for the system at system startup
1. Create a configuration file that holds the host name
# Mkdir/mnt/sysroot/etc/sysconfig
# vim/etc/sysconfig/network
Add the following content:
Hostname=marion.example.com
2. Edit the system initialization script to set the host name during boot
# Vim/mnt/sysroot/etc/init.d/rc.sysinit
Add the following line to the tail of the file:
Hostname=
[-e/etc/sysconfig/network &&-r/etc/sysconfig/network] && source/etc/sysconfig/network
[-Z ${hostname}] && hostname= "localhost"
/bin/hostname ${hostname}
X. Provide banner information when the system is logged in
This can be done via the host, or it can be configured directly on the target host. Here's how to configure directly on the target host:
# vi/etc/issue
Add the following content:
Welcome to mageedu Linux (http://www.magedu.com) ...
Kernel \ r
Xi. provide the host name for the system at system startup
This can be done via the host, or it can be configured directly on the target host. Here's how to configure directly on the target host:
1. Create a configuration file that holds the host name
# Mkdir/etc/sysconfig
# vi/etc/sysconfig/network
Add the following content:
Hostname=marion.example.com
2. Edit the system initialization script to set the host name during boot
# Vi/etc/init.d/rc.sysinit
Add the following line to the tail of the file:
Hostname=
[-e/etc/sysconfig/network &&-r/etc/sysconfig/network] && source/etc/sysconfig/network
[-Z ${hostname}] && hostname= "localhost"
/bin/hostname ${hostname}
XI. SSH Remote Connection service to the system via Dropbear
1. Compile and install dropbear (assuming dropbear source code is within/usr/src/)
# CD/USR/SRC
# Tar XF dropbear-2013.56.tar.bz2
# CD dropbear-2013.56
#./configure
# make
# make Install
2. Transplant Dropbear to target system
Porting the binaries and their dependent library files will enable them to function properly on the target system. It is recommended to use a script (which is saved here as cpso.sh), which will automatically migrate the specified commands and dependent library files.
#!/bin/bash
#
Dest=/mnt/sysroot
LIBCP () {
libpath=${1%/*}
[!-D $DEST $libpath] && mkdir-p $DEST $libpath
[!-e $DEST ${1}] && cp $DEST $libpath && echo "Copy Lib $ finished."
}
BINCP () {
cmdpath=${1%/*}
[!-D $DEST $cmdpath] && mkdir-p $DEST $cmdpath
[!-e $DEST ${1}] && cp $DEST $cmdpath
For LIB in ' LDD $ | 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
Next run this script, enter Dropbear, Dropbearkey, and Dbclient, respectively, and the library files that these commands and commands depend on are stored in the corresponding directory of the target system.
3, for remote login users to provide pseudo-terminal equipment files
To edit/mnt/sysroot/etc/fstab, add the following line:
Devpts/dev/ptsdevptsmode=6200 0
To create the desired directory:
# mkdir/mnt/sysroot/dev/pts
4. Generate the 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/etc/dropbear/dropbear_rsa_host_key-s 2048
# dropbearkey-t Rsa-f/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 the security 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. Provide network service conversion 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/
Linux Learning notes < 21 >--busybox making small systems