FreeBSD10.0 PXE安装
FreeBSD10.0
PXE安装
官方网站:http://www.freebsd.org/
参考文档:http://www.freebsd.org/doc/en_US.ISO8859-1/books/handbook/
环境
物理机:
CentOS Linux 7.0(kvm, dhcp, nfs, http) 192.168.8.254
虚拟机:
FreeBSD10.0 192.168.8.201,192.168.8.202
第一部分:FreeBSD PXE环境搭建
**************************
NFS
**************************
FreeBSD可以通过NFS方式挂载根/,前提是有一个NFS共享的FreeBSD完整运行环境(类似于live CD)
一.编译FreeBSD环境
说明:为了最大限度地与FreeBSD官方handbook保持一致,我这里在KVM虚拟机(192.168.8.201)里事先通过ISO安装了FreeBSD10.0(192.168.8.201)。并且在选择安装包时勾选"source"这一项,因为只有安装了source.txz在/usr/src目录才会有FreeBSD的源码。当然,源码可以通过SVN等其它方式sync。通过光盘或ISO安装FreeBSD这里就不细说,个人觉得安装流程与linux非常相似甚至更简单。
登录FreeBSD(192.168.8.201)系统后,按以下步骤操作。
1.删除/usr/obj目录
If the build world process has previously been run on this system, a copy of the previous build may still exist in /usr/obj. To speed up the new build world process, and possibly save some dependency headaches, remove this directory if it already exists:
# rm -rf /usr/obj
2.编译compiler及userland
Compile the new compiler and a few related tools, then use the new compiler to compile the rest of the new world. The result is saved to /usr/obj.
注意:编译时长似机器性能而定,整个make流程走完可能需要花半天时间。官方handbook中明确指出不要使用make world
Do Not Use make world:
Some older documentation recommends using make world. However, that command skips some important steps and should only be used by experts. For almost all circumstances make world is the wrong thing to do, and the procedure described here should be used instead.
3.用新的compiler编译新kernel
Use the new compiler residing in /usr/obj to build the new kernel, in order to protect against compiler-kernel mismatches. This is necessary, as certain memory structures may have changed, and programs like ps and top will fail to work if the kernel and source code versions are not the same.
4.安装FreeBSD到NFS共享目录
说明:官方handbook中是直接在FreeBSD上搭建一整套pxe环境,包括NFS,我这里只要其中的NFS共享源,所以选择哪个目录无所谓,我在根目录下建了个/chroot目录。
提示:可以设置一个全局变量DESTDIR,这样就可以简写来省略 DESTDIR=/chroot
When using sh(1):
If csh/tcsh is used, execute this instead:
5.修改/chroot/etc/fstab文件
指定NFS共享主机及目录,这样FreeBSD才能正确挂载根目录
Edit ${NFSROOTDIR}/etc/fstab and create an entry to mount the root file system over NFS:
Replace myhost.example.com with the hostname or IP address of the NFS server. In this example, the root file system is mounted read-only in order to prevent NFS clients from potentially deleting the contents of the root file system.
实验环境中是192.168.8.254:/var/lib/tftpboot/freebsd
6.设置root密码
Set the root password in the PXE environment for client machines which are PXE booting :
7.允许root ssh登录(可选)
If needed, enable ssh(1) root logins for client machines which are PXE booting by editing ${NFSROOTDIR}/etc/ssh/sshd_config and enabling PermitRootLogin. This option is documented in sshd_config(5).
a.在配置文件sshd_config中加入以下选项
PermitRootLogin yes
b.sshd加入开机启动项
8.设置主机名(可选)
9.定义/etc/rc环境
When booting from an NFS root volume, /etc/rc detects the NFS boot and runs /etc/rc.initdiskless. In this case, /etc and /var need to be memory backed file systems so that these directories are writable but the NFS root directory is read-only:
When the system boots, memory file systems for /etc and /var will be created and mounted and the contents of the cpio.gz files will be copied into them.
说明:/etc/rc.initdiskless文件定义了FreeBSD的diskless启动流程,NFS是只读共享,FreeBSD启动后所有的文件都是只读的,但不同主机因硬件差异所产生的差异化数据及其它个性化数据需要空间来临时存放,/var/目录就是存放经常变动的数据,所以在启动时从conf/base/var.cpio.gz文件中读取并以读写的方式挂载到var目录,至系统启完后/etc,/var目录是可写的。
注意:后期要对安装源配置(启服务,设置主机名,网络等)作的修改都要写入conf/base/etc.cpio.gz文件,这样才会在系统启动时正确加载。如,在物理机上为FreeBSD启用sshd开机启动并设置主机名
root@jun-live:freebsd#pwd
/var/lib/tftpboot/freebsd
root@jun-live:freebsd#cd conf/base/
root@jun-live:base#ls
etc.cpio.gz
root@jun-live:base#gunzip etc.cpio.gz
root@jun-live:base#file etc.cpio
etc.cpio: ASCII cpio archive (pre-SVR4 or odc)
注:FreeBSD默认采用老的cpio格式odc,所以在修改完后也用相同的格式打包
root@jun-live:base#cpio -id
3061 blocks
root@jun-live:base#rm -rf etc.cpio
root@jun-live:base#echo 'sshd_enable="YES"' >>etc/rc.conf
root@jun-live:base#echo 'hostname="FreeBSD"' >>etc/rc.conf
root@jun-live:base#cat etc/rc.conf
sshd_enable="YES"
hostname="FreeBSD"
root@jun-live:base#find etc/|cpio -ov -H odc >etc.cpio
etc/
... ...
etc/bluetooth/hosts
3061 blocks
root@jun-live:base#gzip etc.cpio
10.将/chroot目录打包后scp到NFS共享主机
二.NFS共享FreeBSD安装源
在物理机CentOS Linux 7.0上操作
root@jun-live:~#tar -xvf freebsd_nfs_pxe.tar -C /var/lib/tftpboot/freebsd
root@jun-live:~#vim /etc/exports
/var/lib/tftpboot/freebsd
root@jun-live:~#exportfs -arv
说明:/var/lib/tftpboot/freebsd是FreeBSD的NFS共享源
**************************
TFTP
**************************
在物理机CentOS Linux 7.0上操作
root@jun-live:~#cat /etc/xinetd.d/tftp
# default: off
# description: The tftp server serves files using the trivial file transfer \
#
#
#
service tftp
{
}
启xinetd服务
root@jun-live:~#systemctl start xinetd
**************************
DHCP+PXE
**************************
在物理机CentOS Linux 7.0上操作
说明:我这里DHCP绑定了两台KVM虚拟机的MAC地址,只有这两台从网络引导时会进入FreeBSD,其它的会进入标准的pxelinux引导菜单,具体依情形而定。
dhcp最简配置如下,
subnet 192.168.8.0 netmask 255.255.255.0 {
}
group {
host client1 {
}
host client2 {
}
}
启dhcp服务
root@jun-live:~#systemctl start dhcpd
至此,FreeBSD的安装环境已经搭建完毕。
第二部分:通过PXE 安装FreeBSD
虚拟机192.168.8.202开机后会自动从网络引导通过NFS挂载FreeBSD的根目录直至进入系统登录console,输入正确的用户名和密码即可进入系统。实际上这就是一个live CD环境,里面的目录结构及操作和正常的安装版系统是一样的体验。要安装的话需要通过bsdinstall工具(FreeBSD9.0开始引进,用来取代FreeBSD8.x的sysinstall)
1.交互式安装
登录FreeBSD后,在命令行下输入bsdinstall就会跳出和通过ISO安装系统类似的界面,或者说更像FreeBSD网络引导镜像bootonly.iso。
特别注意,在选择安装源时,选"Other"然后输入本地源的URL路径(ftp,http)以指定FreeBSD安装时所需的MANIFEST,不支持NFS路径。我是通过apache来共享的安装源(FreeBSD10.0官方ISO挂载至/var/www/pub/ftp/freebsd目录)
http://192.168.8.254/ftp/freebsd/usr/freebsd-dist
只要安装源的路径指对了,安装过程和使用ISO安装相同。
![FreeBSD10.0 <wbr>PXE安装 FreeBSD10.0 <wbr>PXE安装](http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif)
通过dhcp+pxe+tftp正在加载内核
![FreeBSD10.0 <wbr>PXE安装 FreeBSD10.0 <wbr>PXE安装](http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif)
加载完成后可以看到引导启动项和ISO安装时一样
![FreeBSD10.0 <wbr>PXE安装 FreeBSD10.0 <wbr>PXE安装](http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif)
可以看到,这里正在通过NFS挂载根目录,并读取/conf/base/{etc,var}.cpio.gz释放到对应目录,还报了个没有设置hostname的WARNING,没关系,不会影响进系统,要设置的话请返回上文“设置主机名"。
![FreeBSD10.0 <wbr>PXE安装 FreeBSD10.0 <wbr>PXE安装](http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif)
![FreeBSD10.0 <wbr>PXE安装 FreeBSD10.0 <wbr>PXE安装](http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif)
![FreeBSD10.0 <wbr>PXE安装 FreeBSD10.0 <wbr>PXE安装](http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif)
这里己成功登录了live FreeBSD,只是没有主机名。
安装FreeBSD需要键入bsdinstall命令
![FreeBSD10.0 <wbr>PXE安装 FreeBSD10.0 <wbr>PXE安装](http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif)
键盘
![FreeBSD10.0 <wbr>PXE安装 FreeBSD10.0 <wbr>PXE安装](http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif)
主机名
![FreeBSD10.0 <wbr>PXE安装 FreeBSD10.0 <wbr>PXE安装](http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif)
通过"Other"自定义安装源
![FreeBSD10.0 <wbr>PXE安装 FreeBSD10.0 <wbr>PXE安装](http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif)
NFS路径不支持,如果安装源路径输入有误则会报上图的错误。
![FreeBSD10.0 <wbr>PXE安装 FreeBSD10.0 <wbr>PXE安装](http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif)
![FreeBSD10.0 <wbr>PXE安装 FreeBSD10.0 <wbr>PXE安装](http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif)
输入正确的安装源路径后即可进入安装界面(分区,建用户等)。
2.静默安装
bsdinstall提供了很多静默选项(键盘类型,时区,国家等),具体的可以man bsdinstall