Creating a Linux image for booting over the network. Installing Ubuntu over a network via PXE

Quite a long time ago, I wrote an article about network installation of Ubuntu, over a local network, without using various media, only PXE booting and that’s it. This time I decided to update the article and make it closer to modern realities, and most importantly, expand the functionality. Everything will be done using Ubuntu 14.04 as an example!

For different requirements, there must be different solutions, the main catch is that often we need not one operating system, but several. So I decided to write an article on this topic. The previous article can be found at the link.
The main idea is that we will have the opportunity to choose which operating system we will install, as well as its bit depth; in fact, we will have a boot menu after loading the system over the local network.
We will implement all this using the example of the just released Ubuntu 14.04

For those who do not understand, everything is being started so that it becomes possible to choose which operating system to install, as well as its bit capacity, i.e. A menu should appear where all operating systems available for installation over the network will be presented, which will look like this:

Ubuntu_14.04x86
Ubuntu_14.04amd64
For work we will need:
Necessarily!
1) Configured gateway, for example, use the article:
2) DHCP server, for example, you can use the article:
3) Configured TFTP server loading PXE bootloader, use the article:

Optional!
3) An additional, but not obligatory point, may be a configured DNS server, an example is in the article

It is assumed that you have configured a gateway, DHCP, TFTP server, and also added a PXE boot menu because I won’t dwell on this point.

Let's move on to setting up the system to be installed.
First, we will create an installation point for Ubuntu 14.04x86, for which we will need an installation disk of the appropriate capacity. We need to download and mount it in order to extract the boot loader and system installer files.
Because The network bootloader file is located in the directory directory /var/lib/tftpboot/ from where they are pulled up by the TFTP server, then in it we need to create an Ubuntu directory in order to put the files from the installation disks into it, let's do this:
mkdir /var/lib/tftpboot/ubuntu
In the Uubntu directory we create additional directories for the OS installer loader files for x86 bit systems and ADM64, respectively.
mkdir /var/lib/tftpboot/ubuntu/x86 mkdir /var/lib/tftpboot/ubuntu/amd64

Now we need to download the installation disk, you can view it here mirror.yandex.ru/ubuntu-releases/14.04/
We will download the x86 version for the server because... she weighs less
wget http://mirror.yandex.ru/ubuntu-releases/14.04/ubuntu-14.04-server-i386.iso
After downloading the ISO image is completed, we need to mount it in order to extract the files we need, and for this, in the directory /media we will create a directory iso, let's do that:

Mkdir/media/iso
Mount the disk image in this directory:
sudo su
mount ubuntu-14.04-server-i386.iso /media/iso/
The system will display that the disk is mounted in read mode:

mount: warning: /media/iso/ seems to be mounted read-only.
Let's go pick up the bootloader and installer kernel:
cd /media/iso/install/netboot/ubuntu-installer/i386/

We are interested in 2 files linux And initrd.gz, which we need to pick up and move to the directory /var/lib/tftpboot/ubuntu/x86
let's copy them:
cp linux /var/lib/tftpboot/ubuntu/x86 cp initrd.gz /var/lib/tftpboot/ubuntu/x86

That’s it, we don’t need the installation image anymore, first let’s unmount it from the iso directory:
umount /media/iso/

After this, the ISO image can be deleted.
We carry out similar steps for the installation disk image with x64 bit depth, I won’t describe what and where, I’ll just give a listing of the commands, the steps are the same, only the paths are slightly different, it’s written there amd64

Wget http://mirror.yandex.ru/ubuntu-releases/14.04/ubuntu-14.04-server-amd64.iso

Mount ubuntu-14.04-server-amd64.iso /media/iso/

Cd /media/iso/install/netboot/ubuntu-installer/amd64/

Cp linux /var/lib/tftpboot/ubuntu/amd64 cp initrd.gz /var/lib/tftpboot/ubuntu/amd64

Umount /media/iso/

We're done with the installation files, now let's configure the PXE boot menu.

