這個腳本是linux上面做一個文件系統的,我在UML官網下的,有問題如下:
[root@zhoutianzuo azuo]# bash install_fs.sh
Filesystem already created
Filesystem already mounted
going to fetch
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.
源腳本如下:
#!/bin/bash
# Settings !!!*** PLEASE MODIFY THESE ***!!!
export ARCH="i386"
export MIRROR="http://centos.ustc.edu.cn/centos/6.3/os/i386/Packages/"
export TMPDIR=`pwd`"/tmp"
export ROOT_FS=`pwd`"/CentOS6-x86-root_fs"
export RPM_DIR=`pwd`"/RPMS"
export CHROOTCMD="linux32 chroot"
export RPMCMD="rpm"
export PACKAGES="packages.$ARCH"
export ARCHES="$ARCH i686 noarch"
export DEBUG=""
export DUPES=""
export TEXT_BROWSER="lynx"
function doGetRPM() {
rpmfile=""
if [ ! -z "$DEBUG" ]; then
echo "grep $1 $PACKAGES | grep $2 | wc -l"
fi
rpmfilescount=`grep ^$1 $PACKAGES | grep $2 | wc -l`
#echo "filecount=$rpmfilescount"
if [ "$rpmfilescount" -eq 0 ]; then
if [ ! -z "$DEBUG" ]; then
echo "No RPMs found for $1 - $2"
fi
else
if [ "$rpmfilescount" == "1" ]; then
# echo "Found "`$grep`
rpmfile=`grep ^$1 $PACKAGES | grep $2`
else
if [ ! -z "$DEBUG" ]; then
echo "Too many packages matching $1: $rpmfilescount"
fi
rpmfile=`grep ^$1 $PACKAGES | grep $2 | sort | head -n 1`
fi
fi
}
function getRPM() {
for arch in $ARCHES; do
doGetRPM $1- $arch
if [ ! -z "$rpmfile" ]; then
return
fi
done
for arch in $ARCHES; do
doGetRPM $1 $arch
if [ ! -z "$rpmfile" ]; then
return
fi
done
echo "No RPM package found for $1"
exit 1
}
function fetch() {
base=$1
getRPM $base
local=$RPM_DIR"/$rpmfile"
echo "fetch() base=$base, local=$local, rpmfile=$rpmfile"
if [ ! -e "$local" ]; then
echo "Downloading $base ($rpmfile)"
wget -c -O $local "$MIRROR$rpmfile"
fi
}
function rpminstall() {
base=$1
fetch $base
stripped=`echo $base | sed 's+\.noarch++g' | sed 's+\.$ARCH++g'`
exists="0"
if [ ! -z "$DUPES" ]; then
exists=`$RPMCMD -qa --root $TMPDIR | grep $stripped | wc -l`
fi
if [ "$exists" -ne "0" ]; then
echo "skipped: $stripped"
else
$RPMCMD -Uvh --root $TMPDIR $local
rm -fr $TMPDIR/var/lib/rpm/__db*
fi
}
function rpminstallgroup() {
echo "SIZE="$#
list=""
while [ $# -ne 0 ]
do
package=$1
fetch $package
stripped=`echo $1 | sed 's+\.noarch++g' | sed 's+\.$ARCH++g'`
exists="0"
if [ ! -z "$DUPES" ]; then
exists=`$RPMCMD -qa --root $TMPDIR | grep $stripped | wc -l`
fi
if [ "$exists" -ne "0" ]; then
echo "skipped: $stripped"
else
list="$list $local"
fi
shift
done
# echo "installing list=$list"
# echo "installcommand=$RPMCMD -ivh --root $TMPDIR $list"
$RPMCMD -Uvh --root $TMPDIR $list
rm -fr $TMPDIR/var/lib/rpm/__db*
}
# Making the filesystem
if [ ! -e "$ROOT_FS" ]; then
dd if=/dev/zero of=$ROOT_FS bs=1024 count=1 seek=1572864
mkfs.ext4 -L ROOT -F $ROOT_FS
else
echo "Filesystem already created"
fi
if [ ! -e "$TMPDIR" ]; then
mkdir $TMPDIR
mount -o loop $ROOT_FS $TMPDIR
else
echo "Filesystem already mounted"
fi
mkdir -p $RPM_DIR
#rpm bug?
mkdir -p $TMPDIR/var/lock/rpm
mkdir -p $TMPDIR/var/lib/rpm
if [ ! -e "$TMPDIR/var/lib/rpm" ]; then
echo "initrpm:"
echo "$RPMCMD --initdb --root $TMPDIR"
$RPMCMD --initdb --root $TMPDIR
rm -fr $TMPDIR/var/lib/rpm/__db*
fi
if [ ! -e "$TMPDIR/dev" ]; then
mkdir $TMPDIR/dev
mknod $TMPDIR/dev/null c 1 3
fi
#Get the list of packages
if [ ! -f $PACKAGES ]; then
if [ -x "`which ${TEXT_BROWSER}`" ]; then
${TEXT_BROWSER} -dump $MIRROR |grep rpm\$ |awk -F "CentOS/" '{print $2}' >$PACKAGES
else
echo "error :$PACKAGES does not exists, and I could not build it with ${TEXT_BROWSER}"
echo "errot :$PACKAGES should contain a list of all rpms on your mirror site"
exit 1
fi
fi
echo "going to fetch"
# pretend to have a kernel (we don't need one):
KERNEL=`grep kernel-2.6 $PACKAGES`
fetch $KERNEL
$RPMCMD -Uvh --justdb --force --nodeps --root $TMPDIR $local
rm -fr $TMPDIR/var/lib/rpm/__db*
mkdir -p $TMPDIR/etc
# create an /etc/fstab
echo "LABEL=ROOT / auto defaults 1 1" >>$TMPDIR/etc/fstab
echo "none /dev/pts devpts gid=5,mode=620 0 0" >>$TMPDIR/etc/fstab
echo "none /proc proc defaults 0 0" >>$TMPDIR/etc/fstab
echo "tmpfs /tmp tmpfs defaults,size=768M 0 0" >>$TMPDIR/etc/fstab
mkdir -p $TMPDIR/usr/kerberos
# Installing the base packages
# no deps for these - got problems otherwise...
packages="glibc-common glibc libsepol pam passwd usermode setup filesystem basesystem util-linux libgcc tzdata compat-libtermcap zlib zip man-pages bash ncurses info gawk vim-common libattr libacl libstdc++ pcre grep cracklib-dicts glib2 centos-release chkconfig sed iputils rootfiles ethtool audit-libs popt libsysfs sysfsutils cpio less gzip readline tar db4 iproute mingetty libselinux libsemanage sysvinit binutils module-init-tools hwdata logrotate rsyslog udev procps diffutils libcap vim-minimal findutils device-mapper e2fsprogs-libs e2fsprogs net-tools shadow libudev device-mapper-libs lvm2-libs lvm2 MAKEDEV psmisc bzip2-libs libxml crypto-utils expat sqlite tcp_wrappers gdbm coreutils-libs gmp coreutils cracklib python openssl libgssglue krb5-libs libselinux-python initscripts db4 glibc-headers audit-libs-python policycoreutils libselinux-python ustr libsemanage bzip2 dash kbd dracut dmraid nss-softokn nss fipscheck python-iniparse"
for package in $packages; do
fetch $package
$RPMCMD -Uvh --force --noscripts --nodeps --root $TMPDIR $local
rm -fr $TMPDIR/var/lib/rpm/__db*
done
mkdir $TMPDIR/etc/profile.d
$CHROOTCMD $TMPDIR useradd rpm
packages="rpm rpm-libs"
for package in $packages; do
fetch $package
$RPMCMD -Uvh --force --nodigests --noscripts --nodeps --root $TMPDIR $local
rm -fr $TMPDIR/var/lib/rpm/__db*
done
# some extras
packages="which nspr python-libs libidn libssh2 cyrus-sasl-lib openldap nss-util nss-softokn-freebl libcom_err libcurl curl python-pycurl ca-certificates dbus-libs pakchois libgpg-error libgcrypt libtasn1 gnutls xz-libs python-iniparse keyutils-libs crontabs cronie cronie-anacron krb5-libs m2crypto python-urlgrabber elfutils-libelf elfutils-libs elfutils fipscheck-lib openssh ncurses-base ncurses-libs libedit openssh-clients tcp_wrappers-libs tcp_wrappers openssh-server wget elfutils lua file-libs file dhcp db4 db4-utils libffi libusb- libuser libblkid libuuid libcgroup "
for package in $packages; do
rpminstall $package
done
rpminstallgroup libproxy libproxy-bin libproxy-python
rpminstallgroup rpm rpm-libs
for package in pth pinentry gnupg2 gpgme pygpgme neon rpm-python yum-metadata-parser; do
rpminstall $package
done
rpminstallgroup yum-[0-9] yum-plugin-fastestmirror
for package in yum-plugin-keys yum-plugin-post-transaction-actions yum-plugin-protectbase yum-utils; do
rpminstall $package
done
echo "cleaning rpm db"
# re-install yum/rpm from inside...
mount -t proc none $TMPDIR/proc
cp /etc/resolv.conf $TMPDIR/etc
rm -f $TMPDIR/var/lib/rpm/*
#rm -f $TMPDIR/var/lib/rpm/__db*
#for db in Group Requirename Obsoletename Sigmd5 Sha1header Triggername Conflictname Basenames Requirename; do
# rm -fr $TMPDIR/var/lib/rpm/${db}
#done
rm -fr $TMPDIR/var/cache/yum
$CHROOTCMD $TMPDIR rpm --rebuilddb
fetch centos-release
cp $local $TMPDIR/
$CHROOTCMD $TMPDIR rpm -Uvh --nofiledigest --nodigest --nosignature --force /centos-release*rpm
rm $TMPDIR/centos-release*rpm
$CHROOTCMD $TMPDIR rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
$CHROOTCMD $TMPDIR rpm --import /etc/pki/rpm-gpg/*
mkdir -p $TMPDIR/var/cache/yum/$ARCH/6/base/packages/
cp RPMS/*rpm $TMPDIR/var/cache/yum/$ARCH/6/base/packages/
mkdir -p $TMPDIR/var/cache/yum/$ARCH/6/updates/packages/
cp RPMS/*rpm $TMPDIR/var/cache/yum/$ARCH/6/updates/packages/
#$CHROOTCMD $TMPDIR yum reinstall --releasever=6 -y --nogpgcheck centos-release
#$CHROOTCMD $TMPDIR rpm --import /etc/pki/rpm-gpg/*
$CHROOTCMD $TMPDIR yum install -y yum basesystem
#$CHROOTCMD $TMPDIR yum install --releasever=6 -y --nogpgcheck yum basesystem
#$CHROOTCMD $TMPDIR rpm --import /etc/pki/rpm-gpg/*
$CHROOTCMD $TMPDIR yum install -y openssh-server openssh-clients
$CHROOTCMD $TMPDIR yum update
rm -fr $TMPDIR/var/cache/yum
# ldconfig the newly installed libraries
ldconfig -r $TMPDIR
# create the ubd devices
if [ ! -e "$TMPDIR/dev/ubda" ]; then
if [ -e "$TMPDIR/dev" ]; then
for ((i=0; i<8; i++))
do
let=$(echo $i|tr '0-7' 'a-z')
mknod $TMPDIR/dev/ubd$let b 98 $[16 * $i]
for ((p=1; p<=16; p++)) do
mknod $TMPDIR/dev/ubd$let$p b 98 $[16 * $i + $p]
done
done
else
echo "/dev not found!"
exit 1
fi
fi
# basic devices so we can boot without an initrd:
cp -avpr /dev/console /dev/null /dev/zero $TMPDIR/dev
mkdir $TMPDIR/dev/pts
# use yum to update the system
echo "/dev/ROOT / ext4 rw 0 0" > $TMPDIR/etc/mtab
# tweak the inittab to only use tty0 and add it to securetty
for cf in /etc/init/start-ttys.conf /etc/sysconfig/init; do
mv $cf $cf.bak
cat $cf.bak | sed 's/1-6/0/g' > $cf
rm $cf.bak
done
echo "tty0" >> /etc/securetty
#enable shadow passwords
$CHROOTCMD $TMPDIR pwconv
#Remove the root password
sed -i -e "s/^root:\*:/root::/" $TMPDIR/etc/shadow
# We need an /etc/hosts file!
echo "127.0.0.1 localhost localhost.localdomain" > $TMPDIR/etc/hosts
# make the first network interface start with dhcp
# Use ifcfg-eth0 from currrent directory if we have it, otherwise create
$CHROOTCMD $TMPDIR yum install -y dhclient
if [ ! -f "ifcfg-eth0" ]; then
echo "DEVICE=eth0" >> $TMPDIR/ifcfg-eth0
echo "BOOTPROTO=dhcp" >> $TMPDIR/ifcfg-eth0
echo "ONBOOT=yes" >> $TMPDIR/ifcfg-eth0
else
cp "ifcfg-eth0" $TMPDIR/ifcfg-eth0
fi
cp $TMPDIR/ifcfg-eth0 $TMPDIR/etc/sysconfig/networking/devices/ifcfg-eth0
cp $TMPDIR/ifcfg-eth0 $TMPDIR/etc/sysconfig/networking/profiles/default/ifcfg-eth0
mv $TMPDIR/ifcfg-eth0 $TMPDIR/etc/sysconfig/network-scripts/ifcfg-eth0
echo "NETWORKING=yes" > $TMPDIR/etc/sysconfig/network
echo "HOSTNAME=localhost.localdomain" >> $TMPDIR/etc/sysconfig/network
echo "" > $TMPDIR/etc/resolv.conf
# clear rest of image
dd if=/dev/zero of=$TMPDIR/blank >& /dev/null
sync
rm $TMPDIR/blank
umount $TMPDIR/proc
umount $TMPDIR
rmdir $TMPDIR
echo "Done!"