NBash

Форк
0
1130 строк · 30.2 Кб
1
#!/bin/sh
2
# 2007-2023 (c) Vitaly Lipatov <lav@etersoft.ru>
3
# 2007-2023 (c) Etersoft
4
# 2007-2023 Public domain
5

6
# You can set ROOTDIR to root system dir
7
#ROOTDIR=
8

9
PROGVERSION="20230406"
10

11
# TODO: check /etc/system-release
12

13
# Check for DISTRO specific file in /etc
14
distro()
15
{
16
    #[ -n "$ROOTDIR" ] || return
17
    # fill global DISTROFILE
18
    DISTROFILE="$ROOTDIR/etc/$1"
19
    [ -f "$DISTROFILE" ]
20
}
21

22
# Has a distro file the specified word?
23
has()
24
{
25
    [ -n "$DISTROFILE" ] || exit 1
26
    grep "$*" "$DISTROFILE" >/dev/null 2>&1
27
}
28

29
# copied from epm-sh-functions
30
# print a path to the command if exists in $PATH
31
if a='' which which 2>/dev/null >/dev/null ; then
32
    # the best case if we have which command (other ways needs checking)
33
    # TODO: don't use which at all, it is binary, not builtin shell command
34
print_command_path()
35
{
36
    a='' which -- "$1" 2>/dev/null
37
}
38
elif a='' type -a type 2>/dev/null >/dev/null ; then
39
print_command_path()
40
{
41
    a='' type -fpP -- "$1" 2>/dev/null
42
}
43
else
44
print_command_path()
45
{
46
    a='' type "$1" 2>/dev/null | sed -e 's|.* /|/|'
47
}
48
fi
49

50
# check if <arg> is a real command
51
is_command()
52
{
53
    print_command_path "$1" >/dev/null
54
}
55
##########################3
56

57

58
firstupper()
59
{
60
    # FIXME: works with GNU sed only
61
    echo "$*" | sed 's/.*/\u&/'
62
}
63

64
tolower()
65
{
66
    # tr is broken in busybox (checked with OpenWrt)
67
    #echo "$*" | tr "[:upper:]" "[:lower:]"
68
    echo "$*" | awk '{print tolower($0)}'
69
}
70

71
print_bug_report_url()
72
{
73
    echo "$BUG_REPORT_URL"
74
}
75

76
# allows x86_64/Distro/Version
77
override_distrib()
78
{
79
    [ -n "$DISTRNAMEOVERRIDE" ] || DISTRNAMEOVERRIDE="$1"
80
    [ -n "$DISTRNAMEOVERRIDE" ] || return
81

82
    local name="$(echo "$DISTRNAMEOVERRIDE" | sed -e 's|x86_64/||')"
83
    [ "$name" = "$DISTRNAMEOVERRIDE" ] && DIST_ARCH="x86" || DIST_ARCH="x86_64"
84
    DISTRIB_ID="$(echo "$name" | sed -e 's|/.*||')"
85
    DISTRIB_RELEASE="$(echo "$name" | sed -e 's|.*/||')"
86
    [ "$DISTRIB_ID" = "$DISTRIB_RELEASE" ] && DISTRIB_RELEASE=''
87

88
    VENDOR_ID=''
89
    PRETTY_NAME="$DISTRIB_ID"
90
    DISTRO_NAME="$DISTRIB_ID"
91
    DISTRIB_CODENAME="$DISTRIB_RELEASE"
92
    DISTRIB_FULL_RELEASE="$DISTRIB_RELEASE"
93

94
}
95

96
# Translate DISTRIB_ID to vendor name (like %_vendor does or package release name uses), uses VENDOR_ID by default
97
pkgvendor()
98
{
99
    [ "$DISTRIB_ID" = "ALTLinux" ] && echo "alt" && return
100
    [ "$DISTRIB_ID" = "ALTServer" ] && echo "alt" && return
101
    [ "$DISTRIB_ID" = "MOC" ] && echo "alt" && return
102
    [ "$DISTRIB_ID" = "MESh" ] && echo "alt" && return
103
    [ "$DISTRIB_ID" = "AstraLinuxSE" ] && echo "astra" && return
104
    [ "$DISTRIB_ID" = "AstraLinuxCE" ] && echo "astra" && return
105
    [ "$DISTRIB_ID" = "LinuxXP" ] && echo "lxp" && return
106
    [ "$DISTRIB_ID" = "TinyCoreLinux" ] && echo "tcl" && return
107
    [ "$DISTRIB_ID" = "VoidLinux" ] && echo "void" && return
108
    [ "$DISTRIB_ID" = "ManjaroLinux" ] && echo "manjaro" && return
109
    [ "$DISTRIB_ID" = "OpenSUSE" ] && echo "suse" && return
110
    [ "$DISTRIB_ID" = "openSUSETumbleweed" ] && echo "suse" && return
111
    [ "$DISTRIB_ID" = "openSUSELeap" ] && echo "suse" && return
112
    if [ -n "$VENDOR_ID" ] ; then
113
        [ "$VENDOR_ID" = "altlinux" ] && echo "alt" && return
114
        echo "$VENDOR_ID"
115
        return
116
    fi
117
    tolower "$DISTRIB_ID"
118
}
119

120
# TODO: in more appropriate way
121
#which pkcon 2>/dev/null >/dev/null && info "You can run $ PMTYPE=packagekit epm to use packagekit backend"
122

123
# Print package manager (need DISTRIB_ID, DISTRIB_RELEASE vars)
124
pkgmanager()
125
{
126
local CMD
127

128
case $VENDOR_ID in
129
    alt)
130
        echo "apt-rpm" && return
131
        ;;
132
    arch|manjaro)
133
        echo "pacman" && return
134
        ;;
135
    debian)
136
        echo "apt-dpkg" && return
137
        ;;
138
esac
139

140
# FIXME: some problems with multibased distros (Server Edition on CentOS and Desktop Edition on Ubuntu)
141
case $DISTRIB_ID in
142
    PCLinux)
143
        CMD="apt-rpm"
144
        ;;
145
    Ubuntu|Debian|Mint|OSNovaLinux|AstraLinux*|Elbrus)
146
        CMD="apt-dpkg"
147
        #which aptitude 2>/dev/null >/dev/null && CMD=aptitude-dpkg
148
        #is_command snappy && CMD=snappy
149
        ;;
150
    Solus)
151
        CMD="eopkg"
152
        ;;
153
    Mandriva)
154
        CMD="urpm-rpm"
155
        ;;
156
    ROSA)
157
        CMD="urpm-rpm"
158
        is_command yum && CMD="yum-rpm"
159
        is_command dnf && CMD="dnf-rpm"
160
        # use dnf since 2020
161
        #[ "$DISTRIB_ID/$DISTRIB_RELEASE" = "ROSA/2020" ] && CMD="urpm-rpm"
162
        ;;
163
    FreeBSD|NetBSD|OpenBSD|Solaris)
164
        CMD="pkgsrc"
165
        is_command pkg && CMD=pkgng
166
        ;;
167
    Gentoo)
168
        CMD="emerge"
169
        ;;
170
    ArchLinux|ManjaroLinux)
171
        CMD="pacman"
172
        ;;
173
    Fedora|CentOS|OracleLinux|RockyLinux|AlmaLinux|RHEL|RELS|Scientific|GosLinux|Amzn|RedOS)
174
        CMD="dnf-rpm"
175
        is_command dnf || CMD="yum-rpm"
176
        [ "$DISTRIB_ID/$DISTRIB_RELEASE" = "CentOS/7" ] && CMD="yum-rpm"
177
        ;;
178
    Slackware)
179
        CMD="slackpkg"
180
        ;;
181
    SUSE|SLED|SLES|openSUSETumbleweed|openSUSELeap)
182
        CMD="zypper-rpm"
183
        ;;
184
    ForesightLinux|rPathLinux)
185
        CMD="conary"
186
        ;;
187
    Windows)
188
        is_command winget && echo "winget" && return
189
        is_command appget && CMD="appget"
190
        is_command choco && CMD="choco"
191
        is_command npackdcl && CMD="npackd"
192
        ;;
193
    MacOS)
194
        CMD="homebrew"
195
        ;;
196
    OpenWrt)
197
        CMD="opkg"
198
        ;;
199
    GNU/Linux/Guix)
200
        CMD="guix"
201
        ;;
202
    NixOS)
203
        CMD="nix"
204
        ;;
205
    Android)
206
        CMD="android"
207
        # TODO: CMD="termux-pkg"
208
        ;;
209
    Cygwin)
210
        CMD="aptcyg"
211
        ;;
212
    AlpineLinux)
213
        CMD="apk"
214
        ;;
215
    TinyCoreLinux)
216
        CMD="tce"
217
        ;;
218
    VoidLinux)
219
        CMD="xbps"
220
        ;;
221
    *)
222
        if is_command "rpm" && [ -s /var/lib/rpm/Name ] || [ -s /var/lib/rpm/rpmdb.sqlite ] ; then
223
            is_command "apt-get" && [ -d /var/lib/apt ] && echo "apt-rpm" && return
224
            is_command "zypper" && echo "zypper-rpm" && return
225
            is_command "dnf" && echo "dnf-rpm" && return
226
            is_command "yum" && echo "yum-rpm" && return
227
            is_command "urpmi" && echo "urpm-rpm" && return
228
        fi
229

230
        if is_command "dpkg" && [ -s /var/lib/dpkg/status ] ; then
231
            is_command "apt" && echo "apt-dpkg" && return
232
            is_command "apt-get" && echo "apt-dpkg" && return
233
        fi
234

235
        echo "pkgmanager(): We don't support yet DISTRIB_ID $DISTRIB_ID (VENDOR_ID $VENDOR_ID)" >&2
236
        ;;
237
esac
238
echo "$CMD"
239
}
240

241
# Print pkgtype (need DISTRIB_ID var)
242
pkgtype()
243
{
244

245
    case $VENDOR_ID in
246
        arch|manjaro)
247
            echo "pkg.tar.xz" && return
248
            ;;
249
    esac
250

251
# TODO: try use generic names
252
    case $(pkgvendor) in
253
        freebsd) echo "tbz" ;;
254
        sunos) echo "pkg.gz" ;;
255
        slackware|mopslinux) echo "tgz" ;;
256
        archlinux|manjaro) echo "pkg.tar.xz" ;;
257
        gentoo) echo "tbz2" ;;
258
        windows) echo "exe" ;;
259
        android) echo "apk" ;;
260
        alpine) echo "apk" ;;
261
        tinycorelinux) echo "tcz" ;;
262
        voidlinux) echo "xbps" ;;
263
        openwrt) echo "ipk" ;;
264
        cygwin) echo "tar.xz" ;;
265
        solus) echo "eopkg" ;;
266
        *)
267
            case $(pkgmanager) in
268
                *-dpkg)
269
                    echo "deb" ;;
270
                *-rpm)
271
                    echo "rpm" ;;
272
                *)
273
                    echo "" ;;
274
            esac
275
    esac
276
}
277

278
print_codename()
279
{
280
    echo "$DISTRIB_CODENAME"
281
}
282

283
print_repo_name()
284
{
285
    echo "$DISTRIB_CODENAME"
286
}
287

288
get_var()
289
{
290
    # get first variable and print it out, drop quotes if exists
291
    grep -i "^$1 *=" | head -n 1 | sed -e "s/^[^=]*[ \t]*=[ \t]*//" | sed -e "s/^[\'\"]\(.*\)[\'\"]/\1/"
292
}
293

294
# 2010.1 -> 2010
295
get_major_version()
296
{
297
    echo "$1" | sed -e "s/\..*//g"
298
}
299

