最近因為工作需求,需要編輯ISO文件,向ISO文件裡添加配置文件
Linux 環境下編輯ISO文件, 以一個小的Linux (bmu_mini.iso ~ 50M) 為例子。
- 首先是mount iso: #mount -o loop,ro ${iso_file} ${mnt}
- 可以将${mnt}裡的内容拷貝到 ${tmp}中
- 進入${tmp}, 可以根據需求編輯裡面的内容,本次主要是向initrd.img中添加部分文件。
- 首先解壓inintrd0.img,然後添加需要的配置文件。當然用戶也可以做其他的編輯,編輯完後再壓縮并替換initrd.img
- 利用genisoimage 來rebuid iso
- umount ${mnt}及清理tmp文件等。
iso_modify.sh腳本:
#!/bin/bash
#
# Script to modify the contents of the iso image.
#
# functions
# Use can do change in initrd0.img, Here I just add target/BMU.cfg
function add_config_to_initrd()
{
echo "adding config file..."
target_dir="target"
if [ ! -d "${target_dir}" ]; then
echo "Go to create direcroty ${target_dir}"
mkdir -p ${target_dir}
fi
cp ${iso_path}/BMU.cfg ${target_dir}/BMU.cfg
}
function customize_initrd()
{
initrd=${iso_work_tmp}/isolinux/initrd0.img
if [ ! -f ${initrd} ]; then
echo "Something is wrong and ${initrd} was not found"
umount ${iso_work_mnt}
exit 1
fi
orig_size=$(ls -s ${initrd} | cut -d' ' -f 1)
tmp_initrd_dir=${iso_work}/tmp_initrd_dir_${tstamp}
mkdir -p ${tmp_initrd_dir}
pushd ${tmp_initrd_dir}
# extracting initrd0.img
echo "Extracting initrd0.img ..."
ftype=$(file -b ${initrd})
zip=0
if $(echo $ftype | grep -q "gzip"); then
$(zcat ${initrd} | cpio --quiet -idum)
zip=1
elif $(echo $ftype | grep -q "cpio"); then
$(cpio --quiet -idum -I ${initrd})
fi
echo "Adding custom config to initrd ..."
add_config_to_initrd
echo "Rebuilding initrd0.img ..."
newinitrd=${iso_work}/myinitrd-${tstamp}.img
if [ ${zip} -eq 1 ]; then
find . -print0 | cpio --null -o -H newc | gzip -9 > ${newinitrd}
else
find . -print0 | cpio --null -o -H newc -o ${newinitrd}
fi
new_size=$(ls -s ${newinitrd} | cut -d' ' -f 1)
cp ${newinitrd} ${iso_work_tmp}/isolinux/initrd0.img
popd
echo "Cleaning up initrd tmp directory ..."
rm -rf ${tmp_initrd_dir}
echo "Original initrd size : ${orig_size}k ($((${orig_size} / 1024))M)"
echo "New initrd size: ${new_size}k ($((${new_size} / 1024))M)"
}
# rebuild iso
rebuild_iso()
{
##
# Use: genisoimage, isohybrid (if applicable) to recreate ISO image
#
# genisoimage -V <label> -G <boot-code> -r <iso-filesystem> -o <new.iso>
##
# get current ISO image volume ID and reuse
volid=$(isoinfo -d -i ${iso_file} | grep 'Volume id:' | cut -d' ' -f3)
# strip ".iso" from iso image file name, if exists
image_name=${iso_file##*/}
iso_name=${iso_work}/${image_name}-mini-${tstamp}
# remove existing boot catalog file
#rm -f ${iso_work_tmp}/boot.cat
#genisoimage -V "${volid}" -J -l -r -hide-rr-moved -hide-joliet-trans-tbl -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-info-table -boot-load-size 4 -o ${iso_name}.iso ${iso_work_tmp}
genisoimage -verbose -verbose -V "${volid}" -J -l -r -hide-rr-moved -hide-joliet-trans-tbl -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-info-table -boot-load-size 4 -eltorito-alt-boot -efi-boot isolinux/efiboot.img -no-emul-boot -o ${iso_name}.iso ${iso_work_tmp}
iso_format=$(file ${iso_name}.iso | grep "ISO 9660")
[ -n "${iso_format}" ] && {
cp ${iso_name}.iso ${iso_name}-9660.iso
mv ${iso_name}.iso ${iso_name}-hd.iso
isohybrid --uefi ${iso_name}-hd.iso
}
echo "${iso_name}-hd.iso"
}
#
# (1) check iso exist or not
iso_file=$1
if [ ! ${iso_file} ]; then
echo "please input iso full path"
fi
if [ ! -f ${iso_file} ]; then
echo "iso file ${iso_file} not found"
exit 1
fi
#
# get iso file path and prepare tmp folder
iso_path=${iso_file%/*}
echo "iso Path is ${iso_path}"
iso_work=${iso_path}/work
if [ -d ${iso_work} ]; then
rm -rf ${iso_work}
fi
mkdir ${iso_work}
cd ${iso_work}
iso_work_mnt=${iso_work}/mnt
iso_work_tmp=${iso_work}/tmp
mkdir -p ${iso_work_mnt}
mkdir -p ${iso_work_tmp}
before=$(ls -s ${iso_file} | cut -d' ' -f 1)
tstamp=$(date ' %Y%m%d%H%M')
#
# (2) mount iso to /mnt and then copy data to /tmp
mount -o loop,ro ${iso_file} ${iso_work_mnt}
if [ $? -ne 0 ]; then
echo "mount iso image ${iso_file} failed"
fi
echo "copying iso image to tmp area ..."
cp -rf ${iso_work_mnt}/* ${iso_work_tmp}
ls ${iso_work_tmp}
#
# (3) Then user can customize initrd
echo "customizing initrd ..."
customize_initrd
#
# (4) Rebuild iso
echo "rebuilding iso ..."
new_iso_file=$(rebuild_iso)
after=$(ls -s ${new_iso_file} | cut -d' ' -f 1)
echo "Original ISO file size: ${before}K ($((before / 1024))M)"
echo "Shrunk ISO file size:${after}K ($((after / 1024))M)"
echo "Copying new iso image to working directory ..."
cp ${new_iso_file} ${iso_file}
#
# (5) umount iso and clean tmp folder
umount ${iso_work_mnt}
if [ $? -ne 0 ]; then
echo "Unable to unmount iso image"
fi
rmdir ${iso_work_mnt}
rm -rf ${iso_work_tmp}
exit 0
bmu_mini.iso文件是基于CentOS7并裁剪,隻包含了EFI,isolinux, LiveOS, [BOOT] 等文件夾。
運行及輸出結果:
結果比較:
,