📄 buildrootfilesystem.txt
字号:
#!/bin/bash# buildrootfilesystem v0.2 10/23/01# www.embeddedlinuxinterfacing.com## The original location of this script is# http://www.embeddedlinuxinterfacing.com/chapters/04/buildrootfilesystem## Copyright (C) 2001 by Craig Hollabaugh## This program is free software; you can redistribute it and/or modify it under# the terms of the GNU Library General Public License as published by the Free# Software Foundation; either version 2 of the License, or (at your option) any# later version.## This program is distributed in the hope that it will be useful, but WITHOUT# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS# FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more# details.## You should have received a copy of the GNU Library General Public License# along with this program; if not, write to the Free Software Foundation, Inc.,# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA#umask 022SRCFILELOC=/root/crossBUILDLOC=$SRCFILELOC/buildscase "$1" in"ppc" )ARCH=powerpcTARGET=powerpc-linux#MVRPMLOC=ftp://ftp.mvista.com/pub/Journeyman/cd2/ppc_8xx/apps/MVRPMLOC=http://www.embeddedlinuxinterfacing.com/ftp.mvista.com/pub/Journeyman/cd2/ppc_8xx/apps/TEMPFSLOC=$BUILDLOC/$ARCH-rootrpms/opt/hardhat/devkit/ppc/8xx/target/;;"arm" )ARCH=armTARGET=arm-linux#MVRPMLOC=ftp://ftp.mvista.com/pub/Journeyman/cd1/arm_sa_le/apps/MVRPMLOC=http://www.embeddedlinuxinterfacing.com/ftp.mvista.com/pub/Journeyman/cd1/arm_sa_le/apps/TEMPFSLOC=$BUILDLOC/$ARCH-rootrpms/opt/hardhat/devkit/arm/sa_le/target/;;"i386" )ARCH=i386TARGET=i386#MVRPMLOC=ftp://ftp.mvista.com/pub/Journeyman/cd1/x86_586/apps/MVRPMLOC=http://www.embeddedlinuxinterfacing.com/ftp.mvista.com/pub/Journeyman/cd1/x86_586/apps/TEMPFSLOC=$BUILDLOC/$ARCH-rootrpms/opt/hardhat/devkit/x86/586/target/;;* ) echo -n "Usage " `basename $0` echo " i386|arm|ppc [ramdisk]" exit 1;;esac## Step 1 - Determine what packages to download#echo Step 1 - Determine what packages to downloadPACKAGES="glibc-2 bash procps textutils fileutils shellutils sysvinit netbase libncurses libstdc mount telnet-client net-tools ping gdbserver modutils"echo packages are $PACKAGESecho Step 1 - Completeecho## Step 2 - Create build and new target root filesystem directories#echo Step 2 - Create build and new target root filesystem directoriesif [ ! -e /tftpboot ]then mkdir /tftpboot fiif [ ! -e $SRCFILELOC ]then mkdir $SRCFILELOC fiif [ ! -e $BUILDLOC ]then mkdir $BUILDLOC fiROOTFSLOC=/tftpboot/$ARCH-rootfsecho Creating root file system for $ARCHrm -rf $ROOTFSLOCmkdir $ROOTFSLOCif [ ! -e $BUILDLOC/$ARCH-rootrpms ]then mkdir $BUILDLOC/$ARCH-rootrpmsficd $ROOTFSLOCmkdir dev etc etc/init.d bin sbin lib usr usr/bin proc tmp chmod 755 . dev etc etc/init.d bin sbin lib usr usr/bin proc tmpecho Step 2 - Completeecho## Step 3 - Download the packages#echo Step 3 - Download the packagescd $BUILDLOC/$ARCH-rootrpmslynx -dump $MVRPMLOC | grep ftp > /tmp/rpmlistfor i in $PACKAGESdo a=`grep $i /tmp/rpmlist` rpmurl=`echo $a | cut -d " " -f 2`# echo $rpmurl rpm=`basename $rpmurl`# echo $rpm if [ ! -f $BUILDLOC/$ARCH-rootrpms/$rpm ] then echo Getting $rpm wget $rpmurl else echo Have $rpm fidoneecho Step 3 - Completeecho## Step 4 - Extract the package's contents into a temporary directory#echo Step 4 - Extract the package\'s contents into a temporary directorycd $BUILDLOC/$ARCH-rootrpms# this is the old way, too slow because it converts rpms everytime#alien -t *rpm#find . -name "*tgz" -exec tar zxvf {} \;#rm -rf *tgzIFS=''for rpm in `ls *rpm`do if [ ! -f $rpm.extracted ] then alien -t $rpm tgz=`ls *tgz` tar zxvf $tgz rm -rf $tgz touch $rpm.extracted fidoneecho Step 4 - Completeecho## Step 5 - Copy the required programs#echo Step 5 - Copy the required programsecho#lib filescd $TEMPFSLOC/libcp -av ld* $ROOTFSLOC/libcp -av libc-* $ROOTFSLOC/libcp -av libc.* $ROOTFSLOC/libcp -av libutil* $ROOTFSLOC/libcp -av libncurses* $ROOTFSLOC/libcp -av libdl* $ROOTFSLOC/libcp -av libnss_dns* $ROOTFSLOC/libcp -av libnss_files* $ROOTFSLOC/libcp -av libresolv* $ROOTFSLOC/libcp -av libproc* $ROOTFSLOC/libcp -av librt* $ROOTFSLOC/libcp -av libpthread* $ROOTFSLOC/lib#libm and libstdc are needed by telnet-clientcp -av libm* $ROOTFSLOC/libcd $ROOTFSLOC/usrln -s ../lib libcd $TEMPFSLOC/usr/libcp -av libstdc* $ROOTFSLOC/usr/lib#sbin filescd $TEMPFSLOC/sbincp -av init ifconfig route *mod $ROOTFSLOC/sbin#bin filescd $TEMPFSLOC/bincp -av bash cat ls mount umount ps $ROOTFSLOC/bincp -av df kill ping chmod touch rm $ROOTFSLOC/bincp -av echo $ROOTFSLOC/bincd $ROOTFSLOC/binln -s bash sh#usr/bin filescd $TEMPFSLOC/usr/bincp -av telnet gdbserver $ROOTFSLOC/usr/bin#helloworldcd $ROOTFSLOC/tmpcat > helloworld.c << ENDOFINPUT#include <stdio.h>int main(void){ int i; for (i = 1; i < 10; i++) { printf("Hello world %d times!\n",i); }}ENDOFINPUTif [ $ARCH == "i386" ]then gcc -g -o helloworld-$TARGET helloworld.celse $TARGET-gcc -g -o helloworld-$TARGET helloworld.cfifile helloworld-$TARGETchown -R root.root $ROOTFSLOC/*echo Step 5 - Completeecho## Step 6 - Strip the required programs#echo Step 6 - Strip the required programsecho# strip it, strip it goodif [ $ARCH == "i386" ]then strip -s -g $ROOTFSLOC/lib/* strip -s -g $ROOTFSLOC/bin/* strip -s -g $ROOTFSLOC/sbin/* strip -s -g $ROOTFSLOC/usr/bin/* strip -s -g $ROOTFSLOC/usr/lib/*else $TARGET-strip -s -g $ROOTFSLOC/lib/* $TARGET-strip -s -g $ROOTFSLOC/bin/* $TARGET-strip -s -g $ROOTFSLOC/sbin/* $TARGET-strip -s -g $ROOTFSLOC/usr/bin/* $TARGET-strip -s -g $ROOTFSLOC/usr/lib/*fiecho Step 6 - Completeecho## Step 7 - Create configuration files#echo Step 7 - Create configuration filesecho#etc filescd $TEMPFSLOC/etccp -av protocols services $ROOTFSLOC/etccd $ROOTFSLOC/etccat > fstab << endofinput# <file system> <mount point> <type> <options> <dump> <pass>proc /proc proc defaults 0 0endofinputif [ $# == 2 ] && [ $2 == "ramdisk" ]then echo "/dev/ram / ext2 defaults 0 0" >> fstabelse echo "192.168.1.11:/tftpboot/$ARCH-rootfs / nfs defaults 1 1" >> fstabficat > inittab << endofinputid:2:initdefault:l2:2:wait:/etc/init.d/rcS1:2:respawn:/bin/bash2:6:wait:/etc/init.d/umountfsendofinputcat > init.d/rcS << endofinput#!/bin/bash/bin/mount -n -o remount,rw /# clear out mtab>/etc/mtab/bin/mount -aecho Starting Network/sbin/ifconfig lo 127.0.0.1 netmask 255.0.0.0 broadcast 127.255.255.255/sbin/route add -net 127.0.0.0 netmask 255.0.0.0 loendofinputcase "$ARCH" in"powerpc" )cat >> init.d/rcS << endofinput/sbin/ifconfig eth0 192.168.1.22 netmask 255.255.255.0/sbin/route add default gw 192.168.1.254 eth0endofinput;;"arm" )cat >> init.d/rcS << endofinput/sbin/ifconfig eth0 192.168.1.21 netmask 255.255.255.0/sbin/route add default gw 192.168.1.254 eth0endofinput;;"i386" )cat >> init.d/rcS << endofinput/sbin/ifconfig eth0 192.168.1.23 netmask 255.255.255.0/sbin/route add default gw 192.168.1.254 eth0endofinput;;esacchmod 755 init.d/rcScat > init.d/umountfs << endofinput/bin/echo umounting filesystems and remounting / as readonly/bin/umount -f -a -r/bin/mount -n -o remount,ro /endofinputchmod 755 init.d/umountfscat > resolv.conf << endofinputdomain trailblazerdevnameserver 192.168.1.1endofinputecho Step 7 - Completeecho## Step 8 - Create devices in /dev directory#echo Step 8 - Create devices in /dev directory#dev filescd $ROOTFSLOC/devmknod -m 0666 tty c 5 0mknod -m 0666 tty0 c 4 0mknod -m 0666 ttyS0 c 4 64ln -s ttyS0 consolemknod -m 0666 null c 1 3mknod -m 660 ram b 1 1 chown root.disk ram echo Step 8 - Completeecho## Step 9 - Prepare the root filesystem for operation on the target board#echo Step 9 - Prepare the root filesystem for operation on the target boardif [ $# == 2 ]then if [ $2 == "ramdisk" ] then echo Building $ARCH root filesystem ramdisk rm -rf /tmp/tmpmnt mkdir /tmp/tmpmnt rm -rf /tmp/ramrootfs dd if=/dev/zero of=/tmp/ramrootfs bs=1k count=8192 mke2fs -F -m 0 -i 2000 /tmp/ramrootfs mount -o loop -t ext2 /tmp/ramrootfs /tmp/tmpmnt cd /tmp/tmpmnt cp -av $ROOTFSLOC/* . cd /tmp umount /tmp/tmpmnt cat /tmp/ramrootfs | gzip -9 > /tftpboot/$ARCH-ramdisk.gz if [ $ARCH == "powerpc" ] then cp /tftpboot/$ARCH-ramdisk.gz /usr/src/$TARGET/arch/ppc/boot/images/ramdisk.image.gz echo Copying /tftpboot/$ARCH-ramdisk.gz to /usr/src/$TARGET/arch/ppc/boot/images/ramdisk.image.gz fi rm -rf /tmp/ramrootfs rm -rf /tmp/tmpmnt echo Your $ARCH root filesystem ramdisk is /tftpboot/$ARCH-ramdisk.gz fifiif [ $ARCH == "i386" ]then echo echo -n This script is about to work with your /dev/hdc1 partition, is this OK? yes/no?" " read hdc1ok if [ $hdc1ok == "yes" ] then mkdir $ROOTFSLOC/boot #dev files cd $ROOTFSLOC/dev rm -rf console mknod console c 5 1 chmod 666 console mknod hda b 3 0 mknod hda1 b 3 1 mknod hda2 b 3 2 chmod 666 hda* #/boot files cp /boot/boot.b $ROOTFSLOC/boot cp /usr/src/linux/arch/i386/boot/bzImage $ROOTFSLOC/boot/bzImage TMPMOUNT=/tmp/tmpmnt cat > /tmp/lilo.conf.1 << ENDOFINPUT# this is a special lilo.conf file used to install lilo on the /dev/hdc drive# when the machine is booted from the /dev/hda drive. Don't run lilo with this# conf file if you booted the machine from this second drive.disk=/dev/hdc bios=0x80 # This tells LILO to treat hdc as hda#disk=/dev/hdc # bios=0x80 # cylinders=612 # this is experimenting with 40MB SanDisk flash card# heads=4 # sectors=32#linear#append="hd=612/4/32"boot=/dev/hdc # install boot on hdcmap=$TMPMOUNT/boot/map # location of the map fileinstall=$TMPMOUNT/boot/boot.b # boot file to copy to boot sectorprompt # show LILO boot: prompttimeout=1 # wait a second for user inputimage=$TMPMOUNT/boot/bzImage # kernel locationlabel=linux # kernel labelroot=/dev/hda1 # root image location after you finalize disk locationread-only # mount root as read onlyENDOFINPUTcat > $ROOTFSLOC/etc/fstab << endofinput# <file system> <mount point> <type> <options> <dump> <pass>/dev/hda1 / ext2 defaults,errors=remount-ro 0 1proc /proc proc defaults 0 0endofinput e2fsck -y /dev/hdc1 rm -rf $TMPMOUNT mkdir $TMPMOUNT mount -t ext2 /dev/hdc1 $TMPMOUNT echo -n "Do you want to erase the /dev/hdc1 partition, continue yes/no? " read deleteall if [ $deleteall == "yes" ] then # this deletes everything on your partition, WATCH OUT!! rm -rf $TMPMOUNT/* mkdir $TMPMOUNT/lost+found fi cp -av $ROOTFSLOC/* $TMPMOUNT /sbin/lilo -C /tmp/lilo.conf.1 cd / umount $TMPMOUNT rm -rf $TMPMOUNT e2fsck -y /dev/hdc1fi#hdc1okfiecho Step 9 - Completeechoecho Your $ARCH root filesystem is $ROOTFSLOC
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -