grub-mkrescue —modules=“multiboot sh fat scsi ext2 elf pxe echo ntfs chain cat blacklist memdisk ls linux boot reboot biosdisk acpi loopback tar usb iso9660 search search_fs_uuid” —output=grub2.img source_file_to_put
sudo dd if=grub2.img of=/dev/fd0 bs=512 cout=2880
source_file_to_put: 放置一些文件资源,比如自动安装脚本;之后指定Grub安装菜单的timeout,就可以自动安装系统了。
使用案例:
安装好ESXi Server,在datastore上部署好CentOS 5.3的iso,然后下载vSphere的CLI使可以用命令行控制ESXi,可以用pyVmomi封装一层更好用。
# install centos 5.3
# create virtual machine
./vc_createvm.py -H "$host" -u "$username" -p "$password" -m $machinename --memory=1024 --cpus=1 --disk=8192
# insert installation media
./vm_cdrom.py -H "$host" -u "$username" -p "$password" -m $machinename -i $isofile
# insert a bootable floppy to start installation automatically
# grub2 is recommended
./vm_floppy.py -H "$host" -u "$username" -p "$password" -m $machinename -i $flpfile
# start install
./vm_poweron.py -H "$host" -u "$username" -p "$password" -m $machinename
# notice that in the install process, the post installation should
# modify /etc/rc.local to prepare a script which installing vmware
# tools at next time the CentOS start to work and it should be just
# run only once.
# For example:
# cat > /usr/local/bin/firstboot.sh << EOF!!!
# #!/bin/bash
# if [ ! -f \$flag ]; then
# exec > \$flag 2>&1
# mount /dev/cdrom /mnt
# vmtools=\$(basename \`ls /mnt/VM*\`)
# cp /mnt/\$vmtools /tmp/\$vmtools
# cd /tmp
# tar zxf \$vmtools
# cd /tmp/vmware-tools-distrib
# ./vmware-install.pl -d
# touch /usr/local/bin/firstboot.log
# fi
# EOF!!!
# chmod 755 /usr/local/bin/firstboot.sh
# echo /usr/local/bin/firstboot.sh >> /etc/rc.local
# wait
./vm_waitforpoweroff.py -H "$host" -u "$username" -p "$password" -m $machinename
# install vmware tools
# eject floppy and insert installation media
./vm_floppy.py -H "$host" -u "$username" -p "$password" -m $machinename
./vm_cdrom.py -H "$host" -u "$username" -p "$password" -m $machinename -i "[] /usr/lib/vmware/isoimages/linux.iso"
# start virtual machine to launch the new CentOS and install vmware tools
./vm_poweron.py -H "$host" -u "$username" -p "$password" -m $machinename
按最上访提到的方法创建一个软盘文件,里面的ks.cfg放在根目录,然后配置好grub.cfg,运行上面的脚本就可以自动安装CentOS了,末尾连vmware tools也装了,这样不用登录vSphere Client也可以找到这台虚拟机的IP地址了。
#!/bin/sh
# grub.cfg
default=0
timeout=10
menuentry "install centos" {
insmod ata
set root=(ata0)
linux /isolinux/vmlinuz ks=hd:fd0:/ks.cfg
initrd /isolinux/initrd.img
boot
}
# ks.cfg
# Kickstart file automatically generated by anaconda.
install
cdrom
autopart
timezone --utc Europe/London
lang en_US.UTF-8
keyboard us
xconfig --startxonboot
network --device eth0 --bootproto dhcp
rootpw --iscrypted $1$/9pfhev5$IQFIVMyPIWNdYtoG0QX.2.
firewall --enabled --port=22:tcp
authconfig --enableshadow --enablemd5
selinux --enforcing
bootloader --location=mbr --driveorder=sda --append="rhgb quiet"
# The following is the partition information you requested
# Note that any partitions you deleted are not expressed
# here so unless you clear all partitions first, this is
# not guaranteed to work
clearpart --all --drives=sda
#part /boot --fstype ext3 --size=100 --ondisk=sda
#part pv.2 --size=0 --grow --ondisk=sda
#volgroup VolGroup00 --pesize=32768 pv.2
#logvol swap --fstype swap --name=LogVol01 --vgname=VolGroup00 --size=1000 --grow --maxsize=4032
#logvol / --fstype ext3 --name=LogVol00 --vgname=VolGroup00 --size=1024 --grow
%packages
@office
@development-libs
@editors
@system-tools
@text-internet
@dns-server
@gnome-desktop
@dialup
@core
@authoring-and-publishing
@base
@network-server
@games
@java
@legacy-software-support
@base-x
@graphics
@printing
@server-cfg
@sound-and-video
@admin-tools
@development-tools
@graphical-internet
perl-Convert-ASN1
festival
audit
kexec-tools
device-mapper-multipath
vnc-server
xorg-x11-server-Xnest
xorg-x11-server-Xvfb
libsane-hpaio
imake
gettext-devel
parted-devel
kernel-devel
perl-XML-Parser
dejagnu
sharutils
e2fsprogs-devel
%post
/bin/mkdir /build
echo "/build /etc/auto.tc" > /etc/auto.master
/bin/echo "toolchain -ro,soft,intr tc-fs.example.org:/build/toolchain" >> /etc/auto.tc
/sbin/chkconfig --del yum-updatesd
/sbin/chkconfig --del bluetooth
/sbin/chkconfig --del hidd
/sbin/chkconfig --del hplip
/sbin/chkconfig --del restorecond
/sbin/chkconfig --del rpcgssd
/sbin/chkconfig --del rpcidmapd
/sbin/chkconfig --del setroubleshoot
/sbin/chkconfig --del yum-updatesd
cat > /usr/local/bin/firstboot.sh << EOF!!!
#!/bin/bash
flag="/usr/local/bin/firstboot.log"
if [ ! -f \$flag ]; then
exec > \$flag 2>&1
mount /dev/cdrom /mnt
vmtools=\$(basename \`ls /mnt/VM*\`)
cp /mnt/\$vmtools /tmp/\$vmtools
cd /tmp
tar zxf \$vmtools
cd /tmp/vmware-tools-distrib
./vmware-install.pl -d
touch /usr/local/bin/firstboot.log
fi
EOF!!!
chmod 755 /usr/local/bin/firstboot.sh
echo /usr/local/bin/firstboot.sh >> /etc/rc.local