300
normalize_name()
301
{
302
    case "$1" in
303
        "RED OS")
304
            echo "RedOS"
305
            ;;
306
        "Debian GNU/Linux")
307
            echo "Debian"
308
            ;;
309
        "Liya GNU/Linux")
310
            echo "LiyaLinux"
311
            ;;
312
        "CentOS Linux")
313
            echo "CentOS"
314
            ;;
315
        "Fedora Linux")
316
            echo "Fedora"
317
            ;;
318
        "Red Hat Enterprise Linux Server")
319
            echo "RHEL"
320
            ;;
321
        "ROSA Fresh"*|"ROSA Desktop Fresh"*)
322
            echo "ROSA"
323
            ;;
324
        "ROSA Chrome Desktop")
325
            echo "ROSA"
326
            ;;
327
        "MOS Desktop"|"MOS Panel")
328
            echo "ROSA"
329
            ;;
330
        "ROSA Enterprise Linux Desktop")
331
            echo "RELS"
332
            ;;
333
        "ROSA Enterprise Linux Server")
334
            echo "RELS"
335
            ;;
336
        *)
337
            #echo "${1// /}"
338
            #firstupper "$1" | sed -e "s/ //g" -e 's|(.*||'
339
            echo "$1" | sed -e "s/ //g" -e 's|(.*||'
340
            ;;
341
    esac
342
}
343

344
# 1.2.3.4.5 -> 1
345
normalize_version1()
346
{
347
    echo "$1" | sed -e "s|\..*||"
348
}
349

350
# 1.2.3.4.5 -> 1.2
351
normalize_version2()
352
{
353
    echo "$1" | sed -e "s|^\([^.][^.]*\.[^.][^.]*\)\..*|\1|"
354
}
355

356
# 1.2.3.4.5 -> 1.2.3
357
normalize_version3()
358
{
359
    echo "$1" | sed -e "s|^\([^.][^.]*\.[^.][^.]*\.[^.][^.]*\)\..*|\1|"
360
}
361

362

363
fill_distr_info()
364
{
365
# Default values
366
PRETTY_NAME=""
367
DISTRIB_ID=""
368
DISTRIB_RELEASE=""
369
DISTRIB_FULL_RELEASE=""
370
DISTRIB_RELEASE_ORIG=""
371
DISTRIB_CODENAME=""
372
BUG_REPORT_URL=""
373
BUILD_ID=""
374

375
# Default detection by /etc/os-release
376
# https://www.freedesktop.org/software/systemd/man/os-release.html
377
if distro os-release ; then
378
    # shellcheck disable=SC1090
379
    . $DISTROFILE
380
    DISTRO_NAME="$NAME"
381
    DISTRIB_ID="$(normalize_name "$NAME")"
382
    DISTRIB_RELEASE_ORIG="$VERSION_ID"
383
    DISTRIB_RELEASE="$VERSION_ID"
384
    [ -n "$DISTRIB_RELEASE" ] || DISTRIB_RELEASE="CUR"
385
    [ "$BUILD_ID" = "rolling" ] && DISTRIB_RELEASE="rolling"
386
    [ -n "$BUG_REPORT_URL" ] || BUG_REPORT_URL="$HOME_URL"
387
    # set by os-release:
388
    #PRETTY_NAME
389
    VENDOR_ID="$ID"
390
    case "$VENDOR_ID" in
391
        ubuntu|reld|rhel|astra|manjaro)
392
            ;;
393
        *)
394
            [ -n "$ID_LIKE" ] && VENDOR_ID="$(echo "$ID_LIKE" | cut -d" " -f1)"
395
            ;;
396
    esac
397
    DISTRIB_FULL_RELEASE="$DISTRIB_RELEASE"
398
    DISTRIB_CODENAME="$VERSION_CODENAME"
399

400
elif distro lsb-release ; then
401
    DISTRIB_ID=$(cat $DISTROFILE | get_var DISTRIB_ID)
402
    DISTRO_NAME=$(cat $DISTROFILE | get_var DISTRIB_ID)
403
    DISTRIB_RELEASE="$(cat $DISTROFILE | get_var DISTRIB_RELEASE)"
404
    DISTRIB_RELEASE_ORIG="$DISTRIB_RELEASE"
405
    DISTRIB_FULL_RELEASE="$DISTRIB_RELEASE"
406
    DISTRIB_CODENAME=$(cat $DISTROFILE | get_var DISTRIB_CODENAME)
407
    PRETTY_NAME=$(cat $DISTROFILE | get_var DISTRIB_DESCRIPTION)
408
fi
409

410
DISTRIB_RELEASE=$(normalize_version2 "$DISTRIB_RELEASE")
411
[ -n "$DISTRIB_CODENAME" ] || DISTRIB_CODENAME=$DISTRIB_RELEASE
412

413
case "$VENDOR_ID" in
414
    "alt"|"altlinux")
415
        # 2.4.5.99 -> 2
416
        DISTRIB_RELEASE=$(normalize_version1 "$DISTRIB_RELEASE_ORIG")
417
        case "$DISTRIB_ID" in
418
            "ALTServer"|"ALTSPWorkstation"|"Sisyphus")
419
                ;;
420
            *)
421
                DISTRIB_ID="ALTLinux"
422
                ;;
423
        esac
424
        ;;
425
    "astra")
426
        DISTRIB_RELEASE=$(normalize_version2 "$DISTRIB_RELEASE_ORIG" | sed -e 's|_.*||')
427
        DISTRIB_FULL_RELEASE=$(normalize_version3 "$DISTRIB_RELEASE_ORIG" | sed -e 's|_.*||')
428
        if [ "$VARIANT" = "orel" ] || [ "$VARIANT" = "Orel" ] ; then
429
            DISTRIB_ID="AstraLinuxCE"
430
        else
431
            DISTRIB_ID="AstraLinuxSE"
