#!/bin/bash
#:Title: construction.sh
#:Path:/tmp/myscript/mklinux
#:Synopsis:
#:DateTime:2011-08-04 16:24:51
#:Version:Unknow
#:Author: redhat_hu
#:Option:None
read -p
"please input the disk name:" DISKNAME
#提示用戶指定要操做的磁盤;
if fdisk -l $DISKNAME | grep
"^$DISKNAME" &> /dev/
null;then
fdisk -l $DISKNAME | grep
"^$DISKNAME"
Adisk=`fdisk -l $DISKNAME | grep
"^$DISKNAME" | awk '{print $1}'`
for I
in $Adisk ;
do
if mount | grep
"$I" &> /dev/
null;then
mount | grep
"$I"
else
echo
"$I was not mounted."
fi
done
fi
#若是此硬盤中有新區,顯示分區信息,並判斷此中分區是否仍然處於掛載狀態;若是是,則顯示分區和對應的掛載點;不然,則說明分區沒有掛載;
function FRESH {
read -p
"Continue(y/N)?" CHOISE #提示用戶接下來操做會破壞硬盤上的全部數據,並詢問用戶是否繼續進行;
case $CHOISE
in
y|Y)
for I
in $Adisk;
do
if mount | grep
"$I" >/dev/
null;then
fuser -km $I
umount $I
fi
done
dd
if=/dev/zero of=$DISKNAME bs=512 count=1 &> /dev/
null #若是用戶選擇繼續,則抹除此硬盤上的全部分區信息(若是是仍處理掛載狀態的分區,則須要先卸載);不然退出;
echo ' #將此硬盤分爲三個主分區:第一個主分區,50M,ext3文件系統;第二個主分區,512M,ext3;文件系統第三個主分區,256M,swap文件系統;
n
p
1
+50m
n
p
2
+512m
n
p
3
+256m
t
3
82
w' |fdisk $DISKNAME &> /dev/
null
partprobe $DISKNAME
sleep 1
mke2fs -j ${DISKNAME}1 &> /dev/
null
mke2fs -j ${DISKNAME}2 &> /dev/
null
mke2fs -j ${DISKNAME}3 &> /dev/
null
return 0
;;
n|N)
return 1
;;
*)
return 2
;;
esac
}
function MOUNT {
if find /mnt/boot &> /dev/
null ;then
umount /mnt/boot &> /dev/
null
mount $1 /mnt/boot
else
mkdir /mnt/boot
umount /mnt/boot &> /dev/
null
mount $1 /mnt/boot
fi
if find /mnt/sysroot &> /dev/
null ;then
umount /mnt/sysroot &> /dev/
null
mount $2 /mnt/sysroot
else
mkdir /mnt/sysroot
umount /mnt/sysroot &> /dev/
null
mount $2 /mnt/sysroot
fi
}
#掛載到對應的文件目錄
function INIT {
if find [/mnt/sysroot &> /dev/
null || mount|grep
"/mnt/sysroot" &> /dev/
null];then
cd /mnt/sysroot
mkdir -p {boot,proc/sys/kernel,sys,dev,home,root,etc/{rc.d,sysconfig,init.d},bin,sbin,lib,usr/{bin,sbin,lib/modules,include},var/{log,run},tmp,mnt,opt,media}
chmod -R 1777 /mnt/sysroot/tmp
touch /mnt/sysroot/etc/inittab
touch /mnt/sysroot/proc/sys/kernel/hostname
chmod -R 1777 /mnt/sysroot/proc/sys/kernel/hostname
cat > /mnt/sysroot/etc/inittab <<EOF #建立inittab
id:3:initdefault:
si::sysinit:/etc/rc.d/rc.sysinit
l1:1:wait:/sbin/init -t1 S
l0:0:wait:/etc/rc.d/rc.shutdown
#add terminal 1-6
1:2345:respawn:/sbin/mingetty tty1
2:2345:respawn:/sbin/mingetty tty2
3:2345:respawn:/sbin/mingetty tty3
4:2345:respawn:/sbin/mingetty tty4
5:2345:respawn:/sbin/mingetty tty5
EOF
#add hostname
for MyLinux 設置系統名爲MyLinux
echo
"HOSTNAME=alex.mylinux.org" > /mnt/sysroot/etc/sysconfig/network
cat > /mnt/sysroot/etc/rc.d/rc.sysinit << EOF #建立rc.sysinit
#!/bin/bash
echo -e
"\t\tWelcome to \033[31;1mMyLinux\033[0m ..."
swapon -a
mount -n -o remount,rw / #以讀寫權限從新掛載根目錄
mount -n -a
#/bin/bash
EOF
chmod +x /mnt/sysroot/etc/rc.d/rc.sysinit
#add hostname
echo '. /etc/sysconfig/network' >> /mnt/sysroot/etc/rc.d/rc.sysinit
echo 'echo $HOSTNAME > /proc/sys/kernel/hostname' >> /mnt/sysroot/etc/rc.d/rc.sysinit #寫入主機名
#add rc.shutdown
for ini0 shutdown 添加關機命令
cat > /mnt/sysroot/etc/rc.d/rc.shutdown << EOF
#!/bin/bash
#
sync
sleep 1
umount /dev/sda1
umount /dev/sda2
swapoff -a
exec /sbin/halt -p
EOF
chmod +x /mnt/sysroot/etc/rc.d/rc.shutdown
# add root user's information 添加生成新系統的用戶
head -1 /etc/passwd > /mnt/sysroot/etc/passwd
head -1 /etc/shadow > /mnt/sysroot/etc/shadow
head -1 /etc/group > /mnt/sysroot/etc/group
head -1 /etc/gshadow > /mnt/sysroot/etc/gshadow
#add the login words
cat > /mnt/sysroot/etc/issue <<EOF
Welcome to mylinux !
EOF
#add PS1 定義PS1
cat > /mnt/sysroot/root/.bash_profile <<EOF
PS1='[\u@\h \W]\$'
EOF
touch /mnt/sysroot/etc/fstab #建立fstab
cat > /mnt/sysroot/etc/fstab << EOF
/dev/sda2 / ext3 defaults 0 0
/dev/sda1 /boot ext3 defaults 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
EOF
else
echo
"error!"
fi
}
COPYCOMMAND() {
if which $B ;then
COMMAND=`which $B | grep -o
"/.*"`
PTH=`which $B | grep -o
"/.*" |sed
"s@\(/[[:alpha:]]*\)/[[:alpha:]]*@\1@g"`
if find /mnt/sysroot$PTH &> /dev/
null;then
cp $COMMAND /mnt/sysroot$PTH
else
mkdir -p /mnt/sysroot$PTH
cp $COMMAND /mnt/sysroot$PTH
fi
else
echo
"wrong command"
fi
LIBS=`ldd $COMMAND | grep -o
"/.*" | awk '{print $1}'`
for I
in $LIBS;
do
[ ! -e /mnt/sysroot$I ] && cp $I /mnt/sysroot$I
done
}
#將輸入的指令以及相關庫文件,從宿主機複製到對應子系統目錄中
COPYNSS () {
#copy Name Nervice Switch files and links to MyLinux's lib
cp /usr/lib/libnss3.so /mnt/sysroot/usr/lib
cp /usr/lib/libnssckbi.so /mnt/sysroot/usr/lib
cp /usr/lib/libnssutil3.so /mnt/sysroot/usr/lib
cp -d /usr/lib/libnss_files.so /mnt/sysroot/usr/lib
cp -d /usr/lib/libnss_compat.so /mnt/sysroot/usr/lib
cp -d /lib/libnss_file* /mnt/sysroot/lib
cp -d /lib/libnss_compat* /mnt/sysroot/lib
cp /etc/nsswitch.conf /mnt/sysroot/etc
cp /lib/libpam.so.0 /mnt/sysroot/lib
cp /lib/libpam_misc.so.0 /mnt/sysroot/lib
cp /lib/libaudit.so.0 /mnt/sysroot/lib
#copy command 'login' and its lib files
cp /lib/libcrypt-2.5.so /mnt/sysroot/lib
cp /lib/libm.so.6 /mnt/sysroot/lib
cp -d /lib/libcrypt.so.1 /mnt/sysroot/lib
cp /root/login /mnt/sysroot/bin
#make symbolic link between bash and sh
for runlevel 1
cd /mnt/sysroot/bin
ln -s bash sh
}
#複製nsswitch相關庫文件
function KERNEL {
TMPDIR=`mktemp -d /tmp/mylinux` #建立一個臨時文件用於存放解壓後的initrd
cd $TMPDIR
zcat /boot/initrd-`uname -r`.img | cpio -id
sed -i 's@echo Scanning and configuring dmraid supported devices@#&@g' init
sed -i 's@echo Scanning logical volumes@#&@g' init
sed -i 's@lvm vgscan --ignorelockingfailure@#&@g' init
sed -i 's@echo Activating logical volumes@#&@g' init
sed -i 's@lvm vgchange -ay --ignorelockingfailure vol0@#&@g' init
sed -i 's@resume LABEL=SWAP-sda3@#&@g' init
sed -i
"s@\(mkrootdev.* \)/.*@\1sda2@g" init
find . | cpio -H newc -o --quiet | gzip -9 > /mnt/boot/initrd.gz #重新使用cpio封裝修改後的initrd
cp /boot/vmlinuz-`uname -r` /mnt/boot/vmlinuz
}
#修改內核以及initrd部分參數和內容
function GRUB {
MOUNTPOINT=`basename $2`
until [
"$MOUNTPOINT" ==
"boot" ];
do
read -p
"please modify the mountpoint." MOUNTPOINT
done
grub-install --root-directory=/mnt $1 #安裝grub的位置
cat > $2/grub/grub.conf << EOF #建立grub.conf
default=0
timeout=10
title mylinux
root (hd0,0)
kernel /$3 ro root=/dev/sda2
initrd /$4
EOF
}
#安裝grub
FRESH
MOUNT ${DISKNAME}1 ${DISKNAME}2
INIT
for B
in mingetty init bash mount umount halt shutdown poweroff halt reboot vi ls cat clear sync chmod runlevel swapoff hostname insmod rmmod swapon ;
do
COPYCOMMAND
done #將以上命令所有加入mini linux中
COPYNSS
#下列註釋代碼可實現從宿主機向mini linux命令的按需複製
#read -p
"input the command,'c' for skip:" B
#until [
"$B" ==
"c" ];
do
#COPYCOMMAND
#read -p
"input the command,'c' for skip:" B
#done
KERNEL
GRUB $DISKNAME /mnt/boot vmlinuz initrd.gz
rm -rf $TMPDIR #刪除臨時文件
sync
sync
sync