You are on page 1of 6

8/15/2020 Boot Ubuntu via http/ftp server with pxe(diskless boot) / Habr

All streams Development Administrating Design Management Marketing PopSci Log in Sign up

Boozlachu August 3, 2020 at 12:47 PM Ads

Boot Ubuntu via http/ftp server with pxe(diskless boot)


Configuring Linux, Development for Linux

Tutorial

Intro
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.

A better solution:

get bootloader via tftp

get kernel (+ initramfs) via tftp

get main rootfs squash via http/tftp

How i do it
Steps to solve the task:

1. Add modules to initramfs

2. Write my own boot script and add it to initramfs

3. Make new initramfs

https://habr.com/en/post/513568/ 1/6
8/15/2020 Boot Ubuntu via http/ftp server with pxe(diskless boot) / Habr

4. Create squashfs with future rootfs

5. Setup pxe server

6. 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:

/etc/initramfs-tools

/usr/share/initramfs-tools/

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=${x#rooturl=}
;;
maxTryCount=*)
export maxTryCount=${x#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"

https://habr.com/en/post/513568/ 2/6
8/15/2020 Boot Ubuntu via http/ftp server with pxe(diskless boot) / Habr
while [ ! -f ${squashfsFile} ] && [ ${tryCount} -le ${maxTryCount} ]; do
wget ${rooturl} -O ${squashfsFile} || log_failure_msg "Can't download rootfs, count ${tryCount}"
tryCount=$(( ${tryCount} + 1 ))
sleep 0.5
done

if [ -f ${squashfsFile} ]
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 ${rootmnt} || 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
The simplest method:

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):

https://habr.com/en/post/513568/ 3/6
8/15/2020 Boot Ubuntu via http/ftp server with pxe(diskless boot) / Habr

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

Bootloader config:

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;

https://habr.com/en/post/513568/ 4/6
8/15/2020 Boot Ubuntu via http/ftp server with pxe(diskless boot) / Habr
# 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.

Tags: linux, ubuntu, pxe, pxeboot, linux kernel, development, overlayfs, diskless, firmware

Hubs: Configuring Linux, Development for Linux

+6 9 726 3 Share

19.0 4.8
Karma Rating

@ Boozlachu
Пользователь

Medium

SIMILAR POSTS

January 20, 2020 at 08:47 PM

Writing a laptop driver for fun and profit, or How to commit to kernel even if you're not that smart
+5 1k 0 0

March 20, 2019 at 11:28 PM

Using Linux Kernel Sequence Files


+6 2.2k 4 1

January 23, 2019 at 08:23 AM

System call interception in Linux-kernel module


+12 5.2k 8 1

Ads

Comments 3

kvaps August 3, 2020 at 02:35 PM +1

Such an awesome article, thank you!


Have you tried LTSP? — it is doing the same but having ready scripts for many routine tasks.

Boozlachu August 3, 2020 at 02:41 PM 0

I guess LTSP is a complete solution for network boot. But in my practice wa are don works without it.
My article is more than pxe tutorial. I tried to show how to customize ubuntu boot to use ubuntu as firmware from squashfs. PXE is only one
of variant

https://habr.com/en/post/513568/ 5/6
8/15/2020 Boot Ubuntu via http/ftp server with pxe(diskless boot) / Habr

Boozlachu August 3, 2020 at 02:49 PM 0

In addition, LTSP uses the nfs / nbd protocol. I don't like when network connection dependencies work on my system.
HTTP and FTP allows booting ALL systems from rootfs to my client. Then my system will be offline.

Only users with full accounts can post comments. Log in, please.

TOP POSTS

Day Week Month

Памятка для пострадавшего от слезоточивого газа/перцового баллона


+132 47.2k 251 110

Audio over Bluetooth: most detailed information about profiles, codecs, and devices
+22 112k 10 8

Technical analysis of the checkm8 exploit


+22 59.5k 5 4

Java vs .Net: Who will Reign in the Future?


+3 6.2k 5 2

Your account Sections Info Services

Log in Posts How it works Ads

Sign up Hubs For Authors Subscription plans

Companies For Companies Content

Users Documents Seminars

Sandbox Agreement Megaprojects

Terms of service

© 2006 – 2020 «Habr» Language settings About Support Mobile version

https://habr.com/en/post/513568/ 6/6

You might also like