From 1f1785c1053f81dfa7f8e2a4a0ae28cf4934ec7f Mon Sep 17 00:00:00 2001 From: bin456789 Date: Wed, 5 Jul 2023 21:57:27 +0800 Subject: [PATCH] =?UTF-8?q?common:=20=E6=94=B9=E5=96=84=E9=80=BB=E8=BE=91?= =?UTF-8?q?=E5=92=8C=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- reinstall.sh | 42 +++++++++++++++++++++++++++++------------- trans.sh | 6 ++++-- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/reinstall.sh b/reinstall.sh index f472037..eb5e28c 100644 --- a/reinstall.sh +++ b/reinstall.sh @@ -86,8 +86,10 @@ test_url() { } add_community_repo_for_alpine() { - alpine_ver=$(cut -d. -f1,2 >/etc/apk/repositories + if ! grep -x 'http.*/community' /etc/apk/repositories; then + alpine_ver=$(cut -d. -f1,2 >/etc/apk/repositories + fi } is_virt() { @@ -103,10 +105,7 @@ is_virt() { if command -v systemd-detect-virt; then systemd-detect-virt else - if ! install_pkg virt-what && [ -f /etc/alpine-release ]; then - add_community_repo_for_alpine - install_pkg virt-what - fi + install_pkg virt-what virt-what fi fi @@ -123,7 +122,7 @@ setos() { if is_virt; then # alpine aarch64 3.18 才有 virt 直连链接 if [ "$basearch" == aarch64 ]; then - (($("$releasever >= 3.18" | bc))) && flavour=virt + (($(echo "$releasever >= 3.18" | bc))) && flavour=virt else flavour=virt fi @@ -293,12 +292,18 @@ install_pkg() { [ "$pkg" = util-linux ] && pkg=lsmem if ! command -v $pkg >/dev/null; then { - apt_install $pkgs || - dnf install -y $pkgs || - yum install -y $pkgs || - zypper install -y $pkgs || - pacman -Syu --noconfirm $pkgs || - apk add $pkgs + if [ -f /etc/alpine-release ]; then + if ! apk add $pkgs; then + add_community_repo_for_alpine + apk add $pkgs + fi + else + apt_install $pkgs || + dnf install -y $pkgs || + yum install -y $pkgs || + zypper install -y $pkgs || + pacman -Syu --noconfirm $pkgs + fi } 2>/dev/null # break 返回值始终为 0 return @@ -312,12 +317,23 @@ check_ram() { else # lsmem最准确但centos7 arm 和alpine不能用 # arm 24g dmidecode 显示少了128m + # arm 24g lshw 显示23BiB + # ec2 t4g arm alpine 用 lsmem 和 dmidecode 都无效,要用 lshw,但结果和free -m一致,其他平台则没问题 install_pkg util-linux ram_size=$(lsmem -b 2>/dev/null | grep 'Total online memory:' | awk '{ print $NF/1024/1024 }') + if [ -z $ram_size ]; then install_pkg dmidecode ram_size=$(dmidecode -t 17 | grep "Size.*[GM]B" | awk '{if ($3=="GB") s+=$2*1024; else s+=$2} END {print s}') fi + + if [ -z $ram_size ]; then + install_pkg lshw + # 不能忽略 -i,alpine 显示的是 System memory + ram_str=$(lshw -c memory -short | grep -i 'System Memory' | awk '{print $3}') + ram_size=$(grep <<<$ram_str -o '[0-9]*') + grep <<<$ram_str GiB && ram_size=$((ram_size * 1024)) + fi fi case "$distro" in diff --git a/trans.sh b/trans.sh index 0d4fd44..846e709 100644 --- a/trans.sh +++ b/trans.sh @@ -26,8 +26,10 @@ error() { } add_community_repo() { - alpine_ver=$(cut -d. -f1,2 >/etc/apk/repositories + if ! grep -x 'http.*/community' /etc/apk/repositories; then + alpine_ver=$(cut -d. -f1,2 >/etc/apk/repositories + fi } cp() {