432
        fi
433
        if [ "$DISTRIB_ID" = "AstraLinuxSE" ] ; then
434
            local fr="$(cat /etc/astra_version 2>/dev/null)"
435
            [ -n "$fr" ] && echo "$fr" | grep -q "$DISTRIB_RELEASE" && DISTRIB_FULL_RELEASE="$fr"
436
        fi
437
        ;;
438
esac
439

440
case "$DISTRIB_ID" in
441
    "ALTLinux")
442
        echo "$VERSION" | grep -q "c9.* branch" && DISTRIB_RELEASE="c9"
443
        echo "$VERSION" | grep -q "c9f1 branch" && DISTRIB_RELEASE="c9f1"
444
        echo "$VERSION" | grep -q "c9f2 branch" && DISTRIB_RELEASE="c9f2"
445
        echo "$VERSION" | grep -q "c9f3 branch" && DISTRIB_RELEASE="c9f3"
446
        DISTRIB_CODENAME="$DISTRIB_RELEASE"
447
        # FIXME: fast hack for fallback: 10.1 -> p10 for /etc/os-release
448
        if echo "$DISTRIB_RELEASE" | grep -q "^0" ; then
449
            DISTRIB_RELEASE="Sisyphus"
450
            DISTRIB_CODENAME="$DISTRIB_RELEASE"
451
        elif echo "$DISTRIB_RELEASE" | grep -q "^[0-9]" && echo "$DISTRIB_RELEASE" | grep -q -v "[0-9][0-9][0-9]"  ; then
452
            DISTRIB_CODENAME="$(echo p$DISTRIB_RELEASE | sed -e 's|\..*||')"
453
            # TODO: change p10 to 10
454
            DISTRIB_RELEASE="$DISTRIB_CODENAME"
455
        fi
456
        ;;
457
    "ALTServer")
458
        DISTRIB_ID="ALTLinux"
459
        DISTRIB_CODENAME="$(echo p$DISTRIB_RELEASE | sed -e 's|\..*||')"
460
        # TODO: change p10 to 10
461
        DISTRIB_RELEASE="$DISTRIB_CODENAME"
462
        ;;
463
    "ALTSPWorkstation")
464
        DISTRIB_ID="ALTLinux"
465
        case "$DISTRIB_RELEASE_ORIG" in
466
            8.0|8.1)
467
                ;;
468
            8.2|8.3)
469
                DISTRIB_RELEASE="c9f1"
470
            ;;
471
            8.4)
472
                DISTRIB_RELEASE="c9f2"
473
            ;;
474
            8.*)
475
                DISTRIB_RELEASE="c9f3"
476
            ;;
477
        esac
478
        [ -n "$ALT_BRANCH_ID" ] && DISTRIB_RELEASE="$ALT_BRANCH_ID"
479
        DISTRIB_CODENAME="$DISTRIB_RELEASE"
480
#        DISTRIB_RELEASE=$(echo $DISTRIB_RELEASE | sed -e "s/\..*//g")
481
        ;;
482
    "Sisyphus")
483
        DISTRIB_ID="ALTLinux"
484
        DISTRIB_RELEASE="Sisyphus"
485
        DISTRIB_CODENAME="$DISTRIB_RELEASE"
486
        ;;
487
    "ROSA"|"MOSDesktop"|"MOSPanel")
488
        DISTRIB_FULL_RELEASE="$DISTRIB_CODENAME"
489
        DISTRIB_CODENAME="$DISTRIB_RELEASE"
490
        ;;
491
esac
492

493

494
[ -n "$DISTRIB_ID" ] && [ -n "$DISTRIB_RELEASE" ] && return
495

496

497
# check via obsoleted ways
498

499
# ALT Linux based
500
if distro altlinux-release ; then
501
    DISTRIB_ID="ALTLinux"
502
    # FIXME: fast hack for fallback: 10 -> p10 for /etc/os-release
503
    DISTRIB_RELEASE="$(echo p$DISTRIB_RELEASE | sed -e 's|\..*||' -e 's|^pp|p|')"
504
    if has Sisyphus ; then DISTRIB_RELEASE="Sisyphus"
505
    elif has "ALT p10.* p10 " ; then DISTRIB_RELEASE="p10"
506
    elif has "ALTServer 10." ; then DISTRIB_RELEASE="p10"
507
    elif has "ALTServer 9." ; then DISTRIB_RELEASE="p9"
508
    elif has "ALT c10.* c10 " ; then DISTRIB_RELEASE="c10"
509
    elif has "ALT p9.* p9 " ; then DISTRIB_RELEASE="p9"
510
    elif has "ALT 9 SP " ; then DISTRIB_RELEASE="c9"
511
    elif has "ALT c9f1" ; then DISTRIB_RELEASE="c9f1"
512
    elif has "ALT MED72 " ; then DISTRIB_RELEASE="p8"
513
    elif has "ALT 8 SP " ; then DISTRIB_RELEASE="c8"
514
    elif has "ALT c8.2 " ; then DISTRIB_RELEASE="c8.2"
515
    elif has "ALT c8.1 " ; then DISTRIB_RELEASE="c8.1"
516
    elif has "ALT c8 " ; then DISTRIB_RELEASE="c8"
517
    elif has "ALT .*8.[0-9]" ; then DISTRIB_RELEASE="p8"
518
    elif has "Simply Linux 10." ; then DISTRIB_RELEASE="p10"
519
    elif has "Simply Linux 9." ; then DISTRIB_RELEASE="p9"
520
    elif has "Simply Linux 8." ; then DISTRIB_RELEASE="p8"
521
    elif has "Simply Linux 7." ; then DISTRIB_RELEASE="p7"
522
    elif has "Simply Linux 6." ; then DISTRIB_RELEASE="p6"
523
    elif has "ALT Linux p8"  ; then DISTRIB_RELEASE="p8"
524
    elif has "ALT Linux 8." ; then DISTRIB_RELEASE="p8"
