Skip to content

Instantly share code, notes, and snippets.

@voltechs
Last active November 18, 2022 01:29
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save voltechs/fc48c9683d50c7c03cab2f0a6477d8da to your computer and use it in GitHub Desktop.
Save voltechs/fc48c9683d50c7c03cab2f0a6477d8da 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
diskinfo = `diskutil info '#{ARGV[0]}'`.gsub("\n\n", "\n").split("\n").collect { |b|
b.strip.split(/:\s+/)
}.to_h
disk_uuid = diskinfo['Volume UUID']
disk_type = diskinfo['Type (Bundle)']
disk_name = diskinfo['Volume Name']
fstab_filename = '/etc/fstab'
text = File.read(fstab_filename)
new_contents = text.gsub(/UUID=#{disk_uuid}.*(:?\n)/, "")
new_contents << "UUID=#{disk_uuid} none #{disk_type} rw,noauto # #{disk_name}"
File.open(fstab_filename, "w") {|file| file.puts new_contents }
@byassine52
Copy link

file_name is not defined.
I fixed it here:
https://gist.github.com/byassine52/dfb697bafe086585e22333f834074173

@henri
Copy link

henri commented Aug 7, 2019

Also, if you want to casually prevent volumes from auto mounting, take a look at this project which runs a daemon to prevent file systems from mounting : http://henri.shustak.org/tools/ejector

@voltechs
Copy link
Author

voltechs commented Aug 7, 2019

file_name is not defined.
I fixed it here:
https://gist.github.com/byassine52/dfb697bafe086585e22333f834074173

Thanks @byassine52. Fixed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment