linux裁剪—定制自己所需要的linux
? ? Linux以其開源思想和啟動速度快為廣大技術人員所喜愛,本文主要講述通過自己對內(nèi)核的包裝以及對默認程序的設定,來實現(xiàn)自己定制一個自己需要的os系統(tǒng),并能夠?qū)崿F(xiàn)開機自動加載網(wǎng)卡,并為網(wǎng)卡配置ip地址,本文不涉及內(nèi)核的編譯,內(nèi)核編譯內(nèi)容將在后續(xù)推出,敬請大家期待!
? ?本文是通過宿主機——>目標機的形式來實現(xiàn)。
1、為虛擬機添加一個20G的硬盤,并將磁盤設置為單個文件系統(tǒng),并命名為smallcentos.vmdk
? 查看宿主機現(xiàn)在的硬盤信息
[root@localhost ~]# fdisk -l /dev/sd[a-z] Disk /dev/sda: 128.8 GB, 128849018880 bytes 255 heads, 63 sectors/track, 15665 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x0001c38dDevice Boot Start End Blocks Id System /dev/sda1 * 1 26 204800 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 26 7859 62914560 8e Linux LVM Disk /dev/sdb: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000新添加的硬盤被識別為sdb,目標主機將通過加載sdb來啟動
2、在sdb創(chuàng)建兩個基本100M、512M分區(qū),并將文件系統(tǒng)格式化成ext4格式
[root@localhost ~]# echo -e "n\np\n1\n\n+100M\nn\np\n2\n\n+512M\nw" |fdisk /dev/sdb? ?格式化新建的分區(qū)
[root@localhost ~]# mke2fs -t ext4 /dev/sdb1 [root@localhost ~]# mke2fs -t ext4 /dev/sdb23.將新創(chuàng)建的分區(qū)分別掛載至/mnt/boot目錄和/mnt/sysroot
[root@localhost mnt]# mount /dev/sdb1 on /mnt/boot type ext4 (rw) /dev/sdb2 on /mnt/sysroot type ext4 (rw)4.安裝grub至指定的分區(qū)
[root@localhost mnt]# 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/sdb5.復制/boot目錄中的grub和initrd文件至/mnt/boot目錄中(將啟動文件復制到定制系統(tǒng)中)
6.創(chuàng)建Linux需要的一些基本文件(在定制系統(tǒng)上需要的)
[root@localhost grub]# mkdir -pv /mnt/sysroot/{etc/rc.d,usr,var,proc,sys,dev,lib,lib64,bin,sbin,boot,src,mnt,media,home,root}7.在宿主機上移植一個可執(zhí)行的二進制文件和庫到目標機的硬盤上,如ls,cat,mkdir,mount,reboot,useradd,passwd,ifconfig,ip,ping等,
? ? ?此處不再累贅,后面將會附上腳本實現(xiàn)方式
?為了防止內(nèi)核恐慌,需要為bash創(chuàng)建一個軟鏈接sh
8.在目標機的/boot/grub目錄中創(chuàng)建grub.conf,已實現(xiàn)開機自檢,內(nèi)容如下
default=0
timeout=10
hiddenmenu
title wangfengLinux
? root(hd0,0)
? kernel /wangfengvmlinuz ro root=/dev/sda2 selinux=0 init=/sbin/init
? initrd /wangfenginitramfs.img
9.為了能夠?qū)崿F(xiàn)開機啟動網(wǎng)卡,需要將宿主機上的網(wǎng)卡配置文件復制到目標機上,可以通過lsmod查看當前系統(tǒng)的所有模塊,可以通過modinfo 模塊名稱來查看模塊的詳細信息
[root@localhost ~]# lsmod |grep e1000 -->查看網(wǎng)卡的信息 e1000 170646 010.為了使系統(tǒng)能夠開機自動掛載一些文件系統(tǒng)和初始化一些服務,需要在目標機上的/sbin/目錄下創(chuàng)建init文件已實現(xiàn)需求,內(nèi)容如下
#!/bin/bash
echo -e "Welcome to \033[32m Wangfeng\033[0m Linux"
mount -n -t proc /proc proc
mount -n -t sysfs sysfs /sys
insmod /lib/modules/e1000.ko
ifconfig lo 127.0.0.1/8
ifconfig eth0 192.168.1.200/24
route add -net 0.0.0.0 gw 192.168.1.253
/bin/bash
開啟宿主機,可以看到效果
也可以ping通外網(wǎng)
附:拷貝庫文件和二進制文件的腳本
#!/bin/bash
options(){
for i in $*;do
? dirname=`dirname $i`
? [ -d /mnt/sysroot$dirname ] || mkdir -p /mnt/sysroot$dirname
? [ -f /mnt/sysroot$i ]||cp $i /mnt/sysroot$dirname/
done
}
while true;do
read -p "Enter a command : " pidname
?[[ "$pidname" == "quit" ]] && echo "Quit " && exit 0
?bash=`which --skip-alias $pidname`
?if [[ -x $bash ]];then
? ?options `/usr/bin/ldd $bash |grep -o "/[^[:space:]]\{1,\}"`
? ?options $bash
?else
? ?echo "No such command!"
? fi
done
腳本簡要說明:
? 大家都知道一個命令的運行需要依賴于二進制文件和庫文件,本實例以cat為例,列舉cat的二進制文件所在的路徑和文件所在的路徑
二進制文件所在的位置 [root@localhost ~]# which cat /bin/cat依賴的庫文件
由于本人水平有限,請各位大神批評指正,同時為在昆明3.01事件中遇難的同胞默哀,愿逝者安息,傷者平安!!
轉載于:https://blog.51cto.com/wangfeng7399/1366537
總結
以上是生活随笔為你收集整理的linux裁剪—定制自己所需要的linux的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: myeclipse2014新感悟
- 下一篇: 程序员也要多读些专业之外的书