1. 目录结构

dongfeng@worker:~/work/5G/5gc-c-build$ tree
.
├── build.sh
├── README.md
└── source
    ├── 5gc-c.service
    ├── amf
    │   └── amf1
    │       ├── bin
    │       │   ├── amf -> amf_1.0.0
    │       │   ├── amf_1.0.0
    │       │   ├── lo.bin
    │       │   └── ls.bin
    │       ├── config
    │       │   ├── amf.conf
    │       │   └── asn.log.properties
    │       ├── log
    │       │   └── amf.log
    │       └── xml
    │           ├── amfInfo.xml
    │           ├── ausf.xml
    │           ├── guami.xml
    │           ├── nrf.xml
    │           ├── paging-policy.xml
    │           ├── pcf-am-policy-data.xml
    │           ├── pcf-ue.xml
    │           ├── udm-am-subscription-data.xml
    │           ├── udm-smf-selection.xml
    │           └── udm-ue.xml
    ├── ausf
    │   └── ausf1
    │       ├── bin
    │       │   ├── ausf -> ausf_1.0.0
    │       │   ├── ausf_1.0.0
    │       │   ├── lo.bin
    │       │   └── ls.bin
    │       ├── config
    │       │   ├── asn.log.properties
    │       │   └── ausf.conf
    │       ├── log
    │       │   └── ausf.log
    │       └── xml
    │           ├── ausf-example.xml
    │           ├── guami-example.xml
    │           ├── nrf-example.xml
    │           ├── pcf-am-policy-data-example.xml
    │           ├── pcf-ue-example.xml
    │           ├── udm-am-subscription-data-example.xml
    │           ├── udm-smf-seleciont-example.xml
    │           └── udm-ue-example.xml
    ├── bin-sym
    │   ├── amf.sym
    │   ├── ausf.sym
    │   ├── pcf.sym
    │   ├── smf.sym
    │   └── udm.sym
    ├── init.sh
    ├── lib
    │   ├── libev.so.3
    │   ├── liblog4cplus.so.2
    │   └── libusrsctp.so.1
    ├── licensefile
    │   ├── readme
    │   └── register
    ├── pcf
    │   └── pcf1
    │       ├── bin
    │       │   ├── lo.bin
    │       │   ├── ls.bin
    │       │   ├── pcf -> pcf_1.0.0
    │       │   └── pcf_1.0.0
    │       ├── config
    │       │   ├── asn.log.properties
    │       │   └── pcf.conf
    │       ├── context
    │       │   ├── AM_PolicyAsso_Context_DB_
    │       │   │   └── 282574488338534.ctx
    │       │   ├── SM_PolicyAsso_Context_
    │       │   │   └── 282574488338534_1.ctx
    │       │   ├── SM_PolicyAsso_Context_DB_
    │       │   │   └── 317101356768825601_1.ctx
    │       │   ├── SM_PolicyDecision_DB
    │       │   │   └── 1_default_81.ctx
    │       │   └── SystemVersionDB
    │       │       └── 1.ctx
    │       ├── data
    │       │   └── statistics
    │       │       ├── wf0
    │       │       ├── wf1
    │       │       ├── wf2
    │       │       ├── wf3
    │       │       └── wf4
    │       ├── log
    │       │   └── pcf.log
    │       ├── startup.sh
    │       └── xml
    │           ├── af-applicationdata-example.xml
    │           ├── am-policy-data.xml
    │           ├── am-ue-policy.xml
    │           ├── predefined-sm-policy-rules.xml
    │           ├── sm-policy-data.xml
    │           └── udr-policy-subscription-example.xml
    ├── run5gc.sh
    ├── smf
    │   └── smf1
    │       ├── bin
    │       │   ├── lo.bin
    │       │   ├── ls.bin
    │       │   ├── smf -> smf_1.0.0
    │       │   └── smf_1.0.0
    │       ├── config
    │       │   ├── asn.log.properties
    │       │   └── smf.conf
    │       ├── log
    │       │   └── smf.log
    │       ├── startup.sh
    │       └── xml
    │           ├── dnn.xml
    │           ├── pfd.xml
    │           ├── sm-policy-data.xml
    │           ├── udm.xml
    │           └── upf.xml
    ├── udm
    │   └── udm1
    │       ├── bin
    │       │   ├── lo.bin
    │       │   ├── ls.bin
    │       │   ├── udm -> udm_1.0.0
    │       │   └── udm_1.0.0
    │       ├── config
    │       │   ├── asn.log.properties
    │       │   └── udm.conf
    │       ├── log
    │       │   └── udm.log
    │       └── xml
    │           ├── guami.xml
    │           ├── pcf-am-policy-data.xml
    │           ├── pcf-ue.xml
    │           ├── udm-am-subscription-data.xml
    │           ├── udm-session-management-subscription-data.xml
    │           ├── udm-smf-selection.xml
    │           ├── udm-snssai.xml
    │           ├── udm-ue-100k.xml
    │           └── udm-ue-key-100k.xml
    └── watchdog.sh

