Skip to content

Instantly share code, notes, and snippets.

@gfjardim
Last active November 10, 2015 16:55
Show Gist options
  • Save gfjardim/c81fba928425e7a1dfac to your computer and use it in GitHub Desktop.
Save gfjardim/c81fba928425e7a1dfac to your computer and use it in GitHub Desktop.
function listDir($root) {
$iter = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($root,
RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST,
RecursiveIteratorIterator::CATCH_GET_CHILD);
$paths = array();
foreach ($iter as $path => $fileinfo) {
if (! $fileinfo->isDir()) $paths[] = $path;
}
return $paths;
}
function get_unasigned_disks() {
$paths = listDir("/dev/disk/by-id");
$disks_id = preg_grep("#wwn-|-part#", $paths, PREG_GREP_INVERT);
$disks_real = array_map(function($p){return realpath($p);}, $disks_id);
exec("/usr/bin/strings /boot/config/super.dat 2>/dev/null|grep -Po '.{10,}'", $disks_serial);
$disks_cfg = is_file("/boot/config/disk.cfg") ? parse_ini_file("/boot/config/disk.cfg") : array();
$cache_serial = array_flip(preg_grep("#cacheId#i", array_flip($disks_cfg)));
$flash = realpath("/dev/disk/by-label/UNRAID");
$flash_serial = array_filter($paths, function($p) use ($flash) {if(!is_bool(strpos($flash,realpath($p)))) return basename($p);});
$unraid_serials = array_merge($disks_serial,$cache_serial,$flash_serial);
$unraid_disks = array();
foreach($unraid_serials as $serial) {
$unraid_disks = array_merge($unraid_disks, preg_grep("#".preg_quote($serial, "#")."#", $disks_id));
}
$unraid_real = array_map(function($p){return realpath($p);}, $unraid_disks);
$unassigned = array_flip(array_diff(array_combine($disks_id, $disks_real), $unraid_real));
natsort($unassigned);
foreach ($unassigned as $k => $disk) {
unset($unassigned[$k]);
$parts = array_values(preg_grep("#{$disk}-part\d+#", $paths));
$device = realpath($disk);
if (! is_bool(strpos($device, "/dev/sd")) || ! is_bool(strpos($device, "/dev/hd"))) {
$unassigned[$disk] = array("device"=>$device,"partitions"=>$parts);
}
}
echo "\nDisks:\n+ ".implode("\n+ ", array_map(function($k,$v){return "$k => $v";}, $disks_real, $disks_id));
echo "\nunRAID Serials:\n+ ".implode("\n+ ", $unraid_serials);
echo "\nunRAID Disks:\n+ ".implode("\n+ ", $unraid_disks);
echo "\nUnassigned Disks:\n+ ".print_r($unassigned,true);
return $unassigned;
}
get_unasigned_disks();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment