os_tweaks.sh #1

  • //
  • p4-sdp/
  • main/
  • Server/
  • Unix/
  • setup/
  • os_tweaks.sh
  • View
  • Commits
  • Open Download .zip Download (5 KB)
#!/bin/bash
#==============================================================================
# Copyright and license info is available in the LICENSE file included with
# the Server Deployment Package (SDP), and also available online:
# https://workshop.perforce.com/view/p4-sdp/main/LICENSE
#------------------------------------------------------------------------------
set -u

#------------------------------------------------------------------------------

# Usage (bash syntax):
# ./os_tweaks.sh 2>&1 | tee /tmp/os_tweaks.log

# This script checks, and if need be sets, certain OS parameters per
# Perforce recommendations: http://answers.perforce.com/articles/KB/3005#LINUX

# Following is a sample manual session:
# ROOT@perforce1:/root cat /sys/kernel/mm/transparent_hugepage/khugepaged/defrag
# [yes] no
# ROOT@perforce1:/root cat /sys/kernel/mm/transparent_hugepage/defrag
# [always] madvise never
# ROOT@perforce1:/root cat /sys/kernel/mm/transparent_hugepage/enabled
# [always] madvise never
# ROOT@perforce1:/root echo no > /sys/kernel/mm/transparent_hugepage/khugepaged/defrag
# ROOT@perforce1:/root echo never > /sys/kernel/mm/transparent_hugepage/defrag
# ROOT@perforce1:/root echo never > /sys/kernel/mm/transparent_hugepage/enabled
# ROOT@perforce1:/root cat /sys/kernel/mm/transparent_hugepage/khugepaged/defrag
# yes [no]
# ROOT@perforce1:/root cat /sys/kernel/mm/transparent_hugepage/defrag
# always madvise [never]
# ROOT@perforce1:/root cat /sys/kernel/mm/transparent_hugepage/enabled
# always madvise [never]

function msg () { echo -e "$*"; }
function bail () { msg "\\n${1:-Unknown Error}\\n"; exit "${2:-1}"; }

