debian9.6搭建pxe服务器+preseed,并在虚拟机上测试(完善中)

首先感谢N(81855776)的帮助,跟他学到了很多。

一,介绍

        pxe(preboot execute envionment,预启动执行环境),很多人叫它无盘安装,就是不需要我们平时安装系统用光盘或者U盘就可以安装系统,而采用网络安装,就是用远端服务器来充当之前的盘。当然了,最重要的就是客户端(我们即将要安装系统的电脑)和服务器端(已经搭建好的pxe环境的电脑)之间的网络是通的,即可以传递数据。

        并非所有的电脑都支持pxe方式安装的,1,网卡上集成了pxe client,2,主板支持网卡启动。现在新一点的电脑都支持,我的电脑估计是比较早吧,13年初买的戴尔i5,暂时只支持legacy方式的network启动(即pxe),在uefi方式下没有。

        因为pxe服务器的原因,我将pxe方式装机过程分为3个阶段,

        第一阶段:dhcp

        客户端开机选择network启动,然后将网卡上的pxe client信息拷贝到内存上运行,发送dhcp广播信息,从dhcp服务器上获取一个ip,并告知tftp服务器的ip,如果有多个dhcp服务器,客户端会接受第一个dhcp服务器的ip。所以服务器端要有个dhcp服务器。

        第二阶段:tftp/mtftp

        网卡上存储空间有限,所以只能集成轻量级别的tftp客户端。客户端从第一阶段获取到ip之后就会去tftp服务器端获取内核,初始化程序,引导文件等。所以服务器端还要搭建一个tftp服务器。

        第三阶段:ftp/http/ntfs

        这个阶段可以使用ftp/http/ntfs中的一个来获取系统的安装文件,我这里使用ftp。所以服务器端还要搭建一个ftp服务器。

        这三个服务器搭建之后,pxe服务器就搭建好了,这个时候没有盘就可以安装,但是还是需要人去操作。接下来现在再写preseed文件,就可以完全自动化的安装系统了。

二,下载

         iso文件:

         这个是提供安装包的

         需要下载系统的iso文件,官网:https://mirrors.tuna.tsinghua.edu.cn/debian-cd/9.6.0/amd64/iso-dvd/

         我下载的是这个:debian-9.6.0-amd64-DVD-1.iso

         netboot文件:这个是提供Initrd文件的,因为cd或者dvd的initrd文件是在安装的过程会提示为探测到可用的光驱。

         下载:https://mirrors.tuna.tsinghua.edu.cn/debian/dists/Debian9.6/main/installer-amd64/20170615/images/netboot/

         有的人是直接用的netboot里的东西。

三,服务器搭建

        这个阶段,可以使用

sudo dpkg -s xxxx

的命令,来看xxxx是否安装成功。

        1,dhcp服务器的搭建

        安装过程请参考:debian9.6搭建dhcp服务器,这里不暂仔细说了。

        a,设置监听网口,这个地方必须是无线网口,因为目前无线的暂时不支持pxe,所以要去当dhcp服务器的监听网口,而有线网口给虚拟机无盘安装系统。如果两台都是物理机,则可以让其中一台的有线网卡当dhcp服务器的监听网口。

guoyanzhang@bogon:~$ sudo vim /etc/default/isc-dhcp-server 
INTERFACESv4="wlp8s0"

        b,增加两句,最后两句是和其他dhcp服务器不同的,因为这是为了pxe服务的dhcp服务器。

guoyanzhang@bogon:~$ sudo cat /etc/dhcp/dhcpd.conf 
[sudo] guoyanzhang 的密码:
# begin /etc/dhcp/dhcp.conf
ddns-update-style none;
option domain-name "mydebian.org";
option domain-name-servers ns1.mydebian.org,ns2.mydebian.org;
default-lease-time 3600;
max-lease-time 7200;
authoritative;

subnet 192.168.1.0 netmask 255.255.255.0{
	option routers 192.168.1.92;
	option subnet-mask 255.255.255.0;
	option domain-search "mydebian.org";
	option domain-name-servers 192.168.1.1;
	
	range 192.168.1.10 192.168.1.100;
	range 192.168.1.110 192.168.1.200;
	filename "pxelinux.0";
	next-server 192.168.1.92;
}
host debian1-node{
	hardware ethernet d4:be:d9:72:79:f6;
	fixed-address 192.168.1.158;
}
host debian2-node{
	hardware ethernet 00:50:56:2C:98:57;
	fixed-address 192.168.1.156;
}
host wins-node{
	hardware ethernet BC:EE:7B:C8:1A:39;
	fixed-address 192.168.1.160;
}
#filename "pxelinux.0";
#next-server 192.168.1.155;

# end /etc/dhcp/dhcp.conf

pxelinux.0在下面说,next-server即tftp服务器的地址,在这里就是本机无线ip,如下:

