-
Notifications
You must be signed in to change notification settings - Fork 15
/
luks.go
79 lines (60 loc) · 1.78 KB
/
luks.go
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
// 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 http://mozilla.org/MPL/2.0/.
// Package luks probes LUKS encrypted filesystems.
package luks
//go:generate go run ../../cstruct/cstruct.go -pkg luks -struct Luks2Header -input luks2_header.h -endianness BigEndian
import (
"bytes"
"github.com/google/uuid"
"github.com/siderolabs/go-pointer"
"github.com/siderolabs/go-blockdevice/v2/blkid/internal/magic"
"github.com/siderolabs/go-blockdevice/v2/blkid/internal/probe"
"github.com/siderolabs/go-blockdevice/v2/blkid/internal/utils"
)
var luksMagic = magic.Magic{
Offset: 0,
Value: []byte("LUKS\xba\xbe"),
}
// Probe for the filesystem.
type Probe struct{}
// Magic returns the magic value for the filesystem.
func (p *Probe) Magic() []*magic.Magic {
return []*magic.Magic{&luksMagic}
}
// Name returns the name of the filesystem.
func (p *Probe) Name() string {
return "luks"
}
// Probe runs the further inspection and returns the result if successful.
func (p *Probe) Probe(r probe.Reader, _ magic.Magic) (*probe.Result, error) {
buf := make([]byte, LUKS2HEADER_SIZE)
if err := utils.ReadFullAt(r, buf, 0); err != nil {
return nil, err
}
hdr := Luks2Header(buf)
if hdr.Get_version() != 2 {
return nil, nil //nolint:nilnil
}
res := &probe.Result{}
lbl := hdr.Get_label()
if lbl[0] != 0 {
idx := bytes.IndexByte(lbl, 0)
if idx == -1 {
idx = len(lbl)
}
res.Label = pointer.To(string(lbl[:idx]))
}
uuidStr := hdr.Get_uuid()
if uuidStr[0] != 0 {
idx := bytes.IndexByte(uuidStr, 0)
if idx == -1 {
idx = len(uuidStr)
}
uuid, err := uuid.ParseBytes(uuidStr[:idx])
if err == nil {
res.UUID = pointer.To(uuid)
}
}
return res, nil
}