-
Notifications
You must be signed in to change notification settings - Fork 494
/
Copy pathfilesystemmgmt.inc
1006 lines (992 loc) · 36.5 KB
/
filesystemmgmt.inc
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
<?php
/**
* This file is part of OpenMediaVault.
*
* @license http://www.gnu.org/licenses/gpl.html GPL Version 3
* @author Volker Theile <volker.theile@openmediavault.org>
* @copyright Copyright (c) 2009-2018 Volker Theile
*
* OpenMediaVault is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* OpenMediaVault is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenMediaVault. If not, see <http://www.gnu.org/licenses/>.
*/
require_once("openmediavault/functions.inc");
class OMVRpcServiceFileSystemMgmt extends \OMV\Rpc\ServiceAbstract {
/**
* Get the RPC service name.
*/
public function getName() {
return "FileSystemMgmt";
}
/**
* Initialize the RPC service.
*/
public function initialize() {
$this->registerMethod("enumerateFilesystems");
$this->registerMethod("enumerateMountedFilesystems");
$this->registerMethod("getList");
$this->registerMethod("getCandidates");
$this->registerMethod("create");
$this->registerMethod("resize");
$this->registerMethod("delete");
$this->registerMethod("mount");
$this->registerMethod("umount");
$this->registerMethod("hasFilesystem");
}
/**
* Enumerate all filesystems that have been detected, except the
* filesystem containing the operation system.
* @param params The method parameters.
* @param context The context of the caller.
* @return An array of objects with the following fields: \em uuid,
* \em devicefile, \em type, \em label, \em blocks, \em size,
* \em mountpoint, \em blocks, \em used, \em available, \em description,
* \em propposixacl, \em propquota, \em propresize, \em propfstab,
* \em mounted and \em percentage. Additional the internal fields
* \em _used and \em _readonly are set.
* @throw \OMV\Exception
*/
public function enumerateFilesystems($params, $context) {
// Validate the RPC caller context.
$this->validateMethodContext($context, [
"role" => OMV_ROLE_ADMINISTRATOR
]);
// Get list of all detected filesystems.
$filesystems = \OMV\System\Filesystem\Filesystem::getFilesystems();
// Process the detected filesystems and skip unwanted ones.
$result = [];
foreach ($filesystems as $fs) {
// Get the filesystem backend.
$fsb = $fs->getBackend();
if (is_null($fsb)) {
throw new \OMV\Exception(
"No filesystem backend set for '%s'.",
$fs->getDeviceFile());
}
// Set default values.
$object = [
"devicefile" => $fs->getPreferredDeviceFile(),
"parentdevicefile" => $fs->getParentDeviceFile(),
"uuid" => $fs->getUuid(),
"label" => $fs->getLabel(),
"type" => $fs->getType(),
"blocks" => "-1", // as string
"mounted" => FALSE,
"mountpoint" => "",
"used" => "-1", // as string
"available" => "-1", // as string
"size" => "-1", // as string
"percentage" => -1,
"description" => $fs->hasLabel() ? $fs->getLabel() :
$fs->getDeviceFile(),
"propposixacl" => $fsb->hasPosixAclSupport(),
"propquota" => $fsb->hasQuotaSupport(),
"propresize" => $fsb->hasResizeSupport(),
"propfstab" => $fsb->hasFstabSupport(),
"propreadonly" => $fsb->hasReadOnlySupport(),
"propcompress" => $fsb->hasCompressSupport(),
"propautodefrag" => $fsb->hasAutoDefragSupport(),
"hasmultipledevices" => $fs->hasMultipleDevices(),
"devicefiles" => $fs->getDeviceFiles(),
"_readonly" => $fsb->hasReadOnlySupport(),
"_used" => FALSE
];
// Check if the filesystem is used. First try to get the
// corresponding mount point configuration object. If such object
// exists, then check if it is referenced by any other object,
// e.g. by a shared folder configuration object.
if (FALSE !== ($meObject = \OMV\Rpc\Rpc::call("FsTab",
"getByFsName", [ "fsname" => $object['devicefile'] ],
$context))) {
$db = \OMV\Config\Database::getInstance();
$meObject = $db->get("conf.system.filesystem.mountpoint",
$meObject['uuid']);
$object['_used'] = $db->isReferenced($meObject);
}
// Mark the device where the operating system is installed on
// as used and read-only.
if (\OMV\System\System::isRootDeviceFile($object['devicefile'])) {
$object['_used'] = TRUE;
$object['_readonly'] = TRUE;
}
// If the filesystem is mounted then try to get more
// informations about it. Note,it is not possible to get
// details from unmounted filesystems, because on most kinds
// of systems doing so requires very nonportable intimate
// knowledge of filesystem structures. See man (1) df.
if (TRUE === $fs->isMounted()) {
$object['mounted'] = TRUE;
// Get some more filesystem details if possible.
if (FALSE !== ($fsStats = $fs->getStatistics())) {
$object['used'] = binary_format($fsStats['used']);
$object['available'] = $fsStats['available'];
$object['percentage'] = $fsStats['percentage'];
$object['blocks'] = $fsStats['blocks'];
$object['mountpoint'] = $fsStats['mountpoint'];
$object['size'] = $fsStats['size'];
$object['description'] = sprintf(
gettext("%s (%s available)"),
!empty($object['label']) ? $object['label'] :
$object['devicefile'], binary_format(
$object['available']));
}
}
$result[] = $object;
}
return $result;
}
/**
* Enumerate all filesystems that have a mount point configuration
* object, except binds, and that are actually mounted.
* @param params The method parameters.
* \em includeroot TRUE to append the filesystem '/dev/root' if mounted.
* Defaults to FALSE.
* @param context The context of the caller.
* @return An array of objects with the following fields: \em uuid,
* \em devicefile, \em type, \em label, \em blocks, \em size,
* \em mountpoint, \em blocks, \em used, \em available,
* \em description, \em percentage, \em propposixacl, \em propquota,
* \em propresize and \em propfstab.
*/
public function enumerateMountedFilesystems($params, $context) {
// Validate the RPC caller context.
$this->validateMethodContext($context, [
"role" => OMV_ROLE_ADMINISTRATOR
]);
// Validate the parameters of the RPC service method.
if (!is_null($params)) {
$this->validateMethodParams($params,
"rpc.filesystemmgmt.enumeratemountedfilesystems");
}
// Get list of mount points, except bind mounts.
$db = \OMV\Config\Database::getInstance();
$objects = $db->getByFilter("conf.system.filesystem.mountpoint", [
"operator" => "not",
"arg0" => [
"operator" => "stringContains",
"arg0" => "opts",
"arg1" => "bind"
]
]);
// Append '/dev/root'?
if (TRUE === array_boolval($params, "includeroot", FALSE)) {
$rootObject = new \OMV\Config\ConfigObject(
"conf.system.filesystem.mountpoint");
$rootObject->set("fsname", \OMV\System\System::getRootDeviceFile());
$rootObject->set("dir", "/");
array_unshift($objects, $rootObject);
}
// Get the file system details for each mount point.
$result = [];
foreach ($objects as $objectk => $objectv) {
// Get the filesystem backend.
$fsbMngr = \OMV\System\Filesystem\Backend\Manager::getInstance();
$fsb = $fsbMngr->getBackendById($objectv->get("fsname"));
if (is_null($fsb)) {
// The device may not exist anymore, e.g. a USB device. Skip it.
// throw new \OMV\Exception(
// "No file system backend exists for '%s'",
// $objectv['fsname']);
continue;
}
// Get the file system implementation.
$fs = $fsb->getImpl($objectv->get("fsname"));
if (is_null($fs) || !$fs->exists()) {
// throw new \OMV\Exception(
// "Failed to get the '%s' file system implementation or '%s' ".
// "does not exist", $fsb->getType(), $objectv['fsname']);
continue;
}
// Check if the given file system is mounted based on the configured
// mount point. Skip the file systems that are not mounted at the
// moment.
if (FALSE === $fs->isMounted())
continue;
// Get the filesystem details.
$object = [
"devicefile" => $fs->getPreferredDeviceFile(),
"parentdevicefile" => $fs->getParentDeviceFile(),
"uuid" => $fs->getUuid(),
"label" => $fs->getLabel(),
"type" => $fs->getType(),
"blocks" => "-1", // as string
"mountpoint" => $objectv->get("dir"),
"used" => "-1", // as string
"available" => "-1", // as string
"size" => "-1", // as string
"percentage" => -1,
"description" => $fs->hasLabel() ? $fs->getLabel() :
$fs->getDeviceFile(),
"propposixacl" => $fsb->hasPosixAclSupport(),
"propquota" => $fsb->hasQuotaSupport(),
"propresize" => $fsb->hasResizeSupport(),
"propfstab" => $fsb->hasFstabSupport(),
"propcompress" => $fsb->hasCompressSupport(),
"propautodefrag" => $fsb->hasAutoDefragSupport(),
"hasmultipledevices" => $fs->hasMultipleDevices(),
"devicefiles" => $fs->getDeviceFiles()
];
// Get some more filesystem details if possible.
if (FALSE !== ($fsStats = $fs->getStatistics())) {
$object['used'] = binary_format($fsStats['used']);
$object['available'] = $fsStats['available'];
$object['percentage'] = $fsStats['percentage'];
$object['blocks'] = $fsStats['blocks'];
$object['size'] = $fsStats['size'];
$object['description'] = sprintf(
gettext("%s (%s available)"), !empty($object['label']) ?
$object['label'] : $object['devicefile'], binary_format(
$object['available']));
}
$result[] = $object;
}
return $result;
}
/**
* Get the list of filesystems that have been detected.
* @param params An array containing the following fields:
* \em start The index where to start.
* \em limit The number of objects to process.
* \em sortfield The name of the column used to sort.
* \em sortdir The sort direction, ASC or DESC.
* @param context The context of the caller.
* @return An array of objects with the following fields: \em uuid,
* \em devicefile, \em type, \em label, \em blocks, \em size,
* \em mountpoint, \em blocks, \em used, \em available,
* \em description, \em mounted, \em percentage, \em status,
* \em propposixacl, \em propquota, \em propresize and \em propfstab.
* The field 'status' has the following meaning:<ul>
* \li 1 - Online
* \li 2 - Initializing in progress
* \li 3 - Missing
* </ul>
* Additional the internal fields \em _used and \em _readonly are set.
* @ŧhrow \OMV\Exception
*/
public function getList($params, $context) {
// Validate the RPC caller context.
$this->validateMethodContext($context, [
"role" => OMV_ROLE_ADMINISTRATOR
]);
// Validate the parameters of the RPC service method.
$this->validateMethodParams($params, "rpc.common.getlist");
// Enumerate all detected filesystems.
$objects = $this->callMethod("enumerateFilesystems", NULL, $context);
foreach ($objects as $objectk => &$objectv) {
// Mark each filesystem as as initialized and 'Online'
// by default.
$objectv['status'] = 1;
}
// Try to detect filesystems that are being initialized.
foreach (new \DirectoryIterator("/tmp") as $file) {
if ($file->isDot())
continue;
if (!$file->isFile())
continue;
// Check if it is a file we are interested in. The filename
// must look like omv-initfs@<device>.build, e.g.
// omv-initfs@_dev_sdb.build
$regex = '/^omv-initfs@.+\.build$/i';
if (1 !== preg_match($regex, $file->getFilename()))
continue;
$fileName = sprintf("/tmp/%s", $file->getFilename());
// Read the file content and decode JSON data into an
// associative array.
$jsonFile = new \OMV\Json\File($fileName);
$jsonFile->open("r");
$fsInfo = $jsonFile->read();
$jsonFile->close();
// Check whether the filesystem initialization process has
// been finished already. If yes, then unlink the file. The
// filesystem has then been already detected by blkid, thus
// it is already in the list of detected filesystems.
$initialized = FALSE;
foreach ($objects as $objectk => &$objectv) {
if ($objectv['devicefile'] === $fsInfo['devicefile']) {
$initialized = TRUE;
break;
}
}
if (TRUE === $initialized) {
if (TRUE === $jsonFile->exists())
$jsonFile->unlink();
continue;
}
// Get the filesystem backend.
$fsbMngr = \OMV\System\Filesystem\Backend\Manager::getInstance();
$fsb = $fsbMngr->getBackendByType($fsInfo['type']);
if (is_null($fsb)) {
throw new \OMV\Exception(
"No filesystem backend exists for '%s'.",
$fsInfo['type']);
}
// Add the filesystem to the result list.
$objects[] = [
"devicefile" => $fsInfo['devicefile'],
"parentdevicefile" => $fsInfo['parentdevicefile'],
"devicefiles" => [ $fsInfo['devicefile'] ],
"uuid" => "", // Not available
"label" => $fsInfo['label'],
"type" => $fsInfo['type'],
"blocks" => "-1", // as string
"mounted" => FALSE,
"mountable" => FALSE,
"mountpoint" => "",
"used" => "-1", // as string
"available" => "-1", // as string
"size" => "-1", // as string
"percentage" => -1,
"description" => "",
"propposixacl" => $fsb->hasPosixAclSupport(),
"propquota" => $fsb->hasQuotaSupport(),
"propresize" => $fsb->hasResizeSupport(),
"propfstab" => $fsb->hasFstabSupport(),
"propcompress" => $fsb->hasCompressSupport(),
"propautodefrag" => $fsb->hasAutoDefragSupport(),
"hasmultipledevices" => false,
"status" => 2,
"_used" => FALSE
];
}
// Add filesystems configured to be mounted but device does not
// exist anymore. This is necessary to be able remove invalid mount
// point configuration objects. Mark such filesystem as missing.
$db = \OMV\Config\Database::getInstance();
$mntents = $db->getByFilter("conf.system.filesystem.mountpoint", [
"operator" => "not",
"arg0" => [
"operator" => "or",
"arg0" => [
"operator" => "stringContains",
"arg0" => "opts",
"arg1" => "bind"
],
"arg1" => [
"operator" => "stringContains",
"arg0" => "opts",
"arg1" => "loop"
]
]
]);
foreach ($mntents as $mntentk => $mntentv) {
// Get the filesystem backend.
$fsbMngr = \OMV\System\Filesystem\Backend\Manager::getInstance();
$fsb = $fsbMngr->getBackendByType($mntentv->get("type"));
if (is_null($fsb)) {
throw new \OMV\Exception(
"No filesystem backend exists for '%s'.",
$mntentv->get("type"));
}
// Skip valid mount point configuration objects (the filesystem
// exists in this case).
$fs = $fsb->getImpl($mntentv->get("fsname"));
if (!is_null($fs) && $fs->exists())
continue;
// Append as much informations as possible.
$objects[] = [
"devicefile" => is_devicefile($mntentv->get("fsname")) ?
$mntentv->get("fsname") : "",
"devicefiles" => is_devicefile($mntentv->get("fsname")) ?
[ $mntentv->get("fsname") ] : [],
"uuid" => is_uuid($mntentv->get("fsname")) ?
$mntentv->get("fsname") : "",
"label" => "",
"type" => $mntentv->get("type"),
"blocks" => "-1", // as string
"mounted" => FALSE,
"mountable" => TRUE,
"mountpoint" => $mntentv->get("dir"),
"used" => "-1", // as string
"available" => "-1", // as string
"size" => "-1", // as string
"percentage" => -1,
"description" => "",
"propposixacl" => $fsb->hasPosixAclSupport(),
"propquota" => $fsb->hasQuotaSupport(),
"propresize" => $fsb->hasResizeSupport(),
"propfstab" => $fsb->hasFstabSupport(),
"propcompress" => $fsb->hasCompressSupport(),
"propautodefrag" => $fsb->hasAutoDefragSupport(),
"hasMultipleDevices" => FALSE,
"status" => 3,
"_used" => $db->isReferenced($mntentv)
];
}
// Filter result.
return $this->applyFilter($objects, $params['start'],
$params['limit'], $params['sortfield'], $params['sortdir']);
}
/**
* Get list of devices that can be used to create a filesystem on.
* @param params The method parameters.
* @param context The context of the caller.
* @return An array containing objects with the following fields:
* devicefile, size and description.
* @throw \OMV\Exception
*/
public function getCandidates($params, $context) {
// Validate the RPC caller context.
$this->validateMethodContext($context, [
"role" => OMV_ROLE_ADMINISTRATOR
]);
// Get a list of all potential usable devices.
if (FALSE === ($devs = \OMV\System\Storage\StorageDevice::enumerateUnused()))
throw new \OMV\Exception("Failed to get list of unused devices.");
// Get a list of all detected filesystems.
$filesystems = \OMV\System\Filesystem\Filesystem::getFilesystems();
// Get the list of device files that are occupied by a filesystem.
$usedDevs = [];
foreach ($filesystems as $filesystemk => $filesystemv) {
$usedDevs[] = $filesystemv->getParentDeviceFile();
}
// Prepare the result list.
$result = [];
foreach ($devs as $devk => $devv) {
// Get the storage device object for the specified device file.
$sd = \OMV\System\Storage\StorageDevice::getStorageDevice($devv);
if (is_null($sd) || !$sd->exists())
continue;
// Skip read-only devices like CDROM.
if (TRUE === $sd->isReadOnly())
continue;
/* Do not check for references, otherwise a device file which is configured
for S.M.A.R.T. monitoring is not added as a candidate.
// Check if the device is referenced/used by a plugin.
$db = \OMV\Config\Database::getInstance();
if (TRUE === $db->exists("conf.service", [
"operator" => "stringContains",
"arg0" => "devicefile",
"arg1" => $sd->getDeviceFile()
]))
continue;
*/
// Does this device already contain a filesystem?
if (in_array($sd->getCanonicalDeviceFile(), $usedDevs))
continue;
// The device is a potential candidate to create a filesystem
// on it.
$result[] = [
"devicefile" => $sd->getDeviceFile(),
"size" => $sd->getSize(),
"description" => $sd->getDescription()
];
}
return $result;
}
/**
* Create a filesystem on the given device.
* @param params An array containing the following fields:
* \em devicefile The block special device file.
* \em type The filesystem to create, e.g. ext3 or xfs.
* \em label The label of the filesystem.
* @param context The context of the caller.
* @return The name of the background process status file.
* @throw \OMV\Exception
*/
public function create($params, $context) {
// Validate the RPC caller context.
$this->validateMethodContext($context, [
"role" => OMV_ROLE_ADMINISTRATOR
]);
// Validate the parameters of the RPC service method.
$this->validateMethodParams($params, "rpc.filesystemmgmt.create");
// Get the storage device object.
\OMV\System\Storage\StorageDevice::assertStorageDeviceExists(
$params['devicefile']);
$sd = \OMV\System\Storage\StorageDevice::getStorageDevice(
$params['devicefile']);
// Check uniqueness. If there exists a mount point for the given
// device then it has already a filesystem that is in use.
if (FALSE !== \OMV\Rpc\Rpc::call("FsTab", "getByFsName", [
"fsname" => $sd->getDeviceFile()
], $context)) {
throw new \OMV\Exception("A mount point already exists for '%s'.",
$sd->getDeviceFile());
}
// Is the filesystem label unique?
if (FALSE === empty($params['label'])) {
$filesystems = \OMV\System\Filesystem\Filesystem::getFilesystems();
foreach ($filesystems as $filesystemk => $filesystemv) {
if (FALSE === $filesystemv->hasLabel())
continue;
if ($filesystemv->getLabel() === $params['label']) {
throw new \OMV\Exception("The label '%s' already exists.",
$params['label']);
}
}
}
// Get the storage device backend of the given device.
$sdbMngr = \OMV\System\Storage\Backend\Manager::getInstance();
$sdbMngr->assertBackendExists($sd->getDeviceFile());
$sdb = $sdbMngr->getBackend($sd->getDeviceFile());
// Get the corresponding filesystem backend.
$fsbMngr = \OMV\System\Filesystem\Backend\Manager::getInstance();
$fsbMngr->assertBackendExistsByType($params['type']);
$fsb = $fsbMngr->getBackendByType($params['type']);
// Get the filesystem device file name from the storage device
// backend (this may differ depending on the storage device).
$fsDeviceFile = $sdb->fsDeviceFile($sd->getDeviceFile());
// Create a file that contains the details of the filesystem being
// initialized. The file is parsed by the 'FileSystemMgmt.getList'
// RPC to display the state of the filesystem initialization
// process. There is no other way to detect filesystems being
// initialized (blkid detects them after the initialization has
// been finished).
$fileName = sprintf("/tmp/omv-initfs@%s.build", str_replace(
"/", "_", $sd->getDeviceFile()));
$jsonFile = new \OMV\Json\File($fileName);
// Create the background process.
return $this->execBgProc(function($bgStatusFilename, $bgOutputFilename)
use ($params, $sd, $sdb, $fsb, $fsDeviceFile, $jsonFile) {
// Create the file and write the file system information.
$jsonFile->open("c");
$jsonFile->write([
"devicefile" => $fsDeviceFile,
"parentdevicefile" => $sd->getDeviceFile(),
"type" => $fsb->getType(),
"label" => $params['label']
]);
$jsonFile->close();
// Wipe all existing data on the storage device.
$this->writeBgProcOutput($bgOutputFilename,
"===== Wipe signatures from device =====\n");
$sd->wipe();
// Create the partition if necessary.
switch ($sdb->getType()) {
case OMV_STORAGE_DEVICE_TYPE_SOFTWARERAID:
case OMV_STORAGE_DEVICE_TYPE_DEVICEMAPPER:
case OMV_STORAGE_DEVICE_TYPE_LOOPDEVICE:
// No need to create a partition for those types.
break;
default:
// Create a partition across the entire storage device.
$this->writeBgProcOutput($bgOutputFilename,
"===== Create partition =====\n");
$cmdArgs = [];
$cmdArgs[] = "--new=1:0:0";
$cmdArgs[] = "--typecode=1:8300";
$cmdArgs[] = "--print";
$cmdArgs[] = escapeshellarg($sd->getDeviceFile());
$cmd = new \OMV\System\Process("sgdisk", $cmdArgs);
$cmd->setRedirect2to1();
if (0 !== ($exitStatus = $this->exec($cmd->getCommandLine(),
$output, $bgOutputFilename))) {
throw new \OMV\ExecException($cmd->getCommandLine(),
$output, $exitStatus);
}
break;
}
// Re-read the partition table.
$cmdArgs = [];
$cmdArgs[] = escapeshellarg($sd->getDeviceFile());
$cmd = new \OMV\System\Process("partprobe", $cmdArgs);
$cmd->setRedirect2to1();
if (0 !== ($exitStatus = $this->exec($cmd->getCommandLine(),
$output, $bgOutputFilename))) {
throw new \OMV\ExecException($cmd->getCommandLine(),
$output, $exitStatus);
}
// We need to wait to give the kernel some time to re-read the
// partition table and until the device file exists. Abort if
// the device file does not exist after the specified time.
$fsbd = new \OMV\System\BlockDevice($fsDeviceFile);
$fsbd->waitForDevice(10);
// Create the file system.
$this->writeBgProcOutput($bgOutputFilename,
"===== Create file system =====\n");
$cmdArgs = [];
$cmdArgs[] = "-V";
$cmdArgs[] = sprintf("-t %s", $fsb->getType());
$cmdArgs[] = $fsb->getMkfsOptions($sd);
if (!empty($params['label'])) {
$cmdArgs[] = sprintf("-L %s", escapeshellarg(
$params['label']));
}
$cmdArgs[] = escapeshellarg($fsbd->getDeviceFile());
$cmd = new \OMV\System\Process("mkfs", $cmdArgs);
$cmd->setRedirect2to1();
if (0 !== ($exitStatus = $this->exec($cmd->getCommandLine(),
$output, $bgOutputFilename))) {
throw new \OMV\ExecException($cmd->getCommandLine(), $output,
$exitStatus);
}
// Notify configuration changes.
$dispatcher = \OMV\Engine\Notify\Dispatcher::getInstance();
$dispatcher->notify(OMV_NOTIFY_CREATE,
"org.openmediavault.conf.system.filesystem", [
"devicefile" => $fsDeviceFile,
"parentdevicefile" => $sd->getDeviceFile(),
"type" => $fsb->getType(),
"label" => $params['label']
]);
return $output;
}, NULL, function() use($jsonFile) {
// Cleanup
$jsonFile->unlink();
});
}
/**
* Resize a filesystem.
* @param params An array containing the following fields:
* \em id The UUID or block special device of the filesystem to resize.
* @param context The context of the caller.
* @return None.
* @throw \OMV\Exception
*/
public function resize($params, $context) {
// Validate the RPC caller context.
$this->validateMethodContext($context, [
"role" => OMV_ROLE_ADMINISTRATOR
]);
// Validate the parameters of the RPC service method.
$this->validateMethodParams($params, "rpc.filesystemmgmt.resize");
// Get the filesystem backend.
$fsbMngr = \OMV\System\Filesystem\Backend\Manager::getInstance();
$fsb = $fsbMngr->getBackendById($params['id']);
if (is_null($fsb)) {
throw new \OMV\Exception("No filesystem backend exists for '%s'.",
$params['id']);
}
// Check if the filesystem supports online resizing.
if (!$fsb->hasResizeSupport()) {
throw new \OMV\Exception(
"The filesystem '%s' (type=%s) does not support online resizing.",
$params['id'], $fsb->getType());
}
// Get the filesystem implementation.
$fs = $fsb->getImpl($params['id']);
if (is_null($fs) || !$fs->exists()) {
throw new \OMV\Exception(
"Failed to get the '%s' filesystem implementation or '%s' ".
"does not exist.", $fsb->getType(), $params['id']);
}
// Grow the filesystem.
$fs->grow();
}
/**
* Delete a filesystem. The filesystem will be unmounted and deleted.
* @param params An array containing the following fields:
* \em id The UUID or block special device of the filesystem to delete.
* @param context The context of the caller.
* @return None.
* @throw \OMV\Exception
*/
public function delete($params, $context) {
// Validate the RPC caller context.
$this->validateMethodContext($context, [
"role" => OMV_ROLE_ADMINISTRATOR
]);
// Validate the parameters of the RPC service method.
$this->validateMethodParams($params, "rpc.filesystemmgmt.delete");
// !!! Note !!!
// If the file system is missing some of the following code paths
// are ignored. In this case only the configuration is modified.
// Get the file system if available.
$fs = \OMV\System\Filesystem\Filesystem::getImpl($params['id']);
// Get the mount point configuration object that belongs to
// the file system to be deleted.
$filter = [
"operator" => "stringEquals",
"arg0" => "fsname",
"arg1" => $params['id']
];
// If the file system exists, then append additional filters
// to be sure to find a mount point configuration object.
if (!is_null($fs) && $fs->exists()) {
$devs = $fs->getDeviceFileSymlinks();
foreach ($devs as $devk => $devv) {
$filter = [
"operator" => "or",
"arg0" => $filter,
"arg1" => [
"operator" => "stringEquals",
"arg0" => "fsname",
"arg1" => $devv
]
];
}
}
try {
$db = \OMV\Config\Database::getInstance();
$meObject = $db->getByFilter("conf.system.filesystem.mountpoint",
$filter, 1);
} catch(\Exception $e) {
$meObject = NULL;
}
// Initialize the default notification object.
$fsObject = [
"devicefile" => is_devicefile($params['id']) ? $params['id'] : "",
"uuid" => is_uuid($params['id']) ? $params['id'] : "",
"label" => "",
"type" => ""
];
// Update the notification object.
if (!is_null($fs) && $fs->exists()) {
$fsObject = [
"devicefile" => $fs->getDeviceFile(),
"uuid" => $fs->getUuid(),
"label" => $fs->getLabel(),
"type" => $fs->getType()
];
} else if (!is_null($meObject)) {
$fsObject['type'] = $meObject->get("type");
}
// Notify configuration changes.
$dispatcher = \OMV\Engine\Notify\Dispatcher::getInstance();
$dispatcher->notify(OMV_NOTIFY_PREDELETE,
"org.openmediavault.conf.system.filesystem", $fsObject);
// Delete the associated fstab mount point entry.
if (!is_null($meObject)) {
// Delete the mount point configuration object. Unmount the
// filesystem and unlink the mount point. Changes to the fstab
// module must not be applied immediately.
\OMV\Rpc\Rpc::call("FsTab", "delete", [
"uuid" => $meObject->get("uuid")
], $context);
\OMV\Rpc\Rpc::call("Config", "applyChanges", [
"modules" => [ "fstab" ],
"force" => TRUE
], $context);
}
// Finally erase the filesystem.
if (!is_null($fs) && $fs->exists())
$fs->remove();
// Notify configuration changes.
$dispatcher->notify(OMV_NOTIFY_DELETE,
"org.openmediavault.conf.system.filesystem", $fsObject);
// Return the configuration object.
return $fsObject;
}
/**
* Mount a filesystem.
* @param params An array containing the following fields:
* \em id The UUID or block special device of the filesystem to mount.
* \em fstab If set to FALSE, no fstab entry will be created, thus the
* given filesystem is mounted only.
* @param context The context of the caller.
* @return None.
* @throw \OMV\Exception
* @throw \OMV\Config\ConfigDirtyException
*/
public function mount($params, $context) {
// Validate the RPC caller context.
$this->validateMethodContext($context, [
"role" => OMV_ROLE_ADMINISTRATOR
]);
// Validate the parameters of the RPC service method.
$this->validateMethodParams($params, "rpc.filesystemmgmt.mount");
// Get the corresponding filesystem backend.
$fsbMngr = \OMV\System\Filesystem\Backend\Manager::getInstance();
$fsbMngr->assertBackendExistsById($params['id']);
$fsb = $fsbMngr->getBackendById($params['id']);
// Get the filesystem.
$fs = $fsb->getImpl($params['id']);
if (is_null($fs) || !$fs->exists()) {
throw new \OMV\Exception(
"Failed to get the '%s' filesystem implementation or '%s' ".
"does not exist.", $fsb->getType(), $params['id']);
}
// Get the parent storage device containing the filesystem, e.g.
// /dev/sdb or /dev/cciss/c0d0.
$parentDeviceFile = $fs->getParentDeviceFile();
if (FALSE === is_devicefile($parentDeviceFile)) {
throw new \OMV\Exception(
"Failed to get parent storage device file from '%s'.",
$fs->getDeviceFile());
}
// Get the according storage device object.
\OMV\System\Storage\StorageDevice::assertStorageDeviceExists(
$parentDeviceFile);
$sd = \OMV\System\Storage\StorageDevice::getStorageDevice(
$parentDeviceFile);
// Get a predictable device file of the filesystem:
// - /dev/disk/by-label/xxx
// - /dev/disk/by-id/xxx
// - /dev/xxx
// Do not use the filesystem UUID itself, e.g.:
// UUID=448f889a-105b-11e7-a91c-2b545744f57a
// This is because this will make problems with LV/BTRFS snapshots
// due the fact that they have the same filesystem UUID as their
// origin.
$fsName = $fs->getPredictableDeviceFile();
// Try to obtain the mount point configuration object if this exists.
$meObject = \OMV\Rpc\Rpc::call("FsTab", "getByFsName", [
"fsname" => $fsName
], $context);
// Create fstab entry?
if (TRUE === boolvalEx($params['fstab'])) {
// Check for duplicates. Create a new mount point configuration
// object if necessary.
if (FALSE === $meObject) {
\OMV\Rpc\Rpc::call("FsTab", "set", [
"uuid" => \OMV\Environment::get(
"OMV_CONFIGOBJECT_NEW_UUID"),
"fsname" => $fsName,
"dir" => \OMV\System\MountPoint::buildPath($fsName),
"type" => $fs->getType(),
"opts" => implode(",", $fsb->getFstabMntOptions($sd)),
"freq" => 0,
"passno" => 2
], $context);
// Apply the changes to the '/etc/fstab' file immediately
// to mount the filesystem.
\OMV\Rpc\Rpc::call("Config", "applyChanges", [
"modules" => [ "fstab" ],
"force" => TRUE
], $context);
} else {
// Check if the configuration is marked as dirty, otherwise
// the /etc/fstab file is not up-to-date and the mount fails
// because of missing entries.
if ($this->isModuleDirty("fstab"))
throw new \OMV\Config\ConfigDirtyException();
// Umount the mount point if it is already in use, which
// is the case when an USB device is unplugged without
// unmounting it.
if (TRUE === $fs->isMounted())
$fs->umount();
// Does the mount directory exist? Re-create it if
// necessary.
$mp = new \OMV\System\MountPoint($meObject['dir']);
$mp->create();
// Finally mount the filesystem.
$fs->mount();
}
} else {
// Try to create/re-create the mount directory. This is only
// possible if a appropriate mount point configuration object
// exists.
if ((FALSE !== $meObject) && is_object($meObject)) {
$mp = new \OMV\System\MountPoint($meObject['dir']);
$mp->create();
}
// Mount the filesystem.
if (TRUE === $fs->isMounted()) {
throw new \OMV\Exception(
"The filesystem '%s' is already mounted.",
$fsName);
}
$fs->mount();
}
}
/**
* Unmount a filesystem.
* @param params An array containing the following fields:
* \em id The UUID or block special device of the filesystem to unmount.
* \em fstab If set to FALSE, the fstab entry will not be removed (if
* existing), thus the given filesystem is unmounted only.
* @param context The context of the caller.
* @return None.
* @throw \OMV\Exception
*/
public function umount($params, $context) {
// Validate the RPC caller context.
$this->validateMethodContext($context, [
"role" => OMV_ROLE_ADMINISTRATOR
]);
// Validate the parameters of the RPC service method.
$this->validateMethodParams($params, "rpc.filesystemmgmt.umount");
// Get the file system.
$fs = \OMV\System\Filesystem\Filesystem::getImpl($params['id']);
// Remove fstab entry?
if (TRUE === boolvalEx($params['fstab'])) {
// Get the fstab mount point configuration object.
$filter = [
"operator" => "stringEquals",
"arg0" => "fsname",
"arg1" => $params['id']
];
// If the file system exists, then append additional filters
// to be sure to find a mount point configuration object.
if (!is_null($fs) && $fs->exists()) {
// Add all existing filesystem device files.
$devs = $fs->getDeviceFileSymlinks();
foreach ($devs as $devk => $devv) {
$filter = [
"operator" => "or",
"arg0" => $filter,
"arg1" => [
"operator" => "stringEquals",
"arg0" => "fsname",
"arg1" => $devv
]
];
}
// To keep backward compatibility we need to search for the
// filesystem UUID, too.
if (TRUE === $fs->hasUuid()) {
$filter = [
"operator" => "or",
"arg0" => $filter,
"arg1" => [
"operator" => "stringEquals",
"arg0" => "fsname",
"arg1" => $fs->getUuid()
]
];
}
}
$db = \OMV\Config\Database::getInstance();
$meObjects = $db->getByFilter("conf.system.filesystem.mountpoint",
$filter);
// If there is a mount point configuration object for this file
// system then delete it, otherwise unmount the file system only.
if (!empty($meObjects)) {
$meObject = $meObjects[0];
// Delete the mount point configuration object. Unmount the
// filesystem and unlink the mount point. Changes to the fstab
// module must not be applied immediately.
\OMV\Rpc\Rpc::call("FsTab", "delete", [
"uuid" => $meObject->get("uuid")
], $context);
\OMV\Rpc\Rpc::call("Config", "applyChanges", [
"modules" => [ "fstab" ],
"force" => TRUE
], $context);
// Exit here, everything is done.
return;
} else {
// This file systems seems to be not under our control because
// of the missing 'conf.system.filesystem.mountpoint'
// configuration object. In this case unmount the file system
// if it exists, otherwise NO further action is necessary.
}
} else { // Only unmount the filesystem.
// The file system MUST exist here, otherwise throw an error.
\OMV\System\Filesystem\Filesystem::assertGetImpl($params['id']);
}
// Finally unmount the specified file system if it exists.
if (!is_null($fs) && $fs->exists()) {
if (TRUE === $fs->isMounted())
$fs->umount(TRUE);
}
}
/**
* Check if the given device containes a filesystem that is registered.
* @param params An array containing the following fields:
* \em devicefile The device file to check.
* @param context The context of the caller.
* @return TRUE if a filesystem exists on the given device, otherwise
* FALSE.
*/
public function hasFilesystem($params, $context) {
// Validate the RPC caller context.
$this->validateMethodContext($context, [
"role" => OMV_ROLE_ADMINISTRATOR
]);
// Validate the parameters of the RPC service method.