3: wlp8s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 84:a6:c8:e7:ef:b9 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.92/24 brd 192.168.1.255 scope global dynamic wlp8s0
       valid_lft 255764sec preferred_lft 255764sec
    inet6 fe80::ed88:4aed:e1c8:8ad6/64 scope link 
       valid_lft forever preferred_lft forever

 我这个是笔记本连接无线,分的一个ip,如果你没有联网,那你就给你的无线网卡自己配一个ip,在/etc/init.d/interface里面。

        2,tftp服务器的搭建

        详细请参考:debian9.6搭建tftp服务器和安装tftp客户端,我这里不在写。

这个地方需要安装pxelinux,因为我们需要前面提到的pxelinux.0文件,也可以不安装,使用之前netboot里面的,我diff过了,没什么区别,它只是个引导文件。

guoyanzhang@bogon:~$ sudo apt-get install pxelinux
guoyanzhang@bogon:~$ sudo find / -name pxelinux.0
[sudo] guoyanzhang 的密码:
/usr/lib/PXELINUX/pxelinux.0

将netboot.tar.gz解压到mytftp目录下,现在我的tftp根目录下,如下:

guoyanzhang@bogon:~/mytftp$ ls
debian-installer  ldlinux.c32  pxelinux.0  pxelinux.cfg  version.info

 不过,这个不能用因为这里面解压出来的内核,会和我使用dvd,cd的iso里面的内核不一样,如果用Netboot.tar.gz解压的话,后面安装会报错,说是内核模块不兼容。所以采用如下的办法:

guoyanzhang@bogon:~$ apt-cache search debian-installer-9-netboot-amd64
debian-installer-9-netboot-amd64 - Debian-installer network boot images for amd64
guoyanzhang@bogon:~$ sudo apt-get install debian-installer-9-netboot-amd64
guoyanzhang@bogon:~$ dpkg -s debian-installer-9-netboot-amd64
Package: debian-installer-9-netboot-amd64
Status: install ok installed
Priority: optional
Section: misc
Installed-Size: 86624
Maintainer: Debian Install System Team <debian-boot@lists.debian.org>
Architecture: all
Source: debian-installer-netboot-images
Version: 20170615+deb9u5
guoyanzhang@bogon:~$ dpkg -L debian-installer-9-netboot-amd64
这里会看到debian-installer的,将里面的debian-installer拷贝到mytftp里面去

至于prexlinux.cfg是个文件夹,里面有个default文件,内容如下:

guoyanzhang@bogon:~/mytftp/pxelinux.cfg$ cat default 
# D-I config version 2.0
# search path for the c32 support libraries (libcom32, libutil etc.)
path . 
#include ./menu.cfg
default ./debian-installer/amd64/boot-screens/vesamenu.c32
prompt 1
timeout 100
default vmdebian

label vmdebian
	kernel debian-installer/amd64/linux 
#	append vga=normal initrd=./initrd.gz  
#	append vga=normal initrd=./initrd.gz repo=http://192.168.1.92/ 
#	append vga=normal initrd=./initrd.gz mirror_regexps=http://192.168.1.92/ 
	append vga=normal withiscsi=0 initrd=debian-installer/amd64/initrd.gz debian-installer/allow_unauthenticated=true auto=true interface=auto netcfg=no_default_route=true priority=critical url=http://192.168.1.92/preseed.cfg  inetcfg/choose_interface=enp7s0 netcfg/dhcp_timeout=120 DEBCONF_DEBUG=5
	IPAPPEND 2

 指定了内核,初始化程序等的位置。

        3,ftp服务器的搭建

        详细请参考:debian9.6搭建ftp服务器和安装ftp客户端 ,这里我不再详细写,里面内容和这里完全一样。

guoyanzhang@bogon:~$ sudo apt-get install vsftpd

将下载的系统的iso文件下的内容全部拷贝到ftp的根目录下。

这个方法比较麻烦,搞了好久,也没成功,所以我这里换了http服务器apache2。

guoyanzhang@bogon:~$ dpkg -s apache2
Package: apache2
Status: install ok installed
Priority: optional
Section: httpd
Installed-Size: 578
Maintainer: Debian Apache Maintainers <debian-apache@lists.debian.org>
Architecture: amd64
Version: 2.4.25-3+deb9u6
Replaces: apache2.2-bin, apache2.2-common
Provides: httpd, httpd-cgi

 配置如下:这里是跟原来的配置不一样的地方,其他地方都没有变动。

guoyanzhang@bogon:~$ cat /etc/apache2/apache2.conf 
#<Directory />
#	Options FollowSymLinks
#	AllowOverride None
#	Require all denied
#</Directory>

<Directory /usr/share>
	AllowOverride None
	Require all granted
</Directory>
DocumentRoot "/home/guoyanzhang/dvd/"
<Directory /home/guoyanzhang/dvd/>
	Options Indexes FollowSymLinks
	AllowOverride ALL
