PXEBOOTの設定

:::note info
PXEBOOTの手順についての備忘録。
:::

|項目|値|
|---|---|
|開発機|VMware Workstation v16.2.5 |
|サーバーのOS|Debian 12|
|サーバーのIPアドレス|192.168.1.1|
|DHCP払出IPアドレス|192.168.1.16～31|
|ルーターのIPアドレス|192.168.1.254|

:::note info
各設定値は各自の環境に合わせて変更願います。
:::

# 導入作業

## パッケージの導入

``` bash:
sudo apt-get install isc-dhcp-server tftpd-hpa apache2
```

## 設定ファイルの編集

### /etc/default/isc-dhcp-serverの設定

``` bash:
sudo sed -i.org /etc/default/isc-dhcp-server      \
    -e '/DHCPDv4_CONF/ s/^#//'                    \
    -e '/DHCPDv4_PID/  s/^#//'                    \
    -e '/INTERFACESv4/ s/".*"/"ens160"/'
```

### /etc/default/tftpd-hpaの設定

``` bash:
sudo sed -i.org /etc/default/tftpd-hpa            \
    -e '/TFTP_DIRECTORY/ s%".*"%"/var/lib/tftp"%'
```

### /etc/dhcp/dhcpd.confの設定

``` bash:
sudo cp -p /etc/dhcp/dhcpd.conf /etc/dhcp/dhcpd.conf.org
cat <<- '_EOT_' | sed 's/^ *//g' | sudo tee "/etc/dhcp/dhcpd.conf"
	option arch code 93 = unsigned integer 16;
	
	subnet 192.168.1.0 netmask 255.255.255.0 {
	 	range 192.168.1.16 192.168.1.31;
	 	option domain-name-servers 192.168.1.1;
	 	option domain-name "workgroup";
	 	option routers 192.168.1.254 ;
	 	option broadcast-address 192.168.1.255;
	 	default-lease-time 3600;
	 	max-lease-time 86400;
	
	#	option time-servers ntp.nict.jp;
	#	option subnet-mask 255.255.255.0;
	#	option netbios-dd-server 192.168.1.1;
	
	 	group {
	 		next-server 192.168.1.1;
	 		if option arch = 00:07 or option arch = 00:09 {
	 			filename "bootnetx64.efi";
	 		} else {
	 			filename "pxelinux.0";
	 		}
	
	#		MAC address: xx:xx:xx:xx:xx:xx
	 		host Client01 { hardware ethernet xx:xx:xx:xx:xx:xx; }
	 		host Client02 { hardware ethernet xx:xx:xx:xx:xx:xx; }
	 		host Client03 { hardware ethernet xx:xx:xx:xx:xx:xx; }
	 	}
	}
_EOT_
```

## ディレクトリーの作成

``` bash:
sudo mkdir -p /var/lib/tftp
```

## PXEBOOTの整備

``` bash:
cd /var/lib/tftp/
sudo curl -L -# -R -S -o "debian_stable_netboot.tar.gz" "https://deb.debian.org/debian/dists/stable/main/installer-amd64/current/images/netboot/netboot.tar.gz"
sudo tar -xzf debian_stable_netboot.tar.gz
sudo rm debian_stable_netboot.tar.gz
sudo ln -s debian-installer/amd64/bootnetx64.efi .
sudo ln -s debian-installer/amd64/grubx64.efi    .
sudo ln -s debian-installer/amd64/grub           .
```

## サービスの再起動

``` bash:
sudo systemctl restart tftpd-hpa.service isc-dhcp-server.service apache2.service
sudo systemctl status  tftpd-hpa.service isc-dhcp-server.service apache2.service
```

# 実行例

:::note info
以下より自動設定ファイルをダウンロードしDebian12を/dev/nvme0n1にインストール
https://github.com/office-itou/Linux/tree/master/pxeboot
:::

## 設定ファイルの編集

### syslinux.cfgの編集

``` bash: 
OLD_IFS="${IFS}"
IFS= INS_STR=$(
cat <<- '_EOT_' | sed -e 's/^ //g' | sed -z -e 's/\n/\\n/g' | sed -e 's/\\n$//'
label auto
        menu label ^Automated install
        kernel debian-installer/amd64/linux
        append auto=true url=https://raw.githubusercontent.com/office-itou/Linux/master/pxeboot/preseed/debian/preseed_server.cfg priority=critical vga=788 initrd=debian-installer/amd64/initrd.gz --- quiet
_EOT_
)
IFS="${OLD_IFS}"
sudo sed -i.org debian-installer/amd64/boot-screens/syslinux.cfg\
         -e "/^timeout /a \\${INS_STR}"
```

### grub.cfgの編集

``` bash: 
OLD_IFS="${IFS}"
IFS= INS_STR=$(
cat <<- '_EOT_' | sed -e 's/^ //g' | sed -z -e 's/\n/\\n/g' | sed -e 's/\\n$//'
menuentry 'Automated install' {
    set background_color=black
    linux    /debian-installer/amd64/linux auto=true url=https://raw.githubusercontent.com/office-itou/Linux/master/pxeboot/preseed/debian/preseed_server.cfg priority=critical vga=788 --- quiet
    initrd   /debian-installer/amd64/initrd.gz
}
_EOT_
)
IFS="${OLD_IFS}"
sudo sed -i.org debian-installer/amd64/grub/grub.cfg\
         -e "/^play /a \\${INS_STR}"
```