menu

开发进行时...

crazy coder

Avatar

Convert FreeBSD ISO image for PXE bootstrap

Usually ISO's of most Linux distros use isolinux as boot loader, so these may be served up easily for pxe bootstrap through pxelinux/memdisk.
For FreeBSD this does not hold true, so the vanilla ISO's must be converted before they can be bootstrapped.
The basic idea is to convert the FreeBSD ISO into a harddisk image to circumvent a limitation in the ISO boot loader.
#1 Preparation
We need some staging directory where we can safely play around with the files.
#mkdir /tmp/build
#mkdir /tmp/build/iso.mnt
#mkdir /tmp/build/hd.mnt
#mkdir /tmp/build/mfs.mnt

#2 Get the ISO image
I usually use the bootonly image because of it's size (~45MB). It would work with the full ISO's of course, however will not only take longer to download via tftp but also require more memory on the client host.
#wget ftp://ftp.freebsd.org/pub/FreeBSD/releases/i386/ISO-IMAGES/8.0/8.0-RELEASE-i386-bootonly.iso -output-document=/tmp/build/8.0-RELEASE-i386-bootonly.iso

Now we need to attach md (memory device) to the ISO image and mount it.
#mdconfig -a -t vnode -f /tmp/build/8.0-RELEASE-i386-bootonly.iso md0 or
#mdconfig -a -t vnode -f /tmp/build/8.0-RELEASE-i386-bootonly.iso -u 0
#mount_cd9660 /dev/md0 /tmp/build/iso.mnt/

#3 Create a new harddisk image
Let's create a new harddisk image of 46MB in size. It make it a bit bigger than the original ISO so I could include furthe scripts with it.
#dd if=/dev/zero of=/tmp/build/8.0-RELEASE-i386-bootonly.img bs=1m count=46
Then we attach a md devicd to this one, too. Afterwards partition table and bsdlabel are initialized.
#mdconfig -a -t vnode -f /tmp/build/8.0-RELEASE-i386-bootonly.img md1
#fdisk -B -1 /dev/md1
#bsdlabel -B -w /dev/md1
Now we would need to edit the bsdlabel to add the a slice of type 4.2BSD covering the whole harddisk image.
#bsdlabel -e /dev/md1
a: 94192 16 4.2BSD

Finally, the slaved will be newfs'ed with UFS and mounted.
#newfs /dev/md1a
#mount /dev/md1a /tmp/build/hd.mnt

now the boot directory must be copied from the mounted ISO image onto the harddisk image.
#cp -r /tmp/build/iso.mnt/boot /tmp/build/hd.mnt/boot

#4 Optional: prepare SysInstall for automatic installations
This step is completely optional, though will allow us to place an install.cfg file to the image. SysInstall will use this file as comfiguration template for automatic installations. A sample of it is found within the source tree at /usr/src/usr.sbin/sysinstall/install.cfg. Optionally you can use mine as a starting point.

First we need to copy away mfsroot.gz into our build directory. It will then be extracted, attached to a vnode and mounted.
#cp /tmp/build/hd.mnt/boot/mfsroot.gz /tmp/build/hd.mnt
#gunzip /tmp/build/hd.mnt/mfsroot.gz
#mdconfig -a -t vnode -f /tmp/build/hd.mnt/mfsroot md2

Then add the install.cfg file to the mfsroot top level directory.
#cp /tmp/build/install.cfg /tmp/build/mfs.mnt
You are free to add further customizations, like self-build packages, to the mfsroot.
When finishing unmount the mfsroot device, detach it from the md device, compress and copy it back to the harddisk image.
#umount /tmp/build/mfs.mnt
#mdconfig -d -u 2
#gzip /tmp/build/mfsroot
#cp /tmp/build/mfsroot.gz /tmp/build/hd.mnt/boot

#5 Finalize
So let's clean up little.
#umount /tmp/build/hd.mnt
#mdconfig -d -u 1
#umount /tmp/build/iso.mnt
#mdconfig -d -u 0
Let's shift the harddisk image to the tftpd boot image directory, which is /var/tftpd/images for me.
#cp /tmp/build/8.0-RELEASE-i386-bootonly.img /var/tftpd/images

label freebsd8.0
kernel memdisk
append initrd=/images/8.0-RELEASE-i386-bootonly.img harddisk
note: if some errors occur during installation, try append initrd=/images/8.0-RELEASE-i386-bootonly.img harddisk raw

install.cfg

################################
# initialization
################################
debug=YES
nonInteractive=YES
noWarn=NO
tryDHCP=NO

################################
# disk setup
################################
bootManager=standard
diskInteractive=YES
diskPartitionEditor
diskInteractive=YES
diskLabelEditor

################################
# network setup
################################
hostname=myfreebsd
domainname=host.com
defaultrouter=ip-gateway
nameserver=ip-dns
ipaddr=ip-addr
netmask=255.255.255.0
netInteractive=YES

################################
# software selection
################################
_ftpPath=ftp://ftphost/pub/FreeBSD/releases/i386/
mediaSetFTPPassive
distSetDeveloper

################################
# commit pending tasks
################################
installCommit


好文章,就是有几处错误,导致最后失败:
a: 65520 16 4.2BSD (应是:a: 94192 16 4.2BSD,否则复制文件时会出现空间不够。)
#copy -r /tmp/build/iso.mnt/boot /tmp/build/hd.mnt/boot (应是:#cp -r /tmp/build/iso.mnt/boot /tmp/build/hd.mnt/boot)
#mfconfig -d -u 1 (应是:mdconfig -d -u 1)

谢谢指正,马上编辑改正过来~~:)