The officially official Devuan Forum!

You are not logged in.

#1 Yesterday 20:13:47

tom
Member
Registered: 2022-02-03
Posts: 17  

[SOLVED] machine-id

Hi,

Because of the bad Windows GDID, questions about the "machine-id" came out.   For me, i build a tiny small deb-paket for this, but only one init-script (for sysvrc) is needed.

  $ cat delete-maschine-id
#!/bin/sh
### BEGIN INIT INFO
# Provides:     delete-maschine-id
# Required-Start:
# Required-Stop:
# Default-Start:
# Default-Stop:     0 6
# Short-Description:    Remove machine-id at shutdown
# Description:          Deletes /etc/machine-id and /var/lib/dbus/machine-id
#                       so a new unique ID is generated at next boot.
### END INIT INFO

do_stop() {
    echo "removing machine-id"
    if [ -f /etc/machine-id ]; then
        rm -f /etc/machine-id
    fi
    rm -f /var/lib/dbus/machine-id
}

case "$1" in
    start)  echo "invalid option" ;;
    stop)   do_stop ;;
    *)      do_stop ;;
esac

exit 0

works for me.  When shutting down, the machine-id is removed, after restart dbus creates a new one.

EDIT:
Found it myself in /etc/default/dbus

# IDTYPE: how to deal with /var/lib/dbus/machine-id:
#
# if IDTYPE="RANDOM": regenerate /var/lib/dbus/machine-id at each boot
# else keep it fixed across reboots
IDTYPE="RANDOM"

Sorry for your time to read this, an i can delete my initscript.  smile

Last edited by tom (Yesterday 20:56:47)

Offline

#2 Yesterday 21:05:31

fsmithred
Administrator
Registered: 2016-11-25
Posts: 2,950  

Re: [SOLVED] machine-id

