

{"id":552,"date":"2022-09-17T13:43:01","date_gmt":"2022-09-17T11:43:01","guid":{"rendered":"https:\/\/blog.koeckeis-fresel.net\/?p=552"},"modified":"2024-03-16T10:52:26","modified_gmt":"2024-03-16T09:52:26","slug":"silent-install-smartmontools-on-mac","status":"publish","type":"post","link":"https:\/\/blog.koeckeis-fresel.net\/?p=552","title":{"rendered":"silent install smartmontools on mac"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\nset -x\n\n# Define variables\nSMART_PATH=\"\/usr\/local\/sbin\"\nSMARTCTL_PATH=\"$SMART_PATH\/smartctl\"\nSMARTUPDATEDB_PATH=\"$SMART_PATH\/update-smart-drivedb\"\nVERSION=\"7.4-1\"\nBASE_URL=\"https:\/\/sourceforge.net\/projects\/smartmontools\/files\/smartmontools\/7.4\"\nDMG_FILE=\"smartmontools-${VERSION}.dmg\"\nMD5_FILE=\"smartmontools-${VERSION}.dmg.md5\"\nASC_FILE=\"smartmontools-${VERSION}.dmg.asc\"\nGPG_KEY=\"0C9577FD2C4CFCB4B9A599640A30812EFF3AEFF5\"\nKEYSERVER=\"hkps:\/\/keys.openpgp.org\"\nKEY_URL=\"https:\/\/www.smartmontools.org\/browser\/trunk\/www\/SmartmontoolsSigningKey_2021.txt?format=txt\"\nKEY_DIR=$(dirname \"$0\")\nGPG_KEYFILE=\"$KEY_DIR\/smartmontools_key.txt\"\nDRIVE_DB=\"https:\/\/svn.code.sf.net\/p\/smartmontools\/code\/branches\/RELEASE_7_3_DRIVEDB\/smartmontools\/drivedb.h\"\n\n# Ensure running as root\nif &#91; \"$(whoami)\" != \"root\" ]; then\n    echo \"Error: This script must be run with superuser privileges.\"\n    exit 1\nfi\n\n# Check if smartmontools is already installed\nif command -v $SMARTCTL_PATH &amp;> \/dev\/null; then\n    # Define the version pattern\n    PATTERN=\"sf-$VERSION\"\n    echo \"Current smartmontools version: $VERSION\"\n    echo \"Expected pattern: $PATTERN\"\n\n    # Extract the installed version number\n    SMARTCTL_VERSION=$($SMARTCTL_PATH --version | head -1 | awk -F'&#91;(|)]' '{print $2}')\n\n    # Check if the version matches or indicates a local build\n    if &#91;&#91; $SMARTCTL_VERSION == *\"local build\"* ]]; then\n        echo \"Version indicates a local (Homebrew) installation.\"\n        echo \"Please uninstall using the appropriate package manager.\"\n        exit 1\n    elif &#91;&#91; $SMARTCTL_VERSION == $PATTERN ]]; then\n        echo \"Installed version matches the target version. Updating drive database only.\"\n        # Update drive database\n        if ! $SMARTUPDATEDB_PATH; then\n            echo \"Failed to update drive database directly. Attempting to download updated version...\"\n            if ! curl -sSL \"$DRIVE_DB\" -o \"\/usr\/local\/share\/smartmontools\/drivedb.h\"; then\n                echo \"Error: Failed to download updated drive database.\"\n                exit 1\n            fi\n        fi\n        echo \"Drive database updated successfully.\"\n        exit 0\n    else\n        echo \"Installed version does not match the expected pattern and is not a Homebrew installation.\"\n    fi\nfi\n\n# Function definitions for logging errors, importing GPG signatures, downloading files, and verifying downloads\nlog_error() {\n    echo \"$1\" | logger -t smartmontools_installation_error\n    exit 500\n}\n\nimport_gpg_signature() {\n    echo \"Importing GPG signature...\"\n    curl -sSL \"$1\" -o \"$GPG_KEYFILE\" &amp;&amp; gpg --import \"$GPG_KEYFILE\" || log_error \"Failed to import GPG key.\"\n}\n\ndownload_file() {\n    echo \"Downloading $2...\"\n    curl -L \"$1\" -o \"$2\" || log_error \"Failed to download $2.\"\n}\n\nverify_downloads() {\n    # Verify MD5 checksum\n    echo \"Verifying MD5 checksum for $DMG_FILE...\"\n    local md5_check=$(md5 -q \"$DMG_FILE\")\n    local expected_md5=$(cut -d ' ' -f1 \"$MD5_FILE\")\n    if &#91; \"$md5_check\" != \"$expected_md5\" ]; then\n        log_error \"Checksum verification failed for $DMG_FILE.\"\n    fi\n    echo \"MD5 checksum verified successfully.\"\n\n    # Verify GPG signature\n    if command -v gpg &amp;> \/dev\/null; then\n        echo \"Verifying GPG signature for $DMG_FILE...\"\n        gpg --keyring \"$GPG_KEYFILE\" --verify \"$ASC_FILE\" \"$DMG_FILE\" &amp;> \/dev\/null || log_error \"GPG signature verification failed.\"\n        echo \"GPG signature verified successfully.\"\n    fi\n}\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n# Main script\nif &#91; -f \"$DMG_FILE\" ] &amp;&amp; &#91; -f \"$MD5_FILE\" ] &amp;&amp; &#91; -f \"$ASC_FILE\" ]; then\n    # Check checksum and key before downloading\n    verify_downloads\nelse\n    # Download files\n    download_file \"${BASE_URL}\/${DMG_FILE}\/download\" \"$DMG_FILE\"\n    download_file \"${BASE_URL}\/${MD5_FILE}\/download\" \"$MD5_FILE\"\n    download_file \"${BASE_URL}\/${ASC_FILE}\/download\" \"$ASC_FILE\"\n    verify_downloads\nfi\n\n# Installation steps\n# Attempt to attach the DMG file\nif hdiutil attach \"$DMG_FILE\"; then\n    echo \"DMG file attached successfully\"\nelse\n    log_error \"Failed to attach DMG file: $DMG_FILE\"\nfi\n\n# Attempt to install the package\nif installer -allowUntrusted -verbose -pkg \"\/Volumes\/smartmontools\/smartmontools-${VERSION}.pkg\" -target \/; then\n    echo \"Installation successful\"\n    purge \n    sleep 5\nelse\n    log_error \"Installation failed\"\n    hdiutil detach \"\/Volumes\/smartmontools\"\n    sleep 5\nfi\n\n# Attempt to detach the DMG file\nif hdiutil detach \"\/Volumes\/smartmontools\"; then\n    echo \"DMG file detached successfully\"\nelse\n    log_error \"Failed to detach DMG file: $DMG_FILE\"\nfi\n\n# Check if smartmontools is installed\nif ! command -v smartctl &amp;> \/dev\/null; then\n    echo \"Error: smartmontools is not installed. Please install it using Homebrew or another package manager.\"\n    exit 1\nfi\n\n# Check if update-smart-drivedb is available\nif ! command -v update-smart-drivedb &amp;> \/dev\/null; then\n    echo \"Error: update-smart-drivedb is not available. Make sure smartmontools is properly installed.\"\n    exit 1\nfi\n\n# Update drive database\necho \"Updating smartmontools drive database...\"\n\/usr\/local\/sbin\/update-smart-drivedb \n\n# Check if the update was successful\nif &#91; $? -ne 0 ]; then\n    echo \"Error: Failed to update smartmontools drive database.\"\n    # Download the updated drivedb.h file\n    echo \"Downloading updated drivedb.h file...\"\n    curl -sSL \"https:\/\/www.smartmontools.org\/export\/5597\/branches\/RELEASE_7_3_DRIVEDB\/smartmontools\/drivedb.h\" -o \"\/usr\/local\/share\/smartmontools\/drivedb.h\"\n\n        # Check if the download was successful\n        if &#91; $? -ne 0 ]; then\n        echo \"Error: Failed to download the updated drivedb.h file.\"\n        exit 1\nfi\n\necho \"smartmontools drive database updated successfully.\"\nfi\n\n# Further instructions as necessary\necho \"Run the test\"\n\/usr\/local\/sbin\/smartctl -t long \/dev\/disk0\n\necho \"Get information on your drive\"\n\/usr\/local\/sbin\/smartctl -a \/dev\/disk0\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[12,18],"tags":[],"class_list":["post-552","post","type-post","status-publish","format-standard","hentry","category-apple","category-deployment-apple"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/blog.koeckeis-fresel.net\/index.php?rest_route=\/wp\/v2\/posts\/552","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.koeckeis-fresel.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.koeckeis-fresel.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.koeckeis-fresel.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.koeckeis-fresel.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=552"}],"version-history":[{"count":0,"href":"https:\/\/blog.koeckeis-fresel.net\/index.php?rest_route=\/wp\/v2\/posts\/552\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.koeckeis-fresel.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=552"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.koeckeis-fresel.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=552"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.koeckeis-fresel.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=552"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}