Menu

[r8825]: / legacy / etc / inc / disks.inc  Maximize  Restore  History

Download this file

1295 lines (1077 with data), 36.2 kB

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
<?php
/*
disks.inc
part of FreeNAS (http://freenas.org)
Copyright (C) 2005-2010 Olivier Cochard-Labbe <olivier@freenas.org>.
All rights reserved.
Based on m0n0wall (http://m0n0.ch/wall)
Copyright (C) 2003-2006 Manuel Kasper <mk@neon1.net>.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
require_once("functions.inc");
require_once("util.inc");
// Mount all configured disks.
// Return 0 if successful, 1 if error
function disks_mount_all()
{
global $config, $g;
// For each device configured:
if (is_array($config['mounts']['mount'])) {
// Mount real disks first
foreach ($config['mounts']['mount'] as $mount) {
if ("disk" !== $mount['type'])
continue;
// Advanced unmount filesystem if not in booting mode (mount edition).
// Background: 'disks_umount' can't be used in the case the mount point
// has been renamed by the user because it uses the 'sharename' (which is
// now the new name of the share, but we need the old name) for unmounting.
if (!is_booting())
disks_umount_ex($mount);
disks_mount($mount);
}
// Then mount ISOs
foreach ($config['mounts']['mount'] as $mount) {
if ("iso" !== $mount['type'])
continue;
// Advanced unmount filesystem if not in booting mode (mount edition)
if (!is_booting())
disks_umount($mount);
disks_mount($mount);
}
}
return 0;
}
// This function unmounts all configured mountpoints
// Return 0 if successful, otherwise 1
function disks_umount_all()
{
global $config;
// Sync disks first
mwexec("/bin/sync");
if (is_array($config['mounts']['mount'])) {
foreach ($config['mounts']['mount'] as $mountent) {
disks_umount($mountent);
}
}
return 0;
}
// Mount using the configured mount given in parameter
// Return 0 if successful, 1 if error
function disks_mount($mount)
{
global $config, $g;
// Set mount name
$mountname = "{$g['media_path']}/{$mount['sharename']}";
// Create one directory for each device under /mnt
if (!@mkdir($mountname)) {
write_log("Error: Failed to create directory {$mountname}");
}
/* Mount the filesystem */
/* In booting mode, skip encrypted disk (need to enter the passphrase on the webgui) */
if (is_booting()) {
if (is_array($config['geli']['vdisk'])) {
if (array_search_ex($mount['mdisk'], $config['geli']['vdisk'], "devicespecialfile")) {
write_log("Skip mounting '{$mount['sharename']}': Passphrase needed for '{$mountname}'.");
return 0;
}
}
}
switch($mount['type']) {
case "disk":
$devicespecialfile = $mount['devicespecialfile'];
break;
case "iso":
// Create memory disk. 'mdconfig' returns the ID of this disk
exec("/sbin/mdconfig -a -t vnode -f '{$mount['filename']}'", $mdid, $result);
if (0 !== $result) {
write_log("Error: Failed to create memory disk for '{$mount['filename']}'");
return 1;
}
$devicespecialfile = "/dev/{$mdid[0]}";
break;
}
/* Check if filesystem should be mounted read-only */
$readonly = (isset($mount['readonly'])) ? "-r" : "";
/* Check the fileystem only if there is a problem. */
/* This part is too stupid: I must read the FreeBSD start script for use the same intelligent method for checking hard drive */
switch ($mount['fstype']) {
case "ufs":
$result = mwexec("/sbin/mount -t ufs -o acls {$readonly} {$devicespecialfile} '{$mountname}'", true);
if (0 == $result) {
write_log("Device {$devicespecialfile} mounted using UFS on '{$mountname}'");
$result = 0;
} else {
@rmdir($mountname);
write_log("Error: Failed to mount {$devicespecialfile} using UFS");
$result = 1;
}
break;
case "msdosfs":
$result = mwexec("/sbin/mount -t msdosfs -o large {$readonly} {$devicespecialfile} '{$mountname}'", true);
if (0 == $result) {
write_log("Device {$devicespecialfile} mounted using FAT on '{$mountname}'");
$result = 0;
} else {
@rmdir($mountname);
write_log("Error: Failed to mount {$devicespecialfile} using FAT");
$result = 1;
}
break;
case "udf":
case "cd9660":
$result = mwexec("/sbin/mount -t {$mount['fstype']} {$devicespecialfile} '{$mountname}'", true);
if (0 == $result) {
write_log("Device {$devicespecialfile} mounted as CD/DVD on '{$mountname}'");
$result = 0;
} else {
@rmdir($mountname);
write_log("Error: Failed to mount {$devicespecialfile} as CD/DVD (fstype={$mount['fstype']})");
$result = 1;
}
break;
case "ntfs":
// Load the kernel FUSE module
mwexec("/sbin/kldload /boot/kernel/fuse.ko");
$result = mwexec("/sbin/mount -t ntfs {$readonly} {$devicespecialfile} '{$mountname}'", true); // default options: silent,allow_other
if (0 == $result) {
write_log("Disk {$devicespecialfile} mounted using NTFS on '{$mountname}'");
$result = 0;
} else {
@rmdir($mountname);
write_log("Error: Failed to mount {$devicespecialfile} using NTFS");
$result = 1;
}
break;
case "ext2fs":
$result = mwexec("/sbin/mount -t ext2fs {$readonly} {$devicespecialfile} '{$mountname}'", true);
if (0 == $result) {
write_log("Disk {$devicespecialfile} mounted using EXT2 on '{$mountname}'");
$result = 0;
} else {
@rmdir("{$mountname}");
write_log("Error: Failed to mount {$devicespecialfile} using EXT2");
$result = 1;
}
break;
}
// Set access restrictions. Use the following default values.
$mode = "777";
$owner = "root";
$group = "wheel";
if (is_array($mount['accessrestrictions'])) {
$mode = $mount['accessrestrictions']['mode'];
$owner = $mount['accessrestrictions']['owner'];
$group = $mount['accessrestrictions']['group'][0];
}
// Check if target directory is writable, otherwise we will get an error message.
// For ISO's this may be 'chmod: /mnt/xxx: Read-only file system)'
if (file_exists($mountname) && is_writable($mountname)) {
if (!@chmod($mountname, octdec($mode)) || !@chown($mountname, $owner) || !@chgrp($mountname, $group)) {
write_log("Error: Failed to set access restrictions on directory '{$mountname}'");
}
}
return $result;
}
// Unmount the specified configured mount point.
// Return 0 is successfull, 1 if error
function disks_umount($mount)
{
global $g;
// Get memory disk unit for ISOs
if ("iso" === $mount['type']) {
// Get current mount list
$amountlist = get_mounts_list();
foreach ($amountlist as $amountk => $amountv) {
if (0 == strcmp($amountv['sharename'], $mount['sharename'])) {
// Extract memory disk unit id
preg_match("/^\/dev\/md(\d+)$/", $amountv['devicespecialfile'], $matches);
$mdunit = $matches[1];
break;
}
}
}
// Unmount mount point
$mountname = "{$g['media_path']}/{$mount['sharename']}";
if (mwexec("/sbin/umount -f '{$mountname}'") == 0) {
if(@rmdir("{$mountname}")) {
write_log("Successfully unmounted '{$mountname}'");
$result = 0;
} else {
write_log("Error: Failed to unmount '{$mountname}'");
$result = 1;
}
} else {
write_log("Error: Failed to unmount '{$mountname}'");
$result = 1;
}
// Detach memory disk for ISOs
if (0 == $result && "iso" === $mount['type']) {
$result = mwexec("/sbin/mdconfig -d -u {$mdunit}");
}
return $result;
}
// Advanced unmount the specified mount point without using the sharename,
// e.g. used when the 'sharename' has been modified/changed. Use the device
// special file (e.g. /dev/ad1p1) instead.
// Return 0 if successful, 1 if error
function disks_umount_ex($mount)
{
// Get current mount list
$amountlist = get_mounts_list();
foreach ($amountlist as $amountk => $amountv) {
if (0 == strcmp($amountv['devicespecialfile'], $mount['devicespecialfile'])) {
$mountname = $amountv['mp'];
break;
}
}
if ($mountname) {
if (mwexec("/sbin/umount -f '{$mountname}'") == 0) {
if(@rmdir("{$mountname}")) {
write_log("Successfully unmounted '{$mountname}'");
$result = 0;
} else {
write_log("Error: Failed to unmount '{$mountname}'");
$result = 1;
}
} else {
write_log("Error: Failed to unmount '{$mountname}'");
$result = 1;
}
}
return $result;
}
// Check if mount point is mounted.
// $mount - Mount point config array
// Return 0 if not mounted, otherwise 1
function disks_ismounted($mount)
{
// MUST CHECK IF IT'S NTFS filesystem
if (0 == strcmp($mount['fstype'],"ntfs")) {
return disks_ismounted_ex($mount['sharename'], "sharename");
} else {
return disks_ismounted_ex($mount['devicespecialfile'], "devicespecialfile");
}
}
// Check if mountpoint is mounted.
// $var - The attribute value to search for
// $key - The attribute name to search for
// e.g.: disks_ismounted_ex("/dev/ad0s1","devicespecialfile");
// disks_ismounted_ex("data","sharename");
// disks_ismounted_ex("ad0","mdisk");
// Return 0 if not mounted, otherwise 1
function disks_ismounted_ex($var,$key)
{
if (empty($var))
return 0;
$result = 0;
// Get current mount list
$amountlist = get_mounts_list();
foreach ($amountlist as $amountk => $amountv) {
switch ($key) {
case "mdisk":
case "devicespecialfile":
// Must find the $val (e.g. ad0) in 'mdisk' (e.g. ad0s1).
// Must find the $val (e.g. /dev/ad1) in 'devicespecialfile' (e.g. /dev/ad1s1).
if (0 == preg_match("/^.+\/(\S{2,}\d+)([sp]\d+)$/", $var, $matches)) {
// e.g. /dev/ad10, /dev/cd1
if (preg_match("/" . preg_quote($var, "/") . "\D+/", $amountv[$key])) {
$result = 1;
break;
}
} else {
// e.g. /dev/da1p2
if (false !== strstr($amountv[$key], $var)) {
$result = 1;
break;
}
}
break;
default:
if (0 == strcmp($amountv[$key], $var)) {
$result = 1;
break;
}
break;
}
}
return $result;
}
// Configure, create and start gvinum volume.
// $uuid - UUID of the RAID to be configured.
// Return 0 if successful, 1 if error
function disks_raid_gvinum_configure($uuid) {
global $g, $config;
if (!is_array($config['gvinum']['vdisk']))
return 1;
$index = array_search_ex($uuid, $config['gvinum']['vdisk'], "uuid");
if (false === $index)
return 1;
$vdisk = $config['gvinum']['vdisk'][$index];
if (!is_array($vdisk))
return 1;
// Generate the raid.conf file
$fd = fopen("{$g['varetc_path']}/raid-{$vdisk['name']}.conf", "w");
if (!$fd) {
write_console("Error: Failed to open raid.conf in services_raid_configure().\n");
return 1;
}
$raidconf="";
foreach ($vdisk['device'] as $devicek => $devicev) {
$raidconf .= <<<EOD
drive disk_{$devicev} device {$devicev}
EOD;
}
$raidconf .= <<<EOD
volume {$vdisk['name']}
EOD;
switch ($vdisk['type']) {
case "0":
$raidconf .= <<<EOD
plex org striped 256k
EOD;
foreach ($vdisk['device'] as $devicek => $devicev) {
/* Get the disksize */
$diskinfo=disks_get_diskinfo($devicev);
/* $raidconf .= <<<EOD
sd length {$diskinfo['mediasize_mbytes']}M drive disk_{$devicev}
EOD; */
$raidconf .= <<<EOD
sd length 0 drive disk_{$devicev}
EOD;
}
break;
case "1":
foreach ($vdisk['device'] as $devicek => $devicev) {
$raidconf .= <<<EOD
plex org concat
EOD;
/* Get the disksize */
$diskinfo=disks_get_diskinfo($devicev);
/* $raidconf .= <<<EOD
sd length {$diskinfo['mediasize_mbytes']}M drive disk_{$devicev}
EOD; */
$raidconf .= <<<EOD
sd length 0 drive disk_{$devicev}
EOD;
}
break;
case "5":
$raidconf .= <<<EOD
plex org raid5 256k
EOD;
foreach ($vdisk['device'] as $devicek => $devicev) {
/* Get the disksize */
$diskinfo=disks_get_diskinfo($devicev);
/* $raidconf .= <<<EOD
sd length {$diskinfo['mediasize_mbytes']}M drive disk_{$devicev}
EOD; */
$raidconf .= <<<EOD
sd length 0 drive disk_{$devicev}
EOD;
}
break;
}
fwrite($fd, $raidconf);
fclose($fd);
// Create volume
mwexec("/sbin/gvinum create {$g['varetc_path']}/raid-{$vdisk['name']}.conf", true);
// Start volume
mwexec("/sbin/gvinum start {$vdisk['name']}", true);
return 0;
}
// Configure, create and start gmirror volume.
// $uuid - UUID of the RAID to be configured.
// Return 0 if successful, 1 if error
function disks_raid_gmirror_configure($uuid) {
global $config;
if (!is_array($config['gmirror']['vdisk']))
return 1;
$index = array_search_ex($uuid, $config['gmirror']['vdisk'], "uuid");
if (false === $index)
return 1;
$vdisk = $config['gmirror']['vdisk'][$index];
if (!is_array($vdisk))
return 1;
// Create volume
$cmd = "/sbin/gmirror label -b {$vdisk['balance']} {$vdisk['name']} ";
foreach ($vdisk['device'] as $devicek => $devicev) {
$cmd .= "{$devicev} ";
}
return mwexec($cmd, true);
}
// Configure, create and start gconcat volume.
// $uuid - UUID of the RAID to be configured.
// Return 0 if successful, 1 if error
function disks_raid_gconcat_configure($uuid) {
global $config;
if (!is_array($config['gconcat']['vdisk']))
return 1;
$index = array_search_ex($uuid, $config['gconcat']['vdisk'], "uuid");
if (false === $index)
return 1;
$vdisk = $config['gconcat']['vdisk'][$index];
if (!is_array($vdisk))
return 1;
// Create volume
$cmd = "/sbin/gconcat label {$vdisk['name']} ";
foreach ($vdisk['device'] as $devicek => $devicev) {
$cmd .= "{$devicev} ";
}
return mwexec($cmd, true);
}
// Configure, create and start gstripe volume.
// $uuid - UUID of the RAID to be configured.
// Return 0 if successful, 1 if error
function disks_raid_gstripe_configure($uuid) {
global $config;
if (!is_array($config['gstripe']['vdisk']))
return 1;
$index = array_search_ex($uuid, $config['gstripe']['vdisk'], "uuid");
if (false === $index)
return 1;
$vdisk = $config['gstripe']['vdisk'][$index];
if (!is_array($vdisk))
return 1;
// Create volume
$cmd = "/sbin/gstripe label {$vdisk['name']} ";
foreach ($vdisk['device'] as $devicek => $devicev) {
$cmd .= "{$devicev} ";
}
return mwexec($cmd, true);
}
// Configure, create and start graid5 volume.
// $uuid - UUID of the RAID to be configured.
// Return 0 if successful, 1 if error
function disks_raid_graid5_configure($uuid) {
global $config;
if (!is_array($config['graid5']['vdisk']))
return 1;
$index = array_search_ex($uuid, $config['graid5']['vdisk'], "uuid");
if (false === $index)
return 1;
$vdisk = $config['graid5']['vdisk'][$index];
if (!is_array($vdisk))
return 1;
// Create volume
$cmd = "/sbin/graid5 label -s 131072 {$vdisk['name']} ";
foreach ($vdisk['device'] as $devicek => $devicev) {
$cmd .= "{$devicev} ";
}
return mwexec($cmd, true);
}
// Delete geom gvinum volume given in parameter.
// $uuid - UUID of the RAID to be deleted.
// Return 0 if successful, 1 if error
function disks_raid_gvinum_delete($uuid) {
global $config;
if (!is_array($config['gvinum']['vdisk']))
return 1;
$index = array_search_ex($uuid, $config['gvinum']['vdisk'], "uuid");
if (false === $index)
return 1;
$vdisk = $config['gvinum']['vdisk'][$index];
if (!is_array($vdisk))
return 1;
exec("/sbin/gvinum lv {$vdisk['name']}", $rawdata);
if (strpos($rawdata[0],"State: up") === false) {
return 0;
}
mwexec("/sbin/gvinum rm -r {$vdisk['name']}");
foreach ($vdisk['device'] as $device){
mwexec("/sbin/gvinum rm -r disk_{$device}");
}
return 0;
}
// Delete geom mirror volume given in parameter.
// $uuid - UUID of the RAID to be deleted.
// Return 0 if successful, 1 if error
function disks_raid_gmirror_delete($uuid) {
global $config;
if (!is_array($config['gmirror']['vdisk']))
return 1;
$index = array_search_ex($uuid, $config['gmirror']['vdisk'], "uuid");
if (false === $index)
return 1;
$vdisk = $config['gmirror']['vdisk'][$index];
if (!is_array($vdisk))
return 1;
// Stop the volume
mwexec("/sbin/gmirror stop {$vdisk['name']}");
// Clear the gmirror information on the hard drive
foreach ($vdisk['device'] as $device) {
mwexec("/sbin/gmirror clear {$device}");
}
return 0;
}
// Delete geom concat volume given in parameter.
// $uuid - UUID of the RAID to be deleted.
// Return 0 if successful, 1 if error
function disks_raid_gconcat_delete($uuid) {
global $config;
if (!is_array($config['gconcat']['vdisk']))
return 1;
$index = array_search_ex($uuid, $config['gconcat']['vdisk'], "uuid");
if (false === $index)
return 1;
$vdisk = $config['gconcat']['vdisk'][$index];
if (!is_array($vdisk))
return 1;
// Stop the volume
mwexec("/sbin/gconcat stop {$vdisk['name']}");
// Clear the gconcat information on the hard drive
foreach ($vdisk['device'] as $device) {
mwexec("/sbin/gconcat clear {$device}");
}
mwexec("/sbin/gconcat destroy {$vdisk['name']}");
return 0;
}
// Delete geom stripe volume given in parameter.
// $uuid - UUID of the RAID to be deleted.
// Return 0 if successful, 1 if error
function disks_raid_gstripe_delete($uuid) {
global $config;
if (!is_array($config['gstripe']['vdisk']))
return 1;
$index = array_search_ex($uuid, $config['gstripe']['vdisk'], "uuid");
if (false === $index)
return 1;
$vdisk = $config['gstripe']['vdisk'][$index];
if (!is_array($vdisk))
return 1;
// Stop the volume
mwexec("/sbin/gstripe stop {$vdisk['name']}");
// Clear the gconcat information on the hard drive
foreach ($vdisk['device'] as $device) {
mwexec("/sbin/gstripe clear {$device}");
}
mwexec("/sbin/gstripe destroy {$vdisk['name']}");
return 0;
}
// Delete geom raid5 volume given in parameter.
// $uuid - UUID of the RAID to be deleted.
// Return 0 if successful, 1 if error
function disks_raid_graid5_delete($uuid) {
global $config;
if (!is_array($config['graid5']['vdisk']))
return 1;
$index = array_search_ex($uuid, $config['graid5']['vdisk'], "uuid");
if (false === $index)
return 1;
$vdisk = $config['graid5']['vdisk'][$index];
if (!is_array($vdisk))
return 1;
// Stop the volume
// Arne Woerner advice: Don't stop the volume before deleting it.
// mwexec("/sbin/graid5 stop {$vdisk['name']}");
// Clear the graid5 information on the hard drive
foreach ($vdisk['device'] as $device){
// Get the name of the provider
$id = array_search_ex($device, $config['disks']['disk'], "devicespecialfile");
$provider = $config['disks']['disk'][$id]['name'];
// Remove provider
mwexec("/sbin/graid5 remove {$vdisk['name']} {$provider}");
}
mwexec("/sbin/graid5 destroy {$vdisk['name']}");
return 0;
}
// Initialize provider which needs to be encrypted.
// Return 0 if successful, 1 if error
function disks_geli_init($provider, $aalgo, $ealgo, $keylen, $passphrase, $verbose = false)
{
$param = "-t";
$result = 1;
// Add additional parameters
if (true == $verbose) $param .= " -v";
if (0 !== strcmp($aalgo, "none")) $param .= " -a {$aalgo}";
if (!empty($ealgo)) $param .= " -e {$ealgo}";
if (!empty($keylen)) $param .= " -l {$keylen}";
// Init disk
system("(/bin/echo {$passphrase}; /bin/echo {$passphrase}) | /sbin/geli init {$param} {$provider} 2>&1", $result);
return $result;
}
// Attach the given provider. The master key will be decrypted
// using the given passphrase/keyfile and a new GEOM provider
// will be created using the given provider's name with an ".eli"
// suffix.
// Return 0 if successful, 1 if error
function disks_geli_attach($provider, $passphrase, $verbose = false)
{
$param = "-t";
$result = 1;
// Add additional parameters
if (true == $verbose) $param .= " -v";
// Attach disk
system("(/bin/echo {$passphrase}; /bin/echo {$passphrase}) | /sbin/geli attach {$param} {$provider} 2>&1", $result);
return $result;
}
// Detach the given providers, which means remove the devfs entry
// and clear the keys from memory.
// Return 0 if successful, 1 if error
function disks_geli_detach($provider, $verbose = false)
{
if (true == $verbose)
$result = system("/sbin/geli detach {$provider} 2>&1", $result);
else
$result = mwexec("/sbin/geli detach {$provider}");
return $result;
}
// Kill (destroy) a geli encrypted volume.
// Return 0 if successful, 1 if error
function disks_geli_kill($provider)
{
$result = mwexec("/sbin/geli kill {$provider}");
return $result;
}
// Detach all geli encrypted disks.
// Return 0 if successful, 1 if error
function disks_geli_detach_all()
{
global $config;
$result = 0;
if (is_array($config['geli']['vdisk'])) {
foreach($config['geli']['vdisk'] as $geli) {
$result |= disks_geli_detach($geli['devicespecialfile']) ;
}
}
return $result;
}
// Change or setup (if not yet initialized) selected key.
// The passphrase can always be changed: for an attached provider and
// a detached provider. When a provider is attached, the user does not
// have to provide an old passphrase.
// Parameter:
// $devicespecialfile: e.g. /dev/ad1.eli or /dev/raid5/xxx.eli
// $oldpassphrase: Old passphrase
// $passphrase: New passphrase
// $verbose: Display additional information
// Return 0 if successful, 1 if error
function disks_geli_setkey($devicespecialfile, $oldpassphrase, $passphrase, $verbose = false)
{
$param = "-t";
$result = 1;
// Extract provider data.
$provider = preg_replace('/(\S+)(\.eli)/', '${1}', $devicespecialfile);
// Add additional parameters
if (true == $verbose) $param .= " -v";
if (0 == disks_exists($devicespecialfile)) {
// Provider is already attached
system("(/bin/echo {$passphrase}; /bin/echo {$passphrase}) | /sbin/geli setkey {$param} {$provider} 2>&1", $result);
} else {
// Provider has not already been attached
system("(/bin/echo {$oldpassphrase}; /bin/echo {$passphrase}; /bin/echo {$passphrase}) | /sbin/geli setkey {$param} {$provider} 2>&1", $result);
}
return $result;
}
// Format disk. Error and warning messages are redirected to stdout.
// Return none
function disks_format($disk, $type, $notinitmbr, $minspace, $volumelabel, $aft4k)
{
global $g;
// Get disk information
$diskinfo = disks_get_diskinfo($disk);
system("/sbin/sysctl kern.geom.debugflags=0x10 2>&1");
if (!$notinitmbr) {
echo gettext("Deleting MBR and partition table")."...\n";
$count = (1024*1024) / $diskinfo['sectorsize'];
system("/bin/dd if=/dev/zero of={$disk} bs={$diskinfo['sectorsize']} count={$count} 2>&1");
$oseek = $diskinfo['mediasize_sectors'] - $count;
system("/bin/dd if=/dev/zero of={$disk} bs={$diskinfo['sectorsize']} oseek={$oseek} count={$count} 2>&1");
}
if ($aft4k) {
// Advanced Format 4KB (512 x 8)
// IDENTIFY = 6003
$aft_secs = (1 << 3);
$aft_bytes = 512 * $aft_secs;
//$aft_align = $aft_secs; // 4KB
$aft_align = 64; // 32KB
// block:fragment ratio is 8:1
$aft_blocksize = $aft_bytes * 8;
// MBR(1) + Pri GPT header(1) + Pri GPT table(32)
// + Sec GPT table(32) + Sec GPT header(1)
$aft_gptstart = 1 + 1 + 32;
$aft_aligned = ceil($aft_gptstart / $aft_align) * $aft_align;
$aft_counts = $diskinfo['mediasize_sectors'];
$aft_counts -= 1 + ((1 + 32) * 2);
$aft_counts -= ($aft_aligned - $aft_gptstart);
$aft_counts = floor($aft_counts / $aft_align) * $aft_align;
}
switch ($type) {
case "ufsgpt":
echo gettext("Creating partition")."...\n";
// Destroy old GTP partition
//system("/sbin/gpt destroy {$disk} 1>/dev/null 2>&1");
system("/sbin/gpart destroy {$disk} 1>/dev/null 2>&1");
// Create new GPT partition
//system("/sbin/gpt create -f {$disk} 2>&1");
system("/sbin/gpart create -s gpt {$disk} 2>&1");
if ($aft4k) {
//system("/sbin/gpt add -b $aft_aligned -s $aft_counts -t ufs {$disk} 2>&1");
system("/sbin/gpart add -b $aft_aligned -s $aft_counts -t freebsd-ufs {$disk} 2>&1");
} else {
//system("/sbin/gpt add -t ufs {$disk} 2>&1");
system("/sbin/gpart add -t freebsd-ufs {$disk} 2>&1");
}
// Create filesystem
echo gettext("Creating filesystem with 'Soft Updates'")."...\n";
$param = "-U -j";
if (!empty($volumelabel))
$param .= " -L " . escapeshellarg($volumelabel);
if (!empty($minspace))
$param .= " -m {$minspace}";
if ($aft4k) {
echo sprintf(gettext("Using %s bytes fragment size."), $aft_bytes)."\n";
$param .= " -S $aft_bytes -b $aft_blocksize -f $aft_bytes";
}
//echo "/sbin/newfs {$param} {$disk}p1\n";
system("/sbin/newfs {$param} {$disk}p1 2>&1");
echo "\n";
echo gettext("Created UFS file system:")."\n";
system("/sbin/dumpfs -l {$disk}p1 2>&1");
echo "\n";
break;
case "softraid":
case "zfs":
// Calculate last sector of disk containing GEOM metadata.
$oseek = $diskinfo['mediasize_sectors'] - 1;
// Initialize disk
// Ensure that existing GEOM metadata is completely removed.
echo gettext("Deleting GEOM metadata")."...\n";
system("/bin/dd if=/dev/zero of={$disk} bs={$diskinfo['sectorsize']} oseek={$oseek} count=1 2>&1");
break;
case "msdos":
// Get valid CHS information. 'diskinfo' gets values from harddisk firmware that
// are maybe unusable by 'fdisk' (e.g. sectors = 255, fdisk requires <= 63).
disk_sanitize_bios_geom($diskinfo);
// Create fdisk config file (fdisk.conf)
if (1 == disks_create_fdisk_configfile(11, $diskinfo, "{$g['tmp_path']}/fdisk.conf")) {
echo("Error: Failed to create '{$g['tmp_path']}/fdisk.conf'.\n");
break;
}
// Initialize disk
echo gettext("Creating partition")."...\n";
system("/sbin/fdisk -v -f {$g['tmp_path']}/fdisk.conf {$disk} 2>&1");
@unlink("{$g['tmp_path']}/fdisk.conf");
// Create filesystem
echo gettext("Creating filesystem")."...\n";
$param = "-F 32";
if (!empty($volumelabel))
$param .= " -L " . escapeshellarg($volumelabel);
system("/sbin/newfs_msdos {$param} {$disk}s1 2>&1");
break;
case "ext2":
// Get valid CHS information. 'diskinfo' gets values from harddisk firmware that
// are maybe unusable by 'fdisk' (e.g. sectors = 255, fdisk requires <= 63).
disk_sanitize_bios_geom($diskinfo);
// Create fdisk config file (fdisk.conf)
if (1 == disks_create_fdisk_configfile(131, $diskinfo, "{$g['tmp_path']}/fdisk.conf")) {
echo("Error: Failed to create '{$g['tmp_path']}/fdisk.conf'.\n");
break;
}
// Initialize disk
echo gettext("Creating partition")."...\n";
system("/sbin/fdisk -v -f {$g['tmp_path']}/fdisk.conf {$disk} 2>&1");
@unlink("{$g['tmp_path']}/fdisk.conf");
// Create filesystem
// Note: Use revision 0 when formating EXT2 filesystems, otherwise
// mount_ext2fs fails because revision 1 uses a inode size of 256 bytes
// which is not supported by FreeBSD. Using 128 bytes via '-I 128'
// could solve that problem but the file system is permanently in
// 'not clean' state.
echo gettext("Creating filesystem")."...\n";
$param = "-r 0";
if (!empty($volumelabel))
$param .= " -L " . escapeshellarg($volumelabel);
system("/usr/local/sbin/mke2fs {$param} {$disk}s1 2>&1");
break;
}
system("/sbin/sysctl kern.geom.debugflags=0 2>&1");
echo gettext("Done")."!\n";
}
// File system consistency check and interactive repair.
// The filesystem type will be get from the mount configuration information.
// Parameter:
// device - Name of the device (e.g. /dev/ad1s1).
// umount - Unmount disk if necessary.
// Return 0 if successful, 1 if error
function disks_fsck($device,$umount = false)
{
global $config;
// Check if there exits any mounts
if(!is_array($config['mounts']['mount']))
return 1;
// Get the id of the disk
$id = array_search_ex($device, $config['mounts']['mount'], "devicespecialfile");
if (false === $id)
return 1;
// Get the mount configuration
$mount = $config['mounts']['mount'][$id];
// Check if disk is mounted
$ismounted = disks_ismounted($mount);
// Unmount disk temporarily if necessary
if($umount && $ismounted) {
echo("<strong class='red'>" . gettext("Note") . ":</strong> " . gettext("The disk is currently mounted! The mount point will be removed temporarily to perform selected command.") . "<br /><br />");
disks_umount($mount);
}
switch($mount['fstype']) {
case "cd9660":
case "ntfs":
break;
default:
system("/sbin/fsck -t {$mount['fstype']} -y -f " . escapeshellarg($device));
break;
}
// Remount disk again if necessary
if($umount && $ismounted) {
disks_mount($mount);
}
return 0;
}
// Get list of partition information from disk.
// $disk - Name of the disk (e.g. ad0)
// Result is in the form:
// [1] => Array
// (
// [start] => 31
// [size] => 409169
// [type] => 0xa5
// [flags] => 0x80
// )
//
// Called in disks_manage_tools.php
// Return array of partition info
function disks_get_partition_info($disk) {
exec("/sbin/fdisk -s {$disk}", $rawdata);
array_shift($rawdata);
array_shift($rawdata);
$result = array();
foreach($rawdata as $partinfo) {
$apartinfo = preg_split("/\s+/", $partinfo);
$partid = chop($apartinfo[1],":");
$result[$partid] = array();
$result[$partid]['start'] = chop($apartinfo[2]);
$result[$partid]['size'] = chop($apartinfo[3]);
$result[$partid]['type'] = chop($apartinfo[4]);
$result[$partid]['flags'] = chop($apartinfo[5]);
}
return $result;
}
// Get UFS ID from partition.
// the partition (e.g. ada1p1)
// Result is ufsid.
function disks_get_ufsid($partition) {
exec("/sbin/dumpfs -l ${partition}", $rawdata);
$info = preg_split("/\s+/", $rawdata[0]);
$ufsid = "";
if (preg_match("/^\/dev\/ufsid\/(.*)$/", $info[0], $m)) {
$ufsid = $m[1];
}
return $ufsid;
}
// Get disk information.
// $disk - Name of the disk (e.g. ad0)
// Result is in the form:
// [1] => Array
// (
// [name] => ad0
// [sectorsize] => 512
// [mediasize_bytes] => 5242503168
// [mediasize_mbytes] => 4999
// [mediasize_sectors] => 10239264
// [cylinders] => 10158
// [heads] => 16
// [sectors] => 63
// )
// Return array containing diskinfo.
function disks_get_diskinfo($disk)
{
exec("/usr/sbin/diskinfo {$disk}", $rawdata);
$diskinfo = preg_split("/\s+/", $rawdata[0]);
$result = array();
$result[name] = chop($diskinfo[0]);
$result[sectorsize] = chop($diskinfo[1]);
$result[mediasize_bytes] = chop($diskinfo[2]);
$result[mediasize_mbytes] = ceil(chop($diskinfo[2]) / 1024 / 1024);
$result[mediasize_sectors] = chop($diskinfo[3]);
$result[cylinders] = chop($diskinfo[4]);
$result[heads] = chop($diskinfo[5]);
$result[sectors] = chop($diskinfo[6]);
return $result;
}
// Get cylinders/heads/sectors from disk.
// $info - Array containing CHS information.
// [1] => Array
// (
// [cylinders] => 10158
// [heads] => 16
// [sectors] => 63
// )
// Return 0 if successful, 1 if error.
function disks_get_chs_info($disk, &$info)
{
if (!is_array($info))
return 1;
exec("/sbin/fdisk -t {$disk}", $rawdata);
// Parse string: cylinders=77826 heads=255 sectors/track=63 (16065 blks/cyl)
if (0 == preg_match("/.+cylinders=(\d+) heads=(\d+) sectors\/track=(\d+) .+/", implode($rawdata), $chs))
return 1;
$info['cylinders'] = $chs[1];
$info['heads'] = $chs[2];
$info['sectors'] = $chs[3];
return 0;
}
// Check if given disk is available.
// $disk - e.g. /dev/ad4, /dev/ad2.eli
// Return 0 if yes, 1 if no.
function disks_exists($disk)
{
if (file_exists($disk)) {
return 0;
}
return 1;
}
// Wrapper to execute geom commands.
// $class - Name of geom class (e.g. stripe, mirror, vinum, raid5, ...)
// $command - Command to execute (e.g. list, status, load, activate, ...).
// The commands depend on the GEOM class.
// $param - The command parameter.
// $verbose - Display command results or hide them.
// $stderr - Redirect stderr to stdout to display error messages too.
// $output - Output array filled with every line of output.
// Return 0 if successful, 1 if error.
function disks_geom_cmd($class, $command, $param, $verbose = false, $stderr = true, &$output = false)
{
$result = 1;
if ("vinum" !== $class)
$cmd = "/sbin/geom {$class} {$command} {$param}";
else
$cmd = "/sbin/gvinum {$command} {$param}"; // gvinum can't be executed via geom
if (true === $verbose) {
if (true === $stderr)
$cmd .= " 2>&1"; // Redirect error message to stdout
if (false !== $output) {
exec($cmd, $output, $result);
} else {
system($cmd, $result);
}
} else {
$result = mwexec($cmd);
}
return $result;
}
// Calculate valid cylinders/heads/sectors.
// Code taken from FreeBSD kernel /src/lib/libdisk/change.c.
// $info [in|out] - Array containing CHS information.
// [1] => Array
// (
// [name] => ad0
// [sectorsize] => 512
// [mediasize_bytes] => 5242503168
// [mediasize_mbytes] => 4999
// [mediasize_sectors] => 10239264
// [cylinders] => 10158
// [heads] => 16
// [sectors] => 63
// )
// Return: None
function disk_sanitize_bios_geom(&$info) {
$sane = 1;
if ($info['cylinders'] > 1024)
$sane = 0;
if ($info['heads'] > 16)
$sane = 0;
if ($info['sectors'] > 63)
$sane = 0;
if ($info['cylinders'] * $info['heads'] * $info['sectors'] != $info['mediasize_sectors'])
$sane = 0;
if (0 != $sane)
return;
// First try something that IDE can handle
$info['sectors'] = 63;
$info['heads'] = 16;
$info['cylinders'] = intval($info['mediasize_sectors'] / ($info['sectors'] * $info['heads']));
if ($info['cylinders'] < 1024)
return;
// Hmm, try harder...
// Assume standard SCSI parameter
$info['heads'] = 255;
$info['cylinders'] = intval($info['mediasize_sectors'] / ($info['sectors'] * $info['heads']));
}
// Create fdisk configuration file.
// $type - File system identified (131 for EXT2, 11 for MSDOS)
// $diskinfo - Array
// (
// [name] => ad0
// [sectorsize] => 512
// [mediasize_bytes] => 5242503168
// [mediasize_mbytes] => 4999
// [mediasize_sectors] => 10239264
// [cylinders] => 10158
// [heads] => 16
// [sectors] => 63
// )
// $file - Filename of fdisk configuration file.
// Return 0 if successful, 1 if error.
function disks_create_fdisk_configfile($type, $diskinfo, $file) {
global $g;
if (!is_array($diskinfo))
return 1;
$fdiskconf .= <<<EOD
g c{$diskinfo['cylinders']} h{$diskinfo['heads']} s{$diskinfo['sectors']}
p 1 {$type} {$diskinfo['sectors']} {$diskinfo['mediasize_sectors']}
p 2 0 0 0
p 3 0 0 0
p 4 0 0 0
a 1
EOD;
if (false === file_put_contents($file, $fdiskconf)) {
write_log("Error: Failed to create '{$file}'.");
return 1;
}
return 0;
}
/**
* Rescan disks after a Hotswap has been performed
*/
function disks_rescan() {
mwexec2("atacontrol list", $devicelist, $return_val);
if (true == $return_val) {
echo gettext("atacontrol list failed, atacontrol not found !!");
return $return_val;
}
$atachannels = array();
$channel = 'NONE';
foreach ($devicelist as $item) {
if (preg_match("/ATA\s+channel\s+(\d+):/i", $item, $matched)) {
$channel = 'ata' . $matched[1];
$atachannels[$channel] = 0;
} else if (preg_match("/no device present/i", $item, $matched)) {
$atachannels[$channel]++;
}
}
foreach ($atachannels as $channel => $count) {
if ($count == 2) {
echo gettext("Resetting channel ") . "$channel\n";
mwexec("atacontrol detach $channel");
sleep(1);
mwexec("atacontrol attach $channel");
}
}
}
?>
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.