| Serge Bazanski | e50ec39 | 2020-06-30 21:41:39 +0200 | [diff] [blame] | 1 | // Copyright 2020 The Monogon Project Authors. | 
 | 2 | // | 
 | 3 | // SPDX-License-Identifier: Apache-2.0 | 
 | 4 | // | 
 | 5 | // Licensed under the Apache License, Version 2.0 (the "License"); | 
 | 6 | // you may not use this file except in compliance with the License. | 
 | 7 | // You may obtain a copy of the License at | 
 | 8 | // | 
 | 9 | //     http://www.apache.org/licenses/LICENSE-2.0 | 
 | 10 | // | 
 | 11 | // Unless required by applicable law or agreed to in writing, software | 
 | 12 | // distributed under the License is distributed on an "AS IS" BASIS, | 
 | 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
 | 14 | // See the License for the specific language governing permissions and | 
 | 15 | // limitations under the License. | 
 | 16 |  | 
 | 17 | package localstorage | 
 | 18 |  | 
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 19 | // Localstorage is a replacement for the old 'storage' internal library. It is | 
 | 20 | // currently unused, but will become so as the node code gets rewritten. | 
| Serge Bazanski | e50ec39 | 2020-06-30 21:41:39 +0200 | [diff] [blame] | 21 |  | 
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 22 | // The library is centered around the idea of a declarative filesystem tree | 
 | 23 | // defined as mutually recursive Go structs.  This structure is then Placed | 
 | 24 | // onto an abstract real filesystem (eg. a local POSIX filesystem at /), and a | 
 | 25 | // handle to that placed filesystem is then used by the consumers of this | 
 | 26 | // library to refer to subsets of the tree (that now correspond to locations on | 
 | 27 | // a filesystem). | 
| Serge Bazanski | e50ec39 | 2020-06-30 21:41:39 +0200 | [diff] [blame] | 28 | // | 
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 29 | // Every member of the storage hierarchy must either be, or inherit from | 
 | 30 | // Directory or File. In order to be placed correctly, Directory embedding | 
 | 31 | // structures must use `dir:` or `file:` tags for child Directories and files | 
 | 32 | // respectively. The content of the tag specifies the path part that this | 
 | 33 | // element will be placed at. | 
| Serge Bazanski | e50ec39 | 2020-06-30 21:41:39 +0200 | [diff] [blame] | 34 | // | 
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 35 | // Full placement path(available via FullPath()) format is placement | 
 | 36 | // implementation-specific. However, they're always strings. | 
| Serge Bazanski | e50ec39 | 2020-06-30 21:41:39 +0200 | [diff] [blame] | 37 |  | 
 | 38 | import ( | 
 | 39 | 	"sync" | 
 | 40 |  | 
| Serge Bazanski | 31370b0 | 2021-01-07 16:31:14 +0100 | [diff] [blame] | 41 | 	"source.monogon.dev/metropolis/node/core/localstorage/declarative" | 
| Serge Bazanski | e50ec39 | 2020-06-30 21:41:39 +0200 | [diff] [blame] | 42 | ) | 
 | 43 |  | 
 | 44 | type Root struct { | 
 | 45 | 	declarative.Directory | 
| Serge Bazanski | c2c7ad9 | 2020-07-13 17:20:09 +0200 | [diff] [blame] | 46 | 	// UEFI ESP partition, mounted from plaintext storage. | 
 | 47 | 	ESP ESPDirectory `dir:"esp"` | 
 | 48 | 	// Persistent Data partition, mounted from encrypted and authenticated storage. | 
 | 49 | 	Data DataDirectory `dir:"data"` | 
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 50 | 	// FHS-standard /etc directory, containes /etc/hosts, /etc/machine-id, and | 
 | 51 | 	// other compatibility files. | 
| Serge Bazanski | c2c7ad9 | 2020-07-13 17:20:09 +0200 | [diff] [blame] | 52 | 	Etc EtcDirectory `dir:"etc"` | 
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 53 | 	// Ephemeral data, used by runtime, stored in tmpfs. Things like sockets, | 
 | 54 | 	// temporary config files, etc. | 
| Serge Bazanski | cb883e2 | 2020-07-06 17:47:55 +0200 | [diff] [blame] | 55 | 	Ephemeral EphemeralDirectory `dir:"ephemeral"` | 
| Lorenz Brun | 764a2de | 2021-11-22 16:26:36 +0100 | [diff] [blame] | 56 | 	// FHS-standard /tmp directory, used by os.MkdirTemp. | 
| Serge Bazanski | c2c7ad9 | 2020-07-13 17:20:09 +0200 | [diff] [blame] | 57 | 	Tmp TmpDirectory `dir:"tmp"` | 
| Lorenz Brun | 74e8e5c | 2021-01-26 14:00:50 +0100 | [diff] [blame] | 58 | 	// FHS-standard /run directory. Used by various services. | 
 | 59 | 	Run RunDirectory `dir:"run"` | 
| Serge Bazanski | e50ec39 | 2020-06-30 21:41:39 +0200 | [diff] [blame] | 60 | } | 
 | 61 |  | 
 | 62 | type PKIDirectory struct { | 
 | 63 | 	declarative.Directory | 
 | 64 | 	CACertificate declarative.File `file:"ca.pem"` | 
 | 65 | 	Certificate   declarative.File `file:"cert.pem"` | 
 | 66 | 	Key           declarative.File `file:"cert-key.pem"` | 
 | 67 | } | 
 | 68 |  | 
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 69 | // DataDirectory is an xfs partition mounted via cryptsetup/LUKS, with a key | 
 | 70 | // derived from {global,local}Unlock keys. | 
