Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Recipe for network booting.
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
saltine
n00b
n00b


Joined: 25 Mar 2005
Posts: 24
Location: Winnipeg, Canada

PostPosted: Tue Apr 12, 2005 12:21 am    Post subject: Recipe for network booting. Reply with quote

I take no responsibility if this overwrites a config or breaks things in general.
Read the script before attempting to use it!



I have been working on this script over the last while when time permits. The goal is to create a script to install the services necessary to get LTSP up and running with some nice defaults and extras. I am not all knowing so review the script and manuals for the packages before proceeding. The script is more or less self-explanitory for those familliar with bash and awk. It should result in a painless installation if you configure the variables at the top of the script to your needs. For a quick easy install simply set the interface and dhcp_default_range variables to your needs and let the defaults and interface configuration detection handle the rest. Please note that this is a work in progress and currently only allows booting of LTSP to a shell, as well as memtest86+ and FreeDOS.

Todo:
- Add code to configure xdm, gdm or kdm.
- Add ypserv installation and configuration code.
- Add confirmation code to allow stepping through and skipping of steps..
- Add support for --verbose, --force.
- Add user traps so executing this script is less dangerous to an already configured system.

Note:
- See http://syslinux.zytor.com/hardware.php for known pxelinux hardware compatibility issues and work arounds.
- If you are going to use etherboot floppies from http://rom-o-matic.net/ be sure to check PXELOADER_KEEP_ALL during the configuration phase of the floppy generation.
- The FreeDOS floppy is a bare bones ODIN floppy and is more or less intended as to be an example for network booting floppy images. If you want to modify the image mount it and make your changes. I currently use it for running bios flashes and diagnostic utilities.

Code:
#!/bin/bash
# Distributed under the terms of the GNU General Public License v2
# Written by: Cory Oldford
# Purpose: Install LTSP and related services under Gentoo Linux.

# Set the interface.
interface='eth0'
interface_detect='auto'

# If interface_detect is set to auto the network configuration
# will be auto configured based on the output of ifconfig, route and dnsdomainname.
# If this value is set to anything other than auto you must
# uncomment the interface_* variables and configure to your needs.
#interface_ip=''
#interface_broadcast=''
#interface_netmask=''
#interface_gateway=''
#interface_network_ip=''
#interface_domain=''
#interface_nameservers=''

# variables to set to your needs.
chroot_dir='/chroot'
packages='syslog-ng xinetd tftp-hpa dhcp nfs-utils lts
memtest_url='http://www.memtest.org/download/1.55.1/memtest86+-1.55.1.bin.gz'
freedos_url='http://www.fdos.org/ripcord/beta9sr1/disksets/ODIN/fdodin91.img'

# Syslog-ng configuration
syslog_seed_config='/usr/portage/app-admin/syslog-ng/files/syslog-ng.conf.gentoo'
syslog_net_source='source net { udp(); };'
syslog_net_destination='destination netlog { file("/var/log/ltsp.messages"); };'
syslog_net_log='log { source(net); destination(netlog); };'

# tftp variables:
tftp_chroot="$chroot_dir/tftpboot"
tftp_user='tftp'
#tftp_args="-u $tftp_user -s $tftp_chroot"     # Normal
tftp_args="-vvvvv -u $tftp_user -s $tftp_chroot"   # Super verbose
#tftp_args="-vvvvv -u $tftp_user -s $tftp_chroot -r blksize"  # fix for some Intel stacks.

# dhcp variables:
dhcp_opts='-q'
dhcp_chroot="$chroot_dir/dhcp"
dhcp_lease_default=1592000
dhcp_lease_max=1592000
dhcp_default_range='192.168.0.2 192.168.0.254'

# ltsp variables
ltsp_version=`qpkg -I -v -nc ltsp | awk -F - '{print $3}'`

# If net_config_mode is auto configure based on ifconfig, route, dnsdomainname output and /etc/resolv.conf
if [ $interface_detect =  'auto' ] ; then
    # Grab the ip, broadcast and netmask from the output of ifconfig <interface>
    interface_ifconfig=`ifconfig $interface`
    interface_ip=`echo "$interface_ifconfig" | awk '/Bcast/ { x=substr($2, 6) } END { print x }'`
    interface_broadcast=`echo "$interface_ifconfig" | awk '/Bcast/ { x=substr($3, 7) } END { print x }'`
    interface_netmask=`echo "$interface_ifconfig" | awk '/Bcast/ { x=substr($4, 6) } END { print x }'`

    # Grab the network ip and gateway from the output of route -n
    interface_route=`route -n`
    interface_gateway=`echo "$interface_route" | awk '/UG/ && /'$interface'/ { print $2 }'`
    interface_network_ip=`echo "$interface_route" | awk '! /UG/ && /'$interface'/ { print $1 }'`

    # Grab the domainname and nameservers
    interface_domain=`dnsdomainname`
    interface_nameservers=`awk '/nameserver/ {num_ns++; if(num_ns == 1) {ns=$2} else {ns=sprintf("%s,%s",ns,$2)}} END {print ns}' /etc/resolv.conf`
fi

# Display a nice green asterisk followed by $1
function display_info () {
    echo -e "\n \033[32;01m*\033[0m" "$1"
}

# Get the last line number starting with $1 in file $2
function get_last_occur () {
   awk 'BEGIN{x=0}{if($1 == "'$1'") x = NR}END{print x}' $2
}

# Insert line $1 after line $2 in file $3
function insert_line_after () {
   echo "Inserting \"$1\" into $3 after line $2"
   newline=`echo -E $1 | awk '{ gsub(/"/, "\\\\\""); print }'`
   cat $3 | awk '{if(NR=='$2'){print $0; print "'"$newline"'"} else print $0 }' > $3
}

# Swap line $1 with $2 in file $3
function swap_line () {
   echo "swapping $1 with \"$2\" in file $3"
   cat $3 | awk '{if(NR=='$1'){print "'"$2"'"} else print $0 }' > $3
}

for x in syslog-ng portmap nfs dhcp xinetd; do /etc/init.d/$x stop; done;

# create chroot directory
rm -rf $chroot_dir
mkdir -p --verbose $chroot_dir

# Install the necessary packages
display_info "Installing $packages"
echo 'sys-boot/syslinux ~x86' >> /etc/portage/package.keywords
emerge -a $packages

# Copy seed syslog-ng.conf to /etc/syslog-ng/syslog-ng.conf
display_info "Creating /etc/syslog/syslog-ng.conf using $syslog_seed_config as a base."
cp -v $syslog_seed_config /etc/syslog-ng/syslog-ng.conf

# Check if syslog is configured to accept udp connections.
# If the system logger is already configured to accept udp connections
# assume the user has already configured syslog-ng appropriately for network logging.
target=`awk 'BEGIN{x=0}/udp()/{if($1 == "source") x = NR}END{print x}' /etc/syslog-ng/syslog-ng.conf`
if [ $target -eq 0 ]; then
   echo "Syslog-ng not configured for network logging"
   target=`get_last_occur source /etc/syslog-ng/syslog-ng.conf`
   insert_line_after "$syslog_net_source" $target /etc/syslog-ng/syslog-ng.conf
   target=`get_last_occur destination /etc/syslog-ng/syslog-ng.conf`
   insert_line_after "$syslog_net_destination" $target /etc/syslog-ng/syslog-ng.conf
   target=`get_last_occur log /etc/syslog-ng/syslog-ng.conf`
   insert_line_after "$syslog_net_log" $target /etc/syslog-ng/syslog-ng.conf
else
   echo "Syslog-ng seems to configured to accept udp connections"
fi

# Create log directories and files using /var/log entries in syslog-ng
syslog_files=`awk -F \" '/\/var\/log/ && !/# / && !/tty/ {print $2}' /etc/syslog-ng/syslog-ng.conf`
syslog_dirs=`for x in $syslog_files; do dirname $x; done | sort | uniq`
display_info "Creating the syslog-ng directories and files"
for x in $syslog_dirs; do mkdir -p --verbose $x; done
echo $syslog_files
touch $syslog_files

