Contribute
Register

Script to Change Startup Disk (and restart)

Status
Not open for further replies.
Joined
Dec 3, 2010
Messages
460
Motherboard
Gigabyte GA-H55M-S2V
CPU
Intel i3-530
Graphics
HIS HD 6570
Mac
  1. iMac
Mobile Phone
  1. Android
I made this quick script to change the Startup Disk (since I need Windows to wake from hibernation sometimes), and optionally reboot.
  • save it as "startupDisk"[/*:m:2z7jglrz]
  • edit the 'systems' line with your installed systems and their partition numbers (first number is disk number, second is partition, the first usable partition is usually 2); triple boot on one drive might be "$systems=array('mac'=>'0,2','windows'=>'0,3','ubuntu'=>'0,4');"[/*:m:2z7jglrz]
  • install it with "chmod a+x startupDisk;sudo cp startupDisk /usr/local/bin"[/*:m:2z7jglrz]
Code:
#!/usr/bin/php
<?php
$systems=array('mac'=>'0,2','windows'=>'0,3');
$files=array('/Extra/org.chameleon.Boot.plist','/tmp/boot'.time());
if(!isset($argv[1])){echo 'Startup Disk Switcher for Chameleon'."\n".
'Usage: startupDisk ('.implode('|',array_keys($systems)).') [restart]'."\n";
preg_match('/hd\(([0-9,]+)\)/',file_get_contents($files[0]),$system);
echo 'Current startup disk is: '.(isset($system[1])?array_search($system[1],$systems):'[default]')."\n";
exit;}
if(!in_array($argv[1],array_keys($systems))){echo 'Error: System must be one of "'.implode('", "',array_keys($systems)).'"'."\n";exit;}
else{$system=$systems[$argv[1]];}
if(isset($argv[2])){
if($argv[2]!='restart'){echo 'Error: To restart now, use "restart"'."\n";exit;}else{$restart=true;}}
else{$restart=false;}
copy($files[0],$files[1].'.plist');
`defaults write $files[1] "Default Partition" -string 'hd($system)'`;
`plutil -convert xml1 -o $files[0] $files[1].plist`;
//if redirecting to same, command fails at deletion
if($restart){`sudo reboot -l`;}
?>
  • usage is "startupDisk <system> [restart]"[/*:m:2z7jglrz]
  • the optional "restart" parameter reboots the system immediately[/*:m:2z7jglrz]
  • those who know PHP might notice the command is a little funky, its necessary to work around the permissions in the Extra folder[/*:m:2z7jglrz]
--edit
changed the file location to match the new PLIST, also added option detection: when run with no arguments, it will print the current startupDisk.
 
Status
Not open for further replies.
Back
Top