manjaro-tools/lib/util.sh

476 lines
9.8 KiB
Bash
Raw Normal View History

2014-10-08 00:11:53 +02:00
#!/bin/bash
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
read_set(){
local _space="s| ||g" \
_clean=':a;N;$!ba;s/\n/ /g' \
2015-11-28 17:37:13 +01:00
_com_rm="s|#.*||g"
2015-12-01 12:24:23 +01:00
stack=$(sed "$_com_rm" "$1.set" \
| sed "$_space" \
| sed "$_clean")
}
2015-11-25 16:53:55 +01:00
# $1: sets_dir
2015-12-08 12:33:28 +01:00
list_sets(){
2015-11-25 16:53:55 +01:00
local prof temp
for item in $(ls $1/*.set); do
temp=${item##*/}
prof=${prof:-}${prof:+|}${temp%.set}
2015-06-09 00:02:32 +02:00
done
2015-11-25 16:53:55 +01:00
echo $prof
2015-06-06 09:48:56 +02:00
}
2015-12-01 12:24:23 +01:00
# $1: sets_dir
# $2: buildset
2015-11-25 16:53:55 +01:00
eval_buildset(){
2015-12-01 12:24:23 +01:00
eval "case $2 in
2015-12-08 12:33:28 +01:00
$(list_sets $1)) is_buildset=true ;;
2015-11-25 16:53:55 +01:00
*) is_buildset=false ;;
esac"
2015-12-01 12:24:23 +01:00
${is_buildset} && read_set $1/$2
2015-06-06 09:48:56 +02:00
}
2015-11-30 21:35:30 +01:00
eval_edition(){
2015-12-07 00:58:20 +01:00
local result=$(find ${run_dir} -maxdepth 2 -name "$1") path
2015-11-30 21:35:30 +01:00
[[ -z $result ]] && die "$1 is not a valid profile or buildset!"
2015-12-07 00:58:20 +01:00
path=${result%/*}
edition=${path##*/}
2015-11-30 21:35:30 +01:00
}
2015-02-18 04:28:11 +01:00
get_timer(){
echo $(date +%s)
}
2015-09-20 12:40:49 +02:00
get_timer_ms(){
echo $(date +%s%3N)
}
2015-02-18 04:28:11 +01:00
# $1: start timer
elapsed_time(){
echo $(echo $1 $(get_timer) | awk '{ printf "%0.2f",($2-$1)/60 }')
}
# $1: start timer
2015-09-20 12:40:49 +02:00
elapsed_time_ms(){
echo $(echo $1 $(get_timer_ms) | awk '{ printf "%0.3f",($2-$1)/1000 }')
}
2014-11-12 02:19:58 +01:00
check_root() {
2015-02-13 15:17:00 +01:00
(( EUID == 0 )) && return
if type -P sudo >/dev/null; then
exec sudo -- "$@"
else
exec su root -c "$(printf ' %q' "$@")"
fi
2014-11-12 02:19:58 +01:00
}
2015-06-03 16:09:26 +02:00
copy_mirrorlist(){
cp -a /etc/pacman.d/mirrorlist "$1/etc/pacman.d/"
}
2015-06-03 16:09:26 +02:00
copy_keyring(){
if [[ -d /etc/pacman.d/gnupg ]] && [[ ! -d $1/etc/pacman.d/gnupg ]]; then
cp -a /etc/pacman.d/gnupg "$1/etc/pacman.d/"
fi
2015-06-01 21:19:28 +02:00
}
load_vars() {
2015-02-13 15:17:00 +01:00
local var
2015-02-09 23:38:12 +01:00
2015-02-13 15:17:00 +01:00
[[ -f $1 ]] || return 1
2015-02-13 15:17:00 +01:00
for var in {SRC,SRCPKG,PKG,LOG}DEST MAKEFLAGS PACKAGER CARCH GPGKEY; do
[[ -z ${!var} ]] && eval $(grep "^${var}=" "$1")
done
2015-02-09 23:38:12 +01:00
2015-02-13 15:17:00 +01:00
return 0
}
2015-06-12 11:09:29 +02:00
prepare_dir(){
[[ ! -d $1 ]] && mkdir -p $1
}
version_gen(){
2015-11-13 23:33:32 +01:00
local y=$(date +%Y) m=$(date +%m)
dist_release=${y:2}.$m
}
2015-11-17 17:03:37 +01:00
version_gen2(){
2015-12-02 19:59:28 +01:00
local y=$(date +%Y) m=$(date +%m)
case $month in
01|04|07|10) dist_release=${y:2}.$m.1 ;;
02|05|08|11) dist_release=${y:2}.$m.2 ;;
*) dist_release=${y:2}.$m ;;
esac
2015-11-17 17:03:37 +01:00
}
init_common(){
2015-05-21 22:44:32 +02:00
[[ -z ${branch} ]] && branch='stable'
2015-02-09 23:38:12 +01:00
2015-05-21 22:44:32 +02:00
[[ -z ${arch} ]] && arch=$(uname -m)
2015-02-09 23:38:12 +01:00
2015-05-21 22:44:32 +02:00
[[ -z ${cache_dir} ]] && cache_dir='/var/cache/manjaro-tools'
[[ -z ${chroots_dir} ]] && chroots_dir='/var/lib/manjaro-tools'
[[ -z ${build_mirror} ]] && build_mirror='http://mirror.netzspielplatz.de/manjaro/packages'
}
init_buildtree(){
2015-06-09 00:02:32 +02:00
tree_dir=${cache_dir}/pkgtree
tree_dir_abs=${tree_dir}/packages-archlinux
2015-05-21 22:44:32 +02:00
[[ -z ${repo_tree[@]} ]] && repo_tree=('core' 'extra' 'community' 'multilib' 'openrc')
2015-02-09 23:38:12 +01:00
2015-05-21 22:44:32 +02:00
[[ -z ${host_tree} ]] && host_tree='https://github.com/manjaro'
2015-02-09 23:38:12 +01:00
2015-05-21 22:44:32 +02:00
[[ -z ${host_tree_abs} ]] && host_tree_abs='https://projects.archlinux.org/git/svntogit'
}
2015-02-09 23:38:12 +01:00
init_buildpkg(){
chroots_pkg="${chroots_dir}/buildpkg"
2015-12-03 22:54:58 +01:00
sets_dir_pkg="${SYSCONFDIR}/pkg.d"
2015-06-12 11:09:29 +02:00
prepare_dir "${sets_dir_pkg}"
2015-12-08 12:33:28 +01:00
[[ -d ${USERCONFDIR}/pkg.d ]] && sets_dir_pkg=${USERCONFDIR}/pkg.d
2015-05-21 22:44:32 +02:00
[[ -z ${buildset_pkg} ]] && buildset_pkg='default'
2015-11-30 21:35:30 +01:00
cache_dir_pkg=${cache_dir}/pkg
}
2015-02-09 23:38:12 +01:00
init_buildiso(){
chroots_iso="${chroots_dir}/buildiso"
2015-12-03 22:54:58 +01:00
sets_dir_iso="${SYSCONFDIR}/iso.d"
2015-06-12 11:09:29 +02:00
prepare_dir "${sets_dir_iso}"
2015-12-08 12:33:28 +01:00
[[ -d ${USERCONFDIR}/iso.d ]] && sets_dir_iso=${USERCONFDIR}/iso.d
2015-05-21 22:44:32 +02:00
[[ -z ${buildset_iso} ]] && buildset_iso='default'
2015-02-09 23:38:12 +01:00
2015-11-30 21:35:30 +01:00
cache_dir_iso="${cache_dir}/iso"
##### iso settings #####
if [[ -z ${dist_release} ]];then
# source /etc/lsb-release
# dist_release=${DISTRIB_RELEASE}
version_gen
2015-02-13 15:17:00 +01:00
fi
2014-12-03 22:59:30 +01:00
if [[ -z ${dist_codename} ]];then
source /etc/lsb-release
dist_codename="${DISTRIB_CODENAME}"
2015-02-13 15:17:00 +01:00
fi
2014-12-03 22:59:30 +01:00
2015-05-21 22:44:32 +02:00
[[ -z ${dist_branding} ]] && dist_branding="MJRO"
2015-02-09 23:38:12 +01:00
2015-05-21 22:44:32 +02:00
[[ -z ${dist_name} ]] && dist_name="Manjaro"
# [[ -z ${iso_name} ]] && iso_name="manjaro"
iso_name=${dist_name,,}
iso_label="${dist_branding}${dist_release//.}"
iso_label="${iso_label//_}" # relace all _
iso_label="${iso_label//-}" # relace all -
iso_label="${iso_label^^}" # all uppercase
iso_label="${iso_label::8}" # limit to 8 characters
2015-05-21 22:44:32 +02:00
[[ -z ${iso_publisher} ]] && iso_publisher='Manjaro Linux <http://www.manjaro.org>'
2015-05-11 12:47:02 +02:00
2015-05-21 22:44:32 +02:00
[[ -z ${iso_app_id} ]] && iso_app_id='Manjaro Linux Live/Rescue CD'
2015-05-11 12:47:02 +02:00
2015-05-21 22:44:32 +02:00
[[ -z ${iso_compression} ]] && iso_compression='xz'
2015-02-09 23:38:12 +01:00
2015-05-21 22:44:32 +02:00
[[ -z ${iso_checksum} ]] && iso_checksum='md5'
2015-08-01 16:44:18 +02:00
[[ -z ${use_overlayfs} ]] && use_overlayfs='true'
local used_kernel=$(uname -r)
[[ ${used_kernel%%*.} < "4" ]] && use_overlayfs='false'
[[ -z ${profile_repo} ]] && profile_repo='manjaro-tools-iso-profiles'
}
2015-11-12 00:31:51 +01:00
init_deployiso(){
[[ -z ${remote_target} ]] && remote_target="/home/frs/project"
2015-11-12 00:31:51 +01:00
[[ -z ${remote_project} ]] && remote_project="manjaro-testing"
2015-11-12 00:31:51 +01:00
[[ -z ${remote_user} ]] && remote_user="[SetUser]"
2015-11-12 00:31:51 +01:00
2015-11-12 12:25:35 +01:00
[[ -z ${remote_url} ]] && remote_url="sourceforge.net"
2015-11-24 16:58:40 +01:00
[[ -z ${limit} ]] && limit=100
2015-11-12 00:31:51 +01:00
}
load_config(){
[[ -f $1 ]] || return 1
manjaro_tools_conf="$1"
[[ -r ${manjaro_tools_conf} ]] && source ${manjaro_tools_conf}
init_common
init_buildtree
init_buildpkg
init_buildiso
2015-02-09 23:38:12 +01:00
2015-11-12 00:31:51 +01:00
init_deployiso
return 0
}
unset_profile(){
unset initsys
unset displaymanager
unset autologin
unset multilib
unset pxe_boot
unset plymouth_boot
unset nonfree_xorg
unset default_desktop_executable
unset default_desktop_file
unset kernel
unset efi_boot_loader
unset efi_part_size
unset hostname
unset username
unset plymouth_theme
unset password
unset addgroups
unset start_systemd
unset disable_systemd
unset start_openrc
unset disable_openrc
unset start_systemd_live
unset start_openrc_live
unset use_overlayfs
unset packages_custom
unset packages_mhwd
}
load_profile_config(){
[[ -f $1 ]] || return 1
2015-12-18 04:52:34 +01:00
profile_conf="$1"
[[ -r ${profile_conf} ]] && source ${profile_conf}
2015-05-21 22:44:32 +02:00
[[ -z ${initsys} ]] && initsys="systemd"
2015-02-09 23:38:12 +01:00
2015-05-21 22:44:32 +02:00
[[ -z ${displaymanager} ]] && displaymanager="none"
2015-06-06 13:43:00 +02:00
[[ -z ${autologin} ]] && autologin="true"
[[ -z ${multilib} ]] && multilib="true"
[[ -z ${pxe_boot} ]] && pxe_boot="true"
2015-06-22 14:47:06 +02:00
[[ -z ${plymouth_boot} ]] && plymouth_boot="true"
[[ -z ${nonfree_xorg} ]] && nonfree_xorg="true"
[[ -z ${default_desktop_executable} ]] && default_desktop_executable="none"
[[ -z ${default_desktop_file} ]] && default_desktop_file="none"
2015-08-01 16:44:18 +02:00
[[ -z ${kernel} ]] && kernel="linux41"
local used_kernel=${kernel:5:1}
[[ ${used_kernel} < "4" ]] && use_overlayfs='false'
if ${use_overlayfs};then
iso_fs="overlayfs"
else
iso_fs="aufs"
fi
2015-05-21 22:44:32 +02:00
[[ -z ${efi_boot_loader} ]] && efi_boot_loader="grub"
2015-02-09 23:38:12 +01:00
2015-05-21 22:44:32 +02:00
[[ -z ${efi_part_size} ]] && efi_part_size="31M"
2015-02-09 23:38:12 +01:00
2015-05-21 22:44:32 +02:00
[[ -z ${hostname} ]] && hostname="manjaro"
2015-02-09 23:38:12 +01:00
2015-05-21 22:44:32 +02:00
[[ -z ${username} ]] && username="manjaro"
[[ -z ${plymouth_theme} ]] && plymouth_theme="manjaro-elegant"
[[ -z ${password} ]] && password="manjaro"
2015-02-09 23:38:12 +01:00
2015-02-13 15:17:00 +01:00
if [[ -z ${addgroups} ]];then
2015-02-14 00:27:01 +01:00
addgroups="video,audio,power,disk,storage,optical,network,lp,scanner,wheel"
2015-02-13 15:17:00 +01:00
fi
2014-12-17 02:14:15 +01:00
2015-05-21 22:44:32 +02:00
if [[ -z ${start_systemd[@]} ]];then
2015-02-13 15:17:00 +01:00
start_systemd=('bluetooth' 'cronie' 'ModemManager' 'NetworkManager' 'org.cups.cupsd' 'tlp' 'tlp-sleep')
fi
2015-02-09 23:38:12 +01:00
2015-09-12 13:03:47 +02:00
if [[ -z ${disable_systemd[@]} ]];then
disable_systemd=('pacman-init')
fi
2015-05-21 22:44:32 +02:00
if [[ -z ${start_openrc[@]} ]];then
start_openrc=('acpid' 'bluetooth' 'cgmanager' 'consolekit' 'cronie' 'cupsd' 'dbus' 'syslog-ng' 'NetworkManager')
2015-02-13 15:17:00 +01:00
fi
2015-02-09 23:38:12 +01:00
2015-09-12 13:03:47 +02:00
if [[ -z ${disable_openrc[@]} ]];then
disable_openrc=('pacman-init')
fi
2015-05-21 22:44:32 +02:00
if [[ -z ${start_systemd_live[@]} ]];then
start_systemd_live=('livecd' 'mhwd-live' 'pacman-init')
2015-02-13 15:17:00 +01:00
fi
2015-02-09 23:38:12 +01:00
2015-05-21 22:44:32 +02:00
if [[ -z ${start_openrc_live[@]} ]];then
start_openrc_live=('livecd' 'mhwd-live' 'pacman-init')
2015-02-13 15:17:00 +01:00
fi
2015-02-09 23:38:12 +01:00
2015-02-13 15:17:00 +01:00
return 0
}
2015-01-11 21:09:01 +01:00
2015-02-16 20:32:47 +01:00
clean_dir(){
if [[ -d $1 ]]; then
msg "Cleaning [$1] ..."
rm -r $1/*
fi
}
2015-12-05 19:37:40 +01:00
write_repo_conf(){
local repos=$(find $USER_HOME -type f -name ".buildiso")
2015-12-05 19:37:40 +01:00
local path name
[[ -z ${repos[@]} ]] && run_dir=${DATADIR}/iso-profiles && return 1
2015-12-05 19:37:40 +01:00
for r in ${repos[@]}; do
path=${r%/.*}
name=${path##*/}
2015-12-18 01:56:21 +01:00
echo "run_dir=$path" > ${USERCONFDIR}/$name.conf
2015-12-05 19:37:40 +01:00
done
}
2015-01-13 13:41:28 +01:00
load_user_info(){
2015-02-13 15:17:00 +01:00
OWNER=${SUDO_USER:-$USER}
2015-01-13 13:41:28 +01:00
2015-02-13 15:17:00 +01:00
if [[ -n $SUDO_USER ]]; then
eval "USER_HOME=~$SUDO_USER"
else
USER_HOME=$HOME
fi
2015-02-09 23:38:12 +01:00
2015-12-08 12:33:28 +01:00
USERCONFDIR="$USER_HOME/.config/manjaro-tools"
prepare_dir "${USERCONFDIR}"
2015-01-13 13:41:28 +01:00
}
2015-02-14 10:49:16 +01:00
2015-12-05 19:37:40 +01:00
load_run_dir(){
2015-12-08 12:33:28 +01:00
[[ -f ${USERCONFDIR}/$1.conf ]] || write_repo_conf
[[ -r ${USERCONFDIR}/$1.conf ]] && source ${USERCONFDIR}/$1.conf
2015-12-05 19:37:40 +01:00
return 0
}
2015-05-22 16:51:45 +02:00
show_version(){
msg "manjaro-tools"
msg2 "version: ${version}"
}
show_config(){
2015-12-08 12:33:28 +01:00
if [[ -f ${USERCONFDIR}/manjaro-tools.conf ]]; then
msg2 "user_config: ${USERCONFDIR}/manjaro-tools.conf"
2015-05-22 16:51:45 +02:00
else
msg2 "manjaro_tools_conf: ${manjaro_tools_conf}"
fi
}
2015-05-31 23:32:50 +02:00
# $1: chroot
kill_chroot_process(){
2015-05-31 23:32:50 +02:00
# enable to have more debug info
#msg "machine-id (etc): $(cat $1/etc/machine-id)"
#[[ -e $1/var/lib/dbus/machine-id ]] && msg "machine-id (lib): $(cat $1/var/lib/dbus/machine-id)"
#msg "running processes: "
#lsof | grep $1
2015-11-26 12:26:21 +01:00
local prefix="$1" flink pid name
for root_dir in /proc/*/root; do
flink=$(readlink $root_dir)
if [ "x$flink" != "x" ]; then
if [ "x${flink:0:${#prefix}}" = "x$prefix" ]; then
2015-05-31 23:32:50 +02:00
# this process is in the chroot...
2015-11-26 12:26:21 +01:00
pid=$(basename $(dirname "$root_dir"))
name=$(ps -p $pid -o comm=)
msg3 "Killing chroot process: $name ($pid)"
kill -9 "$pid"
2015-05-31 23:32:50 +02:00
fi
fi
done
}
2015-06-01 01:09:05 +02:00
2015-06-01 23:15:29 +02:00
create_min_fs(){
msg "Creating install root at $1"
mkdir -m 0755 -p $1/var/{cache/pacman/pkg,lib/pacman,log} $1/{dev,run,etc}
mkdir -m 1777 -p $1/tmp
mkdir -m 0555 -p $1/{sys,proc}
2015-06-01 15:52:03 +02:00
}
2015-06-09 01:14:58 +02:00
is_valid_bool(){
case $1 in
2015-06-15 10:51:40 +02:00
'true'|'false') return 0 ;;
*) return 1 ;;
esac
}
is_valid_init(){
case $1 in
'openrc'|'systemd') return 0 ;;
*) return 1 ;;
esac
}
2015-11-26 12:26:21 +01:00
2015-11-27 12:22:38 +01:00
is_valid_arch_pkg(){
case $1 in
'i686'|'x86_64'|'multilib') return 0 ;;
*) return 1 ;;
esac
}
is_valid_arch_iso(){
case $1 in
'i686'|'x86_64') return 0 ;;
*) return 1 ;;
esac
}
2015-11-28 17:37:13 +01:00
is_valid_branch(){
case $1 in
'stable'|'testing'|'unstable') return 0 ;;
*) return 1 ;;
esac
}
run(){
if ${is_buildset};then
for item in ${stack[@]};do
$1 $item
2015-11-28 17:37:13 +01:00
done
else
$1 $2
fi
}