bash自动化 定制Linux属于自己的CentOS 系统
bash自动化 定制Linux属于自己的CentOS 系统,直接上图
1、首先在宿主机创建目录,/mnt/boot /mnt/sysroot,格式化磁盘将其挂载
1、相对应的函数:
mkdevice () {
dd if=/dev/zero of=$1 bs=512 count=1 &> /dev/null
[ $? -ne 0 ] && return 67
echo '
n
p
1
+500M
n
p
2
+9G
w' | fdisk $1 &> /dev/null
[ $? -ne 0 ] && return 68
sync
sleep 2
mkfs.ext4 ${1}1 &> /dev/null
PAPT1=$?
mkfs.ext4 ${1}2 &> /dev/null
PAPT2=$?
[ $PAPT1 -ne 0 -o $PAPT2 -ne 0 ] && return 69
}
umountall () {
for PAPT in `fdisk -l $1 | grep -o "${1}[0-9]\{1,\}"`; do
fuser -km $PAPT &> /dev/null
umount $PAPT &> /dev/null
done
}
mountall () {
[ -d $BOOT ] || mkdir -p $BOOT
[ -d $SYSROOT ] || mkdir -p $SYSROOT
mount ${1}1 $BOOT
mount ${1}2 $SYSROOT
}
2、安装grub程序,我们这里是安装到/mnt/boot
2、相对应的函数:
instgrub () {
GRUBROOT=`dirname $BOOT`
grub-install --root-directory=$GRUBROOT $1
}
3、创建系统所需的目录
3与其对应的函数
mkrootfs () {
mkdir -p $SYSROOT/{proc,boot,sys,dev,tmp,lib,lib64,bin,sbin,root,home,opt,mnt,media,misc} &> /dev/null
mkdir -p $SYSROOT/usr{share/{man,doc},bin,sbin,local,src,lib,lib64} &> /dev/null
mkdir -p $SYSROOT/var/{log,run,lock,spool,tmp} &> /dev/null
mkdir -p $SYSROOT/lib/{modules,module} &> /dev/null
mkdir -p $SYSROOT/lib64/{modues,module} &> /dev/null
cp /lib/modules/2.6.32-431.el6.x86_64/kernel/drivers/net/e1000/e1000.ko $SYSROOT/lib/modules/
cp /boot/vmlinuz-2.6.32-431.el6.x86_64 $BOOT/vmlinuz
cp /boot/initramfs-2.6.32-431.el6.x86_64.img $BOOT/initramfs.img
echo -e "default=0 \ntimeout=5 \ntitle MyCentos root (hd0,0) \nkernel /vmlinuz ro root=/dev/sda2 quiet selinux=0 init=/sbin/init
initrd /initramfs.img " > $BOOT/grub/grub.conf
touch $SYSROOT/sbin/init && cat /root/init > $SYSROOT/sbin/init && chmod +x $SYSROOT/sbin/init
}
4复制kernel 到目标主机
4、与其对应的函数
mkrootfs () {
mkdir -p $SYSROOT/{proc,boot,sys,dev,tmp,lib,lib64,bin,sbin,root,home,opt,mnt,media,misc} &> /dev/null
mkdir -p $SYSROOT/usr{share/{man,doc},bin,sbin,local,src,lib,lib64} &> /dev/null
mkdir -p $SYSROOT/var/{log,run,lock,spool,tmp} &> /dev/null
mkdir -p $SYSROOT/lib/{modules,module} &> /dev/null
mkdir -p $SYSROOT/lib64/{modues,module} &> /dev/null
cp /lib/modules/2.6.32-431.el6.x86_64/kernel/drivers/net/e1000/e1000.ko $SYSROOT/lib/modules/
cp /boot/vmlinuz-2.6.32-431.el6.x86_64 $BOOT/vmlinuz
cp /boot/initramfs-2.6.32-431.el6.x86_64.img $BOOT/initramfs.img
echo -e "default=0 \ntimeout=5 \ntitle MyCentos root (hd0,0) \nkernel /vmlinuz ro root=/dev/sda2 quiet selinux=0 init=/sbin/init
initrd /initramfs.img " > $BOOT/grub/grub.conf
touch $SYSROOT/sbin/init && cat /root/init > $SYSROOT/sbin/init && chmod +x $SYSROOT/sbin/init
}