42 directories, 101 files

2. build.sh

#!/bin/bash -

install_path=/root/baicells

function usage() {
    cat << EOF

----------------------------------------------------
Baicells 5gc-c Builder.

The script can build Baicells 5gc-c related software.

Warning: you should use root to build. 

Usage: ./build.sh [options] [args]

Options:
  -h --help      Show this help
  -r --release   set the release of the deb
  -v --version   set the version of the deb
  -c --control   build 5gc-c deb
  -l --lib       build 5gc-c-lib deb

---------
Examples:
--

Print help message.
# ./build.sh -h

--

Build.

Build specified version of 5gc-c deb:
# ./build.sh -c -v 2.0.0

Build specified release of 5gc-c deb:
# ./build.sh -c -r 2

Build specified version and release of 5gc-c deb:
# ./build.sh -c -v 2.0.0 -r 2

Build specified version of 5gc-c-lib deb:
# ./build.sh -l -v 2.0.0

Build specified release of 5gc-c-lib deb:
# ./build.sh -l -r 2

Build specified version and release of 5gc-c-lib deb:
# ./build.sh -l -v 2.0.0 -r 2

Build specified version and release of 5gc-c and 5gc-c-lib deb:
# ./build.sh -cl -v 2.0.0 -r 2

note: if version or release not be specified,they will be "1.0.0" or "1" by default.
--


EOF
exit 0
}



function install_deps() {
    if [[ ! $(which fpm) ]];then
        apt-get install -y ruby rubygems ruby-dev
        #gem sources --add https://gems.ruby-china.org/ --remove https://rubygems.org/
        #gem sources -l
        gem install fpm
    fi
}


function build_5gc_c() {
    VERSION=$1
    RELEASE=$2
    sed -i "s#\(INSTALL_PATH=\).*#\1$install_path#" "source"/runngc.sh
    sed -i "s#\(ExecStart=\).*#\1$install_path/init.sh#" "source"/5gc-c.service
    fpm -f -s dir -t deb -C "source" -n "baicells-5gc-c" -v "$VERSION" --iteration "$RELEASE" -p . \
	    --verbose \
	    --description "BAICELLS-5GC-C" \
	    --maintainer "dongfeng@baicells.com" \
	    --vendor "baicells" \
	    --deb-no-default-config-files \
	    --config-files $install_path/amf/amf1/config/ \
	    --config-files $install_path/amf/amf1/xml/ \
	    --config-files $install_path/amf/amf1/bin/lo.bin \
	    --config-files $install_path/amf/amf1/bin/ls.bin \
	    --config-files $install_path/smf/smf1/config/ \
	    --config-files $install_path/smf/smf1/xml/ \
	    --config-files $install_path/smf/smf1/bin/lo.bin \
	    --config-files $install_path/smf/smf1/bin/ls.bin \
	    --config-files $install_path/ausf/ausf1/config/ \
	    --config-files $install_path/ausf/ausf1/xml/ \
	    --config-files $install_path/ausf/ausf1/bin/lo.bin \
	    --config-files $install_path/ausf/ausf1/bin/ls.bin \
	    --config-files $install_path/udm/udm1/config/ \
	    --config-files $install_path/udm/udm1/xml/ \
	    --config-files $install_path/udm/udm1/bin/lo.bin \
	    --config-files $install_path/udm/udm1/bin/ls.bin \
	    --config-files $install_path/pcf/pcf1/config/ \
	    --config-files $install_path/pcf/pcf1/xml/ \
	    --config-files $install_path/pcf/pcf1/bin/lo.bin \
	    --config-files $install_path/pcf/pcf1/bin/ls.bin \
	    amf=$install_path/ \
	    smf=$install_path/ \
	    ausf=$install_path/ \
	    udm=$install_path/ \
	    pcf=$install_path/ \
	    licensefile=$install_path/ \
	    run5gc.sh=$install_path/ \
	    init.sh=$install_path/ \
	    watchdog.sh=$install_path/ \
	    5gc-c.service=/lib/systemd/system/
}

function build_5gc_c_lib() {
    VERSION=$1
    RELEASE=$2
    fpm -f -s dir -t deb -n "baicells-5gc-c-lib" -v "$VERSION" --iteration "$RELEASE" -C "source" -p . \
	    --verbose \
	    --description "BAICELLS-5GC-C-LIB" \
	    --maintainer "dongfeng@baicells.com" \
	    --vendor "baicells" \
	    --deb-no-default-config-files \
	    lib=$install_path/	
}



