eraseVolume fails, could not mount disk after erase

el2493
Contributor III

I have a simple script that erases a volume and them sets the permissions so that anyone can access the drive:

#!/bin/sh
#erase and reformat Volume_name partition
sudo diskutil eraseVolume JHFS+ Volume_name /dev/disk0s4

#change drive permission
sudo chmod o+rw /volumes/Volume_name

If I run these commands separately on terminal on a computer, it works perfectly. However, when I create a script and try to run it through JSS, I get this error:

#!/bin/sh
#erase and reformat Volume_name partition
sudo diskutil eraseVolume JHFS+ Volume_name /dev/disk0s4
Started erase on disk0s4 Volume_name
Unmounting disk
Erasing
Initialized /dev/rdisk0s4 as a 9 GB case-insensitive HFS Plus volume with a 8192k journal
Mounting disk
Could not mount disk0s4 after erase
Finished erase on disk0s4 Volume_name

#change drive permission
sudo chmod o+rw /volumes/Volume_name
chmod: /volumes/Volume_name: No such file or directory
#sudo chmod /volumes/Volume_name

I get why the 2nd command fails (since it didn't mount it can't change permissions), but does anyone know why the first command would repeatedly fail? I've tried flushing the policy so it would try again in case it was just a fluke, but it continually fails at the same spot (re-mounting).

2 REPLIES 2

el2493
Contributor III

I tried throwing in a "sudo diskutil mount /dev/disk0s4" command in between the commands as a workaround and got the error:

#mount drive
sudo diskutil mount /dev/disk0s4
Volume on disk0s4 failed to mount
If the volume is damaged, try the "readOnly" option

After restarting the volume did mount, so it definitely wasn't damaged. Would like to see if I can get this working without requiring the reboot since I don't think it should be necessary.

el2493
Contributor III

So I'm ultimately doing this because the file format of a lot of our computers' partitions is Mac OS Extended and I need it to be set to Mac OS Extended (Journaled).

Did some troubleshooting, it looks like unless the eraseVolume command completes and mounts the drive it doesn't set the file format to Mac OS Extended (Journaled) but instead leaves it as Mac OS Extended (after rebooting when the drive mounts that's what it shows). Even though I run the command as sudo, it seems like it runs as the user currently logged in (I tried using the GUI and Disk Utility while logged in as a non-Admin and it did the exact same thing as the script). If I login to a computer with an Admin account and then have JSS run the policy, it works. But if I login with a public lab account (which is what is normally logged into my computers) it gives me the error message about being unable to mount.

Am sort of out of ideas at this point, so I'm just going computer by computer, logging in as Admin and erasing the partition. I'm pretty sure once the file format is set to Journaled that the "sudo chmod o+rw /volumes/Volume_name" will work, but since it only takes a few more seconds for me to set volume permission through macOS I'm just doing it that way.