In devuan, /var/lib/dbus/machine-id gets replaced on every boot and /etc/machine-id does not exist. (It might be there if you migrated a debian install to devuan, but I'm not certain of that.) See /etc/default/dbus to turn that on or off.

You're not removing the files in your home directory (in ~/.dbus/session-bus/*) and neither does the default devuan installation.  See https://dev1galaxy.org/viewtopic.php?pid=58117#p58117

I just have a single line in /etc/rc.local to remove the ones in my home so they get removed before I log in, and then a new one is created matching the one in /var/lib/dbus.

Offline

#3 Today 19:44:35

EDX-0
Member
From: Guatemala
Registered: 2020-12-12
Posts: 252  

Re: [SOLVED] machine-id

well i've been messing some with greenjeans' initscript for my own machine-id initscript:

#!/bin/sh
### BEGIN INIT INFO
# Provides:          machine-id
# Required-Start:    $local_fs
# Required-Stop:     $local_fs
# Should-Start:      
# X-Start-Before:    dbus
# Default-Start:     2 3 4 5
# Default-Stop:      0 6
# Short-Description: Handle flexible machine-id modes and session cleanup
# Description:       Controls volatile or static machine-id layout variations
#                    and purges accumulated session bus files on shutdown.
### END INIT INFO

PATH=/sbin:/usr/sbin:/bin:/usr/bin
NAME=machine-id
DESC="Flexible machine-id and session management"

# Source the standard Debian/Devuan LSB logging utility library
. /lib/lsb/init-functions

CONFIG="/etc/default/${NAME}"
# Source configuration overrides if the file exists
if [ -f "$CONFIG" ]; then
    . "$CONFIG"
fi

EtcMachineID=/etc/machine-id
RunMachineID=/run/machine-id
BaseDbusSessDir=".dbus/session-bus"

clean_dir() {
    local target_dir="$1"
    if [ -d "$target_dir" ] && [ "$(echo "$target_dir"/*)" != "$target_dir/*" ]; then
        if [ "$DRYRUN" = "yes" ]; then
            for item in "$target_dir"/*; do
                log_action_msg "Dry-run: Would remove $item"
            done
        else
            rm -f -- "$target_dir"/*
        fi
    fi
}

fallback_machine_id_gen() {
    local mac_hex=""
    local needed_chars=32
    local machine_id_file="$1"

    local BaseNetDir="/sys/class/net"
    local net_dir
    local raw_mac
    # Only attempt MAC acquisition if explicitly enabled by the administrator
    if [ "$USE_MAC_FALLBACK" = "yes" ]; then
        for net_dir in "$BaseNetDir"/*; do
            if [ -d "$net_dir" ] && [ "${net_dir##*/}" != "lo" ] && [ -f "$net_dir/address" ]; then
                read -r raw_mac < "$net_dir/address"
                mac_hex=$(printf '%s' "$raw_mac" | tr -d ':' | tr '[:upper:]' '[:lower:]')
                if [ ${#mac_hex} -eq 12 ] && [ "$mac_hex" != "000000000000" ]; then
                    break
                fi
                mac_hex=""
            fi
        done
    fi

    # Calculate remainder space (defaults to 32 if MAC loop was bypassed or empty)
    needed_chars=$((32 - ${#mac_hex}))

    # Write out any discovered MAC slice first, then append random entropy
    printf "%s" "$mac_hex" > "$machine_id_file"
    tr -dc 'a-f0-8' < /dev/urandom 2>/dev/null | head -c "$needed_chars" >> "$machine_id_file"
    printf "\n" >> "$machine_id_file"
}

machine_id_gen() {
    local target_file="$1"
    
    if command -v dbus-uuidgen >/dev/null 2>&1; then
        dbus-uuidgen --ensure="$target_file"
    else
        fallback_machine_id_gen "$target_file"
    fi
}

do_start() {
    # VOLATILE MODE ENGINE
    if [ -n "$VOLATILE_MACHINE_ID" ] && [ "$VOLATILE_MACHINE_ID" != "no" ]; then
        log_daemon_msg "Setting up volatile machine-id" "$NAME"

        if [ ! -L "$EtcMachineID" ] || [ "$(readlink "$EtcMachineID")" != "$RunMachineID" ]; then
            rm -f "$EtcMachineID"
            ln -sf "$RunMachineID" "$EtcMachineID"
        fi

        if [ ! -s "$RunMachineID" ]; then
            machine_id_gen "$RunMachineID"
        fi
        log_end_msg 0

    # STATIC PERSISTENT MODE ENGINE
    else
        log_daemon_msg "Checking persistent static machine-id" "$NAME"

        # If it was a volatile symlink, safely capture its contents to disk before destroying it
        if [ -L "$EtcMachineID" ]; then
            if [ -s "$RunMachineID" ]; then
                local temp_id
                temp_id=$(cat "$RunMachineID")
                rm -f "$EtcMachineID" "$RunMachineID"
                printf "%s\n" "$temp_id" > "$EtcMachineID"
            else
                rm -f "$EtcMachineID"
            fi
        fi

        # Generate a standard static ID ONLY if the static file is genuinely missing or empty
        if [ ! -f "$EtcMachineID" ] || [ ! -s "$EtcMachineID" ]; then
            rm -f "$EtcMachineID"
            machine_id_gen "$EtcMachineID"
        fi
        log_end_msg 0
    fi
}

do_stop() {
    PowerState="/sys/power/state"
    PowerPMStatus="/sys/power/pm_status"
    if [ -f "$PowerState" ]; then
        if [ -f "$PowerPMStatus" ] && grep -q -E "freeze|suspend|hibernate" "$PowerPMStatus" 2>/dev/null; then
            return 0
        fi
    fi

    log_daemon_msg "Removing stale session-bus files" "$NAME"

    clean_dir "/root/${BaseDbusSessDir}"

    getent passwd | cut -d: -f6 | while read -r userhome; do
        if [ -z "$userhome" ] || [ "$userhome" = "/" ]; then
            continue
        fi
        clean_dir "$userhome/${BaseDbusSessDir}"
    done

    log_end_msg 0
}

case "$1" in
    start)
        do_start
        ;;
    stop)
        do_stop
        ;;
    restart|force-reload)
        do_stop
        do_start
        ;;
    status)
        if [ -L "$EtcMachineID" ]; then
            log_success_msg "Machine-ID Mode: Volatile RAM (Value: $(cat "$EtcMachineID"))"
            exit 0
        elif [ -s "$EtcMachineID" ]; then
            log_success_msg "Machine-ID Mode: Persistent Static (Value: $(cat "$EtcMachineID"))"
            exit 0
        else
            log_failure_msg "Machine-ID file missing or uninitialized."
            exit 3
        fi
        ;;
    *)
        echo "Usage: $NAME {start|stop|restart|force-reload|status}" >&2
        exit 3
        ;;
esac

the idea of this modified version wasn't so much to "just clean up files in devuan" but rather i intend it as part of the debian's initscripts package so that a debian sysvinit-core install would be more like a default devuan install as in having a volatile machine id

worth mentioning i'm still testing the initscript and have not really made full on testing with the different hibernation methods

tho i'm not sure it is polished enough yet to try to submit it to the debian initscripts package

Offline

#4 Today 21:04:14

tom
Member
Registered: 2022-02-03
Posts: 17  

Re: [SOLVED] machine-id

EtcMachineID=/etc/machine-id
RunMachineID=/run/machine-id

I do not understand. Both paths seem not be correct.
I should be -> /var/lib/dbus/machine-id

Or maybe i am missing something.

Offline

Board footer