Kata initrd的生成过程

可参考kata_integration中make initrd流程,大致分为以下几步:

  1. 创建rootfs相关目录
  2. 将kata-agent重命名为init并放在rootfs根目录下
  3. 从编译环境中拷贝kata-agent所需二进制(如mount、umount)到rootfs usr/bin目录下
  4. 从编译环境中拷贝以上二进制(包含kata-agent)所需动态链接库到rootfs lib目录下
  5. 从编译环境中拷贝内核驱动模块ko文件到rootfs中内核驱动模块目录下
  6. 进入rootfs并执行以下命令生成initrd.img
  1. ( find . | cpio -H newc -o | gzip -9 ) > ./kata-containers-initrd.img

initrd定制参考脚本

  1. script_dir="$(dirname $(readlink -f $0))"
  2. rpmlist=${script_dir}/make-initrd-rpm.list
  3. BUILD_PATH="./build"
  4. IMAGE_NAME=${IMAGE_NAME:-kata-containers-initrd.img}
  5. AGENT_INIT=${AGENT_INIT:-yes}
  6. ROOTFS_DIR=${ROOTFS_DIR:-/tmp/kataAgent-rootfs}
  7. # create a temp dir to store rootfs
  8. rm -rf ${ROOTFS_DIR}
  9. mkdir -p ${ROOTFS_DIR}/lib \
  10. ${ROOTFS_DIR}/lib64 \
  11. ${ROOTFS_DIR}/lib/modules
  12. mkdir -m 0755 -p ${ROOTFS_DIR}/dev \
  13. ${ROOTFS_DIR}/sys \
  14. ${ROOTFS_DIR}/sbin \
  15. ${ROOTFS_DIR}/bin \
  16. ${ROOTFS_DIR}/tmp \
  17. ${ROOTFS_DIR}/proc
  18. if [ ! -f "${BUILD_PATH}/kata-agent" ];then
  19. echo "kata-agent doesn't exist!"
  20. exit 1
  21. fi
  22. # kata-agent
  23. cp ${BUILD_PATH}/kata-agent ${ROOTFS_DIR}/init
  24. # busybox
  25. cp /sbin/busybox ${ROOTFS_DIR}/sbin/
  26. # cp run request files in initrd
  27. cat $rpmlist | while read rpm
  28. do
  29. if [ "${rpm:0:1}" != "#" ]; then
  30. rpm -ql $rpm > /dev/null 2>&1
  31. if [ $? -ne 0 ]; then
  32. continue
  33. fi
  34. array=($(rpm -ql $rpm| grep -v "share" | grep -v ".build-id"))
  35. for file in ${array[@]};
  36. do
  37. source=$file
  38. dts_file=${ROOTFS_DIR}$file
  39. dts_folder=${dts_file%/*}
  40. if [ ! -d "$dts_folder" ];then
  41. mkdir -p $dts_folder
  42. fi
  43. cp -r -f -d $source $dts_folder
  44. done
  45. fi
  46. done
  47. #create symlinks to busybox
  48. BUSYBOX_BINARIES=(/bin/sh /bin/mount /bin/umount /bin/ls /bin/ps /bin/file /bin/ldd /bin/tar /bin/hwclock /sbin/modprobe /sbin/depmod /bin/ip /bin/modinfo /bin/insmod /bin/rmmod)
  49. for bin in ${BUSYBOX_BINARIES[@]}
  50. do
  51. mkdir -p ${ROOTFS_DIR}/`dirname ${bin}`
  52. ln -sf /sbin/busybox ${ROOTFS_DIR}/${bin}
  53. done
  54. LDD_BINARIES=(/init /sbin/busybox)
  55. for bin in ${LDD_BINARIES[@]}
  56. do
  57. ldd ${ROOTFS_DIR}${bin} | while read line
  58. do
  59. arr=(${line// / })
  60. for lib in ${arr[@]}
  61. do
  62. echo $lib
  63. if [ "${lib:0:1}" = "/" ]; then
  64. dir=${ROOTFS_DIR}`dirname $lib`
  65. mkdir -p "${dir}"
  66. cp -f $lib $dir
  67. fi
  68. done
  69. done
  70. done
  71. (cd ${ROOTFS_DIR} && find . | cpio -H newc -o | gzip -9 ) > ${BUILD_PATH}/${IMAGE_NAME}