湖濱散記部落格的樹心幽徑[login][主頁]
476:20190124為Fedora 28 server 的用戶建立 Boot Loader並用 QEMU測試遠端以fedora28開機成功

REF:https://fedoramagazine.org/how-to-build-a-netboot-server-part-1/

(1)安裝git 分散式版本控制軟體 (林納斯·托瓦茲為管理Linux內核開發而設計的)

$ sudo dnf install -y git


(2)用git來下載iPXE (一種開放源碼的the Preboot eXecution Environment (PXE) client firmware and bootloader)

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

 

(3)建立iPXE bootloader 的啟動批次命令集(startup script)

$ cat << 'END' > $HOME/ipxe/init.ipxe
> #!ipxe
>
> prompt --key 0x02 --timeout 2000 Press Ctrl-B for the iPXE command line... && shell ||
>
> dhcp || exit
> set prefix file:///linux
> chain ${prefix}/boot.cfg || exit
> END

(4) $ cat $HOME/ipxe/init.ipxe
#!ipxe

prompt --key 0x02 --timeout 2000 Press Ctrl-B for the iPXE command line... && shell ||

dhcp || exit
set prefix file:///linux
chain ${prefix}/boot.cfg || exit

 

(5-1)啟用file下載協定

$ echo '#define DOWNLOAD_PROTO_FILE' > $HOME/ipxe/src/config/local/general.h


(5-2)安裝c語言工具、程式庫。

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

(6)編譯產生boot loader

$ cd $HOME/ipxe/src

$ make clean

$ make bin-x86_64-efi/ipxe.efi EMBED=../init.ipxe

 

(7)記住boot loader的位置

$ IPXE_FILE="$HOME/ipxe/src/bin-x86_64-efi/ipxe.efi"

 

(8)建立EFI系統分割所需的目錄樹及檔案

mkdir -p $HOME/esp/efi/boot

mkdir $HOME/esp/linux

cp $IPXE_FILE $HOME/esp/efi/boot/bootx64.efi

$  DEFAULT_VER=$(ls -c /fc28/lib/modules | head -n 1)

cat << END > $HOME/esp/linux/boot.cfg
> #!ipxe
>
> kernel --name kernel.efi \${prefix}/vmlinuz-$DEFAULT_VER
> initrd=initrd.img ro ip=dhcp rd.peerdns=0 nameserver=192.168.1.1
> nameserver=163.25.20.1 root=nfs4:sice.home.idv.tw:/fc28  console=tty0 console=ttyS0,115200n8 audit=0 selinux=0 quiet
> initrd --name initrd.img \${prefix}/initramfs-$DEFAULT_VER.img
> boot || exit
> END

$ cat  $HOME/esp/linux/boot.cfg
#!ipxe

kernel --name kernel.efi ${prefix}/vmlinuz-4.19.16-200.fc28.x86_64
initrd=initrd.img ro ip=dhcp rd.peerdns=0 nameserver=192.168.1.1
nameserver=163.25.20.1 root=nfs4:sice.home.idv.tw:/fc28  console=tty0 console=ttyS0,115200n8 audit=0 selinux=0 quiet
initrd --name initrd.img ${prefix}/initramfs-4.19.16-200.fc28.x86_64.img
boot || exit

(9-1)將核心(vmlinuz-4.19.16-200.fc28.x86_64)拷到$HOME/esp/linux中。

$ cp $(find /fc28/lib/modules -maxdepth 2 -name 'vmlinuz' | grep -m 1 $DEFAULT_VER) $HOME/esp/linux/vmlinuz-$DEFAULT_VER

(9-2)將初始記憶體檔案系統(initramfs-4.19.16-200.fc28.x86_64.img)拷到$HOME/esp/linux中。

$ sudo cp $(find /fc28/boot -name 'init*' | grep -m 1 $DEFAULT_VER) $HOME/esp/linux/initramfs-$DEFAULT_VER.img

$ tree ~/esp
/home/treehrt/esp
├── efi
│   └── boot
│       └── bootx64.efi
└── linux
    ├── boot.cfg
    ├── initramfs-4.19.16-200.fc28.x86_64.img
    └── vmlinuz-4.19.16-200.fc28.x86_64

3 directories, 4 files

 

(10)安裝磁碟分割工具 parted 及dosfstools

$ sudo dnf install -y parted dosfstools


(11)建立uefi.img檔並拷入esp目錄

$ ESP_SIZE=$(du -ks $HOME/esp | cut -f 1)

$ echo $ESP_SIZE
55964

$ dd if=/dev/zero of=$HOME/uefi.img count=$((${ESP_SIZE}+5000)) bs=1KiB
60964+0 records in
60964+0 records out
62427136 bytes (62 MB, 60 MiB) copied, 0.0828752 s, 753 MB/s

$ UEFI_DEV=$(sudo losetup --show -f $HOME/uefi.img)

$ echo $UEFI_DEV
/dev/loop0

$ sudo parted ${UEFI_DEV} -s mklabel gpt mkpart EFI FAT16 1MiB 100% toggle 1 boot