| Serge Bazanski | e50ec39 | 2020-06-30 21:41:39 +0200 | [diff] [blame] | 71 | type DataDirectory struct { | 
 | 72 | 	declarative.Directory | 
 | 73 |  | 
 | 74 | 	// flagLock locks canMount and mounted. | 
 | 75 | 	flagLock sync.Mutex | 
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 76 | 	// canMount is set by Root when it is initialized. It is required to be set | 
 | 77 | 	// for mounting the data directory. | 
| Serge Bazanski | e50ec39 | 2020-06-30 21:41:39 +0200 | [diff] [blame] | 78 | 	canMount bool | 
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 79 | 	// mounted is set by DataDirectory when it is mounted. It ensures it's only | 
 | 80 | 	// mounted once. | 
| Serge Bazanski | e50ec39 | 2020-06-30 21:41:39 +0200 | [diff] [blame] | 81 | 	mounted bool | 
 | 82 |  | 
| Serge Bazanski | c2c7ad9 | 2020-07-13 17:20:09 +0200 | [diff] [blame] | 83 | 	Containerd declarative.Directory   `dir:"containerd"` | 
 | 84 | 	Etcd       DataEtcdDirectory       `dir:"etcd"` | 
 | 85 | 	Kubernetes DataKubernetesDirectory `dir:"kubernetes"` | 
| Serge Bazanski | 42e61c6 | 2021-03-18 15:07:18 +0100 | [diff] [blame] | 86 | 	Node       DataNodeDirectory       `dir:"node"` | 
| Serge Bazanski | c2c7ad9 | 2020-07-13 17:20:09 +0200 | [diff] [blame] | 87 | 	Volumes    DataVolumesDirectory    `dir:"volumes"` | 
| Serge Bazanski | e50ec39 | 2020-06-30 21:41:39 +0200 | [diff] [blame] | 88 | } | 
 | 89 |  | 
| Serge Bazanski | 42e61c6 | 2021-03-18 15:07:18 +0100 | [diff] [blame] | 90 | type DataNodeDirectory struct { | 
 | 91 | 	declarative.Directory | 
 | 92 | 	Credentials PKIDirectory `dir:"credentials"` | 
 | 93 | } | 
 | 94 |  | 
| Serge Bazanski | cb883e2 | 2020-07-06 17:47:55 +0200 | [diff] [blame] | 95 | type DataEtcdDirectory struct { | 
 | 96 | 	declarative.Directory | 
 | 97 | 	PeerPKI PKIDirectory          `dir:"peer_pki"` | 
 | 98 | 	PeerCRL declarative.File      `file:"peer_crl"` | 
 | 99 | 	Data    declarative.Directory `dir:"data"` | 
 | 100 | } | 
 | 101 |  | 
