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 crypt |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "fmt" |
Serge Bazanski | e50ec39 | 2020-06-30 21:41:39 +0200 | [diff] [blame] | 22 | "os" |
| 23 | "path/filepath" |
| 24 | "strconv" |
Tim Windelschmidt | 3546615 | 2023-06-14 20:01:11 +0200 | [diff] [blame] | 25 | "strings" |
Serge Bazanski | e50ec39 | 2020-06-30 21:41:39 +0200 | [diff] [blame] | 26 | |
Lorenz Brun | a3904fc | 2023-05-02 19:33:52 +0200 | [diff] [blame] | 27 | "github.com/google/uuid" |
Serge Bazanski | e50ec39 | 2020-06-30 21:41:39 +0200 | [diff] [blame] | 28 | "golang.org/x/sys/unix" |
| 29 | |
Lorenz Brun | 35fcf03 | 2023-06-29 04:15:58 +0200 | [diff] [blame] | 30 | "source.monogon.dev/metropolis/node/core/update" |
Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 31 | "source.monogon.dev/metropolis/pkg/blockdev" |
Lorenz Brun | a3904fc | 2023-05-02 19:33:52 +0200 | [diff] [blame] | 32 | "source.monogon.dev/metropolis/pkg/efivarfs" |
| 33 | "source.monogon.dev/metropolis/pkg/gpt" |
| 34 | "source.monogon.dev/metropolis/pkg/supervisor" |
Serge Bazanski | 31370b0 | 2021-01-07 16:31:14 +0100 | [diff] [blame] | 35 | "source.monogon.dev/metropolis/pkg/sysfs" |
Serge Bazanski | e50ec39 | 2020-06-30 21:41:39 +0200 | [diff] [blame] | 36 | ) |
| 37 | |
Lorenz Brun | a3904fc | 2023-05-02 19:33:52 +0200 | [diff] [blame] | 38 | // NodeDataPartitionType is the partition type value for a Metropolis Node |
| 39 | // data partition. |
| 40 | var NodeDataPartitionType = uuid.MustParse("9eeec464-6885-414a-b278-4305c51f7966") |
Serge Bazanski | e50ec39 | 2020-06-30 21:41:39 +0200 | [diff] [blame] | 41 | |
Lorenz Brun | 35fcf03 | 2023-06-29 04:15:58 +0200 | [diff] [blame] | 42 | var ( |
| 43 | SystemAType = uuid.MustParse("ee96054b-f6d0-4267-aaaa-724b2afea74c") |
| 44 | SystemBType = uuid.MustParse("ee96054b-f6d0-4267-bbbb-724b2afea74c") |
| 45 | ) |
| 46 | |
Serge Bazanski | e50ec39 | 2020-06-30 21:41:39 +0200 | [diff] [blame] | 47 | const ( |
Lorenz Brun | 35fcf03 | 2023-06-29 04:15:58 +0200 | [diff] [blame] | 48 | ESPDevicePath = "/dev/esp" |
| 49 | NodeDataRawPath = "/dev/data-raw" |
| 50 | SystemADevicePath = "/dev/system-a" |
| 51 | SystemBDevicePath = "/dev/system-b" |
Serge Bazanski | e50ec39 | 2020-06-30 21:41:39 +0200 | [diff] [blame] | 52 | ) |
| 53 | |
Tim Windelschmidt | 3546615 | 2023-06-14 20:01:11 +0200 | [diff] [blame] | 54 | // nodePathForPartitionType returns the device node path |
| 55 | // for a given partition type. |
| 56 | func nodePathForPartitionType(t uuid.UUID) string { |
| 57 | switch t { |
| 58 | case gpt.PartitionTypeEFISystem: |
| 59 | return ESPDevicePath |
| 60 | case NodeDataPartitionType: |
| 61 | return NodeDataRawPath |
Lorenz Brun | 35fcf03 | 2023-06-29 04:15:58 +0200 | [diff] [blame] | 62 | case SystemAType: |
| 63 | return SystemADevicePath |
| 64 | case SystemBType: |
| 65 | return SystemBDevicePath |
Tim Windelschmidt | 3546615 | 2023-06-14 20:01:11 +0200 | [diff] [blame] | 66 | } |
| 67 | return "" |
| 68 | } |
| 69 | |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 70 | // MakeBlockDevices looks for the ESP and the node data partition and maps them |
| 71 | // to ESPDevicePath and NodeDataCryptPath respectively. This doesn't fail if it |
| 72 | // doesn't find the partitions, only if something goes catastrophically wrong. |
Lorenz Brun | 35fcf03 | 2023-06-29 04:15:58 +0200 | [diff] [blame] | 73 | func MakeBlockDevices(ctx context.Context, updateSvc *update.Service) error { |
Lorenz Brun | a3904fc | 2023-05-02 19:33:52 +0200 | [diff] [blame] | 74 | espUUID, err := efivarfs.ReadLoaderDevicePartUUID() |
| 75 | if err != nil { |
| 76 | supervisor.Logger(ctx).Warningf("No EFI variable for the loader device partition UUID present") |
| 77 | } |
Tim Windelschmidt | 3546615 | 2023-06-14 20:01:11 +0200 | [diff] [blame] | 78 | |
| 79 | blockDevs, err := os.ReadDir("/sys/class/block") |
Serge Bazanski | e50ec39 | 2020-06-30 21:41:39 +0200 | [diff] [blame] | 80 | if err != nil { |
| 81 | return fmt.Errorf("failed to read sysfs block class: %w", err) |
| 82 | } |
Tim Windelschmidt | 3546615 | 2023-06-14 20:01:11 +0200 | [diff] [blame] | 83 | |
| 84 | for _, blockDev := range blockDevs { |
Lorenz Brun | 35fcf03 | 2023-06-29 04:15:58 +0200 | [diff] [blame] | 85 | if err := handleBlockDevice(blockDev.Name(), blockDevs, espUUID, updateSvc); err != nil { |
Tim Windelschmidt | 3546615 | 2023-06-14 20:01:11 +0200 | [diff] [blame] | 86 | supervisor.Logger(ctx).Errorf("Failed to create block device %s: %w", blockDev.Name(), err) |
Serge Bazanski | e50ec39 | 2020-06-30 21:41:39 +0200 | [diff] [blame] | 87 | } |
Tim Windelschmidt | 3546615 | 2023-06-14 20:01:11 +0200 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | return nil |
| 91 | } |
| 92 | |
| 93 | // handleBlockDevice reads the uevent data and continues to iterate over all |
| 94 | // partitions to create all required device nodes. |
Lorenz Brun | 35fcf03 | 2023-06-29 04:15:58 +0200 | [diff] [blame] | 95 | func handleBlockDevice(diskBlockDev string, blockDevs []os.DirEntry, espUUID uuid.UUID, updateSvc *update.Service) error { |
Tim Windelschmidt | 3546615 | 2023-06-14 20:01:11 +0200 | [diff] [blame] | 96 | data, err := readUEvent(diskBlockDev) |
| 97 | if err != nil { |
| 98 | return err |
| 99 | } |
| 100 | |
| 101 | // We only care about disks, skip all other dev types. |
| 102 | if data["DEVTYPE"] != "disk" { |
| 103 | return nil |
| 104 | } |
| 105 | |
Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 106 | blkdev, err := blockdev.Open(fmt.Sprintf("/dev/%v", data["DEVNAME"])) |
Tim Windelschmidt | 3546615 | 2023-06-14 20:01:11 +0200 | [diff] [blame] | 107 | if err != nil { |
Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 108 | return fmt.Errorf("failed to open block device: %w", err) |
Tim Windelschmidt | 3546615 | 2023-06-14 20:01:11 +0200 | [diff] [blame] | 109 | } |
Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 110 | defer blkdev.Close() |
Tim Windelschmidt | 3546615 | 2023-06-14 20:01:11 +0200 | [diff] [blame] | 111 | |
Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 112 | table, err := gpt.Read(blkdev) |
| 113 | if err != nil { |
| 114 | return nil // Probably just not a GPT-partitioned disk |
Tim Windelschmidt | 3546615 | 2023-06-14 20:01:11 +0200 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | skipDisk := false |
| 118 | if espUUID != uuid.Nil { |
| 119 | // If we know where we booted from, ignore all disks which do |
| 120 | // not contain this partition. |
| 121 | skipDisk = true |
| 122 | for _, part := range table.Partitions { |
| 123 | if part.ID == espUUID { |
| 124 | skipDisk = false |
| 125 | break |
Serge Bazanski | e50ec39 | 2020-06-30 21:41:39 +0200 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | } |
Tim Windelschmidt | 3546615 | 2023-06-14 20:01:11 +0200 | [diff] [blame] | 129 | if skipDisk { |
| 130 | return nil |
| 131 | } |
| 132 | |
| 133 | seenTypes := make(map[uuid.UUID]bool) |
| 134 | for _, dev := range blockDevs { |
Lorenz Brun | 35fcf03 | 2023-06-29 04:15:58 +0200 | [diff] [blame] | 135 | if err := handlePartition(diskBlockDev, dev.Name(), table, seenTypes, updateSvc); err != nil { |
Tim Windelschmidt | 3546615 | 2023-06-14 20:01:11 +0200 | [diff] [blame] | 136 | return fmt.Errorf("when creating partition %s: %w", dev.Name(), err) |
| 137 | } |
| 138 | } |
| 139 | |
Serge Bazanski | e50ec39 | 2020-06-30 21:41:39 +0200 | [diff] [blame] | 140 | return nil |
| 141 | } |
Tim Windelschmidt | 3546615 | 2023-06-14 20:01:11 +0200 | [diff] [blame] | 142 | |
Lorenz Brun | 35fcf03 | 2023-06-29 04:15:58 +0200 | [diff] [blame] | 143 | func handlePartition(diskBlockDev string, partBlockDev string, table *gpt.Table, seenTypes map[uuid.UUID]bool, updateSvc *update.Service) error { |
Tim Windelschmidt | 3546615 | 2023-06-14 20:01:11 +0200 | [diff] [blame] | 144 | // Skip all blockdev that dont share the same name/prefix, |
| 145 | // also skip the blockdev itself. |
| 146 | if !strings.HasPrefix(partBlockDev, diskBlockDev) || partBlockDev == diskBlockDev { |
| 147 | return nil |
| 148 | } |
| 149 | |
| 150 | data, err := readUEvent(partBlockDev) |
| 151 | if err != nil { |
| 152 | return err |
| 153 | } |
| 154 | |
| 155 | // We only care about partitions, skip all other dev types. |
| 156 | if data["DEVTYPE"] != "partition" { |
| 157 | return nil |
| 158 | } |
| 159 | |
| 160 | pi, err := data.readPartitionInfo() |
| 161 | if err != nil { |
| 162 | return err |
| 163 | } |
| 164 | |
Tim Windelschmidt | 3546615 | 2023-06-14 20:01:11 +0200 | [diff] [blame] | 165 | part := table.Partitions[pi.partNumber-1] |
| 166 | |
Tim Windelschmidt | 8e87a06 | 2023-07-31 01:33:10 +0000 | [diff] [blame^] | 167 | updateSvc.ProvideESP("/esp", uint32(pi.partNumber), part) |
Lorenz Brun | 35fcf03 | 2023-06-29 04:15:58 +0200 | [diff] [blame] | 168 | |
Tim Windelschmidt | 3546615 | 2023-06-14 20:01:11 +0200 | [diff] [blame] | 169 | nodePath := nodePathForPartitionType(part.Type) |
| 170 | if nodePath == "" { |
| 171 | // Ignore partitions with an unknown type. |
| 172 | return nil |
| 173 | } |
| 174 | |
| 175 | if seenTypes[part.Type] { |
| 176 | return fmt.Errorf("node for this type (%s) already created/multiple partitions found", part.Type.String()) |
| 177 | } |
| 178 | seenTypes[part.Type] = true |
| 179 | |
| 180 | if err := pi.makeDeviceNode(nodePath); err != nil { |
| 181 | return fmt.Errorf("when creating partition node: %w", err) |
| 182 | } |
| 183 | |
| 184 | return nil |
| 185 | } |
| 186 | |
| 187 | type partInfo struct { |
| 188 | major, minor, partNumber int |
| 189 | } |
| 190 | |
| 191 | // validateDeviceNode tries to open a device node and validates that it |
| 192 | // has the expected major and minor device numbers. If the path does non exist, |
| 193 | // no error will be returned. |
| 194 | func (pi partInfo) validateDeviceNode(path string) error { |
| 195 | var s unix.Stat_t |
| 196 | if err := unix.Stat(path, &s); err != nil { |
| 197 | if os.IsNotExist(err) { |
| 198 | return nil |
| 199 | } |
| 200 | |
| 201 | return fmt.Errorf("inspecting device node %q: %w", path, err) |
| 202 | } |
| 203 | |
| 204 | if unix.Major(s.Rdev) != uint32(pi.major) || unix.Minor(s.Rdev) != uint32(pi.minor) { |
| 205 | return fmt.Errorf("device node %q exists for different device %d:%d", path, unix.Major(s.Rdev), unix.Minor(s.Rdev)) |
| 206 | } |
| 207 | |
| 208 | return nil |
| 209 | } |
| 210 | |
| 211 | // makeDeviceNode creates the device node at the given path based on the |
| 212 | // major and minor device number. If the device node already exists and points |
| 213 | // to the same device, no error will be returned. |
| 214 | func (pi partInfo) makeDeviceNode(path string) error { |
| 215 | if err := pi.validateDeviceNode(path); err != nil { |
| 216 | return err |
| 217 | } |
| 218 | |
| 219 | err := unix.Mknod(path, 0600|unix.S_IFBLK, int(unix.Mkdev(uint32(pi.major), uint32(pi.minor)))) |
| 220 | if err != nil { |
| 221 | return fmt.Errorf("create device node %q: %w", path, err) |
| 222 | } |
| 223 | return nil |
| 224 | } |
| 225 | |
| 226 | func readUEvent(blockName string) (blockUEvent, error) { |
| 227 | data, err := sysfs.ReadUevents(filepath.Join("/sys/class/block", blockName, "uevent")) |
| 228 | if err != nil { |
| 229 | return nil, fmt.Errorf("when reading uevent: %w", err) |
| 230 | } |
| 231 | return data, nil |
| 232 | } |
| 233 | |
| 234 | type blockUEvent map[string]string |
| 235 | |
| 236 | func (b blockUEvent) readUdevKeyInteger(key string) (int, error) { |
| 237 | if _, ok := b[key]; !ok { |
| 238 | return 0, fmt.Errorf("missing udev value %s", key) |
| 239 | } |
| 240 | |
| 241 | v, err := strconv.Atoi(b[key]) |
| 242 | if err != nil { |
| 243 | return 0, fmt.Errorf("invalid %s: %w", key, err) |
| 244 | } |
| 245 | |
| 246 | return v, nil |
| 247 | } |
| 248 | |
| 249 | // readPartitionInfo parses all fields for partInfo from a blockUEvent. |
| 250 | func (b blockUEvent) readPartitionInfo() (pi partInfo, err error) { |
| 251 | pi.major, err = b.readUdevKeyInteger("MAJOR") |
| 252 | if err != nil { |
| 253 | return |
| 254 | } |
| 255 | |
| 256 | pi.minor, err = b.readUdevKeyInteger("MINOR") |
| 257 | if err != nil { |
| 258 | return |
| 259 | } |
| 260 | |
| 261 | pi.partNumber, err = b.readUdevKeyInteger("PARTN") |
| 262 | if err != nil { |
| 263 | return |
| 264 | } |
| 265 | |
| 266 | return |
| 267 | } |