525
    elif has "ALT Linux p7"  ; then DISTRIB_RELEASE="p7"
526
    elif has "ALT Linux 7." ; then DISTRIB_RELEASE="p7"
527
    elif has "ALT Linux t7." ; then DISTRIB_RELEASE="t7"
528
    elif has "ALT Linux 6." ; then DISTRIB_RELEASE="p6"
529
    elif has "ALT Linux p6"  ; then DISTRIB_RELEASE="p6"
530
    elif has "ALT Linux p5"  ; then DISTRIB_RELEASE="p5"
531
    elif has "ALT Linux 5.1" ; then DISTRIB_RELEASE="5.1"
532
    elif has "ALT Linux 5.0" ; then DISTRIB_RELEASE="5.0"
533
    elif has "ALT Linux 4.1" ; then DISTRIB_RELEASE="4.1"
534
    elif has "ALT Linux 4.0" ; then DISTRIB_RELEASE="4.0"
535
    elif has "starter kit"   ; then DISTRIB_RELEASE="Sisyphus"
536
    elif has Citron   ; then DISTRIB_RELEASE="2.4"
537
    fi
538
    PRETTY_NAME="$(cat /etc/altlinux-release)"
539
    DISTRIB_CODENAME="$DISTRIB_RELEASE"
540
    DISTRO_NAME="$DISTRIB_ID"
541
    DISTRIB_FULL_RELEASE="$DISTRIB_RELEASE"
542

543
elif distro gentoo-release ; then
544
    DISTRIB_ID="Gentoo"
545
    MAKEPROFILE=$(readlink $ROOTDIR/etc/portage/make.profile 2>/dev/null) || MAKEPROFILE=$(readlink $ROOTDIR/etc/make.profile)
546
    DISTRIB_RELEASE=$(basename $MAKEPROFILE)
547
    echo $DISTRIB_RELEASE | grep -q "[0-9]" || DISTRIB_RELEASE=$(basename "$(dirname $MAKEPROFILE)") #"
548

549
elif distro slackware-version ; then
550
    DISTRIB_ID="Slackware"
551
    DISTRIB_RELEASE="$(grep -Eo '[0-9]+\.[0-9]+' $DISTROFILE)"
552

553
elif distro os-release && is_command tce-ab ; then
554
    # shellcheck disable=SC1090
555
    . $ROOTDIR/etc/os-release
556
    DISTRIB_ID="TinyCoreLinux"
557
    DISTRIB_RELEASE="$VERSION_ID"
558

559
elif distro os-release && is_command xbps-query ; then
560
    # shellcheck disable=SC1090
561
    . $ROOTDIR/etc/os-release
562
    DISTRIB_ID="VoidLinux"
563
    DISTRIB_RELEASE="Live"
564

565
# TODO: use standart /etc/os-release or lsb
566
elif distro arch-release ; then
567
    DISTRIB_ID="ArchLinux"
568
    DISTRIB_RELEASE="rolling"
569

570
# Elbrus
571
elif distro mcst_version ; then
572
    DISTRIB_ID="MCST"
573
    DISTRIB_RELEASE=$(cat "$DISTROFILE" | grep "release" | sed -e "s|.*release \([0-9]*\).*|\1|g") #"
574

575
# OpenWrt
576
elif distro openwrt_release ; then
577
    . $DISTROFILE
578
    DISTRIB_RELEASE=$(cat $ROOTDIR/etc/openwrt_version)
579

580
# Debian based
581
elif distro debian_version ; then
582
    DISTRIB_ID="Debian"
583
    DISTRIB_RELEASE=$(cat $DISTROFILE | sed -e "s/\..*//g")
584

585

586
# SUSE based
587
elif distro SuSe-release || distro SuSE-release ; then
588
    DISTRIB_ID="SUSE"
589
    DISTRIB_RELEASE=$(cat "$DISTROFILE" | grep "VERSION" | sed -e "s|^VERSION = ||g")
590
    if   has "SUSE Linux Enterprise Desktop" ; then
591
        DISTRIB_ID="SLED"
592
    elif has "SUSE Linux Enterprise Server" ; then
593
        DISTRIB_ID="SLES"
594
    fi
595

596
# fixme: can we detect by some file?
597
elif [ "$(uname)" = "FreeBSD" ] ; then
598
    DISTRIB_ID="FreeBSD"
599
    UNAME=$(uname -r)
600
    DISTRIB_RELEASE=$(echo "$UNAME" | grep RELEASE | sed -e "s|\([0-9]\.[0-9]\)-RELEASE|\1|g") #"
601

602
# fixme: can we detect by some file?
603
elif [ "$(uname)" = "SunOS" ] ; then
604
    DISTRIB_ID="SunOS"
605
    DISTRIB_RELEASE=$(uname -r)
606

607
# fixme: can we detect by some file?
608
elif [ "$(uname -s 2>/dev/null)" = "Darwin" ] ; then
609
    DISTRIB_ID="MacOS"
610
    DISTRIB_RELEASE=$(uname -r)
611

612
# fixme: move to up
613
elif [ "$(uname)" = "Linux" ] && is_command guix ; then
614
    DISTRIB_ID="GNU/Linux/Guix"
615
    DISTRIB_RELEASE=$(uname -r)
616

617
# fixme: move to up
618
elif [ "$(uname)" = "Linux" ] && [ -x $ROOTDIR/system/bin/getprop ] ; then
619
    DISTRIB_ID="Android"
620
    DISTRIB_RELEASE=$(getprop | awk -F": " '/build.version.release/ { print $2 }' | tr -d '[]')
621

622
elif [ "$(uname -o 2>/dev/null)" = "Cygwin" ] ; then
623
        DISTRIB_ID="Cygwin"
624
        DISTRIB_RELEASE="all"
625
fi
626

627
}
628

