USB 隨身碟具備攜帶方便、讀寫快速、容量大等優勢,現代主板 BIOS 普遍支援 USB-HDD 開機模式。製作專屬 Linux 開機隨身碟,可構建輕量級系統維護環境:攜帶精簡核心、網路工具、檔案系統修復程式,快速診斷硬體故障、救援資料、部署網路服務。本文以 Red Hat 為例,詳細記錄從核心定制、initrd 製作、GRUB 安裝到檔案系統佈建的完整流程。
進入核心源碼目錄執行 make menuconfig,關鍵選項:
mkdir -p /mnt/initrd
cd /tmp
mkinitrd /tmp/initrd.gz 2.4.20-usb
gunzip initrd.gz
mount -o loop /tmp/initrd /mnt/initrd
cp /sbin/busybox /mnt/initrd/bin
cd /mnt/initrd/bin
ln -s busybox sleep
vi /mnt/initrd/linuxrc # 加入: echo 'wait 3 seconds...' ; /bin/sleep 3
mkdir -p /mnt/initrdusb
dd if=/dev/zero of=/tmp/initrdusb bs=1M count=1
mke2fs -m 0 /tmp/initrdusb
mount -o loop /tmp/initrdusb /mnt/initrdusb
cp -a /mnt/initrd/* /mnt/initrdusb
umount /mnt/initrd /mnt/initrdusb
gzip -9 /tmp/initrdusb
cp /tmp/initrdusb.gz /boot/initrd-2.4.20-usb.img
# 假設 USB 為 /dev/sda
fdisk /dev/sda
# 分區 1: FAT16 (可選,供 Windows 98 開機)
# 分區 2: Linux ext2 (根檔案系統)
注意:若需相容 Windows 98 開機,磁碟幾何參數需設為 63 sectors/track (fdisk 專家模式 x 修改 heads/sectors/cylinders)。
mke2fs -m 0 /dev/sda2
mkdir -p /mnt/sda2 && mount /dev/sda2 /mnt/sda2
cd /mnt/sda2
mkdir -p boot/grub lib/modules dev etc/rc.d proc mnt tmp var bin sbin
# 複製核心與 initrd
cp /boot/vmlinuz-2.4.20-usb /boot/initrd-2.4.20-usb.img boot/
# 複製核心模組
cp -r /lib/modules/2.4.20-usb lib/modules/
# 複製 GRUB 階段檔
cp /boot/grub/stage1 /boot/grub/stage2 boot/grub/
timeout 10
color 0x17 0x70
default 0
title Windows 98
rootnoverify (hd0,0)
makeactive
chainloader +1
title GNU/Linux RedHat 8.0 (2.4.20-usb)
root (hd0,1)
kernel /boot/vmlinuz-2.4.20-usb ro root=/dev/sda2 initrd /boot/initrd-2.4.20-usb.img
grub
grub> root (hd1,1) # hd1 對應 USB,分區 1 為 Linux 根分區
grub> setup (hd1) # 安裝至 USB MBR
grub> quit
# 修改 Makefile: DOSTATIC=true
# 修改 init.c: #define INIT_SCRIPT "/etc/rc.d/rc.sysinit"
make && make install
cp -a ./_install /tmp/floppy-Linux
cd /tmp/floppy-Linux/dev
mknod tty c 5 0; mknod console c 5 1; mknod tty0 c 4 0
mknod ram0 b 1 0; mknod fd0 b 2 0; mknod null c 1 3
chmod 666 tty console tty0 null; chmod 600 ram0 fd0
# etc/rc.d/rc.sysinit
#!/bin/sh
mount -n -t proc none /proc
mount -n -o remount,rw /
ifconfig lo 127.0.0.1
# etc/fstab
/dev/sda2 / ext2 defaults 1 1
none /proc proc defaults 0 0
ifconfig、route、ping、nc (BusyBox 內建或靜態編譯)fsck.ext2、mkdosfs、mount、tar、gzipe3 (極小 vi 相容) 或重編 BusyBox 含 vildd 檢查動態連結程式依賴,按原路徑複製至 lib/正常主機 A (192.168.100.5) 執行:tar cf - win98 | nc -l -p 5555
USB 開機主機 B 操作:
modprobe eepro100 # 載入網卡驅動
ifconfig eth0 192.168.1.10
route add default gw 192.168.1.1
mount -t vfat -o codepage=936,iocharset=cp936 /dev/hda5 /mnt/2dos
nc 192.168.100.5 5555 | tar xf - -C /mnt/2dos
若 format /s 無法建立 USB Windows 98 開機區:
mkdosfs -F 16 /dev/sda1 建立 FAT16xxd -c 16 /dev/sda1 | less 記錄 Boot Record 第 32-35 位元組 (FAT 總扇區數)bvi 將 Boot Record 第 32-35 位元組改回原值最終實現:USB 同時支援 Windows 98 兩種開機模式與 Linux 開機。