display_info "Creating default /etc/exports and ltsp swap directory"
cat <<- EOF > /etc/exports
/opt/ltsp-$ltsp_version/i386         $interface_network_ip/$interface_netmask(ro,no_root_squash,async)
/var/opt/ltsp/swapfiles    $interface_network_ip/$interface_netmask(rw,no_root_squash,async)
EOF
cat /etc/exports
mkdir -p --verbose /var/opt/ltsp/swapfiles

display_info "Creating default /etc/xinetd.d/tftp file."
cat <<- EOF > /etc/xinetd.d/tftp
service tftp
{
    disable = no
    socket_type = dgram
    protocol = udp
    wait = yes
    user = root
    group = $tftp_user
    server = `which in.tftpd`
    server_args = $tftp_args
}
EOF
cat /etc/xinetd.d/tftp

# setup xinetd
display_info "Configuring xinetd."
cat <<- EOF > /etc/xinetd.conf
defaults
{
    instances      = 60
    log_type       = SYSLOG authpriv info
    log_on_success = HOST PID
    log_on_failure = HOST
    cps            = 25 30
}
includedir /etc/xinetd.d
EOF
cat /etc/xinetd.conf

# Move /tftpboot to tftp chroot
display_info "Moving /tftpboot to $chroot_dir directory"
mv -v /tftpboot $chroot_dir
mv -v $chroot_dir/tftpboot/pxelinux.cfg $chroot_dir/tftpboot/pxe

# The pxelinux included with ltsp is lacking some nice extras like
# menu.c32, memdisk and chain.c32 emerging syslinux temporarily will resolve this
display_info "Emerging syslinux and copying pxelinux bootloader and related files to tftp chroot."
emerge -a --oneshot syslinux
cp -v /usr/lib/syslinux/pxelinux.0 $tftp_chroot/pxe
cp -v /usr/lib/syslinux/{memdisk,menu.c32,chain.c32} $tftp_chroot/pxe
 
display_info "Removing syslinux as required files have been obtained"
emerge unmerge -a syslinux

# might as well grab memtest as a boot option..
display_info "Grabbing memtest+ "
wget -O memtest86+.gz $memtest_url
gunzip memtest86+.gz
mv -v memtest86+ $tftp_chroot/pxe/memtest86+

# I'll grab a freedos odin floppy image as a boot option.
display_info "Grabbing freedos "
wget -O freedos $freedos_url
mv -v freedos $tftp_chroot/pxe/freedos

display_info "Creating $tftp_chroot/pxe/pxelinux.cfg/default"
cat  <<- EOF > $tftp_chroot/pxe/pxelinux.cfg/default
default menu.c32
prompt 0
timeout 100
ontimeout harddrive

menu title Choose a boot option....
label ltsp
menu label ^ltsp
kernel `basename $tftp_chroot/pxe/bzImage-*`
append init=/linuxrc rw root=/dev/ram0 initrd=`basename $tftp_chroot/pxe/initrd-*`

label floppy
menu label ^floppy
kernel chain.c32
append fd0

label harddrive
menu label ^harddrive
kernel chain.c32
append hd0

label memtest
menu label ^memtest
kernel memtest86+

label freedos
menu label free^dos
kernel memdisk
append initrd=freedos
EOF
cat $tftp_chroot/pxe/pxelinux.cfg/default

# create tftp group and user account.
display_info "Creating $tftp_user group and user."
groupadd $tftp_user
useradd -d $tftp_chroot -s /bin/false -G $tftp_user -g $tftp_user $tftp_user

# change ownership of tftp chroot and files.
display_info "Changing ownership of $tftp_chroot to $tftp_user:$tftp_user."
chown -R $tftp_user:$tftp_user $tftp_chroot

# configure dhcp chroot
display_info "Configuring dhcp chroot: $dhcp_chroot"
dhcp_version=`qpkg -I -v -nc dhcp | awk '/dhcp-/ {print substr($0,10)}'`
ebuild /var/db/pkg/net-misc/$dhcp_version/$dhcp_version.ebuild config
touch $dhcp_chroot/var/lib/dhcp/dhcpd.leases
ln -s /dev/log $dhcp_chroot/dev/log

