On Tue, Oct 13, 2020 at 10:19:44AM +0800, Wang Chenglong wrote:
Use the tool to create a new centos-$version initramfs image with one command. and we also can customize the pre-installed software by configuring ./bin/packages-to-install.
container/osimage/centos/README.md | 15 +++++++ container/osimage/centos/bin/create-image | 36 ++++++++++++++++ container/osimage/centos/bin/lib | 41 +++++++++++++++++++ .../osimage/centos/bin/packages-to-install | 2 + .../osimage/centos/bin/packages-to-remove | 8 ++++
/bin/ may be improved -- packages-* are not bin, so does lib. Perhaps we can just remove this extra level of path.
container/osimage/centos/run | 26 ++++++++++++ 6 files changed, 128 insertions(+) create mode 100644 container/osimage/centos/README.md create mode 100755 container/osimage/centos/bin/create-image create mode 100755 container/osimage/centos/bin/lib create mode 100644 container/osimage/centos/bin/packages-to-install create mode 100644 container/osimage/centos/bin/packages-to-remove create mode 100755 container/osimage/centos/run
diff --git a/container/osimage/centos/README.md b/container/osimage/centos/README.md new file mode 100644 index 0000000..000ca05 --- /dev/null +++ b/container/osimage/centos/README.md @@ -0,0 +1,15 @@ +# Use the tool to create a new centos-${version} initramfs image.
+Usage:
- cd ${CCI_SRC}/rootfs/initramfs/centos/aarch64/${version}/
- ./build
+Some configuration items: +./bin/packages-to-install
- If you want to pre-install the software, you can write the package names in ./bin/packages-to-install.
+./bin/packages-to-remove
- If you want remove some unnecessary files, you can write the names in ./bin/packages-to-remove.
+$HOME/.config/compass-ci/rootfs.passwd
- Set the password for the image into this file.
rootfs.passwd => rootfs-passwd .passwd is not valid file suffix.
diff --git a/container/osimage/centos/bin/create-image b/container/osimage/centos/bin/create-image new file mode 100755 index 0000000..39aba7a --- /dev/null +++ b/container/osimage/centos/bin/create-image @@ -0,0 +1,36 @@ +#!/bin/bash +# SPDX-License-Identifier: MulanPSL-2.0+ +# Copyright (c) 2020 Huawei Technologies Co., Ltd. All rights reserved.
+# Configure rootfs +make_rootfs() +{
- yum repolist
- yum -y install --skip-broken $(</root/bin/packages-to-install)
- yum clean all
- rm -rf $(</root/bin/packages-to-remove)
Do not rm. Filter out with grep in pack_cgz() instead.
- ln -fs /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
Please split this function to install and setup functions.
+}
+# Config password +pre_config_rootfs() {
pre_config_rootfs => setup_login
- [ -n "$ROOT_NEW_PASSWD" ] && {
|| return
to reduce indent below.
echo "Changing root password"
passwd_md5=$(openssl passwd -1 "$ROOT_NEW_PASSWD")
sed -i -r "s/^root:[^:]*:(.*)/root:${passwd_md5//\//\\/}:\1/" "$ROOTFS_DIR/etc/shadow" # Change the password in shadow
blank line here
remove useless comments
sed -i 's/[# ]PermitRootLogin.*/PermitRootLogin yes/' "$ROOTFS_DIR/etc/ssh/sshd_config" # Configure ssh service
- }
+}
+# Pack cgz package +pack_cgz() +{
- echo "Packing package. Please wait."
- cd /
- find ./ ! -path "./${image_name}" | cpio -o -Hnewc | gzip -9 > /${image_name}
Remove ./ ?
A bit safer to save image_name to /tmp/, and exclude /tmp/ from cpio.
- chmod 644 /${image_name}
+}
+make_rootfs +pre_config_rootfs +pack_cgz diff --git a/container/osimage/centos/bin/lib b/container/osimage/centos/bin/lib new file mode 100755 index 0000000..955717d --- /dev/null +++ b/container/osimage/centos/bin/lib @@ -0,0 +1,41 @@ +#!/bin/bash +# SPDX-License-Identifier: MulanPSL-2.0+ +# Copyright (c) 2020 Huawei Technologies Co., Ltd. All rights reserved.
+DIR="$(pwd)" +os_name="$(echo $DIR |awk -F "/" '{print $(NF - 2)}')" +os_version="$(echo $DIR |awk -F "/" '{print $NF}')" +image="${os_name}:${os_version}" +image_name="${os_name}-${os_version}-$(date +"%Y%m%d").cgz"
+# Check password file +check_passwd_file() {
- root_pwd_file="$HOME/.config/compass-ci/rootfs.passwd"
local
export ROOT_NEW_PASSWD=
[ -f "$root_pwd_file" ] || {
echo "[INFO] Please set the password file."
echo "$HOME/.config/compass-ci/rootfs.passwd"
exit 1
}
export ROOT_NEW_PASSWD=$(cat "$root_pwd_file")
+}
+# Pull docker image +pull_docker_image() +{
- docker pull $image
- if [ $? = 0 ]; then
echo "finish downloading image"
- else
echo "[INFO] Run command in false path ."
exit
- fi
+}
+# cp image package to host +cp_image() +{
- docker cp -a init_docker:/${image_name} $HOME/
- echo "result: $(ls $HOME/${image_name})"
+} diff --git a/container/osimage/centos/bin/packages-to-install b/container/osimage/centos/bin/packages-to-install new file mode 100644 index 0000000..5d31c40 --- /dev/null +++ b/container/osimage/centos/bin/packages-to-install @@ -0,0 +1,2 @@ +openssh-server +openssl diff --git a/container/osimage/centos/bin/packages-to-remove b/container/osimage/centos/bin/packages-to-remove new file mode 100644 index 0000000..ce079b9 --- /dev/null +++ b/container/osimage/centos/bin/packages-to-remove @@ -0,0 +1,8 @@ +/.dockerenv +/lib/modules +/usr/share/doc +/usr/share/man +/usr/share/info +/usr/share/i18n +/usr/share/locale +/usr/share/terminfo diff --git a/container/osimage/centos/run b/container/osimage/centos/run new file mode 100755 index 0000000..6710872 --- /dev/null +++ b/container/osimage/centos/run @@ -0,0 +1,26 @@ +#!/bin/bash +# SPDX-License-Identifier: MulanPSL-2.0+ +# Copyright (c) 2020 Huawei Technologies Co., Ltd. All rights reserved.
+. ${CCI_SRC}/container/osimage/centos/bin/lib +. ${CCI_SRC}/container/defconfig.sh
+check_passwd_file "$root_pwd_file"
Remove $root_pwd_file parameter -- it's defined inside check_passwd_file()!
+DIR=$(dirname $(realpath $0)) +cmd=(
- docker run
- --name init_docker
- -v $DIR/bin/:/root/bin
- -e ROOT_NEW_PASSWD=$ROOT_NEW_PASSWD
- -e image_name=$image_name
- $image
- /root/bin/create-image
+)
+pull_docker_image +${cmd[@]} +cp_image +docker_rm init_docker &> /dev/null +echo "build finished"
-- 2.23.0