-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathzfs.rs
1147 lines (1036 loc) · 39.4 KB
/
zfs.rs
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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//! Utilities for poking at ZFS.
use crate::{execute, PFEXEC};
use anyhow::anyhow;
use anyhow::bail;
use anyhow::Context;
use camino::{Utf8Path, Utf8PathBuf};
use itertools::Itertools;
use omicron_common::api::external::ByteCount;
use omicron_common::disk::CompressionAlgorithm;
use omicron_common::disk::DiskIdentity;
use omicron_common::disk::SharedDatasetConfig;
use omicron_uuid_kinds::DatasetUuid;
use std::collections::BTreeMap;
use std::fmt;
// These locations in the ramdisk must only be used by the switch zone.
//
// We need the switch zone online before we can create the U.2 drives and
// encrypt the zpools during rack initialization. Without the switch zone we
// cannot get the rack initialization request from wicketd in RSS which allows
// us to initialize the trust quorum and derive the encryption keys needed for
// the U.2 disks.
pub const ZONE_ZFS_RAMDISK_DATASET_MOUNTPOINT: &str = "/zone";
pub const ZONE_ZFS_RAMDISK_DATASET: &str = "rpool/zone";
pub const ZFS: &str = "/usr/sbin/zfs";
/// This path is intentionally on a `tmpfs` to prevent copy-on-write behavior
/// and to ensure it goes away on power off.
///
/// We want minimize the time the key files are in memory, and so we rederive
/// the keys and recreate the files on demand when creating and mounting
/// encrypted filesystems. We then zero them and unlink them.
pub const KEYPATH_ROOT: &str = "/var/run/oxide/";
/// Error returned by [`Zfs::list_datasets`].
#[derive(thiserror::Error, Debug)]
#[error("Could not list datasets within zpool {name}: {err}")]
pub struct ListDatasetsError {
name: String,
#[source]
err: crate::ExecutionError,
}
#[derive(thiserror::Error, Debug)]
pub enum DestroyDatasetErrorVariant {
#[error("Dataset not found")]
NotFound,
#[error(transparent)]
Other(crate::ExecutionError),
}
/// Error returned by [`Zfs::destroy_dataset`].
#[derive(thiserror::Error, Debug)]
#[error("Could not destroy dataset {name}: {err}")]
pub struct DestroyDatasetError {
name: String,
#[source]
pub err: DestroyDatasetErrorVariant,
}
#[derive(thiserror::Error, Debug)]
enum EnsureFilesystemErrorRaw {
#[error("ZFS execution error: {0}")]
Execution(#[from] crate::ExecutionError),
#[error("Filesystem does not exist, and formatting was not requested")]
NotFoundNotFormatted,
#[error("Unexpected output from ZFS commands: {0}")]
Output(String),
#[error("Failed to mount encrypted filesystem: {0}")]
MountEncryptedFsFailed(crate::ExecutionError),
#[error("Failed to mount overlay filesystem: {0}")]
MountOverlayFsFailed(crate::ExecutionError),
}
/// Error returned by [`Zfs::ensure_filesystem`].
#[derive(thiserror::Error, Debug)]
#[error("Failed to ensure filesystem '{name}': {err}")]
pub struct EnsureFilesystemError {
name: String,
#[source]
err: EnsureFilesystemErrorRaw,
}
/// Error returned by [`Zfs::set_oxide_value`]
#[derive(thiserror::Error, Debug)]
#[error("Failed to set values '{values}' on filesystem {filesystem}: {err}")]
pub struct SetValueError {
filesystem: String,
values: String,
err: crate::ExecutionError,
}
#[derive(thiserror::Error, Debug)]
enum GetValueErrorRaw {
#[error(transparent)]
Execution(#[from] crate::ExecutionError),
#[error("No value found with that name")]
MissingValue,
}
/// Error returned by [`Zfs::get_oxide_value`] or [`Zfs::get_value`].
#[derive(thiserror::Error, Debug)]
#[error("Failed to get value '{name}' from filesystem {filesystem}")]
pub struct GetValueError {
filesystem: String,
name: String,
#[source]
err: GetValueErrorRaw,
}
#[derive(Debug, thiserror::Error)]
#[error("Failed to list snapshots: {0}")]
pub struct ListSnapshotsError(#[from] crate::ExecutionError);
#[derive(Debug, thiserror::Error)]
#[error("Failed to create snapshot '{snap_name}' from filesystem '{filesystem}': {err}")]
pub struct CreateSnapshotError {
filesystem: String,
snap_name: String,
err: crate::ExecutionError,
}
#[derive(Debug, thiserror::Error)]
#[error("Failed to delete snapshot '{filesystem}@{snap_name}': {err}")]
pub struct DestroySnapshotError {
filesystem: String,
snap_name: String,
err: crate::ExecutionError,
}
/// Wraps commands for interacting with ZFS.
pub struct Zfs {}
/// Describes a mountpoint for a ZFS filesystem.
#[derive(Debug, Clone)]
pub enum Mountpoint {
#[allow(dead_code)]
Legacy,
Path(Utf8PathBuf),
}
impl fmt::Display for Mountpoint {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Mountpoint::Legacy => write!(f, "legacy"),
Mountpoint::Path(p) => write!(f, "{p}"),
}
}
}
/// This is the path for an encryption key used by ZFS
#[derive(Debug, Clone)]
pub struct Keypath(pub Utf8PathBuf);
impl fmt::Display for Keypath {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl Keypath {
/// Constructs a Keypath for the specified disk within the supplied root
/// directory.
///
/// By supplying "root", tests can override the location where these paths
/// are stored to non-global locations.
pub fn new<P: AsRef<Utf8Path>>(id: &DiskIdentity, root: &P) -> Keypath {
let keypath_root = Utf8PathBuf::from(KEYPATH_ROOT);
let mut keypath = keypath_root.as_path();
let keypath_directory = loop {
match keypath.strip_prefix("/") {
Ok(stripped) => keypath = stripped,
Err(_) => break root.as_ref().join(keypath),
}
};
std::fs::create_dir_all(&keypath_directory)
.expect("Cannot ensure directory for keys");
let filename = format!(
"{}-{}-{}-zfs-aes-256-gcm.key",
id.vendor, id.serial, id.model
);
let path: Utf8PathBuf =
[keypath_directory.as_str(), &filename].iter().collect();
Keypath(path)
}
}
#[derive(Debug)]
pub struct EncryptionDetails {
pub keypath: Keypath,
pub epoch: u64,
}
#[derive(Debug, Default)]
pub struct SizeDetails {
pub quota: Option<ByteCount>,
pub reservation: Option<ByteCount>,
pub compression: CompressionAlgorithm,
}
#[derive(Debug)]
pub struct DatasetProperties {
/// The Uuid of the dataset
pub id: Option<DatasetUuid>,
/// The full name of the dataset.
pub name: String,
/// Remaining space in the dataset and descendants.
pub avail: ByteCount,
/// Space used by dataset and descendants.
pub used: ByteCount,
/// Maximum space usable by dataset and descendants.
pub quota: Option<ByteCount>,
/// Minimum space guaranteed to dataset and descendants.
pub reservation: Option<ByteCount>,
/// The compression algorithm used for this dataset.
///
/// This probably aligns with a value from
/// [omicron_common::disk::CompressionAlgorithm], but is left as an untyped
/// string so that unexpected compression formats don't prevent inventory
/// from being collected.
pub compression: String,
}
impl DatasetProperties {
const ZFS_GET_PROPS: &'static str =
"oxide:uuid,name,avail,used,quota,reservation,compression";
}
impl TryFrom<&DatasetProperties> for SharedDatasetConfig {
type Error = anyhow::Error;
fn try_from(
props: &DatasetProperties,
) -> Result<SharedDatasetConfig, Self::Error> {
Ok(SharedDatasetConfig {
compression: props.compression.parse()?,
quota: props.quota,
reservation: props.reservation,
})
}
}
impl DatasetProperties {
/// Parses dataset properties, assuming that the caller is providing the
/// output of the following command as stdout:
///
/// zfs get \
/// [maybe depth arguments] \
/// -Hpo name,property,value,source $ZFS_GET_PROPS $DATASETS
fn parse_many(
stdout: &str,
) -> Result<Vec<DatasetProperties>, anyhow::Error> {
let name_prop_val_source_list = stdout.trim().split('\n');
let mut datasets: BTreeMap<&str, BTreeMap<&str, _>> = BTreeMap::new();
for name_prop_val_source in name_prop_val_source_list {
// "-H" indicates that these columns are tab-separated;
// each column may internally have whitespace.
let mut iter = name_prop_val_source.split('\t');
let (name, prop, val, source) = (
iter.next().context("Missing 'name'")?,
iter.next().context("Missing 'property'")?,
iter.next().context("Missing 'value'")?,
iter.next().context("Missing 'source'")?,
);
if let Some(extra) = iter.next() {
bail!("Unexpected column data: '{extra}'");
}
let props = datasets.entry(name).or_default();
props.insert(prop, (val, source));
}
datasets
.into_iter()
.map(|(dataset_name, props)| {
let id = props
.get("oxide:uuid")
.filter(|(prop, source)| {
// Dataset UUIDs are properties that are optionally attached to
// datasets. However, some datasets are nested - to avoid them
// from propagating, we explicitly ignore this value if it is
// inherited.
//
// This can be the case for the "zone" filesystem root, which
// can propagate this property to a child zone without it set.
!source.starts_with("inherited") && *prop != "-"
})
.map(|(prop, _source)| {
prop.parse::<DatasetUuid>()
.context("Failed to parse UUID")
})
.transpose()?;
let name = dataset_name.to_string();
let avail = props
.get("available")
.map(|(prop, _source)| prop)
.ok_or(anyhow!("Missing 'available'"))?
.parse::<u64>()
.context("Failed to parse 'available'")?
.try_into()?;
let used = props
.get("used")
.map(|(prop, _source)| prop)
.ok_or(anyhow!("Missing 'used'"))?
.parse::<u64>()
.context("Failed to parse 'used'")?
.try_into()?;
// The values of "quota" and "reservation" can be either "-" or
// "0" when they are not actually set. To be cautious, we treat
// both of these values as "the value has not been set
// explicitly". As a result, setting either of these values
// explicitly to zero is indistinguishable from setting them
// with a value of "none".
let quota = props
.get("quota")
.filter(|(prop, _source)| *prop != "-" && *prop != "0")
.map(|(prop, _source)| {
prop.parse::<u64>().context("Failed to parse 'quota'")
})
.transpose()?
.and_then(|v| ByteCount::try_from(v).ok());
let reservation = props
.get("reservation")
.filter(|(prop, _source)| *prop != "-" && *prop != "0")
.map(|(prop, _source)| {
prop.parse::<u64>()
.context("Failed to parse 'reservation'")
})
.transpose()?
.and_then(|v| ByteCount::try_from(v).ok());
let compression = props
.get("compression")
.map(|(prop, _source)| prop.to_string())
.ok_or_else(|| anyhow!("Missing 'compression'"))?;
Ok(DatasetProperties {
id,
name,
avail,
used,
quota,
reservation,
compression,
})
})
.collect::<Result<Vec<_>, _>>()
}
}
#[derive(Debug, Copy, Clone)]
pub enum PropertySource {
Local,
Default,
Inherited,
Temporary,
None,
}
impl fmt::Display for PropertySource {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let ps = match self {
PropertySource::Local => "local",
PropertySource::Default => "default",
PropertySource::Inherited => "inherited",
PropertySource::Temporary => "temporary",
PropertySource::None => "none",
};
write!(f, "{ps}")
}
}
#[derive(Copy, Clone, Debug)]
pub enum WhichDatasets {
SelfOnly,
SelfAndChildren,
}
fn build_zfs_set_key_value_pairs(
size_details: Option<SizeDetails>,
dataset_id: Option<DatasetUuid>,
) -> Vec<(&'static str, String)> {
let mut props = Vec::new();
if let Some(SizeDetails { quota, reservation, compression }) = size_details
{
let quota = quota
.map(|q| q.to_bytes().to_string())
.unwrap_or_else(|| String::from("none"));
props.push(("quota", quota));
let reservation = reservation
.map(|r| r.to_bytes().to_string())
.unwrap_or_else(|| String::from("none"));
props.push(("reservation", reservation));
let compression = compression.to_string();
props.push(("compression", compression));
}
if let Some(id) = dataset_id {
props.push(("oxide:uuid", id.to_string()));
}
props
}
impl Zfs {
/// Lists all datasets within a pool or existing dataset.
///
/// Strips the input `name` from the output dataset names.
pub fn list_datasets(name: &str) -> Result<Vec<String>, ListDatasetsError> {
let mut command = std::process::Command::new(ZFS);
let cmd = command.args(&["list", "-d", "1", "-rHpo", "name", name]);
let output = execute(cmd)
.map_err(|err| ListDatasetsError { name: name.to_string(), err })?;
let stdout = String::from_utf8_lossy(&output.stdout);
let filesystems: Vec<String> = stdout
.trim()
.split('\n')
.filter(|n| *n != name)
.map(|s| {
String::from(s.strip_prefix(&format!("{}/", name)).unwrap())
})
.collect();
Ok(filesystems)
}
/// Get information about datasets within a list of zpools / datasets.
/// Returns properties for all input datasets, and optionally, for
/// their children (depending on the value of [WhichDatasets] is provided
/// as input).
///
/// This function is similar to [Zfs::list_datasets], but provides a more
/// substantial results about the datasets found.
///
/// Sorts results and de-duplicates them by name.
pub fn get_dataset_properties(
datasets: &[String],
which: WhichDatasets,
) -> Result<Vec<DatasetProperties>, anyhow::Error> {
let mut command = std::process::Command::new(ZFS);
let cmd = command.arg("get");
match which {
WhichDatasets::SelfOnly => (),
WhichDatasets::SelfAndChildren => {
cmd.args(&["-d", "1"]);
}
}
cmd.args(&["-Hpo", "name,property,value,source"]);
// Note: this is tightly coupled with the layout of DatasetProperties
cmd.arg(DatasetProperties::ZFS_GET_PROPS);
cmd.args(datasets);
// We are intentionally ignoring the output status of this command.
//
// If one or more dataset doesn't exist, we can still read stdout to
// see about the ones that do exist.
let output = cmd.output().map_err(|err| {
anyhow!(
"Failed to get dataset properties for {datasets:?}: {err:?}"
)
})?;
let stdout = String::from_utf8(output.stdout)?;
DatasetProperties::parse_many(&stdout)
}
/// Return the name of a dataset for a ZFS object.
///
/// The object can either be a dataset name, or a path, in which case it
/// will be resolved to the _mounted_ ZFS dataset containing that path.
pub fn get_dataset_name(object: &str) -> Result<String, ListDatasetsError> {
let mut command = std::process::Command::new(ZFS);
let cmd = command.args(&["get", "-Hpo", "value", "name", object]);
execute(cmd)
.map(|output| {
String::from_utf8_lossy(&output.stdout).trim().to_string()
})
.map_err(|err| ListDatasetsError { name: object.to_string(), err })
}
/// Destroys a dataset.
pub fn destroy_dataset(name: &str) -> Result<(), DestroyDatasetError> {
let mut command = std::process::Command::new(PFEXEC);
let cmd = command.args(&[ZFS, "destroy", "-r", name]);
execute(cmd).map_err(|err| {
let variant = match err {
crate::ExecutionError::CommandFailure(info)
if info.stderr.contains("does not exist") =>
{
DestroyDatasetErrorVariant::NotFound
}
_ => DestroyDatasetErrorVariant::Other(err),
};
DestroyDatasetError { name: name.to_string(), err: variant }
})?;
Ok(())
}
/// Creates a new ZFS filesystem unless one already exists.
///
/// - `name`: the full path to the zfs dataset
/// - `mountpoint`: The expected mountpoint of this filesystem.
/// If the filesystem already exists, and is not mounted here, and error is
/// returned.
/// - `zoned`: identifies whether or not this filesystem should be
/// used in a zone. Only used when creating a new filesystem - ignored
/// if the filesystem already exists.
/// - `do_format`: if "false", prevents a new filesystem from being created,
/// and returns an error if it is not found.
/// - `encryption_details`: Ensures a filesystem as an encryption root.
/// For new filesystems, this supplies the key, and all datasets within this
/// root are implicitly encrypted. For existing filesystems, ensures that
/// they are mounted (and that keys are loaded), but does not verify the
/// input details.
/// - `size_details`: If supplied, sets size-related information. These
/// values are set on both new filesystem creation as well as when loading
/// existing filesystems.
/// - `additional_options`: Additional ZFS options, which are only set when
/// creating new filesystems.
#[allow(clippy::too_many_arguments)]
pub fn ensure_filesystem(
name: &str,
mountpoint: Mountpoint,
zoned: bool,
do_format: bool,
encryption_details: Option<EncryptionDetails>,
size_details: Option<SizeDetails>,
id: Option<DatasetUuid>,
additional_options: Option<Vec<String>>,
) -> Result<(), EnsureFilesystemError> {
let (exists, mounted) = Self::dataset_exists(name, &mountpoint)?;
let props = build_zfs_set_key_value_pairs(size_details, id);
if exists {
Self::set_values(name, props.as_slice()).map_err(|err| {
EnsureFilesystemError {
name: name.to_string(),
err: err.err.into(),
}
})?;
if encryption_details.is_none() {
// If the dataset exists, we're done. Unencrypted datasets are
// automatically mounted.
return Ok(());
} else {
if mounted {
// The dataset exists and is mounted
return Ok(());
}
// We need to load the encryption key and mount the filesystem
return Self::mount_encrypted_dataset(name);
}
}
if !do_format {
return Err(EnsureFilesystemError {
name: name.to_string(),
err: EnsureFilesystemErrorRaw::NotFoundNotFormatted,
});
}
// If it doesn't exist, make it.
let mut command = std::process::Command::new(PFEXEC);
let cmd = command.args(&[ZFS, "create"]);
if zoned {
cmd.args(&["-o", "zoned=on"]);
}
if let Some(details) = encryption_details {
let keyloc = format!("keylocation=file://{}", details.keypath);
let epoch = format!("oxide:epoch={}", details.epoch);
cmd.args(&[
"-o",
"encryption=aes-256-gcm",
"-o",
"keyformat=raw",
"-o",
&keyloc,
"-o",
&epoch,
]);
}
if let Some(opts) = additional_options {
for o in &opts {
cmd.args(&["-o", &o]);
}
}
cmd.args(&["-o", &format!("mountpoint={}", mountpoint), name]);
execute(cmd).map_err(|err| EnsureFilesystemError {
name: name.to_string(),
err: err.into(),
})?;
// We ensure that the currently running process has the ability to
// act on the underlying mountpoint.
if !zoned {
let mut command = std::process::Command::new(PFEXEC);
let user = whoami::username();
let mount = format!("{mountpoint}");
let cmd = command.args(["chown", "-R", &user, &mount]);
execute(cmd).map_err(|err| EnsureFilesystemError {
name: name.to_string(),
err: err.into(),
})?;
}
Self::set_values(name, props.as_slice()).map_err(|err| {
EnsureFilesystemError {
name: name.to_string(),
err: err.err.into(),
}
})?;
Ok(())
}
fn mount_encrypted_dataset(
name: &str,
) -> Result<(), EnsureFilesystemError> {
let mut command = std::process::Command::new(PFEXEC);
let cmd = command.args(&[ZFS, "mount", "-l", name]);
execute(cmd).map_err(|err| EnsureFilesystemError {
name: name.to_string(),
err: EnsureFilesystemErrorRaw::MountEncryptedFsFailed(err),
})?;
Ok(())
}
pub fn mount_overlay_dataset(
name: &str,
) -> Result<(), EnsureFilesystemError> {
let mut command = std::process::Command::new(PFEXEC);
let cmd = command.args(&[ZFS, "mount", "-O", name]);
execute(cmd).map_err(|err| EnsureFilesystemError {
name: name.to_string(),
err: EnsureFilesystemErrorRaw::MountOverlayFsFailed(err),
})?;
Ok(())
}
// Return (true, mounted) if the dataset exists, (false, false) otherwise,
// where mounted is if the dataset is mounted.
fn dataset_exists(
name: &str,
mountpoint: &Mountpoint,
) -> Result<(bool, bool), EnsureFilesystemError> {
let mut command = std::process::Command::new(ZFS);
let cmd = command.args(&[
"list",
"-Hpo",
"name,type,mountpoint,mounted",
name,
]);
// If the list command returns any valid output, validate it.
if let Ok(output) = execute(cmd) {
let stdout = String::from_utf8_lossy(&output.stdout);
let values: Vec<&str> = stdout.trim().split('\t').collect();
if &values[..3] != &[name, "filesystem", &mountpoint.to_string()] {
return Err(EnsureFilesystemError {
name: name.to_string(),
err: EnsureFilesystemErrorRaw::Output(stdout.to_string()),
});
}
let mounted = values[3] == "yes";
Ok((true, mounted))
} else {
Ok((false, false))
}
}
/// Set the value of an Oxide-managed ZFS property.
pub fn set_oxide_value(
filesystem_name: &str,
name: &str,
value: &str,
) -> Result<(), SetValueError> {
Zfs::set_value(filesystem_name, &format!("oxide:{}", name), value)
}
fn set_value(
filesystem_name: &str,
name: &str,
value: &str,
) -> Result<(), SetValueError> {
Self::set_values(filesystem_name, &[(name, value)])
}
fn set_values<K: std::fmt::Display, V: std::fmt::Display>(
filesystem_name: &str,
name_values: &[(K, V)],
) -> Result<(), SetValueError> {
if name_values.is_empty() {
return Ok(());
}
let mut command = std::process::Command::new(PFEXEC);
let cmd = command.args(&[ZFS, "set"]);
for (name, value) in name_values {
cmd.arg(format!("{name}={value}"));
}
cmd.arg(filesystem_name);
execute(cmd).map_err(|err| SetValueError {
filesystem: filesystem_name.to_string(),
values: name_values
.iter()
.map(|(k, v)| format!("{k}={v}"))
.join(","),
err,
})?;
Ok(())
}
/// Get the value of an Oxide-managed ZFS property.
pub fn get_oxide_value(
filesystem_name: &str,
name: &str,
) -> Result<String, GetValueError> {
let property = format!("oxide:{name}");
let [value] = Self::get_values(
filesystem_name,
&[&property],
Some(PropertySource::Local),
)?;
Ok(value)
}
/// Calls "zfs get" with a single value
pub fn get_value(
filesystem_name: &str,
name: &str,
) -> Result<String, GetValueError> {
let [value] = Self::get_values(filesystem_name, &[name], None)?;
Ok(value)
}
/// List all extant snapshots.
pub fn list_snapshots() -> Result<Vec<Snapshot>, ListSnapshotsError> {
let mut command = std::process::Command::new(ZFS);
let cmd = command.args(&["list", "-H", "-o", "name", "-t", "snapshot"]);
execute(cmd)
.map(|output| {
let stdout = String::from_utf8_lossy(&output.stdout);
stdout
.trim()
.lines()
.map(|line| {
let (filesystem, snap_name) =
line.split_once('@').unwrap();
Snapshot {
filesystem: filesystem.to_string(),
snap_name: snap_name.to_string(),
}
})
.collect()
})
.map_err(ListSnapshotsError::from)
}
/// Create a snapshot of a filesystem.
///
/// A list of properties, as name-value tuples, may be passed to this
/// method, for creating properties directly on the snapshots.
pub fn create_snapshot<'a>(
filesystem: &'a str,
snap_name: &'a str,
properties: &'a [(&'a str, &'a str)],
) -> Result<(), CreateSnapshotError> {
let mut command = std::process::Command::new(ZFS);
let mut cmd = command.arg("snapshot");
for (name, value) in properties.iter() {
cmd = cmd.arg("-o").arg(&format!("{name}={value}"));
}
cmd.arg(&format!("{filesystem}@{snap_name}"));
execute(cmd).map(|_| ()).map_err(|err| CreateSnapshotError {
filesystem: filesystem.to_string(),
snap_name: snap_name.to_string(),
err,
})
}
/// Destroy a named snapshot of a filesystem.
pub fn destroy_snapshot(
filesystem: &str,
snap_name: &str,
) -> Result<(), DestroySnapshotError> {
let mut command = std::process::Command::new(ZFS);
let path = format!("{filesystem}@{snap_name}");
let cmd = command.args(&["destroy", &path]);
execute(cmd).map(|_| ()).map_err(|err| DestroySnapshotError {
filesystem: filesystem.to_string(),
snap_name: snap_name.to_string(),
err,
})
}
/// Calls "zfs get" to acquire multiple values
///
/// - `names`: The properties being acquired
/// - `source`: The optioanl property source (origin of the property)
/// Defaults to "all sources" when unspecified.
pub fn get_values<const N: usize>(
filesystem_name: &str,
names: &[&str; N],
source: Option<PropertySource>,
) -> Result<[String; N], GetValueError> {
let mut cmd = std::process::Command::new(PFEXEC);
let all_names =
names.into_iter().map(|n| *n).collect::<Vec<&str>>().join(",");
cmd.args(&[ZFS, "get", "-Ho", "value", &all_names]);
if let Some(source) = source {
cmd.args(&["-s", &source.to_string()]);
}
cmd.arg(filesystem_name);
let output = execute(&mut cmd).map_err(|err| GetValueError {
filesystem: filesystem_name.to_string(),
name: format!("{:?}", names),
err: err.into(),
})?;
let stdout = String::from_utf8_lossy(&output.stdout);
let values = stdout.trim();
const EMPTY_STRING: String = String::new();
let mut result: [String; N] = [EMPTY_STRING; N];
for (i, value) in values.lines().enumerate() {
let value = value.trim();
if value == "-" {
return Err(GetValueError {
filesystem: filesystem_name.to_string(),
name: names[i].to_string(),
err: GetValueErrorRaw::MissingValue,
});
}
result[i] = value.to_string();
}
Ok(result)
}
}
/// A read-only snapshot of a ZFS filesystem.
#[derive(Clone, Debug)]
pub struct Snapshot {
pub filesystem: String,
pub snap_name: String,
}
impl Snapshot {
/// Return the full path to the snapshot directory within the filesystem.
pub fn full_path(&self) -> Result<Utf8PathBuf, GetValueError> {
let mountpoint = Zfs::get_value(&self.filesystem, "mountpoint")?;
Ok(Utf8PathBuf::from(mountpoint)
.join(format!(".zfs/snapshot/{}", self.snap_name)))
}
}
impl fmt::Display for Snapshot {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}@{}", self.filesystem, self.snap_name)
}
}
/// Returns all datasets managed by Omicron
pub fn get_all_omicron_datasets_for_delete() -> anyhow::Result<Vec<String>> {
let mut datasets = vec![];
// Collect all datasets within Oxide zpools.
//
// This includes cockroachdb, clickhouse, and crucible datasets.
let zpools = crate::zpool::Zpool::list()?;
for pool in &zpools {
let internal =
pool.kind() == omicron_common::zpool_name::ZpoolKind::Internal;
let pool = pool.to_string();
for dataset in &Zfs::list_datasets(&pool)? {
// Avoid erasing crashdump, backing data and swap datasets on
// internal pools. The swap device may be in use.
if internal
&& (["crash", "backing", "swap"].contains(&dataset.as_str())
|| dataset.starts_with("backing/"))
{
continue;
}
datasets.push(format!("{pool}/{dataset}"));
}
}
// Collect all datasets for ramdisk-based Oxide zones, if any exist.
if let Ok(ramdisk_datasets) = Zfs::list_datasets(&ZONE_ZFS_RAMDISK_DATASET)
{
for dataset in &ramdisk_datasets {
datasets.push(format!("{}/{dataset}", ZONE_ZFS_RAMDISK_DATASET));
}
};
Ok(datasets)
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn parse_dataset_props() {
let input = "dataset_name\tavailable\t1234\t-\n\
dataset_name\tused\t5678\t-\n\
dataset_name\tname\tI_AM_IGNORED\t-\n\
dataset_name\tcompression\toff\tinherited from parent";
let props = DatasetProperties::parse_many(&input)
.expect("Should have parsed data");
assert_eq!(props.len(), 1);
assert_eq!(props[0].id, None);
assert_eq!(props[0].name, "dataset_name");
assert_eq!(props[0].avail.to_bytes(), 1234);
assert_eq!(props[0].used.to_bytes(), 5678);
assert_eq!(props[0].quota, None);
assert_eq!(props[0].reservation, None);
assert_eq!(props[0].compression, "off");
}
#[test]
fn parse_dataset_too_many_columns() {
let input = "dataset_name\tavailable\t1234\t-\tEXTRA\n\
dataset_name\tused\t5678\t-\n\
dataset_name\tname\tI_AM_IGNORED\t-\n\
dataset_name\tcompression\toff\tinherited from parent";
let err = DatasetProperties::parse_many(&input)
.expect_err("Should have parsed data");
assert!(
err.to_string().contains("Unexpected column data: 'EXTRA'"),
"{err}"
);
}
#[test]
fn parse_dataset_props_with_optionals() {
let input =
"dataset_name\toxide:uuid\td4e1e554-7b98-4413-809e-4a42561c3d0c\tlocal\n\
dataset_name\tavailable\t1234\t-\n\
dataset_name\tused\t5678\t-\n\
dataset_name\tquota\t111\t-\n\
dataset_name\treservation\t222\t-\n\
dataset_name\tcompression\toff\tinherited from parent";
let props = DatasetProperties::parse_many(&input)
.expect("Should have parsed data");
assert_eq!(props.len(), 1);
assert_eq!(
props[0].id,
Some("d4e1e554-7b98-4413-809e-4a42561c3d0c".parse().unwrap())
);
assert_eq!(props[0].name, "dataset_name");
assert_eq!(props[0].avail.to_bytes(), 1234);
assert_eq!(props[0].used.to_bytes(), 5678);
assert_eq!(props[0].quota.map(|q| q.to_bytes()), Some(111));
assert_eq!(props[0].reservation.map(|r| r.to_bytes()), Some(222));
assert_eq!(props[0].compression, "off");
}
#[test]
fn parse_dataset_bad_uuid() {
let input = "dataset_name\toxide:uuid\tbad\t-\n\
dataset_name\tavailable\t1234\t-\n\
dataset_name\tused\t5678\t-";
let err = DatasetProperties::parse_many(&input)
.expect_err("Should have failed to parse");
assert!(
format!("{err:#}").contains("error parsing UUID (dataset)"),
"{err}"
);
}
#[test]
fn parse_dataset_bad_avail() {
let input = "dataset_name\tavailable\tBADAVAIL\t-\n\
dataset_name\tused\t5678\t-";
let err = DatasetProperties::parse_many(&input)
.expect_err("Should have failed to parse");