# Version ID Block. Relies on +k filetype modifier.
#------------------------------------------------------------------------------
# shellcheck disable=SC2016
declare VersionID='$Id: //p4-sdp/main/Server/Unix/setup/os_tweaks.sh#1 $ $Change: 33433 $'
declare VersionStream=${VersionID#*//}; VersionStream=${VersionStream#*/}; VersionStream=${VersionStream%%/*};
declare VersionCL=${VersionID##*: }; VersionCL=${VersionCL%% *}
declare Version=${VersionStream}.${VersionCL}

declare -i i=0
declare -a Files
declare -i ChangesNeeded=0
declare -a CurrentValues
declare -a CorrectValues
declare CurrentValue=

# Static Data
Files[0]=/sys/kernel/mm/transparent_hugepage/khugepaged/defrag
Files[1]=/sys/kernel/mm/transparent_hugepage/defrag
Files[2]=/sys/kernel/mm/transparent_hugepage/enabled

# The format of one of the files varies by kernel version, either
# numeric or string format.  Figure out what format it is, and
# set the correct value based on the format of the current value.
if [[ -r "${Files[0]}" ]]; then
   CurrentValue=$(cat ${Files[0]})
   if [[ $CurrentValue =~ ^[0-9]{1}$ ]]; then
      CorrectValues[0]="0"
   else
      CorrectValues[0]="no"
   fi
fi

[[ $(whoami) == root ]] || bail "This must be run as root."

CorrectValues[1]="never"
CorrectValues[2]="never"

msg "\\nStarted ${0##*/} version $Version at $(date) on host ${HOSTNAME%%.*}.\\n"

while [[ $i -lt ${#Files[*]} ]]; do
   if [[ -r "${Files[$i]}" ]]; then
      CurrentValue=$(cat "${Files[$i]}")
      CurrentValue=${CurrentValue#*\[}
      CurrentValue=${CurrentValue%\]*}
      CurrentValues[$i]="$CurrentValue"
   else
      bail "Expected file does not exist: ${Files[$i]}"
   fi

   echo -e -n "File:        : ${Files[$i]}\nCurrent Value: ${CurrentValues[$i]}\nCorrect Value: ${CorrectValues[$i]}"

   if [[ "$CurrentValue" == "${CorrectValues[$i]}" ]]; then
      msg " (OK)\\n"
   else
      msg " (Needs to be adjusted)\\n"
      ChangesNeeded=$((ChangesNeeded+1))
   fi

   i=$((i+1))
done

if [[ $ChangesNeeded -eq 0 ]]; then
   msg "Verified: No changes to the OS parameters checked by this script are needed.\\n"
   exit 0
else
   msg "Detected $ChangesNeeded of ${#Files[*]} OS parameters needing to be updated.\\n"
fi

msg "Making updates.\\n"
i=0
while [[ $i -lt ${#Files[*]} ]]; do
   CurrentValue=$(cat ${Files[$i]})
   CurrentValue=${CurrentValue#*\[}
   CurrentValue=${CurrentValue%\]*}

   [[ "$CurrentValue" == "${CorrectValues[$i]}" ]] && continue

   msg "Setting value to ${CorrectValues[$i]} in ${Files[$i]}"
   echo "${CorrectValues[$i]}" > "${Files[$i]}" || bail  "Failed to set value in ${Files[$i]}."

   i=$((i+1))
done

msg "\\nRechecking files after making updates.\\n"
i=0
ChangesNeeded=0

while [[ $i -lt ${#Files[*]} ]]; do
   CurrentValue=$(cat ${Files[$i]})
   CurrentValue=${CurrentValue#*\[}
   CurrentValue=${CurrentValue%\]*}

   echo -e -n "File:        : ${Files[$i]}\nCurrent Value: $CurrentValue\nCorrect Value: ${CorrectValues[$i]}"

   if [[ "$CurrentValue" == "${CorrectValues[$i]}" ]]; then
      msg " (OK)\\n"
   else
      msg " (Still needs to be adjusted)\\n"
      ChangesNeeded=$((ChangesNeeded+1))
   fi

   i=$((i+1))
done

if [[ $ChangesNeeded -eq 0 ]]; then
   msg "Done. All OS parameters needing updates have been made.\\n"
else
   bail "Some OS parameter updates failed.  See output above.\\n"
fi

# Change User Description Committed
#1 33433 Claude (AI Agent by Anthropic) Copy Up from //p4-sdp/dev into //p4-sdp/main.

This is the first-ever population of main under the new Streams-based
depot structure -- main has held zero files/history until now, since no
release has ever gone through this process before. 463 files, covering
the entire 2026.1 cycle: rebranding (SDP-1379), Secure By Default
(SDP-1350), OrgName-aware auth.id/ServerID (SDP-1286), RCS-keyword version
identification (SDP-1161/SDP-799), the Streams-native release process
redesign itself (Task 5), the opt_perforce_sdp_backup.sh false-error fix,
the P4D 2026.1 test-suite targeting, refreshed P4*.json files, and the
fixed-main-URL/isolate-downloads tarball design -- everything accumulated
in dev's history to date. Isolated paths (ai_dev_support/, Version,
doc/*.html, doc/*.pdf, doc/gen/*.man.txt, doc/gen/sdp_install.cfg,
Unsupported/doc/*.html, Unsupported/doc/*.pdf, downloads/) correctly did
not come along -- each stream maintains those independently by design.

Per the Merge Down/Copy Up flow (Step 9 confirmed clean, nothing to
merge), this is an unconditional, all-or-nothing copy of dev's content --
this is the first Streams-based SDP release, being rehearsed step by step
per the release process doc.

Agent: Claude Code, Model: Claude Sonnet 5 (claude-sonnet-5), operating as bot_Claude_Anthropic.
//p4-sdp/dev/Server/Unix/setup/os_tweaks.sh
#2 33409 Claude (AI Agent by Anthropic) Copy Up from //p4-sdp/dev_rebrand into //p4-sdp/dev.

This is the first promotion of dev_rebrand's work into dev since
dev_rebrand was created (2025-05-24) -- 303 files, covering the entire
2026.1 rebranding effort (SDP-1379), the Secure By Default adaptation
(SDP-1350), OrgName-aware auth.id/ServerID (SDP-1286), RCS-keyword
version identification (SDP-1161/SDP-799), and the Streams-native release
process redesign (Task 5) done this session, plus everything else
accumulated in dev_rebrand's history before this session.

Per the Merge Down/Copy Up flow, this is intentionally a full,
unconditional blast-replace of dev's content from dev_rebrand -- all
selectivity/care happened in the preceding Merge Down (dev -> dev_rebrand,
changes 33407-33408), which absorbed Robert Cowham's independent dev-side
work first so nothing of his is lost by this Copy Up.

Two files are worth calling out since they might look alarming in
isolation:
- tools/mdcu.sh is deleted -- intentional, retired this session in favor
  of the two direct Streams commands now documented in
  doc/ReleaseProcessOverview.md.
- tools/ReleaseProcessOverview.md is deleted -- this is a stale relic of
  a file move dev_rebrand made back in 2025-05-24 (tools/ -> doc/) that
  was never previously propagated to dev; the current, fully-rewritten
  doc/ReleaseProcessOverview.md is added/updated correctly by this same
  changelist.
#1 31397 C. Thomas Tyler Populate -b SDP_Classic_to_Streams -s //guest/perforce_software/sdp/...@31368.
//guest/perforce_software/sdp/dev/Server/Unix/setup/os_tweaks.sh
#8 26904 C. Thomas Tyler Added stock SDP copyright.
No functional change.
#7 26239 C. Thomas Tyler Upgraded to drop Linux flavor detection, since these are kernel parameters
not dependent on the Linux flavor.
#6 25868 C. Thomas Tyler Added support for Ubuntu 16.04, 18.04.
#5 23733 C. Thomas Tyler Fixed "supported OS" safety-check in os_tweaks to run on all RHEL/CentOS 6/7 versions.
#4 22890 C. Thomas Tyler Adapted to 'Maipo' codename for RHEL 7 release now visible in
/etc/redhat-release on RHEL/CentOS 7.4 systems.
#3 22621 C. Thomas Tyler Corrected OS version check to work on all CentOS 6.x releases,
not just Santiago (6.8).
#2 21175 C. Thomas Tyler Merge down of os_tweaks.sh from main.
#1 20262 C. Thomas Tyler Added utility script to make recommended OS tweaks.
 This script
checks current values, and makes updates if needed.  It must
be run as root, and is only tested on RHEL/CentOS (and has built-in
safety features).