裁剪Linux小系统
一)从linux本身系统上裁剪出来一个小系统。
博客目的和实现功能:
从本身存在的系统上新添加一块硬盘,做一个grub引导,然后把新加的硬盘做好后,加上网卡驱动,放到其余的主机上能正常进入使用,ping通外网即可。
/dev/sdb1 1 14 112423+ 83 Linux /dev/sdb2 15 146 1060290 83 Linux
mke2fs -t ext4 /dev/sdb1 mke2fs -t ext4 /dev/sdb2
2)创建两个目录,挂载使用。
mkdir /mnt/{boot,sysroot} mount /dev/sdb1 /mnt/boot/ mount /dev/sdb2 /mnt/sysroot/
3)安装grub到目标磁盘。
[root@station106 ~]# grub-install --root-directory=/mnt /dev/sdb 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 /mnt/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/sda (hd1) /dev/sdb
4)复制源主机上的引导程序到目标磁盘上。
cp /boot/vmlinuz-2.6.32-358.el6.x86_64 /mnt/boot/vmlinuz cp /boot/initramfs-2.6.32-358.el6.x86_64.img /mnt/boot/initramfs.img
5)创建一些文件放到根系统上。
[root@station106 ~]# mkdir -pv /mnt/sysroot/{etc/rc.d,usr,var,proc,sys, dev, lib, lib64, bin, sbin, boot, srv, mnt, media, home, root} mkdir: created directory `/mnt/sysroot/{etc‘ mkdir: created directory `/mnt/sysroot/{etc/rc.d,usr,var,proc,sys,‘ mkdir: created directory `dev,‘ mkdir: created directory `lib,‘ mkdir: created directory `lib64,‘ mkdir: created directory `bin,‘ mkdir: created directory `sbin,‘ mkdir: created directory `boot,‘ mkdir: created directory `srv,‘ mkdir: created directory `mnt,‘ mkdir: created directory `media,‘ mkdir: created directory `home,‘ mkdir: created directory `root}‘
6)移植bash和一些需要的命令道根系统中。让裁剪好的小系统也能正常使用。
我这里用的脚本实现的。
#!/bin/bash target=/mnt/sysroot(定义一个变量) read -p "enter a command:" cmd(交互模式) clearcmd() { (定义个函数,函数名字为clearcmd) if which $cmd &> /dev/null;then (如果输出的命令存在则放到位桶) cmdpath=`which --skip-alias $cmd` (这里特殊用法--skip-alias是为了跳出别名) else (不存在就显示not,则以退出码为5退出) echo "Not such command" exit 5 fi } cmdcopy() { (在定义一个函数,函数名为cmdcopy) cmddir=`dirname $cmdpath` (截取命令的头文件) [ -d ${target}{cmddir} ] || mkdir -p ${target}${cmddir} 判断/mnt/sysroot下的截取的命令头文件是否存在。或者创建。 [ -f ${target}${1} ] || cp $1 ${target}${cmddir} 检测文件是否存在$1传递的是cmdpath。 } libcopy() { (定义一个函数,名字为,libcopy) for lib in `ldd $1 | grep -o "/[^[:space:]]\{1,\}"`;do (循环命令库文件) libdir=`dirname $lib` (定义解出来的库目录) [ -d ${target}${libdir} ] || mkdir -p ${target}${libdir} [ -f ${target}${lib} ] || cp $lib ${target}${libdir} done } while true;do 为真进入循环 read -p "enter a command:" cmd if [ "$cmd" == ‘quit‘ ];then 等于quit就退出。 echo "quit" exit 6 fi clearcmd $cmd 调用第一个函数。为5就是不存在跳出循环 [ $? -eq 5 ] && continue cmdcopy $cmdpath 调用函数 libcopy $cmdpath调用函数 done
7)提供grub配置文件。
vim /mnt/boot/grub/grub.conf
default=0 默认启动第一个内核
timeout=5 启动显示时间
title shunzi Linux 标题
root (hd0,0) 第一个磁盘的第一个分区
kernel /vmlinuz ro root=/dev/sda2 quiet selinux=0 init=/bin/bash 内核文件
initrd /initramfs.img 额外驱动文件。
8)添加init程序。实现模块加载能ping通外网。
#!/bin/bash
echo -e "welcome to \033[34 lixianshun Tiny\033[0m Linux"
mount -n -t proc proc /proc
mount -n -t sysfs sysfs /sys
insmod /lib/modules/e1000.ko
[ $? -eq 0 ] && echo -e "Load e1000 module succeeded [\033[32mOK\033[0m ]"
ifconfig lo 127.0.0.1/8
ifconfig eth0 192.168.1.10/24
route add -net 0.0.0.0 gw 192.168.1.253
mount -n -o remount,rw /dev/sda2 /
/bin/bash
本文出自 “落叶飘远方” 博客,请务必保留此出处http://shunzi.blog.51cto.com/8289655/1367850
原文:http://shunzi.blog.51cto.com/8289655/1367850