| Serge Bazanski | c2c7ad9 | 2020-07-13 17:20:09 +0200 | [diff] [blame] | 102 | type DataKubernetesDirectory struct { | 
 | 103 | 	declarative.Directory | 
 | 104 | 	ClusterNetworking DataKubernetesClusterNetworkingDirectory `dir:"clusternet"` | 
 | 105 | 	Kubelet           DataKubernetesKubeletDirectory           `dir:"kubelet"` | 
 | 106 | } | 
 | 107 |  | 
 | 108 | type DataKubernetesClusterNetworkingDirectory struct { | 
 | 109 | 	declarative.Directory | 
 | 110 | 	Key declarative.File `file:"private.key"` | 
 | 111 | } | 
 | 112 |  | 
 | 113 | type DataKubernetesKubeletDirectory struct { | 
 | 114 | 	declarative.Directory | 
 | 115 | 	Kubeconfig declarative.File `file:"kubeconfig"` | 
 | 116 | 	PKI        PKIDirectory     `dir:"pki"` | 
 | 117 |  | 
| Lorenz Brun | 842536b | 2021-01-26 13:54:57 +0100 | [diff] [blame] | 118 | 	DevicePlugins struct { | 
 | 119 | 		declarative.Directory | 
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 120 | 		// Used by Kubelet, hardcoded relative to | 
 | 121 | 		// DataKubernetesKubeletDirectory | 
| Lorenz Brun | 842536b | 2021-01-26 13:54:57 +0100 | [diff] [blame] | 122 | 		Kubelet declarative.File `file:"kubelet.sock"` | 
 | 123 | 	} `dir:"device-plugins"` | 
 | 124 |  | 
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 125 | 	// Pod logs, hardcoded to /data/kubelet/logs in | 
 | 126 | 	// @com_github_kubernetes//pkg/kubelet/kuberuntime:kuberuntime_manager.go | 
| Lorenz Brun | 842536b | 2021-01-26 13:54:57 +0100 | [diff] [blame] | 127 | 	Logs declarative.Directory `dir:"logs"` | 
 | 128 |  | 
| Serge Bazanski | c2c7ad9 | 2020-07-13 17:20:09 +0200 | [diff] [blame] | 129 | 	Plugins struct { | 
 | 130 | 		declarative.Directory | 
| Serge Bazanski | 662b5b3 | 2020-12-21 13:49:00 +0100 | [diff] [blame] | 131 | 		VFS declarative.File `file:"dev.monogon.metropolis.vfs.sock"` | 
| Lorenz Brun | 4e09035 | 2021-03-17 17:44:41 +0100 | [diff] [blame] | 132 | 		KVM declarative.File `file:"devices.monogon.dev_kvm.sock"` | 
| Serge Bazanski | c2c7ad9 | 2020-07-13 17:20:09 +0200 | [diff] [blame] | 133 | 	} `dir:"plugins"` | 
 | 134 |  | 
 | 135 | 	PluginsRegistry struct { | 
 | 136 | 		declarative.Directory | 
| Serge Bazanski | 662b5b3 | 2020-12-21 13:49:00 +0100 | [diff] [blame] | 137 | 		VFSReg declarative.File `file:"dev.monogon.metropolis.vfs-reg.sock"` | 
| Lorenz Brun | 4e09035 | 2021-03-17 17:44:41 +0100 | [diff] [blame] | 138 | 		KVMReg declarative.File `file:"devices.monogon.dev_kvm-reg.sock"` | 
| Serge Bazanski | c2c7ad9 | 2020-07-13 17:20:09 +0200 | [diff] [blame] | 139 | 	} `dir:"plugins_registry"` | 
 | 140 | } | 
 | 141 |  | 
 | 142 | type DataVolumesDirectory struct { | 
 | 143 | 	declarative.Directory | 
 | 144 | } | 
 | 145 |  | 