629
get_uname()
630
{
631
    tolower "$(uname $1)" | tr -d " \t\r\n"
632
}
633

634
get_glibc_version()
635
{
636
    for i in /lib/x86_64-linux-gnu /lib64 /lib/i386-linux-gnu /lib ; do
637
        [ -x "$ROOTDIR$i/libc.so.6" ] && $ROOTDIR$i/libc.so.6 | head -n1 | grep "version" | sed -e 's|.*version ||' -e 's|\.$||' && return
638
    done
639
}
640

641
get_base_os_name()
642
{
643
local DIST_OS
644
# Resolve the os
645
DIST_OS="$(get_uname -s)"
646
case "$DIST_OS" in
647
    'sunos')
648
        DIST_OS="solaris"
649
        ;;
650
    'hp-ux' | 'hp-ux64')
651
        DIST_OS="hpux"
652
        ;;
653
    'darwin' | 'oarwin')
654
        DIST_OS="macosx"
655
        ;;
656
    'unix_sv')
657
        DIST_OS="unixware"
658
        ;;
659
    'freebsd' | 'openbsd' | 'netbsd')
660
        DIST_OS="freebsd"
661
        ;;
662
esac
663
echo "$DIST_OS"
664
}
665

666

667
get_arch()
668
{
669
[ -n "$DIST_ARCH" ] && return 0
670
# Resolve the architecture
671
DIST_ARCH="$(get_uname -m)"
672
case "$DIST_ARCH" in
673
    'ia32' | 'i386' | 'i486' | 'i586' | 'i686')
674
        DIST_ARCH="x86"
675
        ;;
676
    'amd64' | 'x86_64')
677
        DIST_ARCH="x86_64"
678
        ;;
679
    'ia64' | 'ia-64')
680
        DIST_ARCH="ia64"
681
        ;;
682
    'ip27' | 'mips')
683
        DIST_ARCH="mips"
684
        ;;
685
    'powermacintosh' | 'power' | 'powerpc' | 'power_pc' | 'ppc64')
686
        DIST_ARCH="ppc"
687
        ;;
688
    'pa_risc' | 'pa-risc')
689
        DIST_ARCH="parisc"
690
        ;;
691
    'sun4u' | 'sparcv9')
692
        DIST_ARCH="sparc"
693
        ;;
694
    '9000/800')
695
        DIST_ARCH="parisc"
696
        ;;
697
    'arm64' | 'aarch64')
698
        DIST_ARCH='aarch64'
699
        ;;
700
    armv7*)
701
        # TODO: use uname only
702
        # uses binutils package
703
        if is_command readelf && [ -z "$(readelf -A /proc/self/exe | grep Tag_ABI_VFP_args)" ] ; then
704
            DIST_ARCH="armel"
705
        else
706
            DIST_ARCH="armhf"
707
        fi
708
        ;;
709
esac
710
echo "$DIST_ARCH"
711
}
712

713
get_debian_arch()
714
{
715
    local arch="$(get_arch)"
716
    case $arch in
717
    'x86')
718
        arch='i386' ;;
719
    'x86_64')
720
        arch='amd64' ;;
721
    'aarch64')
722
        arch='arm64' ;;
723
    esac
724
    echo "$arch"
725
}
726

727
get_distro_arch()
728
{
729
    local arch="$(get_arch)"
730
    case "$(pkgtype)" in
731
        rpm)
732
            case $arch in
733
            'x86')
734
                arch='i586' ;;
735
            esac
736
            ;;
737
        deb)
738
            get_debian_arch
739
            return
740
            ;;
741
    esac
742
    echo "$arch"
743
}
744

745
get_bit_size()
746
{
747
local DIST_BIT
748

749
DIST_BIT="$(getconf LONG_BIT 2>/dev/null)"
750
if [ -n "$DIST_BIT" ] ; then
751
    echo "$DIST_BIT"
752
    return
753
fi
754

755
# Try detect arch size by arch name
756
case "$(get_uname -m)" in
757
    'amd64' | 'ia64' | 'x86_64' | 'ppc64')
758
        DIST_BIT="64"
759
        ;;
760
    'aarch64')
761
        DIST_BIT="64"
762
        ;;
763
    'e2k')
764
        DIST_BIT="64"
765
        ;;
766
#    'pa_risc' | 'pa-risc') # Are some of these 64bit? Least not all...
767
#       BIT="64"
768
#        ;;
769
    'sun4u' | 'sparcv9') # Are all sparcs 64?
770
        DIST_BIT="64"
771
        ;;
772
#    '9000/800')
773
#       DIST_BIT="64"
774
#        ;;
775
    *) # In any other case default to 32
776
        DIST_BIT="32"
777
        ;;
778
esac
779
echo "$DIST_BIT"
780
}
781

782
# TODO: check before calc
783
get_memory_size()
784
{
785
    local detected=""
786
    local DIST_OS="$(get_base_os_name)"
787
    case "$DIST_OS" in
788
        macosx)
789
            detected=$((`sysctl hw.memsize | sed s/"hw.memsize: "//`/1024/1024))
790
            ;;
791
        freebsd)
792
            detected=$((`sysctl hw.physmem | sed s/"hw.physmem: "//`/1024/1024))
793
            ;;
794
        linux)
795
            [ -r /proc/meminfo ] && detected=$((`cat /proc/meminfo | grep MemTotal | awk '{print $2}'`/1024))
796
            ;;
797
        solaris)
798
            detected=$(prtconf | grep Memory | sed -e "s|Memory size: \([0-9][0-9]*\) Megabyte.*|\1|") #"
799
            ;;
800
#        *)
801
#            fatal "Unsupported OS $DIST_OS"
802
    esac
803

804
    [ -n "$detected" ] || detected=0
805
    echo $detected
806
}
807

808
print_name_version()
809
{
810
    [ -n "$DISTRIB_RELEASE" ] && echo $DISTRIB_ID/$DISTRIB_RELEASE || echo $DISTRIB_ID
811
}
812

813
get_core_count()
814
{
815
    local detected=""
816
    local DIST_OS="$(get_base_os_name)"
817
    case "$DIST_OS" in
818
        macos|freebsd)
819
            detected=$(a= sysctl hw.ncpu | awk '{print $2}')
820
            ;;
821
        linux)
822
            detected=$(grep -c "^processor" /proc/cpuinfo)
823
            ;;
824
        solaris)
825
            detected=$(a= prtconf | grep -c 'cpu[^s]')
826
            ;;
827
        aix)
828
            detected=$(a= lsdev -Cc processor -S A | wc -l)
829
            ;;
830
#        *)
831
#            fatal "Unsupported OS $DIST_OS"
832
    esac
833

834
    [ -n "$detected" ] || detected=0
835
    echo $detected
836
}
837

838
get_core_mhz()
839
{
840
    cat /proc/cpuinfo | grep "cpu MHz" | head -n1 | cut -d':' -f2 | cut -d' ' -f2 | cut -d'.' -f1
841
}
842

843

844
get_virt()
845
{
846
    local VIRT
847
    if is_command systemd-detect-virt ; then
848
        VIRT="$(systemd-detect-virt)"
849
        [ "$VIRT" = "none" ] && echo "(host system)" && return
850
        [ -z "$VIRT" ] && echo "(unknown)" && return
851
        echo "$VIRT" && return
852
    fi
853

854
    # TODO: use virt-what under root
855

856
    # inspired by virt_what
857
    if [ -d "/proc/vz" -a ! -d "/proc/bc" ]; then
858
        echo "openvz" && return
859
    fi
860

861
    if [ -r "/sys/bus/xen" ] ; then
862
        echo "xen" && return
863
    fi
864

865
    # use util-linux
866
    if LC_ALL=C a= lscpu | grep "Hypervisor vendor:" | grep -q "KVM" ; then
867
        echo "kvm" && return
868
    fi
869

870
    echo "(unknown)"
871
    # TODO: check for openvz
872
}
873

874
get_init_process_name()
875
{
876
    [ ! -f /proc/1/comm ] && echo "(unknown)" && return 1
877
    cat /proc/1/comm | head -n1
878
    #ps --no-headers -o comm 1
879
}
880

881
# https://unix.stackexchange.com/questions/196166/how-to-find-out-if-a-system-uses-sysv-upstart-or-systemd-initsystem
882
get_service_manager()
883
{
884
    [ -d /run/systemd/system ] && echo "systemd" && return
885
    # TODO
886
    #[ -d /usr/share/upstart ] && echo "upstart" && return
887
    is_command systemctl && [ "$(get_init_process_name)" = 'systemd' ] && echo "systemd" && return
888
    [ -d /etc/init.d ] && echo "sysvinit" && return
889
    get_init_process_name
890
}
891

892
filter_duplicated_words()
893
{
894
    echo "$*" | xargs -n1 echo | uniq | xargs -n100 echo
895
}
896

897
print_pretty_name()
898
{
899
    if [ -z "$PRETTY_NAME" ] ; then
900
        PRETTY_NAME="$DISTRIB_ID $DISTRIB_RELEASE"
901
    fi
902

903
    if ! echo "$PRETTY_NAME" | grep -q "$DISTRIB_FULL_RELEASE" ; then
904
        PRETTY_NAME="$PRETTY_NAME ($DISTRIB_FULL_RELEASE)"
905
    fi
906

907
    if ! echo "$PRETTY_NAME" | grep -q "$DISTRIB_RELEASE" ; then
908
        PRETTY_NAME="$PRETTY_NAME ($DISTRIB_RELEASE)"
909
    fi
910

911
    echo "$(filter_duplicated_words "$PRETTY_NAME")"
912
}
913

914
print_total_info()
915
{
916
local orig=''
917
[ -n "$BUILD_ID" ] && [ "$DISTRIB_FULL_RELEASE" != "$BUILD_ID" ] && orig=" (orig. $BUILD_ID)"
918
local EV=''
919
[ -n "$EPMVERSION" ] && EV="(EPM version $EPMVERSION) "
920
cat <<EOF
921
distro_info v$PROGVERSION $EV: Copyright © 2007-2024 Etersoft
922

923
                       Pretty name (--pretty): $(print_pretty_name)
924
           (--distro-name / --distro-version): $DISTRO_NAME / $DISTRIB_FULL_RELEASE$orig
925
         Base distro name (-d) / version (-v): $(print_name_version)
926
     Vendor distro name (-s) / Repo name (-r): $(pkgvendor) / $(print_repo_name)
927
                 Package manager/type (-g/-p): $(pkgmanager) / $(pkgtype)
928
            Base OS name (-o) / CPU arch (-a): $(get_base_os_name) $(get_arch)
929
                 CPU norm register size  (-b): $(get_bit_size) bit
930
                          Virtualization (-i): $(get_virt)
931
                        CPU Cores/MHz (-c/-z): $(get_core_count) / $(get_core_mhz) MHz
932
                      System memory size (-m): $(get_memory_size) MiB
933
                 Running service manager (-y): $(get_service_manager)
934
            Bug report URL (--bug-report-url): $(print_bug_report_url)
935

936
(run with -h to get help)
937
EOF
938
}
939

940
print_help()
941
{
942
    echo "distro_info v$PROGVERSION - distro information retriever"
943
    echo "Usage: distro_info [options] [SystemName/Version]"
944
    echo "Options:"
945
    echo " -h | --help            - this help"
946
    echo " -a                     - print hardware architecture (use --distro-arch for distro depended arch name)"
947
    echo " -b                     - print size of arch bit (32/64)"
948
    echo " -c                     - print number of CPU cores"
949
    echo " -i                     - print virtualization type"
950
    echo " -m                     - print system memory size (in MB)"
951
    echo " -y|--service-manager   - print running service manager"
952
    echo " -z                     - print current CPU MHz"
953
    echo " --glibc-version        - print system glibc version"
954
    echo
955
    echo " -d|--base-distro-name  - print distro id (short distro name)"
956
    echo " -e                     - print full name of distro with version"
957
    echo " -o | --os-name         - print base OS name"
958
    echo " -p | --package-type    - print type of the packaging system (f.i., apt-dpkg)"
959
    echo " -g                     - print name of the packaging system (f.i., deb)"
960
    echo " -s|-n|--vendor-name    - print name of the distro family (vendor name) (ubuntu for all Ubuntu family, alt for all ALT family) (see _vendor macros in rpm)"
961
    echo " --pretty|--pretty-name - print pretty distro name"
962
    echo " -v | --base-version    - print version of the distro"
963
    echo " --distro-name          - print distro name"
964
    echo " --distro-version       - print full version of the distro"
965
    echo " --full-version         - print full version of the distro"
966
    echo " --codename (obsoleted) - print distro codename (focal for Ubuntu 20.04)"
967
    echo " -r|--repo-name         - print repository name (focal for Ubuntu 20.04)"
968
    echo " --build-id             - print a string uniquely identifying the system image originally used as the installation base"
969
    echo " -V                     - print the utility version"
970
    echo "Run without args to print all information."
971
}
972

973
# print code for eval with names for eepm
974
print_eepm_env()
975
{
976
cat <<EOF
977
# -d | --base-distro-name
978
DISTRNAME="$(echo $DISTRIB_ID)"
979
# -v | --base-version
980
DISTRVERSION="$(echo "$DISTRIB_RELEASE")"
981
# distro dependent arch
982
DISTRARCH="$(get_distro_arch)"
983
# -s | --vendor-name
984
BASEDISTRNAME=$(pkgvendor)
985
# --repo-name
986
DISTRREPONAME=$(print_repo_name)
987

988
# -a
989
SYSTEMARCH="$(get_arch)"
990
# -y | --service-manager
991
DISTRCONTROL="$(get_service_manager)"
992
# -g
993
PMTYPE="$(pkgmanager)"
994
# -p | --package-type
995
PKGFORMAT=$(pkgtype)
996
# -m
997
DISTRMEMORY="$(get_memory_size)"
998

999
# TODO: remove?
1000
PKGVENDOR=$(pkgvendor)
1001
RPMVENDOR=$(pkgvendor)
1002

1003
EOF
1004

1005
}
1006

1007
override_distrib "$DISTRNAMEOVERRIDE"
1008

1009
if [ -n "$*" ] ; then
1010
    eval lastarg=\${$#}
1011
    case "$lastarg" in
1012
        -*)
1013
            ;;
1014
        *)
1015
            override_distrib "$lastarg"
1016
            # drop last arg
1017
            set -- "${@:1:$(($#-1))}"
1018
            ;;
1019
    esac
1020
fi
1021

1022
# if without override
1023
if [ -z "$DISTRIB_ID" ] ; then
1024
    fill_distr_info
1025
    [ -n "$DISTRIB_ID" ] || DISTRIB_ID="Generic"
1026
fi
1027

1028
if [ -z "$1" ] ; then
1029
    print_total_info
1030
    exit
1031
fi
1032

1033
while [ -n "$1" ] ; do
1034
case "$1" in
1035
    -h|--help)
1036
        print_help
1037
        exit 0
1038
        ;;
1039
    -p|--package-type)
1040
        pkgtype
1041
        ;;
1042
    -g)
1043
        pkgmanager
1044
        ;;
1045
    --pretty|--pretty-name)
1046
        print_pretty_name
1047
        ;;
1048
    --distro-arch)
1049
        get_distro_arch
1050
        ;;
1051
    --debian-arch)
1052
        get_debian_arch
1053
        ;;
1054
    --glibc-version)
1055
        get_glibc_version
1056
        ;;
1057
    -d|--base-distro-name)
1058
        echo $DISTRIB_ID
1059
        ;;
1060
    --distro-name)
1061
        echo $DISTRO_NAME
1062
        ;;
1063
    --codename)
1064
        print_codename
1065
        ;;
1066
    -a)
1067
        if [ -n "$DIST_ARCH" ] ; then
1068
            echo "$DIST_ARCH"
1069
        else
1070
            get_arch
1071
        fi
1072
        ;;
1073
    -b)
1074
        get_bit_size
1075
        ;;
1076
    -c)
1077
        get_core_count
1078
        ;;
1079
    -z)
1080
        get_core_mhz
1081
        ;;
1082
    -i)
1083
        get_virt
1084
        ;;
1085
    -m)
1086
        get_memory_size
1087
        ;;
1088
    -o|--os-name)
1089
        get_base_os_name
1090
        ;;
1091
    -r|--repo-name)
1092
        print_repo_name
1093
        ;;
1094
    --build-id)
1095
        echo "$BUILD_ID"
1096
        ;;
1097
    -v|--base-version)
1098
        echo "$DISTRIB_RELEASE"
1099
        ;;
1100
    --full-version|--distro-version)
1101
        echo "$DISTRIB_FULL_RELEASE"
1102
        ;;
1103
    --bug-report-url)
1104
        print_bug_report_url
1105
        ;;
1106
    -s|-n|--vendor-name)
1107
        pkgvendor
1108
        ;;
1109
    -y|--service-manager)
1110
        get_service_manager
1111
        ;;
1112
    -V)
1113
        echo "$PROGVERSION"
1114
        ;;
1115
    -e)
1116
        print_name_version
1117
        ;;
1118
    --print-eepm-env)
1119
        print_eepm_env
1120
        exit 0
1121
        ;;
1122
    -*)
1123
        echo "Unsupported option $1" >&2
1124
        # print empty line in any case
1125
        echo
1126
        exit 1
1127
        ;;
1128
esac
1129
shift
1130
done
1131

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.