# change ownership of dhcp chroot
display_info "Changing ownership of $dhcp_chroot to dhcp:dhcp."
chown -R dhcp:dhcp $dhcp_chroot

display_info "Creating default $dhcp_chroot/etc/conf.d/dhcp file."
cat <<- EOF >  /etc/conf.d/dhcp
IFACE="$interface"
DHCPD_OPTS="$dhcp_opts"
CHROOT="$dhcp_chroot"
export LD_PRELOAD="/usr/lib/libresolv.so /usr/lib/libnss_dns.so"
EOF
cat /etc/conf.d/dhcp

display_info  "Creating $dhcp_chroot/etc/dhcp/dhcpd.conf"
cat <<- EOF > $dhcp_chroot/etc/dhcp/dhcpd.conf
ddns-update-style none;
authoritative;

option pxelinuxmagic code 208 = string;
option pxelinuxpathprefix code 210 = text;
option pxelinuxreboottime code 211 = unsigned integer 32;

use-host-decl-names on;

group "network-boot"{
    class "PXE" {
        match if substring(option vendor-class-identifier, 0, 9) = "PXEClient";
    }
    class "Etherboot" {
        match if substring (option vendor-class-identifier, 0, 9) = "Etherboot";
    }
    if exists dhcp-parameter-request-list {
        option dhcp-parameter-request-list = concat(option dhcp-parameter-request-list,d0,d2,d3);
    }
    option pxelinuxmagic f1:00:74:7e;
    option pxelinuxpathprefix         "/pxe/";
    option pxelinuxreboottime          30;
    filename "/pxe/pxelinux.0";
}

shared-network "local" {
    subnet $interface_network_ip netmask $interface_netmask {
        default-lease-time              $dhcp_lease_default;
        max-lease-time                  $dhcp_lease_max;
        option subnet-mask              $interface_netmask;
        option broadcast-address        $interface_broadcast;
        option routers                  $interface_gateway;
        option domain-name-servers      $interface_nameservers;
        option domain-name              "$interface_domain";
        option root-path                "$interface_ip:/opt/ltsp-$ltsp_version/i386";
        range                           $dhcp_default_range;
    }
}
EOF
cat $dhcp_chroot/etc/dhcp/dhcpd.conf

# Necessary because of Gentoo bug 39809 :(
if ! [ -e /opt/ltsp-$ltsp_version/i386/dev ]; then
    display_info  "Gentoo bug 39809 detected creating necessary dirs to compensate"
    mkdir --verbose /opt/ltsp-$ltsp_version/i386/{dev,oldroot,proc}
fi

# We need a basic lts.conf.
cat <<- EOF > /opt/ltsp-$ltsp_version/i386/etc/lts.conf
[DEFAULT]
    SERVER          = $interface_ip
    RUNLEVEL        = 3
    SCREEN_1        = "shell"
EOF

# Add services to default runlevel and start.
for x in syslog-ng nfs dhcp xinetd; do rc-update add $x default; /etc/init.d/$x start; done;


Last edited by saltine on Wed Jun 29, 2005 1:44 am; edited 44 times in total
Back to top
View user's profile Send private message
chrisyu
Apprentice
Apprentice


Joined: 10 Apr 2003
Posts: 206
Location: China

PostPosted: Fri Apr 15, 2005 6:28 pm    Post subject: Reply with quote

Just made it work by following your script~~
Of course step by step, pasting them to a term.
Cool.

I could boot my IBM X24 by PXE(IBA 4.0.22 Slot 240) and run memtest now.:D
Thank you!
Back to top
View user's profile Send private message
saltine
n00b
n00b


Joined: 25 Mar 2005
Posts: 24
Location: Winnipeg, Canada

PostPosted: Fri Apr 15, 2005 7:35 pm    Post subject: Reply with quote

Its good to hear the script came in handy for someone. I just added freedos floppy image boot option as a default. I wanted a nice open licensed example for the script. I would like to add a Linux based firewall on a floppy image but haven't had the time to modify a disk image to facilitate network booting it.

Last edited by saltine on Mon Apr 18, 2005 7:18 am; edited 4 times in total
Back to top
View user's profile Send private message
chrisyu
Apprentice
Apprentice


