這篇會一直修改到步驟都正確為止

0. 準備相關東西

mkdir /mfsroot
mkdir -p /srv/pxe/tftpboot/pxelinux.cfg
mkdir -p /srv/pxe/tftpboot/freebsd
mkdir -p /srv/pxe/OS/FreeBSD/8.1-RELEASE-amd64

1. 製作 mfsroot , 參考文獻

cd /tmp
dd if=/dev/zero of=mfsroot bs=1k count=9k
mdconfig -at vnode -f mfsroot -u 2
bsdlabel -r -w /dev/md2
newfs -U -m0 /dev/md2
mount /dev/md2 /mfsroot
cd /mfsroot
mkdir auto boot dev etc jumpstart mnt proc ramdisk root usr
ln -s jumpstart bin
ln -s jumpstart sbin
ln -s jumpstart stand
ln -s jumpstart usr/bin
ln -s jumpstart usr/sbin
ln -s ramdisk/var var
ln -s ramdisk/var/tmp tmp
 
cd /boot
cp boot boot0 bot0sio boot1 boot2 mbr /mfsroot/boot
 
cd /etc
cp group login.conf login.conf.db master.password netconfig passwd pwd.db services spwd.db ttys /mfsroot/etc
 
cd /usr/share/misc
cp termcap* /mfsroot/etc
 
cd /usr/src
cp -R rescue jumpstart

參考文獻 6.2.2. Creating a bootcrunch file from the rescue make files 產生必要的執行檔, 但不建議用原作的 Makefile , 會讓 sed 出問題, 原因為包進 grep 時, 讓 sed 就使用者會 core dump, 我的設定檔晚一點會放上來

cd /usr/src/jumpstart
make obj && make
make DESTDIR=/mfsroot install

2. 編輯 /mfsroot/etc/rc.jumpstart, 用於產生 ramdisk, 及其他相關的檔案目錄

mdconfig -a -t malloc -s 16M -u 10
newfs -m0 /dev/md10
mount /dev/md10 /ramdisk
 
mkdir -p /ramdisk/var/db
mkdir -p /ramdisk/var/empty
mkdir -p /ramdisk/var/tmp
chmod 1777 /ramdisk/var/tmp
sh /auto/install.sh

3. 編輯 /mfsroot/auto 下幾個檔案 install.sh memory.sh partition.sh sed.disks
sed.disks

s/..*: //
s/ /\
/g

install.sh

#!/bin/sh
version=`uname -r`
arch=`uname -m`
 
exec tput clear
 
echo "
We will install FreeBSD $version $arch on this machine.
                                                        by cytseng
"
 
. /auto/partition.sh
 
echo "Prepare to install FreeBSD's basic binaries"
echo ""
 
mkdir /ramdisk/cdrom
 
DESTDIR=/mnt
 
mount /dev/${disks}a /$DESTDIR
 
ifn=`ifconfig -l | awk '{print $1}'`
 
dhclient $ifn > /dev/null 2>&1
 
mount_nfs 192.168.4.249:/srv/pxe/OS/FreeBSD/${version}-${arch} /ramdisk/cdrom
 
cd /ramdisk/cdrom/$version/base ; cat base.?? | tar --unlink -xpzf - -C $DESTDIR
cd /ramdisk/cdrom/$version/kernels ; cat generic.?? | tar --unlink -xpzf - -C $DESTDIR/boot
 
cd /$DESTDIR/boot ; rmdir kernel ; mv GENERIC kernel
 
echo "Set options in /etc/fstab and /etc/rc.conf"
echo ""
echo "/dev/${disks}a        /       ufs rw      1   1" > /$DESTDIR/etc/fstab
 
echo "
#
hostname=\"test\"
 
ifconfig_${ifn}=\"DHCP\"
" > /$DESTDIR/etc/rc.conf
 
echo 'sshd_enable="YES"
usbd_enable="NO"
sendmail_enable="NO"
sendmail_submit_enable="NO"
sendmail_outbound_enable="NO"
sendmail_msp_queue_enable="NO"
fsck_y_enable="YES"
background_fsck="NO"
clear_tmp_enable="YES"
' >> /$DESTDIR/etc/rc.conf
 
echo "Finish installing....."
echo "And reboot this machine!!!"
reboot

memory.sh

