From 405700cc8511127b3214c036db7544b63b2db424 Mon Sep 17 00:00:00 2001 From: Mario Panighetti Date: Tue, 24 Oct 2023 15:16:05 -0700 Subject: [PATCH] added interactive prompts for undefined script parameters - script now checks each Jamf Pro script parameter individually and prompts user for selection if missing - removed check_jamf_pro_arguments - newVolumeQuota defaults to "none" (share all container space) if undefined - removed startupDiskName and check_startup_disk (now detects startup volume name programmatically) - moved file system personality check to function - renamed script to clarify functionality - major version change (guess it's finally time) --- .../Add APFS Volume to Startup Container.sh | 154 ++++++++++++++++++ scripts/Add APFS Volume.sh | 134 --------------- 2 files changed, 154 insertions(+), 134 deletions(-) create mode 100755 scripts/Add APFS Volume to Startup Container.sh delete mode 100755 scripts/Add APFS Volume.sh diff --git a/scripts/Add APFS Volume to Startup Container.sh b/scripts/Add APFS Volume to Startup Container.sh new file mode 100755 index 0000000..bd491e0 --- /dev/null +++ b/scripts/Add APFS Volume to Startup Container.sh @@ -0,0 +1,154 @@ +#!/bin/sh + +### +# +# Name: Add APFS Volume to Startup Container.sh +# Description: Creates additional APFS volume at /Volumes/$newVolumeName (sharing space with other volumes in the startup volume container). +# Created: 2016-06-06 +# Last Modified: 2023-10-24 +# Version: 8.0 +# +# +# Copyright 2016 Palantir Technologies, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# +### + + + +########## variable-ing ########## + + + +# Jamf Pro script parameter: "New Volume Name" +# If undefined, script will prompt user to enter a desired name. +newVolumeName="${4}" +# Jamf Pro script parameter: "New Volume APFS Format" +# If undefined, script will prompt user to optionally select a desired format. See diskutil listFilesystems for expected formats. +newVolumeAPFSFormat="${5}" +# Jamf Pro script parameter: "New Volume Quota (GB)" +# A number of gigabytes to set the quota for the new volume. If set to "none", the volume shares all available space with other volumes in the target container. If undefined, script will prompt user to optionally enter a desired quota. +newVolumeQuota="${6}" +startupVolumeInfo=$(/usr/sbin/diskutil info "/") +startupVolumeName=$(echo "$startupVolumeInfo" | /usr/bin/awk -F: '/Volume Name/ {print $NF}' | /usr/bin/sed 's/^ *//') +startupVolumeDevice=$(echo "$startupVolumeInfo" | /usr/bin/awk '/Part of Whole/ {print $4}') + + + +########## function-ing ########## + + + +# Checks for presence of $newVolumeName, prompts for entry if missing. +check_volume_name () { + + if [ -z "$newVolumeName" ]; then + newVolumeName=$(/usr/bin/osascript -e "set new_volume_name to text returned of (display dialog \"Please enter new volume name:\" default answer \"\")") + if [ -z "$newVolumeName" ]; then + echo "❌ ERROR: New volume name undefined, unable to proceed." + exit 74 + fi + fi + volumeCheck=$(/usr/sbin/diskutil info "$newVolumeName" 2>&1) + if echo "$volumeCheck" | grep -v -q "Could not find disk: ${newVolumeName}"; then + echo "Volume ${newVolumeName} already present, no action required." + exit 0 + fi + +} + + +# Checks for presence of $newVolumeAPFSFormat, prompts for selection if missing. +check_volume_apfs_format () { + + if [ -z "$newVolumeAPFSFormat" ]; then + newVolumeAPFSFormat=$(/usr/bin/osascript -e "set new_volume_apfs_format to (choose from list {\"APFS\", \"Case-sensitive APFS\"} with prompt \"Select desired volume APFS format:\")") + if [ -z "$newVolumeAPFSFormat" ]; then + echo "❌ ERROR: New volume APFS format undefined, unable to proceed." + exit 74 + fi + fi + +} + + +# Checks for presence of $newVolumeQuota, prompts for optional entry if missing. +check_volume_quota () { + + if [ -z "$newVolumeQuota" ]; then + newVolumeQuota=$(/usr/bin/osascript -e "set new_volume_quota to text returned of (display dialog \"Please enter new volume quota as a number of gigabytes (or enter 'none' to share all container space with your startup volume):\" default answer \"none\")") + if [ -z "$newVolumeQuota" ]; then + newVolumeQuota="none" + fi + fi + +} + + +# Verifies that the startup volume container uses an APFS file system personality. +check_file_system_personality () { + + fileSystemPersonality=$(echo "$startupVolumeInfo" | /usr/bin/awk -F: '/File System Personality/ {print $NF}' | /usr/bin/sed 's/^ *//') + if echo "$fileSystemPersonality" | /usr/bin/grep -q "APFS"; then + echo "File system personality: ${fileSystemPersonality}" + else + echo "❌ ERROR: Unsupported file system (${fileSystemPersonality}), unable to proceed." + exit 1 + fi + +} + + +# Creates volume with specified name, format, and quota size in gigabytes (optional), sharing space with other volumes in the startup volume container. +add_volume () { + + if [ "${newVolumeQuota}" = "none" ]; then + /usr/sbin/diskutil \ + apfs addVolume \ + "${1}" \ + "${2}" \ + "${3}" + echo "Volume ${3} created (sharing all available container space with ${startupVolumeName}), formatted as ${2}." + else + /usr/sbin/diskutil \ + apfs addVolume \ + "${1}" \ + "${2}" \ + "${3}" \ + -quota "${newVolumeQuota}G" + echo "Volume ${3} created (${newVolumeQuota} GB), formatted as ${2}." + fi + +} + + + +########## main process ########## + + + +# Verify system meets all script requirements (each function will exit if respective check determines that the script cannot be run). +check_volume_name +check_volume_apfs_format +check_volume_quota +check_file_system_personality + + +# Add APFS volume. +add_volume "$startupVolumeDevice" "$newVolumeAPFSFormat" "$newVolumeName" + + + +exit 0 diff --git a/scripts/Add APFS Volume.sh b/scripts/Add APFS Volume.sh deleted file mode 100755 index c85558e..0000000 --- a/scripts/Add APFS Volume.sh +++ /dev/null @@ -1,134 +0,0 @@ -#!/bin/sh - -### -# -# Name: Add APFS Volume.sh -# Description: Creates additional APFS volume at /Volumes/$newVolumeName (sharing disk space with other volumes in the startup disk container). -# Created: 2016-06-06 -# Last Modified: 2023-10-24 -# Version: 7.1 -# -# -# Copyright 2016 Palantir Technologies, Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -# -### - - - -########## variable-ing ########## - - - -# Jamf Pro script parameter: "Startup Disk Name" -startupDiskName="${4}" -# Jamf Pro script parameter: "New Volume Name" -newVolumeName="${5}" -# Jamf Pro script parameter: "New Volume APFS Format" -# See diskutil listFilesystems for expected formats. -newVolumeAPFSFormat="${6}" -# Jamf Pro script parameter: "New Volume Quota (GB) (optional)" -# Enter a number of gigabytes to set the quota for the new volume. If no number is specified, the volume shares all available space with other volumes in the target container. -newVolumeQuota="${7}" - - - -########## function-ing ########## - - - -# Exits if any required Jamf Pro arguments are undefined. -check_jamf_pro_arguments () { - - if [ -z "$startupDiskName" ] || [ -z "$newVolumeName" ] || [ -z "$newVolumeAPFSFormat" ]; then - echo "❌ ERROR: Undefined Jamf Pro argument, unable to proceed." - exit 74 - fi - -} - - -# Checks for presence of target startup disk. -check_startup_disk () { - - startupDiskCheck=$(/usr/sbin/diskutil info "$startupDiskName" 2>&1) - if echo "$startupDiskCheck" | grep -q "Could not find disk: ${startupDiskName}"; then - echo "❌ ERROR: Volume ${startupDiskName} missing, unable to proceed." - exit 72 - fi - -} - - -# Checks for presence of $newVolumeName. -check_volume () { - - volumeCheck=$(/usr/sbin/diskutil info "$newVolumeName" 2>&1) - if echo "$volumeCheck" | grep -v -q "Could not find disk: ${newVolumeName}"; then - echo "Volume ${newVolumeName} already present, no action required." - exit 0 - fi - -} - - -# Creates volume with specified name, format, and quota size in gigabytes (optional). -add_volume () { - - if [ -n "${newVolumeQuota}" ]; then - /usr/sbin/diskutil \ - apfs addVolume \ - "${1}" \ - "${2}" \ - "${3}" \ - -quota "${newVolumeQuota}G" - echo "Volume ${3} created (${newVolumeQuota} GB), formatted as ${2}." - else - /usr/sbin/diskutil \ - apfs addVolume \ - "${1}" \ - "${2}" \ - "${3}" - echo "Volume ${3} created (sharing all available container space with ${startupDiskName}), formatted as ${2}." - fi - -} - - - -########## main process ########## - - - -# Verify system meets all script requirements (each function will exit if respective check determines that the script cannot be run). -check_jamf_pro_arguments -check_startup_disk -check_volume - - -# Add APFS volume. -startupDiskInfo=$(/usr/sbin/diskutil info "$startupDiskName") -fileSystemPersonality=$(echo "$startupDiskInfo" | /usr/bin/awk -F: '/File System Personality/ {print $NF}' | /usr/bin/sed 's/^ *//') -if echo "$fileSystemPersonality" | /usr/bin/grep -q "APFS"; then - startupDiskDevice=$(echo "$startupDiskInfo" | /usr/bin/awk '/Part of Whole/ {print $4}') - add_volume "$startupDiskDevice" "$newVolumeAPFSFormat" "$newVolumeName" -else - echo "❌ ERROR: Unsupported file system (${fileSystemPersonality}), unable to proceed." - exit 1 -fi - - - -exit 0