Joined: 10 Apr 2003
Posts: 206
Location: China

PostPosted: Sun Apr 17, 2005 5:14 pm    Post subject: Reply with quote

I want to know can PXE boot from a bootable ISO?
or convert an ISO to PXE mode?

It's nice to have a rescue cd option.:)

--
and seems there is sth wrong in the script.
around here
Code:

EOF
cat ${tftp_chroot}${pxe_pathprefix}/pxelinux.cfg/default
Back to top
View user's profile Send private message
saltine
n00b
n00b


Joined: 25 Mar 2005
Posts: 24
Location: Winnipeg, Canada

PostPosted: Sun Apr 17, 2005 5:51 pm    Post subject: Reply with quote

Ooops thats what I get for cutting and pasting parts of the script.
I have corrected it now. I don't know of a method to boot ISOs via PXE.
It would be nice but ISOs are typically rather large.
Back to top
View user's profile Send private message
jamapii
Guru
Guru


Joined: 16 Sep 2004
Posts: 571

PostPosted: Mon Apr 18, 2005 5:01 pm    Post subject: Reply with quote

You can boot into an unpacked stage3. Make sure the kernel can do "Root file system on NFS". Give the computer a fixed IP address with dhcpd.conf, or make it an option in pxelinux.cfg/default . An example for a pxelinux configuration would be:
Code:
DEFAULT kernel-2.4.26-gentoo-r9
APPEND ip=dhcp root=/dev/nfs nfsroot=192.168.10.81:/disks/hdd1/diskless/lily mem=512M pci=noacpi
This way, you can make a diskless computer, or have a "rescue NFS", or install directly from a stage3/stage4. And you don't need a CD drive in each computer.
Back to top
View user's profile Send private message
saltine
n00b
n00b


Joined: 25 Mar 2005
Posts: 24
Location: Winnipeg, Canada

PostPosted: Mon Apr 18, 2005 5:25 pm    Post subject: Reply with quote

I haven't bothered with adding support for mounting root over NFS. I was holding off until the other components are completed and LTSP installation/configuration code has been added. I still need to figure in how I should add the kernel related aspects. I am highly picky and rather daring when it comes to choosing and patching a kernel but want to leave the script openended enough for others to find it useful.
Back to top
View user's profile Send private message
jamapii
Guru
Guru


Joined: 16 Sep 2004
Posts: 571

PostPosted: Wed Apr 20, 2005 8:37 pm    Post subject: Reply with quote

"Root file system on NFS" is a kernel config option and has been in all linux kernels for as long as I remember, no patch required. In 2.6 it's called CONFIG_ROOT_NFS.
Back to top
View user's profile Send private message
saltine
n00b
n00b


Joined: 25 Mar 2005
Posts: 24
Location: Winnipeg, Canada

PostPosted: Wed Apr 20, 2005 8:53 pm    Post subject: Reply with quote

I see you misunderstood what I was saying. In order to include support for booting a Linux kernel to the script a linux kernel needs to exist. I suppose I could just put a variable at the top of the script to allow user to set the target kernel however I think a better default option would be to utilize the one bundled with LTSP.

I have been working on a method of autodetecting the network configuration given the interface(ie: eth0). I've used gawk to parse the necessary information from ifconfig and route. Users of the script will be able to decide whether to configure these settings manually or via the autodetection.

UPDATE: Network interface configuration detection and NFS installation/configuration is functional however only tested with my configuration. If it doesn't work in yours fix it then submit your changes please. LTSP now only boots to a shell until I add code to configure xdm, gdm or kdm.


Last edited by saltine on Wed Jun 29, 2005 2:05 am; edited 1 time in total
Back to top
View user's profile Send private message
saltine
n00b
n00b


Joined: 25 Mar 2005
Posts: 24
Location: Winnipeg, Canada

PostPosted: Thu Apr 21, 2005 6:49 pm    Post subject: Reply with quote

Here is the code I will work into the script:

Code:
# Set the interface.
interface='eth0'

# If net_config_mode is set to auto the network configuration
# will be auto configured based on the output of ifconfig and route.
# If this value is set to anything other than auto you must
# uncomment the interface_* variables and configure to your needs.
net_config_mode='auto'
#interface_ip=''
#interface_broadcast=''
#interface_netmask=''
#interface_gateway=''
#interface_network_ip=''

# If net_config_mode is auto configure based on ifconfig output
if [ $net_config_mode =  'auto' ] ; then
    # I'll grab the ip, broadcast and netmask from the output of ifconfig <interface>
    interface_ifconfig=`ifconfig $interface`
    interface_ip=`echo "$interface_ifconfig" | gawk '/Bcast/ { x=substr($2, 6) } END { print x }'`
    interface_broadcast=`echo "$interface_ifconfig" | gawk '/Bcast/ { x=substr($3, 7) } END { print x }'`
    interface_netmask=`echo "$interface_ifconfig" | gawk '/Bcast/ { x=substr($4, 6) } END { print x }'`

    # I'll grab the network ip and gateway from the output of route -n
    interface_route=`route -n`
    interface_gateway=`echo "$interface_route" | awk '/UG/ && /'$interface'/ { print $2 }'`
    interface_network_ip=`echo "$interface_route" | awk '! /UG/ && /'$interface'/ { print $1 }'`
fi


Last edited by saltine on Fri May 13, 2005 5:34 am; edited 1 time in total
Back to top
View user's profile Send private message
danix
n00b
n00b


Joined: 06 Sep 2003
Posts: 16

PostPosted: Thu May 12, 2005 1:23 pm    Post subject: Reply with quote

I've got a glitch that I can't seem to get around.
I originally tried doing this on my own, then later gave up and decided to use the script.
I unmerged and removed everything that I thought would conflict, and I've done this a number of times.
I always stall out here at the end:

Adding xinetd to default runlevel and starting.
* xinetd added to runlevel default
* Caching service dependencies...
* rc-update complete.
* WARNING: "xinetd" has already been started.

Adding dhcp to default runlevel and starting.
* dhcp added to runlevel default
* Caching service dependencies...
* rc-update complete.
* WARNING: "dhcp" has already been started.

---
bash-2.05b# ps -ef | grep dhcp
root 17630 26069 0 09:11 pts/4 00:00:00 grep dhcp
bash-2.05b# /etc/init.d/dhcp start
* WARNING: "dhcp" has already been started.
bash-2.05b# /etc/init.d/dhcp stop
* Stopping dhcpd... [ !! ]

I'm not sure what the problem is. There were no other errors and everything else looks fine.
Thanks for any suggestions.
Back to top
View user's profile Send private message
saltine
n00b
n00b


Joined: 25 Mar 2005
Posts: 24
Location: Winnipeg, Canada

PostPosted: Thu May 12, 2005 1:46 pm    Post subject: Reply with quote

Try the following:
Code:
kill -9 `ps -A | awk '/dhcpd/ || /xinet/ { print $1}'`
for x in dhcp xinetd; do /etc/init.d/$x stop; /etc/init.d/$x zap; /etc/init.d/$x start; done;


Last edited by saltine on Mon May 16, 2005 9:54 pm; edited 3 times in total
Back to top
View user's profile Send private message
danix
n00b
n00b


Joined: 06 Sep 2003
Posts: 16

PostPosted: Fri May 13, 2005 4:10 am    Post subject: Reply with quote

The zap was the key - I didn't even know that option existed.
Thanks!

Now to figure out the NFS piece and how to load the images for the o/s installs.
Back to top
View user's profile Send private message
R1cardo
n00b
n00b


Joined: 02 Sep 2003
Posts: 38
Location: Auckland

PostPosted: Tue Aug 29, 2006 3:22 pm    Post subject: Network PXE install using a windows based PXE server Reply with quote

I am about to try and install gentoo on a machine which doesn't have a CD-rom or floppy drive which will require netbooting from a pxe tftp server.

I don't have any gentoo or linux machines on the network so will have to be using TFTPD32 http://tftpd32.jounin.net/ one one of my windows xp machines.

I understand how to set that up as a pxe server but was wondering what files etc need to be on it to allow the client machine to boot and allow me to install gentoo.

Any advice would be appreciated.

Thanks.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum