QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 2353|回复: 0

创造PXE启动Fanx2.0 and Slax6.X 镜像脚本.

[复制链接]
发表于 2008-4-18 12:04:28 | 显示全部楼层 |阅读模式
#!/bin/bash
# PXE BOOT SLAX FROM AN HTTP SERVER
# this is a script I created to automate the procedure of creating an initrd image for me
# this script was written by Jeff Sadowski
#
MYPWD=`pwd`
ipaddr=`ifconfig eth0|grep "inet addr"|cut -d":" -f2|cut -d" " -f1`

output=/tmp/prep
mkdir -p $output
mount -t tmpfs "" $output

#copy slax's initrd while uncompressing it
gunzip -c /mnt/live/mnt/*/boot/initrd.gz >$output/initrd

#create a place to mount slax's initrd
mkdir -p /mnt/initrd

#mount the initrd
mount $output/initrd /mnt/initrd/ -o loop

#create a temporary folder to work out of
TMP=/tmp/workinginitrd.`date +%Y-%m-%d.%H:%M`
mkdir -p $TMP

#copy all of the stuff from the old initrd into the working directory
cp -ra /mnt/initrd/* $TMP
umount /mnt/initrd

#setup the netmods varable
netmods=`ls -1d $BASE/lib/modules/*/kernel/drivers/net|tail -n 1`
netmods=`echo $netmods|sed s/".*\/lib"/lib/`

#setup the module path varable
modpath=`dirname \`echo $netmods\``
modpath=`dirname \`echo $modpath\``
modpath=`dirname \`echo $modpath\``

#copy all network modules
cp -r $BASE/$netmods $TMP/$modpath/kernel/drivers
#copy the fuse modules
cp -r $BASE/$modpath/kernel/fs/fuse $TMP/$modpath/kernel/fs
#copy the dependency file so I can load network modules and the fuse module I copied
cp $BASE/$modpath/modules.dep $TMP/$modpath/modules.dep
#copy alias file so I can lookup network cards
cp $BASE/$modpath/modules.alias $TMP/$modpath/modules.alias

#copy needed tools for fuse and needed network tools
cp $BASE/usr/bin/fusermount $TMP/bin
cp $BASE/usr/lib/libfuse.so.2 $TMP/lib
cp $BASE/sbin/ifconfig $TMP/bin
cp $BASE/sbin/dhcpcd $TMP/bin
cp $BASE/sbin/lspci $TMP/bin
cp $BASE/bin/httpfs $TMP/bin

#create patch for linuxrc in initrd
cat <<END >patch
httplocation="\`cmdline_value httpfs\`"
if [ "\$httplocation" != "" ]; then
# try to auto-detect NIC
NIC_STRING=\`lspci -m -n | grep "0200"| sed s/" \""//g| sed s/".*0200\""/v0000/g|sed s/"\""/d0000/|sed s/"\".*"//\`
for NIC in \$NIC_STRING; do
DETECTED_NIC=\`cat /lib/modules/\\\`uname -r\\\`/modules.alias | grep -i "\$NIC" | cut -d " " -f 3 | cut -d " " -f -1 | tail -n 1\`
if [ "\$DETECTED_NIC" != "" ];then
echo loading [\$DETECTED_NIC] driver
/sbin/modprobe \$DETECTED_NIC
fi
done

ifconfig eth0 up # sometimes i need this. doesn't hurt anyway
mkdir -p /var/lib/dhcpc
dhcpcd eth0
. /etc/dhcpc/dhcpcd-eth0.info

mkdir -p /mnt/httpfs
mkdir -p /mnt/vcdrom
/sbin/modprobe fuse

httpfs \$httplocation /mnt/httpfs
mount -o loop,ro /mnt/httpfs/\`basename \$httplocation\` /mnt/vcdrom
DATA=/mnt/vcdrom/slax
fi
END

#find the place I want to patch
LINE=`grep -n "\\"\\$DATA\\" = \\"\\"" $TMP/linuxrc|head -n 1|cut -d: -f1`
let LINE=$LINE-1
head -n $LINE $TMP/linuxrc > linuxrc.head
TOTAL=`wc -l $TMP/linuxrc|cut -d " " -f1`
let REMANDER=$TOTAL-$LINE
tail -n $REMANDER $TMP/linuxrc > linuxrc.tail
#create patched linuxrc for my pxe boot implementation
cat linuxrc.head patch linuxrc.tail > $TMP/linuxrc

#find out how big I need to make the initrd image's filesystem
SIZE=`du --max-depth=0 $TMP|cut -f1`
#allow extra size for filesystem to sit on
let SIZE=$SIZE+1000
#create a blank file of needed size
dd if=/dev/zero of=$output/pxe_initrd bs=1024 count=$SIZE
#format the file
mkfs.ext2 -F $output/pxe_initrd
mount $output/pxe_initrd /mnt/initrd/ -o loop
cp -ra $TMP/* /mnt/initrd
umount /mnt/initrd
gzip $output/pxe_initrd
cp /boot/vmlinuz $output

mkdir -p $output/pxelinux.cfg
cat <<END >$output/pxelinux.cfg/default
default slax
prompt 1
timeout 30
label slax
kernel vmlinuz
append initrd=pxe_initrd.gz httpfs=http://$ipaddr/slax-6.0.0.iso ramdisk_size=$SIZE root=/dev/ram0 rw copy2ram autoexec=xconf;telinit~4 changes=/slax/
END

rm -f patch
rm -f linuxrc.head
rm -f linuxrc.tail
rm -rf $TMP
rm -rf $output/initrd

mkdir -p /tftpboot
mv -f $output/* /tftpboot
umount $output
rm -rf $output

wget http://www.kernel.org/pub/linux/ ... x/syslinux-3.61.zip
mkdir syslinux
cd syslinux
unzip ../syslinux-3.61.zip
cp pxelinux.0 /tftpboot
cd ../
rm -rf syslinux*

cd /tftpboot
zip -Jr $MYPWD/pxe_pkg.zip *

原文出自:http://www.slax.org/forum.php?action=view&parentID=367

[ 本帖最后由 vincentzpf 于 2008-4-18 12:11 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-3-29 23:19 , Processed in 0.275378 second(s), 15 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表