#	Require all granted
	Order allow,deny
	Allow from all
</Directory>

另外一个,同样,也是只看我改的地方,其他地方都没有动。

guoyanzhang@bogon:~$ cat /etc/apache2/sites-available/000-default.conf 
<VirtualHost *:80>
	# The ServerName directive sets the request scheme, hostname and port that
	# the server uses to identify itself. This is used when creating
	# redirection URLs. In the context of virtual hosts, the ServerName
	# specifies what hostname must appear in the request's Host: header to
	# match this virtual host. For the default virtual host (this file) this
	# value is not decisive as it is used as a last resort host regardless.
	# However, you must set it for any further virtual host explicitly.
	#ServerName www.example.com

	ServerAdmin webmaster@localhost
	##DocumentRoot /var/www/html
	DocumentRoot /home/guoyanzhang/dvd/

可以看到,我将http的根目录设置为/home/guoyanzhang/dvd了。

现在将iso文件挂载到这个目录下:

guoyanzhang@bogon:~/os$ sudo mount debian-9.6.0-amd64-DVD-1.iso /home/guoyanzhang/dvd/debianiso/
mount: /home/guoyanzhang/os/debian-9.6.0-amd64-DVD-1.iso is already mounted

因为我挂载过了,所以提示这个。

        4,写preseed.cfg文件,放到/home/guoyanzhang/dvd下。

guoyanzhang@bogon:~/dvd$ cat preseed.cfg
#d-i live-installer/net-image string http://192.168.1.92/extrafiles
# locale sets language and country
d-i debian-installer/locale string zh_CN
# keyboard selection
d-i keyboard-configuration/xkb-keymap select us
# network configuration
# mirror settings
d-i mirror/country string manual
d-i mirror/protocol string http
d-i mirror/http/hostname string 192.168.1.92
d-i mirror/http/directory string /debianiso
d-i mirror/http/proxy string http://192.168.1.92
d-i mirror/udeb/suite string stretch
# clock and time zone setup
d-i clock-setup/utc boolean false
d-i time/zone string Asia/Shanghai
# partitioning
d-i partman-auto/disk string /dev/sda
d-i partman-auto/method string regular
d-i partman-auto/choose-recipe string atomic
d-i partman-partitioning/confirm_write_new_label boolean true
d-i partman/choose_partition string finish
d-i partman/confirm boolean true
d-i partman/confirm_nooverwrite boolean true
# account setup
d-i passwd/root-login boolean true
d-i passwd/root-password password 123456
d-i passwd/root-password-again password 123456
d-i passwd/make-user boolean false
d-i user-setup/encrypt-home boolean false
d-i user-setup/allow-password-weak boolean true 
# package selection
tasksel tasksel/first multiselect standard,debian-servr
d-i pkgsel/include string openssh-server
d-i pkgsel/upgrade select none
d-i pkgsel/language-packs multiselect en,zh
d-i pkgsel/update-policy select none
# boot loader installation
d-i grub-installer/only_debian boolean true
# finishing up the installation
d-i finish-install/reboot_in_progress note

(partition这应该有点问题)

四,客户端搭建

方法1:virtualbox(最后不能获取ip不知道为什么,但是我连另外的物理机是可以的)

这里我另外写了文章,请参阅:debian9.6安装virtualbox,我这边戴尔电脑,在虚拟机上启动空的虚拟机,要我按F12选启动方式,我选择network。

安装好的virtualbox,我已经设置了网桥,网卡mac是我的有线网卡的。

1,新建一个debian

这里不详细说,网上很多virtualbox安装系统的教程。

启动服务:

guoyanzhang@bogon:~$ ./all.sh 
[ ok ] Restarting isc-dhcp-server (via systemctl): isc-dhcp-server.service.
[ ok ] Restarting tftpd-hpa (via systemctl): tftpd-hpa.service.
[ ok ] Restarting xinetd (via systemctl): xinetd.service.
[ ok ] Restarting vsftpd (via systemctl): vsftpd.service.

2,启动,按f12,选择lan,即l键

3,获取ip,结果失败了

有人说vbox的ipxe是有点问题,但是我另外的物理机是获取了ip的,所以我换vmware试试。 如果有人知道是怎么回事的,希望留言告知。

方法2:

搭建vmware:debian9.6安装vmware15教程,里面的内容完全一样,这里不再细说。我这边启动空的虚拟机,自己就选择network方式安装了,即pxe。

同样的方法又在vm14上出现,我以为是vm的问题,换了之后还是不行。

出现问题:

参考1:https://baike.baidu.com/item/PXE/6107945?fr=aladdin

参考2:https://blog.csdn.net/u013451404/article/details/77103759

参考3:https://blog.csdn.net/soulwish/article/details/53192247

参考4:http://blog.chinaunix.net/uid-24648266-id-5609167.html

参考5:https://www.syslinux.org/wiki/index.php?title=PXELINUX#Description

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值