rootfs: support adding optional kernel modules

Caller of rootfs.sh can define `KERNEL_MODULES_DIR` as a kernel
module directory and then the directory will be copied to
`/lib/modules/` of the created rootfs. This allows additional
kernel modules to be put into rootfs image and initrd image.

Signed-off-by: Peng Tao <bergwolf@gmail.com>
This commit is contained in:
Peng Tao 2018-02-01 16:47:21 +08:00
parent 7245b21206
commit 9680f08ebf
2 changed files with 33 additions and 0 deletions

View File

@ -1,5 +1,6 @@
* [Supported base OSs](#supported-base-oss)
* [Creating a rootfs](#creating-a-rootfs)
* [Creating a rootfs with kernel modules](#creating-a-rootfs-with-kenrel-modules)
* [Build a rootfs using Docker*](#build-a-rootfs-using-docker*)
* [Adding support for a new guest OS](#adding-support-for-a-new-guest-os)
* [Create template files](#create-template-files)
@ -48,6 +49,15 @@ To build a rootfs for your chosen distribution, run:
$ sudo ./rootfs.sh <distro>
```
## Creating a rootfs with kernel modules
To build a rootfs with additional kernel modules, run:
```
$ sudo KERNEL_MODULES_DIR=${kernel_mod_dir} ./rootfs.sh <distro>
```
Where `kernel_mod_dir` points to the kernel modules directory to be put under
`/lib/modules/` directory of the created rootfs.
## Build a rootfs using Docker*
Depending on the base OS to build the rootfs guest OS, it is required some

View File

@ -13,6 +13,7 @@ AGENT_VERSION=${AGENT_VERSION:-master}
GO_AGENT_PKG=${GO_AGENT_PKG:-github.com/kata-containers/agent}
AGENT_BIN=${AGENT_BIN:-kata-agent}
AGENT_INIT=${AGENT_INIT:-no}
KERNEL_MODULES_DIR=${KERNEL_MODULES_DIR:-""}
#Load default vesions for golang and other componets
source "${script_dir}/versions.txt"
@ -50,6 +51,8 @@ USE_DOCKER: If set will build rootfs in a Docker Container (requries docker)
DEFAULT: not set
AGENT_INIT : Use $(AGENT_BIN) as init process.
DEFAULT: no
KERNEL_MODULES_DIR: Optional kernel modules to put into the rootfs.
DEFAULT: ""
EOT
exit "${error}"
}
@ -115,6 +118,17 @@ setup_agent_init() {
OK "Agent is installed as init process"
}
copy_kernel_modules() {
local module_dir=$1
local rootfs_dir=$2
[ -z "module_dir" -o -z "rootfs_dir" ] && die "module dir and rootfs dir must be specified"
info "Copy kernel modules from ${KERNEL_MODULES_DIR}"
mkdir -p ${rootfs_dir}/lib/modules/
cp -a ${KERNEL_MODULES_DIR} ${rootfs_dir}/lib/modules/
OK "Kernel modules copied"
}
while getopts c:hr: opt
do
@ -131,6 +145,8 @@ shift $(($OPTIND - 1))
[ "$AGENT_INIT" == "yes" -o "$AGENT_INIT" == "no" ] || die "AGENT_INIT($AGENT_INIT) is invalid (must be yes or no)"
[ -n "${KERNEL_MODULES_DIR}" ] && [ ! -d "${KERNEL_MODULES_DIR}" ] && die "KERNEL_MODULES_DIR defined but is not an existing directory"
distro="$1"
init="${ROOTFS_DIR}/sbin/init"
@ -155,6 +171,9 @@ if [ -n "${USE_DOCKER}" ] ; then
--build-arg https_proxy="${https_proxy}" \
-t "${image_name}" "${distro_config_dir}"
# fake mapping if KERNEL_MODULES_DIR is unset
kernel_mod_dir=${KERNEL_MODULES_DIR:-${ROOTFS_DIR}}
#Make sure we use a compatible runtime to build rootfs
# In case Clear Containers Runtime is installed we dont want to hit issue:
#https://github.com/clearcontainers/runtime/issues/828
@ -168,8 +187,10 @@ if [ -n "${USE_DOCKER}" ] ; then
--env AGENT_BIN="${AGENT_BIN}" \
--env AGENT_INIT="${AGENT_INIT}" \
--env GOPATH="${GOPATH}" \
--env KERNEL_MODULES_DIR="${KERNEL_MODULES_DIR}" \
-v "${script_dir}":"/osbuilder" \
-v "${ROOTFS_DIR}":"/rootfs" \
-v "${kernel_mod_dir}":"${kernel_mod_dir}" \
-v "${GOPATH}":"${GOPATH}" \
${image_name} \
bash /osbuilder/rootfs.sh "${distro}"
@ -180,6 +201,8 @@ fi
mkdir -p ${ROOTFS_DIR}
build_rootfs ${ROOTFS_DIR}
[ -n "${KERNEL_MODULES_DIR}" ] && copy_kernel_modules ${KERNEL_MODULES_DIR} ${ROOTFS_DIR}
info "Pull Agent source code"
go get -d "${GO_AGENT_PKG}" || true
OK "Pull Agent source code"