#!/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