Skip to content

Instantly share code, notes, and snippets.

@cmer
Forked from voltechs/no_automount
Last active December 23, 2018 05:45
Show Gist options
  • Save cmer/060d7c09506c62d3ab98cbc48503dc75 to your computer and use it in GitHub Desktop.
Save cmer/060d7c09506c62d3ab98cbc48503dc75 to your computer and use it in GitHub Desktop.
Add volume (UUID) to fstab to prevent automount (macOS)
#!/usr/bin/env ruby
# Usage: no_automount /Volumes/My\ Disk
if ARGV[0].nil?
puts "Usage: no_automount /Volumes/My\ Disk"
exit 1
end
diskinfo = `diskutil info '#{ARGV[0]}'`.gsub("\n\n", "\n").split("\n").collect do |b|
b.strip.split(/:\s+/)
end.to_h
disk_uuid = diskinfo['Volume UUID']
disk_type = diskinfo['Type (Bundle)']
disk_name = diskinfo['Volume Name']
if disk_uuid.nil? || disk_uuid.strip.empty?
puts "Disk not found."
exit 1
end
file_name = "/etc/fstab"
new_contents = if File.exist?(file_name)
text = File.read(file_name)
text.gsub(/UUID=#{disk_uuid}.*(:?\n)/, "")
else
""
end
new_contents << "UUID=#{disk_uuid} none #{disk_type} rw,noauto # #{disk_name}"
File.open(file_name, "w") {|file| file.puts new_contents }
puts "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment