- Boot Ubuntu via http/ftp server with pxe(diskless boot)
- Pxe short info
- How i do it
- Customize initramfs
- Creating squashfs
- Setup bootloader, squashfs, and pxe server
- Conclusion
- PXE network boot on Linux
- What is PXE?
- Network layout for PXE booting
- Setup TFTP
- Setup DHCP
- Configuring PXE
- Final thoughts
- Related Linux Tutorials:
Boot Ubuntu via http/ftp server with pxe(diskless boot)
PXE is a great solution for booting a diskless computer (or a computer without an OS installed). This method is often used for terminal stations and OS mass installation.
Stock ubuntu (16.04) in pxe-mode can mount rootfs only from NFS. But this is not a great idea: any difficulties with the network/NFS server and the user gets problems.
In my opinion, it’s best to use other protocols, such as http/ftp. Once booting, you will have an independent system
You should add information about the limits of applicability of the proposed solution and what are the dependencies and restrictions.
Pxe short info
PXE (Preboot Execution Environment) is a special method for booting a computer from the bios / EFI.
How it works (simplified scheme):
- The computer bios/uefi sends ordinary dhcp request (DHCPDISCOVER)
- The dhcp server sends a response with the next-server option
- The computer sends DHCPREQUEST/DHCPINFORM request
- The dhcp server sends TFTP server address and the filename to upload
- The computer downloads this file from the tftp server. Its size is limited so, often, it’s a bootloader like pxeinux
- pxelinux reads its own config and downloads Linux kernel using initramfs
- Linux kernel downloads squashfs with main rootfs
- switch_root to its squashfs
Keep in mind that TFTP is a slow protocol. It works around UDP with a small block size (512K). Of course, you can increase this value, but this is a way of unstable operation.
- get bootloader via tftp
- get kernel (+ initramfs) via tftp
- get main rootfs squash via http/tftp
How i do it
- Add modules to initramfs
- Write my own boot script and add it to initramfs
- Make new initramfs
- Create squashfs with future rootfs
- Setup pxe server
- Run it
I used squashfs for rootfs (the simplest way is to create squashfs from installed ubuntu). Overlayfs is necessary to make rootfs writable.
Supported protocols are http/ftp, but you can try to add others via curl/other software.
Customize initramfs
There are 2 places where you can customize initramfs in ubuntu:
I’ll use /usr/share/initramfs-tools. First, I added needed support modules in initramfs:
boozlachu@comp:~$ cat /usr/share/initramfs-tools/modules.d/pxe overlayfs squashfs
Next, I wrote a boot script that does all the work:
boozlachu@comp:~$ cat /usr/share/initramfs-tools/scripts/pxe #!/bin/bash mountroot() < maxTryCount=5 squashfsFile="/tmp/rootfs.squashfs" squashfsMountPoint="/mnt/ro" tmpfsMountPoint="/mnt/rw" overlayfsUppderDir="$tmpfsMountPoint/upper" overlayfsWorkDir="$tmpfsMountPoint/work" overlayfsDir="/mnt/overlayfs" tryCount="1" # run udevadm wait_for_udev 10 # parce kernel cmdline args. rooturl needed for x in $(cat /proc/cmdline); do case $x in rooturl=*) export rooturl=$;; maxTryCount=*) export maxTryCount=$ ;; esac done log_begin_msg "Loading modules" modprobe squashfs || panic "can't modprobe squashfs" modprobe af_packet || panic "can't modprobe af_packet" modprobe overlay || panic "can't modprobe overlayfs" log_success_msg "modules loaded" log_begin_msg "Configure network" configure_networking || panic "Can't configure network" log_success_msg "Network configured" log_begin_msg "Download rootfs" while [ ! -f $ ] && [ $ -le $ ]; do wget $ -O $ || log_failure_msg "Can't download rootfs, count $" tryCount=$(( $ + 1 )) sleep 0.5 done if [ -f $ ] then log_success_msg "Rootfs downloaded" else panic "Can't download rootfs" fi log_begin_msg "Mount rootfs" mkdir -p $squashfsMountPoint mount -t squashfs -o loop $squashfsFile $squashfsMountPoint || panic "Can't mount rootfs" log_success_msg "Rootfs mounted" log_begin_msg "Mount tmpfs" mkdir -p $tmpfsMountPoint mount -t tmpfs none $tmpfsMountPoint || panic "Tmpfs mount failed " log_success_msg "Tmpfs mounted" log_begin_msg "Mount overlayfs" mkdir -p $overlayfsUppderDir $overlayfsWorkDir $overlayfsDir mount -t overlay overlay -o lowerdir=$squashfsMountPoint,upperdir=$overlayfsUppderDir,workdir=$overlayfsWorkDir $overlayfsDir \ || panic "Overlayfs mount failed" log_success_msg "Overlayfs mounted" log_begin_msg "Move tmpfs and squashfs to new root" mkdir -p $overlayfsDir/$tmpfsMountPoint $overlayfsDir/$squashfsMountPoint mount --move $squashfsMountPoint $overlayfsDir/$squashfsMountPoint || panic "squashfs move failed" mount --move $tmpfsMountPoint $overlayfsDir/$tmpfsMountPoint || panic "tmpfs move failed" log_success_msg "Tmpfs and squashfs moved" log_begin_msg "Move overlayfs to new root" mount --move $overlayfsDir $ || panic "" >
The script has a lot of messages for understanding how it works.
After the modules and script, add your need to generate new initramfs:
boozlachu@comp:~$ sudo update-initramfs -c -k all
Creating squashfs
- install ubuntu on drive
- boot from LiveCD
- create squashfs from the installed system
I don’t recommend this way for production since you’ll have a very large squashfs (not the best idea for pxe)!
Setup bootloader, squashfs, and pxe server
I use pxelinux as a pxe bootloader. It’s an easy way. My pxe servers are Debian 10, tftp-hpa,dhcpd, and lighttpd.
I’ll omit the installation details, but I’ll show the important info.
TFRP server file struct (/srv/tftp is root dir fot tftp-hpa):
root@debian:/srv/tftp/ubuntu# tree /srv/tftp/ /srv/tftp/ └── ubuntu ├── firmware.sq ├── initrd ├── ldlinux.c32 ├── libcom32.c32 ├── libutil.c32 ├── menu.c32 ├── pxelinux.bin ├── pxelinux.cfg │ └── default ├── vesamenu.c32 └── vmlinuz
- firmware.sq is squashfs with rootfs
- *c32 are files for pxelinux
- vmlinuz is kernel
- initrd is initramfs(which i rebuild earler)
- pxelinux.bin — main pxelinux file
- default is config for pxelinux
root@debian:/srv/tftp/ubuntu# cat /srv/tftp/ubuntu/pxelinux.cfg/default ui menu.c32 timeout 30 default ubuntu_pxe font UniCyr_8x16.psf menu title PXE Special Boot Menu menu color tabmsg 37;40 #80ffffff #00000000 menu color hotsel 30;47 #40000000 #20ffffff menu color sel 30;47 #40000000 #20ffffff menu color scrollbar 30;47 #40000000 #20ffffff LABEL ubuntu_pxe menu label Run ubuntu pxe kernel vmlinuz append initrd=initrd rooturl=http://192.168.56.2/ubuntu/firmware.sq boot=pxe maxTryCount=10
It’s impotant to set the correct kernel parameters:
- rooturl=http://192.168.56.2/ubuntu/firmware.sq, url for rootfs
- boot=pxe, use my script for boot
- maxTryCount=10, number of tries for rootfs download (optional, default value 5)
And the last one is the dhcp config:
root@debian:/srv/tftp/ubuntu# cat /etc/dhcp/dhcpd.conf subnet 192.168.56.0 netmask 255.255.255.0 < range 192.168.56.10 192.168.56.45; option routers 192.168.56.2; option domain-name-servers 192.168.2.1 ; option broadcast-address 192.168.56.255; default-lease-time 3600; max-lease-time 7200; # Important! Set bootloader file filename "ubuntu/pxelinux.bin"; >
The extended variant (if the dhcp and tftp servers placed on different machines) requires the next-server option for dhcp.
Conclusion
This article shows you how to change the boot mode of ubuntu without any difficulties. Use it as information and write your solutions. This can be a system in the form of firmware (with squashfs), pxe, or another solution.
PXE network boot on Linux
The purpose of this tutorial is to cover the subject of booting and installing Linux using the network, be it local or not.
We will treat installing Linux without optical, USB or other removable media, by just using the LAN. You are expected to have at least two computers in your network, and the client will need a NIC and a BIOS capable of using PXE.
We will guide you from beginning to end, but some basic networking and Linux configuration knowledge, plus the use of an editor of your choice are required. You will learn what PXE is, how to configure a DHCP server, how to configure a TFTP server so the client can have access to the files, plus lots of interesting things, as usual. Let’s get started.
In this tutorial you will learn:
- What is PXE?
- Typical network layout for PXE booting
- How to setup DHCP and TFTP
- How to configure PXE for network boot
Category | Requirements, Conventions or Software Version Used |
---|---|
System | Any Linux system |
Software | N/A |
Other | Privileged access to your Linux system as root or via the sudo command. |
Conventions | # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command $ – requires given linux commands to be executed as a regular non-privileged user |
What is PXE?
PXE (pronounced “pixie”) stands for Preboot eXecution Environment and was introduced by Intel and Systemsoft in 1999. In short, it’s a capability most modern network cards and BIOSes have that enables the system to boot from LAN, just like it would boot from hard disk or CD-ROM.
The PXE support must be present in the NIC’s firmware which, if set up accordingly in the BIOS, will get an IP address from the PXE server and download the necessary boot images. In order for an IP address to be available, the server must offer DHCP.
After an IP address is leased, the TFTP server (which can be the same box as the DHCP server) hands out the necessary files to the client, so it can boot them after loading. That’s the whole idea, so enough talk, let’s get to work, shall we?
Network layout for PXE booting
Before we start, it’s important to understand the type of network layout one would need to have in order for PXE booting to be possible. As an example, we will show you how we have setup our test environment.
Our server is a Debian machine with two network cards, and the distribution we will install is also Debian. You can use any other distribution, but probably some config file locations will differ.
This article does not deal with how you will set up the software sources for the actual installation. We will only take you to a working debian-installer and that’s about it. You’ll find lots of tutorials out there about how to set up a local Debian repository or how to configure a gateway on Linux.
So, the checklist. We will need the following:
- DHCP server
- TFTP server
- the initrd and the kernel image to be transferred to the client
We chose a rather unusual approach in our setup, and you will see why.
Outside world ------> Router ------> Switch ------>(eth0) Server(eth1) ------> Client
So, the router gives out DHCP addresses (small, home router) in the form of 192.168.0.x . The server, which will also be handing out DHCP addresses, has its outside connection via eth0 and the inside connection for the client via eth1 .
The client has the only Ethernet connection directly to the PXE server, but that doesn’t mean you can’t configure the server as a gateway for netinstall or add another NIC to the client for outside access. There are a lot of possibilities, the important issue at hand is booting via PXE. Let’s start by installing the TFTP server.
Setup TFTP
TFTP stands for Trivial File Transfer Protocol and it’s the de facto “language” when it comes to transferring files to use with PXE.
- On Debian, we install it thusly:
$ sudo apt install tftpd-hpa pxelinux
$ sudo systemctl start tftpd-hpa
Setup DHCP
We have to offer the client an address before we commence installing via network, and this is done by using the Dynamic Host Configuration Protocol. The DHCP server has a pool of addresses from which it offers IPs when requested.
- One installs the server part with:
$ sudo apt install isc-dhcp-server
ddns-update-style none; # Remember the semicolons at the end of each line! DHCPDARGS=eth1; default-lease-time 86400; max-lease-time 604800; authoritative; subnet 192.168.1.0 netmask 255.255.255.0 < range 192.168.1.10 192.168.1.30; filename "pxelinux.0"; option subnet-mask 255.255.255.0; ># There is more to this file than we've shown, but what you have here is enough for PXE.
$ sudo systemctl restart isc-dhcp-server
Configuring PXE
- Now we need to provide the boot file. To do so, we will download the netboot image from Debian.
- Decompress the netinstall archive into your /srv/tftp directory. The contents should look like this:
debian-installer/ pxelinux.0@ pxelinux.cfg@ version.info
Before we finish, we owe you an explanation for this network setup we chose. We could have probably made it simpler, like using our ISP’s connection directly, and being able to access the network (Internet) as well. The idea is that the PXE server in our scenario can’t get its IP address (eth0) from DHCP as this setup won’t work: if you’re not going to use the two-NIC scenario on the server, like we did, you need some kind of fixed IP address.
So, the possibilities would be: ISP connected directly, set router to give only static IPs, make the PXE/DHCP/TFTP server also a gateway or create a local repository. We chose the latter because it suits our topology and purposes best, and it only involves setting up a webserver on the LAN, really.
If we would have explained all your choices, including iptables for a gateway or apache for a HTTP server, this article would have been huge. Instead, we preferred to stick to the title and give you exactly that: Network booting with Linux.
Remember that this is just an example: like with NetbootCD, you can add as many distributions as you want, provided you have the necessary space on the server.
Final thoughts
First of all, we recommend patience. PXE is known to be kind of slow, regardless of your top Gigabit switch you bought last week. Second, read the comments and examples in dhcpd.conf as they will be useful now and later.
Related Linux Tutorials:
Comments and Discussions