From e7056369296be8617bcde6d61c8613863f8fa69c Mon Sep 17 00:00:00 2001 From: fbt Date: Fri, 17 Nov 2017 20:17:03 +0300 Subject: [PATCH] new tools Signed-off-by: fbt --- builder-chroot | 54 ------------------------------ builder-chroot.cfg | 9 ----- builder-chroot.cfg.sh | 9 ----- builder-chroot.sh | 48 --------------------------- lxf | 70 +++++++++++++++++++++++++++++++++++++++ makepkg-chroot.sh | 62 ---------------------------------- makepkg-overlay | 44 +++++++++++++++++++++++++ mount-chroot.sh | 77 ------------------------------------------- 8 files changed, 114 insertions(+), 259 deletions(-) delete mode 100755 builder-chroot delete mode 100755 builder-chroot.cfg delete mode 100755 builder-chroot.cfg.sh delete mode 100755 builder-chroot.sh create mode 100755 lxf delete mode 100755 makepkg-chroot.sh create mode 100755 makepkg-overlay delete mode 100755 mount-chroot.sh diff --git a/builder-chroot b/builder-chroot deleted file mode 100755 index 1fe99e4..0000000 --- a/builder-chroot +++ /dev/null @@ -1,54 +0,0 @@ -#!/usr/bin/env bash -# A simple script to build packages into multiple repos - -err() { echo "$1" >&2; } - -build_pkg() { - declare package repo - - package="$1" - repo="$2" - - echo -n "Building ${repo}/${package}... " - - cd "/tmp/build-${package}" - sudo /usr/local/bin/makepkg-chroot.sh "${cfg_build_dir}/${package}.chroot" "${cfg_repos_dir}/${repo}" -} - -main() { - source 'builder-chroot.cfg' - - package="$1" - repo_name="$2" - -# if [[ -f "/tmp/build-${package}/.spark.repo" ]]; then -# repo_name=$( <"/tmp/build-${package}/.spark.repo" ) -# else -# err "Repo not specified, not building package." -# return 1 -# fi - - if [[ ! -d "${cfg_repos_dir}/${repo_name}" ]]; then - echo "Creating ${cfg_repos_dir}/${repo_name}" - mkdir -p "${cfg_repos_dir}/${repo_name}" || { - return 1 - } - fi - - build_pkg "$package" "$repo_name" - - cd "${cfg_repos_dir}/${repo_name}" - - printf 'Cleaning the %s repo...\n' "$repo_name" - "$HOME/scripts/repo-clean" > repo_clean.log - - printf 'Signing packages...\n' - "$HOME/scripts/repo-sign" > repo_sign.log - - printf 'Adding packages to the %s repo...\n' "$repo_name" - repo-add -n "${repo_name}.db.tar.gz" *.pkg.tar.xz &> update.log - - printf 'Done\n' -} - -main "$@" diff --git a/builder-chroot.cfg b/builder-chroot.cfg deleted file mode 100755 index 144bedd..0000000 --- a/builder-chroot.cfg +++ /dev/null @@ -1,9 +0,0 @@ -# The config for building packages in chroots - -cfg_git_repo="$HOME/git/pkgbuilds.git" - -cfg_build_dir="/tmp" -cfg_repos_dir="$HOME/public" -cfg_chroot_dir="/tmp/chroot" - -export GIT_DIR='.git' diff --git a/builder-chroot.cfg.sh b/builder-chroot.cfg.sh deleted file mode 100755 index 67f9782..0000000 --- a/builder-chroot.cfg.sh +++ /dev/null @@ -1,9 +0,0 @@ -# The config for building packages in chroots - -cfg_git_repo="$HOME/git/pkgbuilds.git" - -cfg_build_dir="/tmp/build" -cfg_repos_dir="$HOME/public" -cfg_chroot_dir="/tmp/chroot/build" - -export GIT_DIR='.git' diff --git a/builder-chroot.sh b/builder-chroot.sh deleted file mode 100755 index 41efc9c..0000000 --- a/builder-chroot.sh +++ /dev/null @@ -1,48 +0,0 @@ -#!/usr/bin/env bash -# A simple script to build packages into multiple repos - -err() { echo "$1" >&2; } - -build_pkg() { - local makepkg_output makepkg_exit_code makepkg_status - - repo="$1" - package="$2" - - [[ -d "${cfg_build_dir}/repos/${repo}/${package}" ]] || { - err "Package ${package} doesn't exist in ${cfg_build_dir}/repos/${repo}" - return 1 - } - - [[ -d "${cfg_repos_dir}/${repo}" ]] || { - echo "Creating ${cfg_repos_dir}/${repo}" - mkdir -p "${cfg_repos_dir}/${repo}" || { - return 1 - } - } - - echo -n "Building ${repo}/${package}... " - - cd "${cfg_build_dir}/repos/${repo}/${package}" - sudo /usr/local/bin/makepkg-chroot.sh "${cfg_build_dir}/${package}.chroot" "${cfg_repos_dir}/${repo}" -} - -main() { - source 'builder-chroot.cfg.sh' - - for i in "${cfg_build_dir}/repos"/*; do - repo_name="${i##*/}" - repos+=( "$repo_name" ) - - for r in "$i"/*; do - build_pkg "$repo_name" "${r##*/}" - done - done - - for i in "${repos[@]}"; do - cd "${cfg_repos_dir}/${i}" - repo-add -n "${i}.db.tar.gz" *.pkg.tar.xz - done -} - -main diff --git a/lxf b/lxf new file mode 100755 index 0000000..953582f --- /dev/null +++ b/lxf @@ -0,0 +1,70 @@ +#!/usr/bin/env bash + +mkconf() { + for i in "${includes[@]}"; do + echo "lxc.include = $lxf_conf_dir/$i.conf" + done + + echo "lxc.rootfs.path = $lxf_cont_dir/$cont_name/rootfs" + echo "lxc.uts.name = $cont_name" +} + +mount_cont() { + ov-mount -n "$cont_name" "$rootfs" "$cont_dir/rootfs" +} + +lxf_conf_dir='/etc/lxf/conf' +lxf_cont_dir='/var/lib/lxf/cont' +lxf_rootfs_dir='/var/lib/lxf/fs' + +[[ -f '/etc/lxf.conf' ]] && source '/etc/lxf.conf' + +while (( $# )); do + case $1 in + (-i) includes+=( "$2" ); shift;; + (-r) rootfs="$2"; shift;; + + (--cont-dir) lxf_cont_dir=$2; shift;; + (--conf-dir) lxf_conf_dir=$2; shift;; + + (--) shift; break;; + (-*) echo "Unknown key: $1" >&2: exit 1;; + (*) break;; + esac + + shift +done + + +action=$1; shift; [[ $action ]] || exit 1 +cont_name=$1; shift; [[ $cont_name ]] || exit 1 + +cont_dir="$lxf_cont_dir/$cont_name" + +case $action in + (create|new) + [[ "$rootfs" ]] || exit 1 + rootfs="$lxf_rootfs_dir/$rootfs" + + [[ -d "$cont_dir" ]] && { + printf 'Container already exists: %s\n' "$cont_dir" >&2 + exit 1 + } + + mkdir -p "$cont_dir" || exit $? + + mkconf > "$cont_dir/config" + mount_cont + ;; + + (mount) + mountpoint -q "$cont_dir/rootfs" && { + printf 'Container already mounted: %s\n' "$cont_dir/rootfs" >&2 + exit 1 + } + + mount_cont + ;; + + (umount) umount "$cont_dir/rootfs";; +esac diff --git a/makepkg-chroot.sh b/makepkg-chroot.sh deleted file mode 100755 index f05dcb5..0000000 --- a/makepkg-chroot.sh +++ /dev/null @@ -1,62 +0,0 @@ -#!/usr/bin/env bash - -# Config -chroot_base='/var/chroot/root' - -# Functions -icat() { - while read; do - echo -e "$REPLY" - done -} - -build_script() { - icat <<- EOF - #!/bin/sh - - pacman -Sy &>pacman.log - - cd '/build'; chown builder:wheel /build - sudo -u builder PKGDEST='/pkg' makepkg -s --noconfirm -L &>makepkg.log - - makepkg_exit="\$?" - - case "\$makepkg_exit" in - 0) echo 'ok';; - 3) echo 'already built';; - *) echo 'FAIL';; - esac - - exit "\$makepkg_exit" - EOF -} - -main() { - chroot_dir="$1" - pkg_dest_dir="$2" - workdir="$PWD" - - [[ "${chroot_dir}" ]] || { usage; exit 1; } - - /usr/local/bin/mount-chroot.sh -s "$chroot_base" -n 'chroot_build' "$chroot_dir" - - mkdir -m777 "$chroot_dir/pkg" "$chroot_dir/build" - - mount --bind "$workdir" "$chroot_dir/build" - mount --bind "$pkg_dest_dir" "$chroot_dir/pkg" - - build_script > "${chroot_dir}/build/build.sh" - - #chmod 755 "${chroot_dir}/build/build.sh" - chroot "${chroot_dir}" sh /build/build.sh - - if /usr/local/bin/mount-chroot.sh -u "$chroot_dir"; then - rm -r "${chroot_dir}"{,.work} - else - err "Cannot unmount $chroot_dir" - exit 1 - fi -} - -main "$@" - diff --git a/makepkg-overlay b/makepkg-overlay new file mode 100755 index 0000000..18d0a48 --- /dev/null +++ b/makepkg-overlay @@ -0,0 +1,44 @@ +#!/usr/bin/env bash + +cleanup() { + umount "$cnt_dir/rootfs" +} + +wrk_dir='/home/lxc' +pkg_url=$1 +pkg_dest=$2 + +cnt="_makepkg.$$" +cnt_dir="$wrk_dir/containers/$cnt" + +trap 'cleanup' INT TERM EXIT + +lxf -r builder -i builder new "$cnt" + +# Add the build script +cp "$wrk_dir/files/buildpackage" "$cnt_dir/rootfs/init" +chmod 755 "$cnt_dir/rootfs/init" + +# Tell it where to fetch the package +echo "$pkg_url" > "$cnt_dir/rootfs/repo" + +# Start the container +lxc-start -n "$cnt" -F; lxc_exit=$? +(( lxc_exit )) && exit "$lxc_exit" + +# Put the artifacts where asked to +[[ "$pkg_dest" ]] && { + artifacts=( "$cnt_dir/rootfs/buildroot/"*.pkg.* ) + + for i in "${artifacts[@]}"; do + i_name="${i##*/}" + + printf 'Found artifact: %s\n' "$i_name" + + if [[ -f "$pkg_dest/$i_name" ]]; then + echo "$pkg_dest/$i_name already exists, not overwriting." + else + cp -vn "$i" "$pkg_dest" + fi + done +} diff --git a/mount-chroot.sh b/mount-chroot.sh deleted file mode 100755 index ef21e08..0000000 --- a/mount-chroot.sh +++ /dev/null @@ -1,77 +0,0 @@ -#!/usr/bin/env bash - -err() { echo "$1" >&2; } - -usage() { -cat <<- EOF - Usage: "${0##*/}" [options] - Options: - -s chroot base dir - -n mount name - -u unmount chroot -EOF -} - -chroot.mount() { - for i in "$target_dir" "$chroot_base"; do - [[ "$i" ]] || { - usage - return 1 - } - done - - [[ -d "$chroot_base" ]] || { - err "Chroot base ($chroot_base) does not exist." - return 1 - } - - mount_name="${mount_name:-chroot}" - - for i in "$target_dir" "${target_dir}.work"; do - [[ -d "$i" ]] || { - mkdir -p "$i" || { - return 3 - } - } - done - - mount -t overlay -o lowerdir="$chroot_base",upperdir="$target_dir",workdir="${target_dir}.work" "$mount_name" "$target_dir" - mount -t proc "${mount_name}_proc" "${target_dir}/proc" - mount --rbind /dev "${target_dir}/dev" - mount --rbind /sys "${target_dir}/sys" -} - -chroot.umount() { - umount --recursive "$target_dir" -} - -main() { - while [[ "$1" ]]; do - case "$1" in - -s) chroot_base="$2"; shift;; - -n) mount_name="$2"; shift;; - -u) action='umount';; - - --) shift; break;; - -*) - err "Unknown key: $1" - usage - return 1 - ;; - *) break;; - esac - shift - done - - target_dir="$1" - action="${action:-mount}" - mount_name="${mount_name:-chroot}" - - case "$action" in - mount) chroot.mount;; - umount) chroot.umount;; - esac -} - -main "$@" -