网络启动一个 Fedora Live CD
iPXE 远程启动
![](/data/attachment/album/201912/23/224848yaa09am9i3svmxxs.jpg)


[Live CD](https://en.wikipedia.org/wiki/Live_CD) 对于很多任务是很有用的，例如:


* 将操作系统安装到一个硬盘驱动器
* 修复一个启动加载程序或执行其它救援模式操作
* 为 Web 浏览提供一个相适应的最小环境
* …以及[更多的东西](https://en.wikipedia.org/wiki/Live_CD#Uses)。


作为使用 DVD 和 USB 驱动器来存储你的 Live CD 镜像是一个替代方案，你可以上传它们到一个不太可能丢失或损坏的 [iSCSI](https://en.wikipedia.org/wiki/ISCSI) 服务器中。这个指南向你展示如何加载你的 Live CD 镜像到一个 ISCSI 服务器上，并使用 [iPXE](https://ipxe.org/) 启动加载程序来访问它们。


### 下载一个 Live CD 镜像



```
$ MY_RLSE=27
$ MY_LIVE=$(wget -q -O - https://dl.fedoraproject.org/pub/archive/fedora/linux/releases/$MY_RLSE/Workstation/x86_64/iso | perl -ne '/(Fedora[^ ]*?-Live-[^ ]*?\.iso)(?{print $^N})/;')
$ MY_NAME=fc$MY_RLSE 
$ wget -O $MY_NAME.iso https://dl.fedoraproject.org/pub/archive/fedora/linux/releases/$MY_RLSE/Workstation/x86_64/iso/$MY_LIVE
```

上面的命令下载 `Fedora-Workstation-Live-x86_64-27-1.6.iso` Fedora Live 镜像，并保存为 `fc27.iso`。更改 `MY_RLSE` 的值来下载其它档案版本。或者，你可以浏览 <https://getfedora.org/> 来下载最新的 Fedora live 镜像。在 21 之前的版本使用不同的命名约定，必需[在这里手动下载](https://dl.fedoraproject.org/pub/archive/fedora/linux/releases/https://dl.fedoraproject.org/pub/archive/fedora/linux/releases/)。如果你手动下载一个 Live CD 镜像，设置 `MY_NAME` 变量为不带有扩展名的文件的基本名称。用此方法，下面部分中命令将引用正确的文件。


### 转换 Live CD 镜像


使用 `livecd-iso-to-disk` 工具来转换 ISO 文件为一个磁盘镜像，并添加 `netroot` 参数到嵌入的内核命令行：



```
$ sudo dnf install -y livecd-tools
$ MY_SIZE=$(du -ms $MY_NAME.iso | cut -f 1)
$ dd if=/dev/zero of=$MY_NAME.img bs=1MiB count=0 seek=$(($MY_SIZE+512)) 
$ MY_SRVR=server-01.example.edu 
$ MY_RVRS=$(echo $MY_SRVR | tr '.' "\n" | tac | tr "\n" '.' | cut -b -${#MY_SRVR}) 
$ MY_LOOP=$(sudo losetup --show --nooverlap --find $MY_NAME.img) 
$ sudo livecd-iso-to-disk --format --extra-kernel-args netroot=iscsi:$MY_SRVR:::1:iqn.$MY_RVRS:$MY_NAME $MY_NAME.iso $MY_LOOP 
$ sudo losetup -d $MY_LOOP
```

### 上传 Live 镜像到你的服务器


在你的 ISCSI 服务器上创建一个目录来存储你的 live 镜像，随后上传你修改的镜像到其中。


对于 21 及更高发布版本：



```
$ MY_FLDR=/images 
$ scp $MY_NAME.img $MY_SRVR:$MY_FLDR/
```

对于 21 以前发布版本：



```
$ MY_FLDR=/images 
$ MY_LOOP=$(sudo losetup --show --nooverlap --find --partscan $MY_NAME.img) 
$ sudo tune2fs -O ^has_journal ${MY_LOOP}p1 
$ sudo e2fsck ${MY_LOOP}p1 
$ sudo dd status=none if=${MY_LOOP}p1 | ssh $MY_SRVR "dd of=$MY_FLDR/$MY_NAME.img" 
$ sudo losetup -d $MY_LOOP
```

### 定义 iSCSI 目标


在你的 iSCSI 服务器上运行下面的命令：



```
$ sudo -i 
# MY_NAME=fc27 
# MY_FLDR=/images 
# MY_SRVR=`hostname` 
# MY_RVRS=$(echo $MY_SRVR | tr '.' "\n" | tac | tr "\n" '.' | cut -b -${#MY_SRVR}) 
# cat << END > /etc/tgt/conf.d/$MY_NAME.conf 
<target iqn.$MY_RVRS:$MY_NAME>
    backing-store $MY_FLDR/$MY_NAME.img
    readonly 1
    allow-in-use yes 
</target> 
END 
# tgt-admin --update ALL
```

### 创建一个可启动 USB 驱动器


[iPXE](https://ipxe.org/) 启动加载程序有一个 [sanboot](http://ipxe.org/cmd/sanboot/) 命令，你可以使用它来连接并启动托管于你 ISCSI 服务器上运行的 live 镜像。它可以以很多不同的[格式](https://ipxe.org/appnote/buildtargets#boot_type)编译。最好的工作格式依赖于你正在运行的硬件。例如，下面的说明向你展示如何在一个 USB 驱动器上从 [syslinux](https://www.syslinux.org/wiki/index.php?title=SYSLINUX) 中 [链式加载](https://en.wikipedia.org/wiki/Chain_loading) iPXE。


首先，下载 iPXE，并以它的 lkrn 格式构建。这应该作为一个工作站上的普通用户完成：



```
$ sudo dnf install -y git 
$ git clone http://git.ipxe.org/ipxe.git $HOME/ipxe 
$ sudo dnf groupinstall -y "C Development Tools and Libraries"
$ cd $HOME/ipxe/src 
$ make clean 
$ make bin/ipxe.lkrn 
$ cp bin/ipxe.lkrn /tmp
```

接下来，准备一个带有一个 MSDOS 分区表和一个 FAT32 文件系统的 USB 驱动器。下面的命令假设你已经连接将要格式化的 USB 驱动器。**注意：你要格式正确的驱动器！**



```
$ sudo -i 
# dnf install -y parted util-linux dosfstools 
# echo; find /dev/disk/by-id ! -regex '.*-part.*' -name 'usb-*' -exec readlink -f {} \; | xargs -i bash -c "parted -s {} unit MiB print | perl -0 -ne '/^Model: ([^(]*).*\n.*?([0-9]*MiB)/i && print \"Found: {} = \$2 \$1\n\"'"; echo; read -e -i "$(find /dev/disk/by-id ! -regex '.*-part.*' -name 'usb-*' -exec readlink -f {} \; -quit)" -p "Drive to format: " MY_USB
# umount $MY_USB? 
# wipefs -a $MY_USB 
# parted -s $MY_USB mklabel msdos mkpart primary fat32 1MiB 100% set 1 boot on 
# mkfs -t vfat -F 32 ${MY_USB}1
```

最后，在 USB 驱动器上安装并配置 syslinux ，来链式加载 iPXE：



```
# dnf install -y syslinux-nonlinux 
# syslinux -i ${MY_USB}1 
# dd if=/usr/share/syslinux/mbr.bin of=${MY_USB} 
# MY_MNT=$(mktemp -d) 
# mount ${MY_USB}1 $MY_MNT 
# MY_NAME=fc27 
# MY_SRVR=server-01.example.edu 
# MY_RVRS=$(echo $MY_SRVR | tr '.' "\n" | tac | tr "\n" '.' | cut -b -${#MY_SRVR}) 
# cat << END > $MY_MNT/syslinux.cfg
ui menu.c32
default $MY_NAME
timeout 100
menu title SYSLINUX
label $MY_NAME
    menu label ${MY_NAME^^}
    kernel ipxe.lkrn
    append dhcp && sanboot iscsi:$MY_SRVR:::1:iqn.$MY_RVRS:$MY_NAME
END 
# cp /usr/share/syslinux/menu.c32 $MY_MNT 
# cp /usr/share/syslinux/libutil.c32 $MY_MNT 
# cp /tmp/ipxe.lkrn $MY_MNT 
# umount ${MY_USB}1
```

通过简单地编辑 `syslinux.cfg` 文件，并添加附加的菜单项，你应该能够使用这同一个 USB 驱动器来网络启动附加的 ISCSI 目标。


这仅是加载 IPXE 的一种方法。你可以直接在你的工作站上安装 syslinux 。再一种选项是编译 iPXE 为一个 EFI 可执行文件，并直接放置它到你的 [ESP](https://en.wikipedia.org/wiki/EFI_system_partition) 中。又一种选项是编译 iPXE 为一个 PXE 加载器，并放置它到你的能够被 DHCP 引用的 TFTP 服务器。最佳的选项依赖于的环境


### 最后说明


* 如果你以 IPXE 的 EFI 格式编译 IPXE ，你可能想添加 `–filename \EFI\BOOT\grubx64.efi` 参数到 `sanboot` 命令。
* 能够创建自定义 live 镜像。更多信息参考[创建和使用 live CD](https://docs.fedoraproject.org/en-US/quick-docs/creating-and-using-a-live-installation-image/#proc_creating-and-using-live-cd)。
* 可以添加 `–overlay-size-mb` 和 `–home-size-mb` 参数到 `livecd-iso-to-disk` 命令来创建永久存储的 live 镜像。然而，如果你有多个并发用户，你将需要设置你的 ISCSI 服务器来管理独立的每个用户的可写覆盖。这与 “[如何构建一个网络启动服务器，部分 4](https://fedoramagazine.org/how-to-build-a-netboot-server-part-4/)” 一文所示类似。
* Live 镜像在它们的内核命令行中支持一个 `persistenthome` 选项（例如， `persistenthome=LABEL=HOME`）。与经过 CHAP 身份验证的 iSCSI 目标一起使用，对于中心控制主目录，`persistenthome` 选项为 NFS 提供一个有趣的替代方案。




---


via: <https://fedoramagazine.org/netboot-a-fedora-live-cd/>


作者：[Gregory Bartholomew](https://fedoramagazine.org/author/glb/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[robsean](https://github.com/robsean) 校对：[wxy](https://github.com/wxys)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
[Live CDs](https://en.wikipedia.org/wiki/Live_CD) are useful for many tasks such as:

- installing the operating system to a hard drive
- repairing a boot loader or performing other rescue-mode operations
- providing a consistent and minimal environment for web browsing
- …and
[much more](https://en.wikipedia.org/wiki/Live_CD#Uses).

As an alternative to using DVDs and USB drives to store your Live CD images, you can upload them to an [iSCSI](https://en.wikipedia.org/wiki/ISCSI) server where they will be less likely to get lost or damaged. This guide shows you how to load your Live CD images onto an iSCSI server and access them with the [iPXE](https://ipxe.org/) boot loader.

## Download a Live CD Image

$ MY_RLSE=27

$ MY_LIVE=$(wget -q -O - https://dl.fedoraproject.org/pub/archive/fedora/linux/releases/$MY_RLSE/Workstation/x86_64/iso | perl -ne '/(Fedora[^ ]*?-Live-[^ ]*?\.iso)(?{print $^N})/;')

$ MY_NAME=fc$MY_RLSE

$ wget -O $MY_NAME.iso https://dl.fedoraproject.org/pub/archive/fedora/linux/releases/$MY_RLSE/Workstation/x86_64/iso/$MY_LIVE

The above commands download the *Fedora-Workstation-Live-x86_64-27-1.6.iso* Fedora Live image and save it as *fc27.iso*. Change the value of *MY_RLSE* to download other archived versions. Or you can browse to [https://getfedora.org/](https://getfedora.org/) to download the latest Fedora live image. Versions prior to 21 used different naming conventions, and must be [downloaded manually here](https://dl.fedoraproject.org/pub/archive/fedora/linux/releases/https://dl.fedoraproject.org/pub/archive/fedora/linux/releases/). If you download a Live CD image manually, set the *MY_NAME* variable to the basename of the file without the extension. That way the commands in the following sections will reference the correct file.

## Convert the Live CD Image

Use the *livecd-iso-to-disk* tool to convert the ISO file to a disk image and add the *netroot* parameter to the embedded kernel command line:

$ sudo dnf install -y livecd-tools

$ MY_SIZE=$(du -ms $MY_NAME.iso | cut -f 1)

$ dd if=/dev/zero of=$MY_NAME.img bs=1MiB count=0 seek=$(($MY_SIZE+512))

$ MY_SRVR=server-01.example.edu

$ MY_RVRS=$(echo $MY_SRVR | tr '.' "\n" | tac | tr "\n" '.' | cut -b -${#MY_SRVR})

$ MY_LOOP=$(sudo losetup --show --nooverlap --find $MY_NAME.img)

$ sudo livecd-iso-to-disk --format --extra-kernel-args netroot=iscsi:$MY_SRVR:::1:iqn.$MY_RVRS:$MY_NAME $MY_NAME.iso $MY_LOOP

$ sudo losetup -d $MY_LOOP

## Upload the Live Image to your Server

Create a directory on your iSCSI server to store your live images and then upload your modified image to it.

**For releases 21 and greater:**

$ MY_FLDR=/images

$ scp $MY_NAME.img $MY_SRVR:$MY_FLDR/

**For releases prior to 21:**

$ MY_FLDR=/images

$ MY_LOOP=$(sudo losetup --show --nooverlap --find --partscan $MY_NAME.img)

$ sudo tune2fs -O ^has_journal ${MY_LOOP}p1

$ sudo e2fsck ${MY_LOOP}p1

$ sudo dd status=none if=${MY_LOOP}p1 | ssh $MY_SRVR "dd of=$MY_FLDR/$MY_NAME.img"

$ sudo losetup -d $MY_LOOP

## Define the iSCSI Target

Run the following commands on your iSCSI server:

$ sudo -i

# MY_NAME=fc27

# MY_FLDR=/images

# MY_SRVR=`hostname`

# MY_RVRS=$(echo $MY_SRVR | tr '.' "\n" | tac | tr "\n" '.' | cut -b -${#MY_SRVR})

# cat << END > /etc/tgt/conf.d/$MY_NAME.conf

<target iqn.$MY_RVRS:$MY_NAME>

backing-store $MY_FLDR/$MY_NAME.img

readonly 1

allow-in-use yes

</target>

END

# tgt-admin --update ALL

## Create a Bootable USB Drive

The [iPXE](https://ipxe.org/) boot loader has a [sanboot](http://ipxe.org/cmd/sanboot/) command you can use to connect to and start the live images hosted on your iSCSI server. It can be compiled in many different [formats](https://ipxe.org/appnote/buildtargets#boot_type). The format that works best depends on the type of hardware you’re running. As an example, the following instructions show how to [chain load](https://en.wikipedia.org/wiki/Chain_loading) iPXE from [syslinux](https://www.syslinux.org/wiki/index.php?title=SYSLINUX) on a USB drive.

First, download iPXE and build it in its *lkrn* format. This should be done as a normal user on a workstation:

$ sudo dnf install -y git

$ git clone http://git.ipxe.org/ipxe.git $HOME/ipxe

$ sudo dnf groupinstall -y "C Development Tools and Libraries"

$ cd $HOME/ipxe/src

$ make clean

$ make bin/ipxe.lkrn

$ cp bin/ipxe.lkrn /tmp

Next, prepare a USB drive with a MSDOS partition table and a FAT32 file system. The below commands assume that you have already connected the USB drive to be formatted. **Be careful that you do not format the wrong drive!**

$ sudo -i

# dnf install -y parted util-linux dosfstools

# echo; find /dev/disk/by-id ! -regex '.*-part.*' -name 'usb-*' -exec readlink -f {} \; | xargs -i bash -c "parted -s {} unit MiB print | perl -0 -ne '/^Model: ([^(]*).*\n.*?([0-9]*MiB)/i && print \"Found: {} = \$2 \$1\n\"'"; echo; read -e -i "$(find /dev/disk/by-id ! -regex '.*-part.*' -name 'usb-*' -exec readlink -f {} \; -quit)" -p "Drive to format: " MY_USB

# umount $MY_USB?

# wipefs -a $MY_USB

# parted -s $MY_USB mklabel msdos mkpart primary fat32 1MiB 100% set 1 boot on

# mkfs -t vfat -F 32 ${MY_USB}1

Finally, install syslinux on the USB drive and configure it to chain load iPXE:

# dnf install -y syslinux-nonlinux

# syslinux -i ${MY_USB}1

# dd if=/usr/share/syslinux/mbr.bin of=${MY_USB}

# MY_MNT=$(mktemp -d)

# mount ${MY_USB}1 $MY_MNT

# MY_NAME=fc27

# MY_SRVR=server-01.example.edu

# MY_RVRS=$(echo $MY_SRVR | tr '.' "\n" | tac | tr "\n" '.' | cut -b -${#MY_SRVR})

# cat << END > $MY_MNT/syslinux.cfg

ui menu.c32

default $MY_NAME

timeout 100

menu title SYSLINUX

label $MY_NAME

menu label ${MY_NAME^^}

kernel ipxe.lkrn

append dhcp && sanboot iscsi:$MY_SRVR:::1:iqn.$MY_RVRS:$MY_NAME

END

# cp /usr/share/syslinux/menu.c32 $MY_MNT

# cp /usr/share/syslinux/libutil.c32 $MY_MNT

# cp /tmp/ipxe.lkrn $MY_MNT

# umount ${MY_USB}1

You should be able to use this same USB drive to netboot additional iSCSI targets simply by editing the *syslinux.cfg* file and adding additional menu entries.

This is just one method of loading iPXE. You could install syslinux directly on your workstation. Another option is to compile iPXE as an EFI executable and place it directly in your [ESP](https://en.wikipedia.org/wiki/EFI_system_partition). Yet another is to compile iPXE as a PXE loader and place it on your TFTP server to be referenced by DHCP. The best option depends on your environment.

## Final Notes

- You may want to add the
*–filename \EFI\BOOT\grubx64.efi*parameter to the*sanboot*command if you compile iPXE in its EFI format. - It is possible to create custom live images. Refer to
[Creating and using live CD](https://docs.fedoraproject.org/en-US/quick-docs/creating-and-using-a-live-installation-image/#proc_creating-and-using-live-cd)for more information. - It is possible to add the
*–overlay-size-mb*and*–home-size-mb*parameters to the*livecd-iso-to-disk*command to create live images with persistent storage. However, if you have multiple concurrent users, you’ll need to set up your iSCSI server to manage separate per-user writeable overlays. This is similar to what was shown in the “[How to Build a Netboot Server, Part 4](https://fedoramagazine.org/how-to-build-a-netboot-server-part-4/)” article. - The live images support a
*persistenthome*option on their kernel command line (e.g.*persistenthome=LABEL=HOME*). Used together with CHAP-authenticated iSCSI targets, the*persistenthome*option provides an interesting alternative to NFS for centralized home directories.

*Photo by **Chris Yates** on **Unsplash**.*

## svsv sarma

I don’t see any advantage of the Net-boot with so much technical struggle, when a full fledged Fedora live DVD is handy. Perhaps I am a lay man or it is just for fun!

## Gregory Bartholomew

It’s not for everyone. But I hear you. Next time I’ll try to write something more useful to the typical end user.

## Leslie Satenstein

I have external backup drives. I copy the ISO’s to the external drive.

I also have internal drives a through f.

I often want to install a Linux ISO on one of the b through f drives.

What would interest me as a topic woule be a demonstration of being able, while in my installed Fedora system, to:

mount the Linux ISO,

do a chroot or some other action to the ISO

run the ISO’s boot and installation software.

In otherwords, using a “xyz.sh” script or a “xyz” application program, do the above avoid installing the ISO copy to a flashdrive and then booting that flashdrive to perform the evaluation or installation of the desired Linux ISO

I want to avoid copying the ISO to a USB flash drive and booting from that flashdrive.

## Gregory Bartholomew

Hi Leslie,

Thank you for your article suggestion! We are always looking for new ideas.

The 3 steps that you describe are essentially what happens “behind the scenes” when the following command is run:

$ qemu-system-x86_64 -machine accel=kvm -m 1024M -drive format=raw,file=,cache=none,if=virtio -cdrom .iso -boot once=d

Does that command satisfy your criteria? If so, it may be a bit too brief to do an article for. However, we might be able to make an article out of it if we provided more examples and variations of the command or made the article about QEMU and provided the above command as one example of its usage.

Alternatively, if you wanted to

replacethe currently running OS with the one from the ISO, that might be doable with the kexec command.I’m not sure how reliable it would be to run anaconda from the installation disk without running (booting) the provided kernel if that is what you are wanting to do.

I once had a situation where I wanted to keep an installation ISO on a secondary drive of a server and be able to reboot the server and do a full (re)installation of the OS on the primary drive remotely through the command line (the “command line” was routed to a separate server using a serial cable). That wasn’t too difficult. All I had to do was to copy the contents of the installation CD to a partition on the secondary drive and tweak the syslinux.cfg file by adding the line:

serial 0 115200

at the top and append “console=ttyS0,115200n8 nomodeset inst.text” to the kernel command line (I also made a few aesthetic tweaks to the syslinux.cfg file — numer of rows, helpmsgrow, etc.). I could then access (“chain load”) syslinux on the secondary drive by pressing “c” when grub came up and entering something like:

root=(hd1)

chainloader +1

boot

I’m not sure if there would be much interest in a how to for something like that, but if several people reply requesting it, I could do a detailed write up about it.

Anyway, I’ll write a “pitch” for a QEMU article if there isn’t already one in the magazine archives.

Suggestions are welcome. Thanks!

## Gregory Bartholomew

Whoops, the editor butchered by reply. I always forget that things between angle brackets need to be escaped or they will get removed when I click “post comment”. That example command line should have been:

$ qemu-system-x86_64 -machine accel=kvm -m 1024M -drive format=raw,file=<device node>,cache=none,if=virtio -cdrom <isoname>.iso -boot once=d

## Gregory Bartholomew

One caveat that I should note about using qemu to install an OS to a secondary drive is that if you subsequently try to boot your physical machine directly off of that drive, the hardware might not be detected properly because the initramfs will have been build against the “virtualized” hardware at installtion time. You should be able to work around that problem by selecting the “rescue” initramfs (it includes ALL drivers), and then running “dracut -f –regenerate-all” to rebuild the initramfs against the correct hardware.