This script is a file system built on Linux. On the official UML website, I have the following problems:
[root@zhoutianzuo azuo]# bash install_fs.sh Filesystem already createdFilesystem already mountedgoing to fetchUsage: 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.
The source script is as follows:
#!/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" ]; thenecho "grep $1 $PACKAGES | grep $2 | wc -l"firpmfilescount=`grep ^$1 $PACKAGES | grep $2 | wc -l`#echo "filecount=$rpmfilescount"if [ "$rpmfilescount" -eq 0 ]; thenif [ ! -z "$DEBUG" ]; thenecho "No RPMs found for $1 - $2"fielseif [ "$rpmfilescount" == "1" ]; then# echo "Found "`$grep`rpmfile=`grep ^$1 $PACKAGES | grep $2`elseif [ ! -z "$DEBUG" ]; thenecho "Too many packages matching $1: $rpmfilescount"firpmfile=`grep ^$1 $PACKAGES | grep $2 | sort | head -n 1`fifi}function getRPM() {for arch in $ARCHES; dodoGetRPM $1- $archif [ ! -z "$rpmfile" ]; thenreturnfidonefor arch in $ARCHES; dodoGetRPM $1 $archif [ ! -z "$rpmfile" ]; thenreturnfidoneecho "No RPM package found for $1"exit 1}function fetch() {base=$1getRPM $baselocal=$RPM_DIR"/$rpmfile"echo "fetch() base=$base, local=$local, rpmfile=$rpmfile"if [ ! -e "$local" ]; thenecho "Downloading $base ($rpmfile)"wget -c -O $local "$MIRROR$rpmfile"fi}function rpminstall() {base=$1fetch $basestripped=`echo $base | sed 's+\.noarch++g' | sed 's+\.$ARCH++g'`exists="0"if [ ! -z "$DUPES" ]; thenexists=`$RPMCMD -qa --root $TMPDIR | grep $stripped | wc -l`fiif [ "$exists" -ne "0" ]; thenecho "skipped: $stripped"else$RPMCMD -Uvh --root $TMPDIR $localrm -fr $TMPDIR/var/lib/rpm/__db*fi}function rpminstallgroup() {echo "SIZE="$#list=""while [ $# -ne 0 ]dopackage=$1fetch $packagestripped=`echo $1 | sed 's+\.noarch++g' | sed 's+\.$ARCH++g'`exists="0"if [ ! -z "$DUPES" ]; thenexists=`$RPMCMD -qa --root $TMPDIR | grep $stripped | wc -l`fiif [ "$exists" -ne "0" ]; thenecho "skipped: $stripped"elselist="$list $local"fishiftdone# echo"installing list=$list"# echo "installcommand=$RPMCMD -ivh --root $TMPDIR $list"$RPMCMD -Uvh --root $TMPDIR $listrm -fr $TMPDIR/var/lib/rpm/__db*}# Making the filesystemif [ ! -e "$ROOT_FS" ]; thendd if=/dev/zero of=$ROOT_FS bs=1024 count=1 seek=1572864mkfs.ext4 -L ROOT -F $ROOT_FSelseecho "Filesystem already created"fiif [ ! -e "$TMPDIR" ]; thenmkdir $TMPDIRmount -o loop $ROOT_FS $TMPDIRelseecho "Filesystem already mounted"fimkdir -p $RPM_DIR#rpm bug?mkdir -p $TMPDIR/var/lock/rpmmkdir -p $TMPDIR/var/lib/rpmif [ ! -e "$TMPDIR/var/lib/rpm" ]; thenecho "initrpm:"echo "$RPMCMD --initdb --root $TMPDIR"$RPMCMD --initdb --root $TMPDIRrm -fr $TMPDIR/var/lib/rpm/__db*fiif [ ! -e "$TMPDIR/dev" ]; thenmkdir $TMPDIR/devmknod $TMPDIR/dev/null c 1 3fi#Get the list of packagesif [ ! -f $PACKAGES ]; then if [ -x "`which ${TEXT_BROWSER}`" ]; then${TEXT_BROWSER} -dump $MIRROR |grep rpm\$ |awk -F "CentOS/" '{print $2}' >$PACKAGES elseecho "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 fifiecho "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 $localrm -fr $TMPDIR/var/lib/rpm/__db*mkdir -p $TMPDIR/etc# create an /etc/fstabecho "LABEL=ROOT / auto defaults 1 1" >>$TMPDIR/etc/fstabecho "none /dev/pts devpts gid=5,mode=620 0 0" >>$TMPDIR/etc/fstabecho "none /proc proc defaults 0 0" >>$TMPDIR/etc/fstabecho "tmpfs /tmp tmpfs defaults,size=768M 0 0" >>$TMPDIR/etc/fstabmkdir -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; dofetch $package$RPMCMD -Uvh --force --noscripts --nodeps --root $TMPDIR $localrm -fr $TMPDIR/var/lib/rpm/__db*donemkdir $TMPDIR/etc/profile.d$CHROOTCMD $TMPDIR useradd rpmpackages="rpm rpm-libs"for package in $packages; dofetch $package$RPMCMD -Uvh --force --nodigests --noscripts --nodeps --root $TMPDIR $localrm -fr $TMPDIR/var/lib/rpm/__db*done# some extraspackages="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; dorpminstall $packagedonerpminstallgroup libproxy libproxy-bin libproxy-pythonrpminstallgroup rpm rpm-libsfor package in pth pinentry gnupg2 gpgme pygpgme neon rpm-python yum-metadata-parser; dorpminstall $packagedonerpminstallgroup yum-[0-9] yum-plugin-fastestmirrorfor package in yum-plugin-keys yum-plugin-post-transaction-actions yum-plugin-protectbase yum-utils; dorpminstall $packagedoneecho "cleaning rpm db"# re-install yum/rpm from inside...mount -t proc none $TMPDIR/proccp /etc/resolv.conf $TMPDIR/etcrm -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}#donerm -fr $TMPDIR/var/cache/yum$CHROOTCMD $TMPDIR rpm --rebuilddbfetch centos-releasecp $local $TMPDIR/$CHROOTCMD $TMPDIR rpm -Uvh --nofiledigest --nodigest --nosignature --force /centos-release*rpmrm $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 updaterm -fr $TMPDIR/var/cache/yum# ldconfig the newly installed librariesldconfig -r $TMPDIR# create the ubd devicesif [ ! -e "$TMPDIR/dev/ubda" ]; thenif [ -e "$TMPDIR/dev" ]; thenfor ((i=0; i<8; i++))dolet=$(echo $i|tr '0-7' 'a-z')mknod $TMPDIR/dev/ubd$let b 98 $[16 * $i]for ((p=1; p<=16; p++)) domknod $TMPDIR/dev/ubd$let$p b 98 $[16 * $i + $p]donedoneelseecho "/dev not found!"exit 1fifi# basic devices so we can boot without an initrd:cp -avpr /dev/console /dev/null /dev/zero $TMPDIR/devmkdir $TMPDIR/dev/pts# use yum to update the systemecho "/dev/ROOT/ ext4 rw 0 0" > $TMPDIR/etc/mtab# tweak the inittab to only use tty0 and add it to securettyfor cf in /etc/init/start-ttys.conf /etc/sysconfig/init; domv $cf $cf.bakcat $cf.bak | sed 's/1-6/0/g' > $cfrm $cf.bakdoneecho "tty0" >> /etc/securetty#enable shadow passwords$CHROOTCMD $TMPDIR pwconv#Remove the root passwordsed -i -e "s/^root:\*:/root::/" $TMPDIR/etc/shadow# We need an /etc/hosts file!echo "127.0.0.1localhostlocalhost.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 dhclientif [ ! -f "ifcfg-eth0" ]; then echo "DEVICE=eth0" >> $TMPDIR/ifcfg-eth0 echo "BOOTPROTO=dhcp" >> $TMPDIR/ifcfg-eth0 echo "ONBOOT=yes" >> $TMPDIR/ifcfg-eth0else cp "ifcfg-eth0" $TMPDIR/ifcfg-eth0ficp $TMPDIR/ifcfg-eth0 $TMPDIR/etc/sysconfig/networking/devices/ifcfg-eth0cp $TMPDIR/ifcfg-eth0 $TMPDIR/etc/sysconfig/networking/profiles/default/ifcfg-eth0mv $TMPDIR/ifcfg-eth0 $TMPDIR/etc/sysconfig/network-scripts/ifcfg-eth0echo "NETWORKING=yes" > $TMPDIR/etc/sysconfig/networkecho "HOSTNAME=localhost.localdomain" >> $TMPDIR/etc/sysconfig/networkecho "" > $TMPDIR/etc/resolv.conf# clear rest of imagedd if=/dev/zero of=$TMPDIR/blank >& /dev/nullsyncrm $TMPDIR/blankumount $TMPDIR/procumount $TMPDIRrmdir $TMPDIRecho "Done!"