$ sudo mkfs -t msdos ${UEFI_DEV}p1

mkfs.fat 4.1 (2017-01-24)

$ mkdir -p $HOME/mnt

$ sudo mount ${UEFI_DEV}p1 $HOME/mnt

$ sudo cp -r $HOME/esp/* $HOME/mnt

$ tree  $HOME/mnt

/home/treehrt/mnt
├── efi
│   └── boot
│       └── bootx64.efi
└── linux
    ├── boot.cfg
    ├── initramfs-4.19.16-200.fc28.x86_64.img
    └── vmlinuz-4.19.16-200.fc28.x86_64

3 directories, 4 files

$ df
檔案系統                               1K-區段    已用    可用 已用% 掛載點
:

/dev/loop0p1                             59784   55962    3822   94% /home/treehrt/mnt

$ sudo umount $HOME/mnt

$ sudo losetup -d ${UEFI_DEV}

(12) 安裝qemu-system-x86

sudo dnf install -y qemu-system-x86

註:QEMU is a generic and open source machine emulator and virtualizer。

 

 

(13)允許qemu存取bridge br0

sudo su -

# echo 'allow br0' > /etc/qemu/bridge.conf

# exit

(14)拷入qemu-system-x86虛擬機器所需的bios設定

$ cp /usr/share/edk2/ovmf/OVMF_VARS.fd $HOME

(15)執行qemu-system-x86虛擬機器失敗, 進入UEFI Interactive Shell

qemu-system-x86_64 -machine accel=kvm -nographic -m 1024 -drive if=pflash,format=raw,unit=0,file=/usr/share/edk2/ovmf/OVMF_CODE.fd,readonly=on -drive if=pflash,format=raw,unit=1,file=$HOME/OVMF_VARS.fd -drive if=ide,format=raw,file=$HOME/uefi.img -net bridge,br=br0 -net nic,model=virtio

:

UEFI Interactive Shell v2.2
EDK II
UEFI v2.70 (EDK II, 0x00010000)
Mapping table
:

 Press ESC in 1 seconds to skip startup.nsh or any other key to continue.
Shell>

(16) 將BIOS的Secure Boot選項 Disable並進階CPU選項:Intel(R) Virtualization Technology要enable

(17)重新執行qemu-system-x86虛擬機器失敗(mount.nfs4: Connection timed out)

qemu-system-x86_64 -machine accel=kvm -nographic -m 1024 -drive if=pflash,format=raw,unit=0,file=/usr/share/edk2/ovmf/OVMF_CODE.fd,readonly=on -drive if=pflash,format=raw,unit=1,file=$HOME/OVMF_VARS.fd -drive if=ide,format=raw,file=$HOME/uefi.img -net bridge,br=br0 -net nic,model=virtio

出現訊息如下

iPXE initialising devices...ok
iPXE 1.0.0+ (de45) -- Open Source Network Boot Firmware -- http://ipxe.org
Features: DNS HTTP iSCSI TFTP SRP AoE EFI Menu
Configuring (net0 52:54:00:12:34:56)...... ok
file:///linux/boot.cfg... ok
file:///linux/vmlinuz-4.19.16-200.fc28.x86_64... ok
file:///linux/initramfs-4.19.16-200.fc28.x86_64.img... ok
[    3.556745] dracut-initqueue[301]: RTNETLINK answers: File exists
[  267.323393] dracut-initqueue[301]: mount.nfs4: Connection timed out          [  394.250823] dracut-initqueue[301]: Warning: dracut-initqueue timeout - starting timeout scripts
[  394.784444] dracut-initqueue[301]: Warning: dracut-initqueue timeout - starting timeout scripts

 

(18) 修改nfs server的指定方式:不用DNS name, 改用ip:192.168.1.103:

$ vi  $HOME/esp/linux/boot.cfg
#!ipxe

kernel --name kernel.efi ${prefix}/vmlinuz-4.19.16-200.fc28.x86_64
initrd=initrd.img ro ip=dhcp rd.peerdns=0 nameserver=192.168.1.1
nameserver=163.25.20.1 root=nfs4:192.168.1.103:/fc28  console=tty0 console=ttyS0,115200n8 audit=0 selinux=0 quiet
initrd --name initrd.img ${prefix}/initramfs-4.19.16-200.fc28.x86_64.img
boot || exit

(19)執行qemu-system-x86虛擬機器,終於成功

qemu-system-x86_64 -machine accel=kvm -nographic -m 1024 -drive if=pflash,format=raw,unit=0,file=/usr/share/edk2/ovmf/OVMF_CODE.fd,readonly=on -drive if=pflash,format=raw,unit=1,file=$HOME/OVMF_VARS.fd -drive if=ide,format=raw,file=$HOME/uefi.img -net bridge,br=br0 -net nic,model=virtio

(20)成功畫面如下: 

alt

 

colord usage: https://www.freedesktop.org/software/colord/using.html


select id,article_id,topic,text from lt_articles_text where article_id =476; ok. update lt_articles set num_reads=num_reads +1 where id=476; ok.