#!/bin/sh
echo "Detect the physical memory capacities
and prepare to set SWAP capacities on the HDD"
echo ""
memory=`sysctl hw.realmem | awk '{print $2}'`
memory=`expr $memory / 1024`
memory=`expr $memory / 1024`
 
get_real_memory ()
{
    num=$1
 
    quotient=`expr $num / 2`
    num=$quotient
    memory=2
 
    while [ $quotient -gt 2 ]
    do
        quotient=`expr $num / 2`
        num=$quotient
        memory=`expr $memory \* 2`
    done
 
    memory=`expr $memory \* 2`
    echo $memory
}
 
memory=`get_real_memory $memory`
 
swap=`expr $memory \* 1024`
 
if [ $memory -le 16384 ]; then
    swap=`expr $swap \* 2`
fi

partition.sh

#!/bin/sh
echo "Prepare HDD to install FreeBSD"
echo ""
disks=`sysctl kern.disks | sed -f /auto/sed.disks | sort | tr '\012' ' ' | awk '{print $1}'`
 
dd if=/dev/zero of=/dev/$disks bs=1k count=1 > /dev/null 2>&1
bsdlabel -Brw $disks auto
 
C='/ramdisk/c'
 
if [ -e $C ]; then
    rm $C
fi
 
bsdlabel /dev/$disks | sed "/c:..*/w $C" > /dev/null
 
total_size=`cat $C | sed 's/  *c:  *\([0-9][0-9]*\)..*/\1/'`
 
. /auto/memory.sh
 
label='/ramdisk/label'
if [ -e $label ]; then
    rm -f $label
fi
 
amount=`expr $total_size - $swap`
 
echo "a:  $amount  0  4.2BSD
b:  $swap  $amount swap" > $label
 
cat $C >> $label
 
bsdlabel -R /dev/$disks $label
 
newfs -U /dev/${disks}a > /dev/null 2>&1 # root

經過以上的修改編輯, mfsroot 製作完成

3. 製作 syslinux 可以使用的 .img 檔案, 參考文件, 這篇寫得很簡單, 依上面的步驟就可以產生要的 .img 檔, 這裡我使用的是 8.1 RELEASE boot-only 來改

假設檔案放在 /srv/pxe/tftpboot/freebsd/8.1-RELEASE-amd64.img

mdconfig -at vnode -f /srv/pxe/tftpboot/freebsd/8.1-RELEASE-amd64.img -u 0
mount /dev/md0a /mnt

4. 修改 /mnt/boot/lodaer.conf

mfsroot_load="YES"
mfsroot_type="mfs_root"
mfsroot_name="/boot/mfsroot"
 
init_path="/sbin/init"
init_shell="/stand/sh"
init_script="/etc/rc.jumpstart"
 
vfs.root.mountfrom.options="rw"
vfs.root.mountfrom="ufs:/dev/md0"

其中 vfs.root.mountfrom 可能不用設定

5. 把 mfsroot copy 至 .img 內

cp /tmp
gzip -9 mfsroot
cp mfsroot.gz /mnt/boot

經過以上, 就把 FreeBSD 自動安裝的所有必要的步驟都完成了

配合前一篇 syslinux 的設定, 就可以讓一台完全新的機器透過 PXE 來安裝 FreeBSD 8.1 Release

而這些步驟可以改成 x86 版本或是其他 FreeBSD release 的版本

雖然利用 google 找了 n 百篇文章, 但只有以下四篇是這篇文章中最有用的部份

a. http://www.unixarea.de/installEeePC.txt

b. http://www.daemonforums.org/showthread.php?t=1538

c. http://www.locolomo.org/howto/pxeboot/automatic-installation.html

d. http://syslinux.zytor.com/wiki/index.php/FreeBSD_disk_image_creation

e. http://www.freebsd.org/doc/handbook/disks-adding.html

若有相關問題, 歡迎提問, 也歡迎轉載, 但請註明出處

我相信這篇文章可以讓管很多機器的苦力 MIS 輕鬆許多, 而且可以一次把所有伺服器都上架後, power on 之後, 連 console 就不用看, 它就會自動安裝到好

希望對有需要的人有幫助

但我目前知道的問題

1. 需原本 pxe boot 改成 local disk boot 以免進入無限自動安裝的循環中

TODO:

1. 有空有閒會把整個步驟改成一個 script 完成

2. 製作 boot on ZFS 自動安裝的方法

終極版 auto install FreeBSD beta release

發表迴響

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Verified by MonsterInsights