之前需要在旧PC上快速部署VDI客户端,因此采用ISO打包Linux系统环境的方案实现,优点是部署容易、重启即可还原抗病毒能力强。为了避免遗忘,本文记录一下关键操作以备查阅。

提取ISO文件

本文基于Archlinux,直接下载一个最新的安装镜像,本文以2019.02.01版为例:

$ wget 'https://mirrors.ustc.edu.cn/archlinux/iso/2019.02.01/archlinux-2019.02.01-x86_64.iso' -O ~/arch.iso

创建两个目录

$ mkdir ~/archiso ~/customiso

挂载镜像

# mount -t iso9660 -o loop ~/arch.iso ~/archiso

把文件复制出来

$ cp -a ~/archiso ~/customiso

修改系统

进入x86_64系统目录:

$ cd ~/customiso/arch/x86_64

解包根文件系统:

$ unsquashfs airootfs.sfs

进入镜像系统上下文环境:

# arch-chroot squashfs-root /bin/shell

初始化系统环境

(chroot) # pacman-key --init
(chroot) # pacman-key --populate archlinux
(chroot) # pacman -Syu --force archiso linux

安装需要的软件,并写好配置文件

...

修改/etc/mkinitcpio.conf

(chroot) # nano /etc/mkinitcpio.conf

修改HOOKS="...开头的行为:

HOOKS="base udev memdisk archiso_shutdown archiso archiso_loop_mnt archiso_pxe_common archiso_pxe_nbd archiso_pxe_http archiso_pxe_nfs archiso_kms block pcmcia filesystems keyboard"

更新initramfs:

(chroot) # mkinitcpio -p linux

创建一份安装的软件包列表,退出chroot环境

(chroot) # LANG=C pacman -Sl | awk '/\[installed\]$/ {print $1 "/" $2 "-" $3}' > /pkglist.txt
(chroot) # pacman -Scc
(chroot) # exit

移动软件包列表和 kernet、initramfs 文件:

$ mv squashfs-root/pkglist.txt ~/customiso/arch/pkglist.x86_64.txt
$ mv squashfs-root/boot/vmlinuz-linux ~/customiso/arch/boot/x86_64/vmlinuz
$ mv squashfs-root/boot/initramfs-linux.img ~/customiso/arch/boot/x86_64/archiso.img
$ rm squashfs-root/boot/initramfs-linux-fallback.img

重新生成airootfs.sfs文件

$ rm airootfs.sfs
$ mksquashfs squashfs-root airootfs.sfs
$ md5sum airootfs.sfs > airootfs.md5

清理环境

# rm -r squashfs-root

修改EFI镜像

$ mkdir mnt
# mount -t vfat -o loop ~/customiso/EFI/archiso/efiboot.img mnt
# cp ~/customiso/arch/boot/x86_64/vmlinuz mnt/EFI/archiso/vmlinuz.efi
# cp ~/customiso/arch/boot/x86_64/archiso.img mnt/EFI/archiso/archiso.img
$ dd if=/dev/zero bs=1M count=50 of=efiboot-new.img
$ mkfs.fat -n "ARCHISO_EFI" efiboot-new.img
$ mkdir new
# mount -t fat -o loop efiboot-new.img new
$ cp -r mnt/* new/
# umount new mnt
$ mv efiboot-new.img ~/customiso/EFI/archiso/efiboot.img

创建新的ISO

$ iso_label="ARCH_201209"
$ xorriso -as mkisofs \
       -iso-level 3 \  
       -full-iso9660-filenames \
       -volid "${iso_label}" \
       -eltorito-boot isolinux/isolinux.bin \
       -eltorito-catalog isolinux/boot.cat \
       -no-emul-boot -boot-load-size 4 -boot-info-table \
       -isohybrid-mbr ~/customiso/isolinux/isohdpfx.bin \
       -output ~/arch-custom.iso \ 
       ~/customiso

参考文档

Archlinux wiki: Remastering_the_Install_ISO