OPTION=$(whiptail --title "5GC Builder" --nocancel --menu "Choose your option" 15 60 4 \
"1" "Build 5gc-c" \
"2" "Build 5gc-c lib" \
"3" "Build 5gc-c and lib" \
"4" "EXIT"  3>&1 1>&2 2>&3)

case $OPTION in
    1)
	version=$(whiptail \
		--title "Version Input Box" \
		--inputbox "Input 5gc-c version(#default:1.0.0)" 15 60 "" \
		3>&1 1>&2 2>&3)
	release=$(whiptail \
		--title "Release Input Box" \
		--inputbox "Input 5gc-c release(#default:1)" 15 60 "" \
		3>&1 1>&2 2>&3)
        version=${version:-"1.0.0"}
        release=${release:-"1"}
	{
	    build_5gc_c $version $release &>/dev/null &
	    for i in {1..100};do
                sleep 0.1
                echo $i
            done
	} | whiptail --gauge "Building \"baicells-5gc-c_$version-$release-amd64.deb\",please wait..." 6 70 0 \
		&& whiptail --title "Build success note!!!" \
		--msgbox "Congratulations! the package(s) is(are) builded successful~~~" 10 60 
        ;;
    2)
	version=$(whiptail \
		--title "Version Input Box" \
		--inputbox "Input 5gc-c lib version(#default:1.0.0)" 15 60 "" \
		3>&1 1>&2 2>&3)
	release=$(whiptail \
		--title "Release Input Box" \
		--inputbox "Input 5gc-c lib release(#default:1)" 15 60 "" \
		3>&1 1>&2 2>&3)
        version=${version:-"1.0.0"}
        release=${release:-"1"}
        {
	    build_5gc_c_lib $version $release &>/dev/null &
            for i in {1..100};do
                sleep 0.1
                echo $i
            done
        } | whiptail --gauge "Building \"baicells-5gc-c-lib_$version-$release-amd64.deb\",please wait..." 6 70 0 \
                && whiptail --title "Build success note!!!" \
                --msgbox "Congratulations! the package(s) is(are) builded successful~~~" 10 60
        ;;
    3)
	version=$(whiptail \
		--title "Version Input Box" \
		--inputbox "Input 5gc-c and lib version(#default:1.0.0)" 15 60 "" \
		3>&1 1>&2 2>&3)
	release=$(whiptail \
		--title "Release Input Box" \
		--inputbox "Input 5gc-c and lib release(#default:1)" 15 60 "" \
		3>&1 1>&2 2>&3)
        version=${version:-"1.0.0"}
        release=${release:-"1"}
        {
	    build_5gc_c $version $release &>/dev/null &
	    build_5gc_c_lib $version $release &>/dev/null &
            for i in {1..100};do
                sleep 0.15
                echo $i
            done
        } | whiptail --gauge "Building \"baicells-5gc-c(-lib)_$version-$release-amd64.deb\",please wait..." 6 70 0 \
                && whiptail --title "Build success note!!!" \
                --msgbox "Congratulations! the package(s) is(are) builded successful~~~" 10 60
        ;;
    4)
	exit 0
        ;;
	
esac





#ARGS=`getopt -o hr:v:cl --long help,release:,version:,control,lib, -- "$@"`
#if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
#eval set -- "$ARGS"
#while true;do
#    case "$1" in
#        -r|--release)
#            release=$2
#            shift 2
#            ;;
#        -v|--version)
#            version=$2
#            shift 2
#            ;;
#        -c|--control)
#	    b1=build_5gc_c
#            shift
#            ;;
#        -l|--lib)
#            b2=build_5gc_c_lib
#            shift
#            ;;
#        -h|--help)
#            usage
#            shift
#            ;;
#        --)
#            shift
#            break
#            ;;
#        *)
#            echo "unknown:{$1}"
#            exit 1
#            ;;
#    esac
#done
#
#version=${version:-"1.0.0"}
#release=${release:-"1"}
#
#for fn in $b1 $b2;do
#    if [[ -n $fn ]];then
#        $fn $version $release
#    fi
#done

3. 5gc-c.service

[Unit]
Description=BAICELLS-5GC-C
Requires=network.target multi-user.target
After=network.target multi-user.target
#Before=vpp.service

[Service]
Type=forking
ExecStart=/root/baicells/init.sh
#ExecStop=

[Install]
WantedBy=multi-user.target

4. init.sh

#!/bin/bash -

INSTALL_PATH="/root/baicells"
#network_script="$INSTALL_PATH/kernel.sh"
main_process="$INSTALL_PATH/run5gc.sh"
watchdog="$INSTALL_PATH/watchdog.sh"

function main() {
    #$network_script
    #systemctl start vpp
    $main_process stop_start
    $watchdog &
};main

5. run5gc.sh

#!/bin/bash -

INSTALL_PATH=/root/baicells
action=$1
module_take=${2:-"amf smf ausf udm"}
param=$*
param_num=$#

function version() {
    for module in $module_take
    do	    
        binary_link=$(readlink $INSTALL_PATH/$module/${module}1/bin/$module)
        echo "Current $module binary is linked to $binary_link."
        if [ -x $INSTALL_PATH/$module/${module}1/bin/$module ]; then
            echo "";
        else
            echo "debug:"$($INSTALL_PATH/$module/${module}1/bin/$module)
            echo "The $module binary is NOT executable!!!";
            return 1;
        fi
    done
}

function init_net() {
    if [[ -f network.conf ]];then
        while read line
        do
            if [[ "${line:0:1}" != "#" ]]; then
                [[ -z "$line" ]] && continue
                $line 2>&1>/dev/null
            fi
        done < network.conf
    fi
}

function init_system() {
    ulimit -c unlimited
    export LD_LIBRARY_PATH=${INSTALL_PATH}/lib
}

check_param() {
    if [ $param_num != 1 ] && [ $param_num != 2 ]; then
        echo -e "\033[31mInput error.\033[0m"
        echo -e "\033[31mUsage: $0 {version|start|stop|status|restart} [Optional: amf|smf].\033[0m"
        exit 1
    else
        if [ "$action" != "start" ] && [ "$action" != "stop" ] && [ "$action" != "status" ] \
		&& [ "$action" != "restart" ] && [ "$action" != "stop_start" ] && [ "$action" != "version" ]; then
            echo -e "\033[31mError action: $action\033[0m"
            echo -e "\033[31mUsage: $0 {version|start|stop|status|restart} [Optional: amf|smf].\033[0m"
            exit 2
        fi
        if  [ -n "$module" ]; then
            if [ "$module" != "amf" ] && [ "$module" != "smf" ]; then
                echo -e "\033[31mError module: $module\033[0m"
                echo -e "\033[31mUsage: $0 {version|start|stop|status|restart} [Optional: amf|smf].\033[0m"
                exit 3
            fi
        fi
    fi
}

function start_module() {
    for module in $module_take
    do
        cd $INSTALL_PATH/$module/${module}1
        bin/$module -s -c config/$module.conf -l config/asn.log.properties -t >/dev/null 2>&1 &
    done
}

function stop_module() {
    for module in $module_take
    do
    	pkill -9 $module
    	sleep 1
    done
}

function status_module() {
    for module in $module_take
    do
        if [ -n "$(pgrep $module)" ]; then
	    echo "$module is running..."
	else
	    echo "$module is NOT running..."
        fi
    done
}

function init() {
    init_system
    init_net
}

function main() {
    check_param $param
    case $action in
	version)
            version
	    echo ""
	    ;;
	stop_start)
	    init
            stop_module
            start_module
	    #status_module
	    #echo
            ;;	    
        start|restart)
	    systemctl restart 5gc-c
	    ;;
        stop)
	    echo "Stop $module_take..."
	    #stop_module
	    systemctl stop 5gc-c
	    ;;
	status)
            status_module
	    ;;
	*)
	    echo -e "\033[31mError action: $action\033[0m"
	    echo -e "\033[31mUsage: $0 {start|stop|status|restart} [Optional: amf|smf].\033[0m"
	    exit
	    ;;
   esac
};main

6. watchdog.sh

#!/usr/bin/env bash

INSTALL_PATH="/root/baicells"
module_all=${1:-"amf smf ausf udm"}
timer=3
log_num=6

function init_system() {
    ulimit -c unlimited
    export LD_LIBRARY_PATH=${INSTALL_PATH}/lib
}

function ngc_ps() {
    local restart_ngc="false"
    for module in $module_all
    do
        cur_path=$INSTALL_PATH/$module/${module}1
        filename=$cur_path/bin/lo.bin
	if [ $(stat -c %s $filename) -ne 0 ];then
            pidof $module >/dev/null
            if [[ $? == 1 ]];then
                restart_ngc="true"
                break
            fi
        fi
    done
    if [[ $restart_ngc == "true" ]];then
	echo "$(date +%Y-%m-%d' '%H:%M:%S) ngc restart"
        $INSTALL_PATH/run5gc.sh stop_start
    fi
}

function ngc_log() {
    for module in $module_all
    do 
        cur_path=$INSTALL_PATH/$module/${module}1
        ls -lt $cur_path/log/$module.log* 2>/dev/null| \
        awk -v log_num=$log_num '{if(NR>log_num){print $9}}'|xargs rm -rf
    done
}

function core_log() {
    for module in $module_all
    do
        ls -lt /root/coredump/core-${module}* 2>/dev/null |awk -v log_num=$log_num '{if(NR>log_num){print $9}}'|xargs rm -rf
    done
}

function main() {
    init_system
    while :
    do
        ngc_ps
        #ngc_log
        core_log
        sleep $timer
    done
};main