diff --git a/scripts/opkg_offline.sh b/scripts/opkg_offline.sh new file mode 100755 index 0000000000000000000000000000000000000000..b05c25165a5ad889cd6aeb1f86d246acc799020c --- /dev/null +++ b/scripts/opkg_offline.sh @@ -0,0 +1,142 @@ +#!/bin/sh + +# Helper utility to configure iopsys specific template for LXC container images. +# +# Copyright © 2021 IOPSYS Software Solutions AB +# Author: Vivek Dutta <vivek.dutta@iopsys.eu> +# + +OPKG_LIB_PATH="/usr/lib/opkg" + +function log() { + echo "${@}"|logger -t swmodd.template -p debug +} + +function check_installed() +{ + local root=${1} + local pkg=${2} + + opkg --offline-root ${root} list ${pkg} |grep -q "${pkg} " + if [ "$?" -eq 0 ]; then + # Installed + return 0; + fi + + return 1; +} + +function check_dependency() +{ + local root=${1} + local pkg=${2} + local depends="" + local list="" + + if [ -z ${pkg} ]; then + return 0; + fi + + list=$(opkg depends ${pkg} |tail -n +2) + + for f in ${list}; do + check_installed ${root} ${f} + if [ "$?" -eq 1 ]; then + echo ${depends} |grep -q ${f} + if [ "$?" -ne 0 ]; then + depends="${depends} ${f}" + fi + fi + done + + #log "PKG[${pkg}] has dependency[${depends}]" + echo ${depends} +} + +function copy_single_opkg() +{ + local root=${1} + local pkg=${2} + + if [ -z ${pkg} ]; then + return 0; + fi + + check_installed / ${pkg} + if [ "$?" -eq 1 ]; then + log "Package [${pkg}] not available locally"; + return 0; + fi + + check_installed ${root} ${pkg} + if [ "$?" -eq 0 ]; then + log "Package [${pkg}] already installed in target"; + return 0; + fi + + cp ${OPKG_LIB_PATH}/info/${pkg}.* ${root}/${OPKG_LIB_PATH}/info/ + opkg info ${pkg} >> ${root}/${OPKG_LIB_PATH}/status + + # copy the artifacts associated with the package + for f in `cat ${OPKG_LIB_PATH}/info/${pkg}.list`; do + if [ -f ${f} ]; then + local p=$(dirname $f) + mkdir -p ${root}/${p} + cp ${f} ${root}/${p}/ + fi + done +} + +function resolve_dependency() +{ + local deps=${1} + + for d in ${deps}; do + #log "Installing dependency [${d}] from list[${deps}]" + install_package ${root} ${d} + done +} + +function install_package() +{ + local root=${1} + local pkg=${2} + local ret=0 + local dep="" + + check_installed / ${pkg} + if [ "$?" -eq 1 ]; then + echo "Package [${pkg}] not available locally"; + return 0; + fi + + check_installed ${root} ${pkg} + if [ "$?" -eq 0 ]; then + echo "Package [${pkg}] already installed in target"; + return 0; + fi + + log "Processing [${pkg}]" + dep=$(check_dependency ${root} ${pkg}) + + if [ -n "${dep}" ]; then + resolve_dependency ${dep} + install_package ${root} ${pkg} + else + echo "Installing package ${pkg}" + copy_single_opkg ${root} ${pkg} + fi +} + +if [ ! -d "${1}" ]; then + echo "Root [${1}] does not exist" + exit 0; +fi + +if [ -z "${2}" ]; then + echo "No package specified" + exit 0; +fi + +echo "Preparing container for swmodd" +install_package ${1} ${2} diff --git a/templates/lxc-iopsys b/templates/lxc-iopsys index cbd64badd8320ae0abac1ae5eeaa0767df05284d..675d205786a49bc2ddbd7037384d91cde8bd33b7 100755 --- a/templates/lxc-iopsys +++ b/templates/lxc-iopsys @@ -52,6 +52,7 @@ install_busybox() ${rootfs}/etc \ ${rootfs}/etc/config \ ${rootfs}/etc/init.d \ + ${rootfs}/etc/rc.d \ ${rootfs}/bin \ ${rootfs}/sbin \ ${rootfs}/usr/bin \ @@ -97,16 +98,21 @@ install_busybox() # root user defined cat <<EOF >> "${rootfs}/etc/passwd" -root:x:0:0:root:/root:/bin/sh +root:x:0:0:root:/root:/bin/ash EOF - cat <<EOF >> "${rootfs}/etc/group" -root:x:0:root + cat <<EOF >> "${rootfs}/etc/shadow" +root::18844:0:99999:7::: EOF + cat <<EOF >> "${rootfs}/etc/group" +root:x:0: +EOF # mount everything cat <<EOF >> "${rootfs}/etc/rc.local" #!/bin/sh + +echo "Running rc.local" /bin/mount -a /bin/udhcpc EOF @@ -122,16 +128,19 @@ EOF # run this script to get ip START=9 + start() { # process user commands [ -f /etc/rc.local ] && { sh /etc/rc.local } - echo "All init.d scripts started." | tee /dev/console + echo "All init.d scripts started." } EOF + chmod +x ${rootfs}/etc/init.d/boot + cat <<EOF >> "${rootfs}/etc/inittab" ::sysinit:/etc/init.d/rcS S boot ::shutdown:/etc/init.d/rcS K shutdown @@ -206,9 +215,6 @@ configure_busybox() ./busybox --list | grep -v busybox | xargs -n1 ln -s busybox ) - # Copy init - cp "/sbin/init" "${rootfs}/sbin/init" - # /etc/fstab must exist for "mount -a" touch "${rootfs}/etc/fstab" @@ -222,23 +228,16 @@ configure_busybox() copy_prereq() { rootfs="${1}" - req_bin="/sbin/init \ - /sbin/procd \ - /sbin/ubusd \ - /bin/ubus \ - /sbin/uci \ - /sbin/ip \ - /bin/opkg" - - for b in ${req_bin}; do - if [ -x ${b} ]; then - cp ${b} ${rootfs}/sbin/ - fi + req_pkg="procd uci opkg ip-full" + + for p in ${req_pkg}; do + /usr/share/swmodd/opkg_offline ${rootfs} ${p} done - cp /etc/opkg.conf ${rootfs}/etc/ - cp /etc/init.d/done ${rootfs}/etc/init.d/done cp /etc/preinit ${rootfs}/etc/preinit + cp /etc/diag.sh ${rootfs}/etc/diag.sh + cp /sbin/mount_root ${rootfs}/sbin/mount_root + cp /sbin/ip ${rootfs}/sbin/ip cp /etc/rc.common ${rootfs}/etc/rc.common return 0; @@ -259,9 +258,6 @@ lxc.tty.max = 1 lxc.pty.max = 1 lxc.cap.drop = sys_module mac_admin mac_override sys_time -# When using LXC with apparmor, uncomment the next line to run unconfined: -#lxc.apparmor.profile = unconfined - lxc.mount.auto = cgroup:mixed proc:mixed sys:mixed lxc.mount.entry = shm dev/shm tmpfs defaults,create=dir 0 0 lxc.mount.entry = mqueue dev/mqueue mqueue defaults,optional,create=dir 0 0 @@ -277,23 +273,13 @@ EOF lib64 \ usr/lib64" - for dir in ${libdirs}; do - if [ -d "/${dir}" ] && [ -d "${rootfs}/${dir}" ]; then - echo "lxc.mount.entry = /${dir} ${dir} none ro,bind 0 0" >> "${path}/config" - fi - done + #cp -rf /lib/* ${rootfs}/lib/ + #for dir in ${libdirs}; do + # if [ -d "/${dir}" ] && [ -d "${rootfs}/${dir}" ]; then + # echo "lxc.mount.entry = /${dir} ${dir} none ro,bind 0 0" >> "${path}/config" + # fi + #done echo "lxc.mount.entry = /sys/kernel/security sys/kernel/security none ro,bind,optional 0 0" >> "${path}/config" - - mkdir -p ${rootfs}/usr/lib - usr_libs="libjson-c.so* \ - libbpf.so* \ - libelf.so* \ - libz.so* \ - libintl.so*" - - for l in ${usr_libs}; do - cp /usr/lib/${l} ${rootfs}/usr/lib/ - done } remap_userns()