| Serge Bazanski | e50ec39 | 2020-06-30 21:41:39 +0200 | [diff] [blame] | 146 | type EtcDirectory struct { | 
 | 147 | 	declarative.Directory | 
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 148 | 	// Symlinked to /ephemeral/hosts, baked into the erofs system image | 
 | 149 | 	Hosts declarative.File `file:"hosts"` | 
 | 150 | 	// Symlinked to /ephemeral/machine-id, baked into the erofs system image | 
 | 151 | 	MachineID declarative.File `file:"machine-id"` | 
| Serge Bazanski | e50ec39 | 2020-06-30 21:41:39 +0200 | [diff] [blame] | 152 | } | 
| Serge Bazanski | cb883e2 | 2020-07-06 17:47:55 +0200 | [diff] [blame] | 153 |  | 
 | 154 | type EphemeralDirectory struct { | 
 | 155 | 	declarative.Directory | 
| Serge Bazanski | c2c7ad9 | 2020-07-13 17:20:09 +0200 | [diff] [blame] | 156 | 	Consensus         EphemeralConsensusDirectory  `dir:"consensus"` | 
| Serge Bazanski | 7f17d9b | 2021-06-17 16:11:40 +0200 | [diff] [blame] | 157 | 	Curator           EphemeralCuratorDirectory    `dir:"curator"` | 
| Serge Bazanski | c2c7ad9 | 2020-07-13 17:20:09 +0200 | [diff] [blame] | 158 | 	Containerd        EphemeralContainerdDirectory `dir:"containerd"` | 
 | 159 | 	FlexvolumePlugins declarative.Directory        `dir:"flexvolume_plugins"` | 
| Lorenz Brun | 3a99c59 | 2021-01-26 19:57:21 +0100 | [diff] [blame] | 160 | 	Hosts             declarative.File             `file:"hosts"` | 
 | 161 | 	MachineID         declarative.File             `file:"machine-id"` | 
| Serge Bazanski | cb883e2 | 2020-07-06 17:47:55 +0200 | [diff] [blame] | 162 | } | 
 | 163 |  | 
 | 164 | type EphemeralConsensusDirectory struct { | 
 | 165 | 	declarative.Directory | 
| Serge Bazanski | 50009e0 | 2021-07-07 14:35:27 +0200 | [diff] [blame] | 166 | 	ClientSocket   declarative.File `file:"client.sock"` | 
 | 167 | 	ServerLogsFIFO declarative.File `file:"server-logs.fifo"` | 
| Serge Bazanski | cb883e2 | 2020-07-06 17:47:55 +0200 | [diff] [blame] | 168 | } | 
| Serge Bazanski | c2c7ad9 | 2020-07-13 17:20:09 +0200 | [diff] [blame] | 169 |  | 
| Serge Bazanski | 7f17d9b | 2021-06-17 16:11:40 +0200 | [diff] [blame] | 170 | type EphemeralCuratorDirectory struct { | 
 | 171 | 	declarative.Directory | 
 | 172 | 	// Curator ephemeral socket, dialed by local curator clients. | 
 | 173 | 	// See: //metropolis/node/core/curator. | 
 | 174 | 	ClientSocket declarative.File `file:"client.sock"` | 
 | 175 | } | 
 | 176 |  | 
| Serge Bazanski | c2c7ad9 | 2020-07-13 17:20:09 +0200 | [diff] [blame] | 177 | type EphemeralContainerdDirectory struct { | 
 | 178 | 	declarative.Directory | 
 | 179 | 	ClientSocket  declarative.File      `file:"client.sock"` | 
 | 180 | 	RunSCLogsFIFO declarative.File      `file:"runsc-logs.fifo"` | 
 | 181 | 	Tmp           declarative.Directory `dir:"tmp"` | 
 | 182 | 	RunSC         declarative.Directory `dir:"runsc"` | 
 | 183 | 	IPAM          declarative.Directory `dir:"ipam"` | 
| Lorenz Brun | 74e8e5c | 2021-01-26 14:00:50 +0100 | [diff] [blame] | 184 | 	CNI           declarative.Directory `dir:"cni"` | 
 | 185 | 	CNICache      declarative.Directory `dir:"cni-cache"` // Hardcoded @com_github_containernetworking_cni via patch | 
| Serge Bazanski | c2c7ad9 | 2020-07-13 17:20:09 +0200 | [diff] [blame] | 186 | } | 
 | 187 |  | 
 | 188 | type TmpDirectory struct { | 
 | 189 | 	declarative.Directory | 
 | 190 | } | 
| Lorenz Brun | 74e8e5c | 2021-01-26 14:00:50 +0100 | [diff] [blame] | 191 |  | 
 | 192 | type RunDirectory struct { | 
 | 193 | 	declarative.Directory | 
 | 194 | 	// Hardcoded in @com_github_containerd_containerd//pkg/process:utils.go and | 
 | 195 | 	// @com_github_containerd_containerd//runtime/v2/shim:util_unix.go | 
 | 196 | 	Containerd declarative.Directory `dir:"containerd"` | 
 | 197 | } |