Skip to content
This repository
tree: 2be48ada17
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 159 lines (138 sloc) 4.703 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
#!/bin/bash

# A basic script to setup a PXE server enviroment for Arch Linux live-media.
# Contributed by Gerardo Exequiel Pozzi <vmlinuz386@yahoo.com.ar>

# Requires: dnsmasq and nbd packages

set -e

TMPDIR=/tmp/archiso-pxe-server
VERSION=20101216
INSTALL_DIR=lglive
DNSMASQ_SKIP=false
NBD_SKIP=false

iso_umount() {
    trap - 0 1 2 15
    umount ${TMPDIR}/mnt
    rmdir ${TMPDIR}/mnt
}

iso_mount() {
    mkdir -p ${TMPDIR}/mnt
    trap 'iso_umount' 0 1 2 15
    mount ${ISO} ${TMPDIR}/mnt -o ro,loop
}

show_help()
{
    exitvalue=${1}
    echo
echo "archiso-pxe-server version ${VERSION}"
    echo
echo "archiso-pxe-server [options]"
    echo
echo " options:"
    echo " -i [ip] ip address of the local interface to serve"
    echo " (default: ${IP} )"
    echo " -d [device] boot device of Arch Linux Live media"
    echo " (default: ${DEVICE} )"
    echo " -l [install_dir] install_dir of Arch Linux Live media"
    echo " (default: ${INSTALL_DIR} )"
    echo " -s [isofile] Arch Linux Live media iso image"
    echo " (default: ${ISO} )"
    echo " -m [mountpoint] path to [device] is mounted"
    echo " (default: ${MNTPNT} )"
    echo " -t [tftpboot] path to setup PXE enviroment"
    echo " (default: ${TMPDIR} )"
    echo " -j Skip dnsmasq start up"
    echo " (default: ${DNSMASQ_SKIP} )"
    echo " -k Skip nbd start up"
    echo " (default: ${NBD_SKIP} )"
    echo
exit ${exitvalue}
}

copy_files()
{
    if [ -f ${MNTPNT}/${INSTALL_DIR}/boot/i686/vmlinuz26 ]; then
mkdir -p ${TMPDIR}/${INSTALL_DIR}/boot/i686
        cp ${MNTPNT}/${INSTALL_DIR}/boot/i686/vmlinuz26 ${TMPDIR}/${INSTALL_DIR}/boot/i686
        cp ${MNTPNT}/${INSTALL_DIR}/boot/i686/lglive.img ${TMPDIR}/${INSTALL_DIR}/boot/i686
    fi
if [ -f ${MNTPNT}/${INSTALL_DIR}/boot/x86_64/vmlinuz26 ]; then
mkdir -p ${TMPDIR}/${INSTALL_DIR}/boot/x86_64
        cp ${MNTPNT}/${INSTALL_DIR}/boot/x86_64/vmlinuz26 ${TMPDIR}/${INSTALL_DIR}/boot/x86_64
        cp ${MNTPNT}/${INSTALL_DIR}/boot/x86_64/lglive.img ${TMPDIR}/${INSTALL_DIR}/boot/x86_64
    fi

mkdir -p ${TMPDIR}/pxelinux.cfg
    cp ${MNTPNT}/${INSTALL_DIR}/boot/memtest ${TMPDIR}/${INSTALL_DIR}/boot
    cp ${MNTPNT}/${INSTALL_DIR}/boot/splash.png ${TMPDIR}/${INSTALL_DIR}/boot

    cp ${MNTPNT}/syslinux/*.0 ${TMPDIR}
    cp ${MNTPNT}/syslinux/*.c32 ${TMPDIR}
    cp ${MNTPNT}/syslinux/*.com ${TMPDIR}
    cp -r ${MNTPNT}/syslinux/hdt ${TMPDIR}
    for _cfg in ${MNTPNT}/syslinux/*.cfg; do
sed 's|^#IPAPPEND|IPAPPEND|g' ${_cfg} > ${TMPDIR}/$(basename ${_cfg})
    done
cp ${TMPDIR}/syslinux.cfg ${TMPDIR}/pxelinux.cfg/default
}

start_pxe_server()
{
    pkill dnsmasq > /dev/null 2>&1 || true
dnsmasq \
        --port=0 \
        --enable-tftp \
        --tftp-root=${TMPDIR} \
        --dhcp-boot=/pxelinux.0,"${IP}" \
        --dhcp-range=${IP%.*}.2,${IP%.*}.254,86400
}

start_nbd_server()
{
    pkill nbd-server > /dev/null 2>&1 || true
echo "[generic]" > ${TMPDIR}/nbd-server.conf
    echo "[archiso]" >> ${TMPDIR}/nbd-server.conf
    echo " readonly = true" >> ${TMPDIR}/nbd-server.conf
    echo " exportname = ${DEVICE}" >> ${TMPDIR}/nbd-server.conf
    nbd-server -C ${TMPDIR}/nbd-server.conf
}

guess_enviroment()
{
    if grep archisolabel /proc/cmdline > /dev/null; then
DEVICE="/dev/disk/by-label/"$(sed "s/.\+archisolabel=\([^ ]\+\).\+/\1/" /proc/cmdline)
        MNTPNT=/bootmnt
    fi

IP=$(ifconfig eth0 | awk -F":| +" '/inet addr/{print $4}')
}

check_parameters()
{
    if [ -z "${IP}" ]; then
echo "ERROR: missing IP address"
        show_help 1
    fi

if [ -n "${ISO}" ]; then
iso_mount
        DEVICE=$(readlink -f ${ISO})
        MNTPNT="${TMPDIR}/mnt"
    fi

if [ -z "${DEVICE}" ]; then
echo "ERROR: can not determine boot device, please specify on command line"
        show_help 1
    fi
}

guess_enviroment

while getopts 'i:s:d:l:m:t:jkh' arg; do
case "${arg}" in
        i) IP="${OPTARG}" ;;
        s) ISO="${OPTARG}" ;;
        d) DEVICE=$(readlink -f ${OPTARG}) ;;
        l) INSTALL_DIR="${OPTARG}" ;;
        m) MNTPNT="${OPTARG}" ;;
        t) TMPDIR="${OPTARG}" ;;
        j) DNSMASQ_SKIP=true ;;
        k) NBD_SKIP=true ;;
        h) show_help 0 ;;
        *) echo; echo "*ERROR*: invalid argument '${arg}'"; show_help 1 ;;
    esac
done

check_parameters
copy_files
if ! ${DNSMASQ_SKIP} ; then
start_pxe_server
fi
if ! ${NBD_SKIP} ; then
start_nbd_server
fi
Something went wrong with that request. Please try again.