Configuring the PXE Boot Menu
First, let's add a menu item with the name Linux Install, for this we need to edit the file:
nano /var/lib/tftpboot/pxelinux.cfg/default

Add an entry to the end of the file:
LABEL Linux Install MENU LABEL ^Linux Install KERNEL vesamenu.c32 APPEND pxelinux.cfg/linux.menu

As you can see from the entry, we will have another page with the choice of distributions for installation, let's create it:

Nano /var/lib/tftpboot/pxelinux.cfg/linux.menu

Let's add to it the names of the download points with the ability to select the operating system to install, while this will be Ubuntu 14.04 x86 And amd64 architecture:
LABEL Install Ubuntu 14.04x86 MENU label ^Ubuntu 14.04x86 KERNEL ubuntu/x86/linux APPEND initrd=ubuntu/x86/initrd.gz LABEL Install Ubuntu 14.04amd64 MENU label ^Ubuntu 14.04amd64 KERNEL ubuntu/amd64/linux APPEND initrd=ubuntu/amd64 /initrd.gz

At this point, the setup is complete, you can connect the client machine and try to boot via PXE, as a result, after booting the system over the network, we will have a menu like this:

We select what we want to install, the installation will begin automatically, and then just follow the instructions of the system installer.
Other operating systems are added in a similar way.
This is where I’ll probably finish for today.
Well, as always, if you have any questions, please leave a comment, if you find an error, write in a personal message or by email.

In server rooms, servers without CD/DVD drives are increasingly being found. From time to time they need to install an operating system, and installing over a network can greatly help with this. You simply turn on the server and begin the installation. The network card must support PXE technology. PXE - Pre-Boot Execution Environment - allows you to boot over the network.

But PXE is not enough for complete happiness; a technology that will completely automate the installation is kickstart (developed by Red Hat). Its essence is simple - we compile a file in advance containing the values ​​of all options that may be needed during installation. Moreover, we can execute our scripts before and after installation, thereby setting the settings for the future OS.

Installing a typical Linux kit using kickstart takes 5-7 minutes.

The Install server requires 3 services and 1 package.


  • DHCP provides clients with network credentials

  • TFTP is an easy way to share files over the network

  • Syslinux contains the pxelinux.0 bootloader and some other files

  • NFS allows file system access over the network
The installation process can be divided into stages:

  1. pxe - pxe firmware starts working when we set the installation over the network in the BIOS, or when the MBR is not found on the HDD.

  2. DHCP phase 1 - the client receives network details and the address of the tftp server, as well as the name of the loader file (pxelinux.0). By default, the TFTP server is a DHCP server.

  3. TFTP - the pxelinux.0 loader contacts the TFTP server and requests from it initrd.img (Initial RAM disk, temporary file system), the Linux kernel.

  4. Kernel - transfer of control to the Linux kernel.

  5. DHCP phase 2 - the Linux kernel makes a request to the DHCP server to obtain network details and subsequently the NFS server address.

  6. NFS - the stage when the NFS partition is mounted

  7. init - /sbin/init is launched and control is transferred to it. Init is the main process in the system, other processes are child processes of init.
Freely stated:

The DHCP server listens for bootp requests on its network; after it receives a request, it looks at the source MAC address, and if it has a corresponding entry for that MAC address, it starts working with it. The DHCP server provides the client with network details (IP address, gateway, DNS server,...) and sends the boot image pxelinux.0 using the TFTP protocol. This image is enough to display the OS selection menu.

Having selected the OS, we begin loading the kernel and begin the installation, in the process selecting the installation source - the NFS server. You need to upload the prepared content of the future operating system to the NFS server and make sure that the corresponding directories are exported.

DHCP

Install DHCPD and add it to startup:
# yum -y install dhcp
# chkconfig dhcpd on

Make the file /etc/dhcpd.conf like this:

Ddns-update-style interim;
ignore client-updates;
subnet 192.168.146.0 netmask 255.255.255.0 (
option routers 192.168.146.1;
option subnet-mask 255.255.255.0;
option domain-name "domain.local";
option domain-name-servers 192.168.146.1;
default-lease-time 21600;
max-lease-time 43200;
Allow boot;
Allow booting;
host unixbox (
hardware ethernet 00:0c:29:77:9c:9c;
fixed-address 192.168.146.128;
filename "pxelinux.0";
option host-name "unixbox";
next-server 192.168.146.1;
}
}

Launch DHCPD or reboot if it was running:
# service dhcpd restart

Disable the firewall, which is enabled by default (otherwise the target computer will receive the error “ICMP Destination unreachable (Host administratively prohibited)” upon boot):
# service iptables stop
# chkconfig iptables off

TFTP

Install the tftp-server package from the repository:
# yum -y install tftp-server

Now you need to enable tftp in the xinetd configuration; to do this, in the /etc/xinetd.d/tftp file, change “disable = yes” to “disable = no” and enable xinetd:
# service xinetd start

We check that the tftp server port is listening (tftp runs on port 69):
# netstat -nlp | grep:69
udp 0 0 0.0.0.0:69 0.0.0.0:* 3105/xinetd

Syslinux

The package contains a set of files for downloading over the network. We need pxelinux.0, which we will serve as a boot image via DHCP, and menu.c32, with which a more attractive user menu will be drawn. (For CentOS 4, the updated syslinux with dependencies must be downloaded from rpmfind.net.)

# cp $(rpm -ql syslinux | grep menu.c32) /tftpboot/
# cp $(rpm -ql syslinux | grep pxelinux.0) /tftpboot/

NFS

By default, the system most likely has NFS, if not, install it using yum.
# chkconfig nfs on

Add an entry to the /etc/exports file:
echo “/var/install-server/ *(ro,no_root_squash)” >> /etc/exports

Launch the nfs server:
# service nfs start

We check that the directory has been exported:
#exportfs
/var/install-server

We create the structure of the tftp server, add content to the server:
# mkdir -p /tftpboot/(pxelinux.cfg,centos5_x86)
# mkdir -p /var/install-server/centos5_x86

We mount our DVD with CentOS 5 and upload the contents to /var/install-server/centos5_x86:
# mount /dev/cdrom /mnt/
# cp -r /mnt/* /var/install-server/centos5_x86/
# cp /var/install-server/centos5_x86/images/p xeboot/* /tftpboot/centos5_x86/

In the /tftpboot/pxelinux.cfg directory, create a default file and fill it in as shown below:
default menu.c32

prompt 0
timeout 100

kernel /centos5_x86/vmlinuz
append initrd=/centos52_x86/initrd.img
label Quit
localboot 0

Installing the OS over the network

After all the manipulations described above, we can begin installing the OS. We start our machine with the MAC address 00:0c:29:77:9c:9c, enabling network boot in the BIOS. When the installation begins, we do everything in the standard way, except that in the list of where to install the OS, you need to select NFS, and then, when asked, indicate:
NFS server name: 192.168.146.1
CentOS directory: /var/install-server/centos5_x86

Automate installation with Kickstart

To automate, you need to create a file containing all the necessary information that may be required during the installation process. Such a file is created by the system-config-kickstart program (GUI tool) in any CentOS with X Window:
# yum -y install system-config-kickstart
#system-config-kickstart

After we have created the file using system-config-kickstart, it needs to be transferred to the Install server and made available via one of the HTTP, NFS or FTP protocols. Since the Install server actively uses NFS, we will use it.

In my case, the kickstart file is located in /var/install-server/centos5_x86/centos5_ x86_ks.cfg .

To the file /tftpboot/pxelinux.cfg/default you just need to add the ks directive indicating the location of the kickstart file. Example with kickstart file:
default menu.c32
menu title Linux Install Server. Please choose OS to install.
prompt 0
timeout 100
label CentOS 5 x86 Custom install
kernel /centos5_x86/vmlinuz
append initrd=/centos5_x86/initrd.img
label CentOS 5 x86 Kickstart Install
kernel /centos52_x86/vmlinuz
append initrd=/centos5_x86/initrd.img ks=nfs:192.168.146.1:/var/install-server/c entos5_x86/centos5_x86_ks.cfg
label Quit
localboot 0

Now, having selected “CentOS 5 x86 Kickstart Install” in the OS selection menu, we will only have to wait for the server with the OS installed on it.

Below is an example of my Kickstart file. I wanted the installed OS to have the “PermitRootLogin yes” option in the sshd settings. The Kickstart file allows you not only to set OS installation parameters, but also to execute scripts before installation (%pre) and after (%post). This way you can write a lot of tuning scripts and get a completely finished OS in 5-10 minutes of installation.

#platform =x86, AMD64, or Intel EM64T
# System authorization information
auth --useshadow --enablemd5
# System bootloader configuration
bootloader --location=mbr
# Clear the Master Boot Record
zerombr
#Partition clearing information
clearpart --all --initlabel
# Use text mode install
text
# Firewall configuration
firewall --disabled
# Run the Setup Agent on first boot
firstboot --disable
#System keyboard
keyboard us
# System language
lang en_US
# Installation logging level
logging --level=info
# Use NFS installation media
nfs --server=192.168.146.1 --dir=/var/install-server/centos5_x86
# Network information
network --bootproto=dhcp --device=eth0 --onboot=on
#Root password
rootpw --iscrypted $1$Bz09jb2I$hfzh2vApqMjG0sEPsAwNr/
# SELinux configuration
selinux --disabled
# Do not configure the X Window System
skipx
#System timezone
timezone Europe/Moscow
# Install OS instead of upgrade
install
# Disk partitioning information
part swap --bytes-per-inode=4096 --fstype=”swap” --size=512
part / --bytes-per-inode=4096 --fstype=”ext3” --grow --size=1

%post --interp /bin/bash
PATH=/somework
/bin/mkdir$PATH
/bin/sed -e ‘s/#PermitRootLogin yes/PermitRootLogin yes/g’ /etc/ssh/sshd_config > $PATH/sshd_config_edited
/bin/cp $PATH/sshd_config_edited /etc/ssh/sshd_config
/bin/rm -rf $PATH

This article will cover installing Ubuntu Linux from a network boot repository.

So, we will install Ubuntu 10.04 on client computers from server 192.168.0.3, which hosts the network boot server and dhcpd. To install, the repository must have a directory dists/lucid/main/debian-installer. If you add similar directories for other repositories (multiverse, universe, restricted), then at the “Selecting and installing software” stage the list of software will be larger. In this guide I will consider this option. If you made a debmirror mirror, then most likely you don’t have these directories at all. You can download them, for example, from ftp://mirror.yandex.ru. Also, during installation, the installer, regardless of the selected repository, often accesses security.ubuntu.com.

Step 1: Server Installation

Installation of necessary applications:

apt-get install tftpd-hpa openbsd-inetd

Download and unpack the image for network installation:

tar -xvzf netboot.tar.gz -C /var/lib/tftpboot/

chown -R nobody:nogroup /var/lib/tftpboot

For network boot, you need to pass 2 parameters to the client: the name of the boot file and the boot file server (in dhcpd these are the filename and next-server parameters):

next-server 192.168.0.3;
filename "pxelinux.0";

The server is installed, you can install the system.

Step 2: System Installation

To install the system from a server on the network, you must enable network boot (PXE) in the BIOS. If your bios supports the boot menu, then it is better to use it to select the boot device - so that there are no problems if you forget to change the boot order.

If everything is fine, then you will see the installer welcome screen:

  • Basic Ubuntu Server
  • Name server (DNS)
  • Edubuntu server
  • LAMP server
  • Mail server
  • OpenSSH server
  • PostgreSQL database
  • Print server
  • Samba file server
  • Ubuntu Enterprise Cloud
  • Virtual Machine host
  • 2D/3D creation and editing suite
  • Audio creation and editing suite
  • Edubuntu KDE desktop
  • Edubuntu desktop
  • Kubuntu desktop
  • Kubuntu netbook
  • LADSPA and DSSI audio plugins
  • Large selection of font packages
  • Mythbuntu additional roles
  • Mythbuntu frontend
  • Mythbuntu master backend
  • Mythbuntu slave backend
  • Ubuntu Netbook
  • Ubuntu Desktop
  • Video creation and editing suite
  • Xubuntu desktop
  • Manual package selection

Today, more and more tasks are being automated, and virtualization is increasingly being used to maximize the efficiency of servers. But you still have to install operating systems. Everyone does it in their own way: some have pockets full of different images for all occasions, others carry a “purse” with discs, or even two, in the old fashioned way. As a rule, administrators do this work with little pleasure. Let's look at how to reduce time for trivial tasks, how to teach computers to install systems on their own, without the participation of an administrator at all, using only a local network.

So, today we will learn: install Windows and Linux over the network, load small ISO images, useful software (all sorts of Kaspersky, Acronis, WinPE, memtests), deploy thin clients and manage them. So that, for example, an accountant working with 1C via RDP doesn’t beat you up because her Windows crashed, and the report needed to be prepared yesterday... Or a stingy boss who doesn’t want to update his computer admired your professionalism when will see how Windows 8 flies on old computers... A server that provides boot over the network (PXE) will help us achieve our insidious goals.

Every system administrator has a universal USB drive in their stash for emergency computer resuscitation. Agree, it would be much better to have the same functionality using just one network card. It should also be noted that it is possible to simultaneously work with several nodes at once. So, based on our needs, we have two solutions: use PXE or LTSP.

LTSP is not very suitable for us: it is designed to load the OS installed on the server itself over the network, which allows the use of LTSP server applications. This is not exactly what we need. PXE is a tool for booting a computer over a network without using local storage media, just like LTSP. PXE allows you to organize a multi-boot boot menu, similar to a universal “USB resuscitator”.


What will we implement?

It all started with the need to have at hand a tool for remote installation of Ubuntu/Debian Server over the network, with the ability to boot a Live CD of a small system, like SliTaz or Kolibri OS.
As they say, appetite comes with eating: we didn’t have time to implement what we planned, and a number of “wants” were added to the plan. As a result, the list turned out to be quite impressive.

  1. Thinstation Linux-based thin clients.
  2. Linux section.
    1. Installing Ubuntu 14.04 x86.
    2. Installing Ubuntu 14.04 x64.
    3. Installing Ubuntu 12.04 x86.
    4. Installing Ubuntu 12.04 x64.
  3. Windows partition.
    1. Installing Windows 2012.
    2. Installing Windows 7.
  4. Acronis.
    1. Windows PE with a package of useful software.
    2. Acronis True Image.
      1. Legacy BIOS.
      2. UEFI.
    3. Acronis Disk Director.
      1. Legacy BIOS.
      2. UEFI.
  5. Kaspersky Rescue v 10.
  6. ERD Commander from 5 to 8 via ISO image.
  7. Memtest.

We put everything together and take off

As the distribution for the server, the choice fell on Ubuntu Server 14.04.2 LTS. You can choose any other OS, the only difference will be in the syntax. So let's get started. We will need TFTP, DHCP (not necessarily installed on the same server; a router can act as a DHCP server), a service for organizing an NFS network file system. We will consider only those settings that interest us within the topic. First of all, let's install everything you need, after making all the updates:

Continuation is available only to members

Option 1. Join the “site” community to read all materials on the site

Membership in the community within the specified period will give you access to ALL Hacker materials, increase your personal cumulative discount and allow you to accumulate a professional Xakep Score rating!

Everyone who even occasionally does administration has needed to install an operating system on their computer at least once. And often everything happens like this: the image is downloaded, written to a disk or flash drive, loaded and installed. But it may turn out that you don’t have a flash drive or disk at hand, and the installation needs to be completed yesterday... Then you can boot over the network, and there are no particular problems with Linux in this regard. A typical configuration is a bunch DHCP, TFTP And NFS. But I didn’t have it set up at hand NFS, but there was Samba. So I had to figure out how to use Samba instead of NFS.

It seemed like nothing, I changed the settings and that’s it, but something didn’t work at first, I had to tinker...

But I'll start from the very beginning here. So first you need to get a distribution kit. Since we put Linux Mint We are looking for a fresh image. Download the distribution that suits us (I installed the one with MATE, but obviously this doesn't really matter). After downloading the image, you need to gain access to the image files. Some guides recommend unpacking the image, but I don’t see the point in this, so I support the opinion that is to mount the image as loop. In order not to interfere with the existing order, we simply create a new directory where we mount the image:

# mkdir /mnt/mint
# mount -o loop /home/user/download/linuxmint-17-mate-64bit-v2.iso /mnt/mint

Next, you need to make the files available over the network for the installer to read. As I said above, for this purpose I use Samba. Therefore, open the configuration file and add a new share by adding the following section:


path = /mnt/mint
available = yes
browsable = yes
public = yes
writable = no

Should work over default settings Samba. If it doesn’t work, then most likely you changed something yourself, which means you’ll find a way to solve the problem. So we added the ball mint, through which you can get files for installation from the network. Don't forget to restart Samba to accept the new settings.

For Archlinux:

# systemctl restart smbd
# systemctl restart nmbd

For Gentoo:

# /etc/init.d/samba restart

Next you need to prepare a network bootloader. The program will help us with this tftp-hpa. IN Archlinux and in Gentoo That's what the package is called. You need to place the loader files in the working directory of the ftp server (in arch /srv/tftp). Copy the kernel and initrd from the mounted image there:

# cp /mnt/mint/casper/(vmlinux,initrd.lz) /srv/http

# cp /usr/lib/syslinux/bios/(pxelinux.0,menu.c32,ldlinux.c32,libutil.c32) /srv/http

DEFAULT menu.c32
LABEL LinuxMint
KERNEL vmlinuz
APPEND showmounts toram root=/dev/cifs boot=casper netboot=cifs nfsroot=//192.168.0.1/mint NFSOPTS=-oguest,ro initrd=initrd.lz nosplash

Where 192.168.0.1 - our IP address relative to the machine on which we will install. Oh yes, and about nfs there - this is not a typo, in the mode of working with cifs, you need to pass type options as for nfs, I can't say why. Particular attention should be paid to the toram option, since without it everything falls off halfway and we cannot boot (maybe these are features of samba). But with this option, you need to remember that the target machine must have enough RAM to copy the installer there. The TFTP server needs to be started. For Archlinux:

# systemctl tftpd.socket start

For Gentoo:

# /etc/init.d/in.tftpd start

Now the BIOS of the target machine needs to somehow tell where it is loading from. This will help us dnsmasq(although not necessary, you can use another DHCP server; here I describe what was at hand). Setting up IP distribution (optional dhcp-range) at your own discretion, for your own “network” between machines. For installation, only three options are important to us:

Dhcp-boot=/pxelinux.0
dhcp-option=vendor:PXEClient,6,2b
pxe-service=x86PC, "Install Linux", pxelinux

They need to be added to the configuration dnsmasq, and of course launch it (or restart).

For Archlinux:

# systemctl start dnsmasq

For Gentoo:

# /etc/init.d/dnsmasq start

Remember, for everything to work, the ports must be on DHCP(UDP 67.68), TFTP(UDP 69), Samba(UDP 137.138, TCP 139.445) must be open; be sure to take this into account in your firewall configuration. If there is no firewall or the interface is completely open - oh well, it should work.

Note: Due to the fact that systems are constantly updated, the given file paths may be invalid, that is, different from those on your system. Therefore, carefully look at the package structure of your system.