m/p/gpt: add GPT package

This introduces our own GPT package. It will be used for provisioning
and Metropolis images.

Change-Id: I905cd5d540673fd4b69c01d8975f98b88e24edd4
Reviewed-on: https://review.monogon.dev/c/monogon/+/956
Tested-by: Jenkins CI
Reviewed-by: Sergiusz Bazanski <serge@monogon.tech>
diff --git a/metropolis/pkg/gpt/linux_test.go b/metropolis/pkg/gpt/linux_test.go
new file mode 100644
index 0000000..19ac519
--- /dev/null
+++ b/metropolis/pkg/gpt/linux_test.go
@@ -0,0 +1,51 @@
+package gpt
+
+import (
+	"os"
+	"testing"
+
+	"github.com/google/uuid"
+	"golang.org/x/sys/unix"
+)
+
+var testUUID = uuid.MustParse("85c0b60f-caf9-40dd-86fa-f8797e26238d")
+
+func TestKernelInterop(t *testing.T) {
+	if os.Getenv("IN_KTEST") != "true" {
+		t.Skip("Not in ktest")
+	}
+	ram0, err := os.OpenFile("/dev/ram0", os.O_RDWR, 0)
+	if err != nil {
+		panic(err)
+	}
+	g := Table{
+		ID:         uuid.NewSHA1(testUUID, []byte("test")),
+		BlockSize:  512,
+		BlockCount: 2048,
+		BootCode:   []byte("just some test code"),
+		Partitions: []*Partition{
+			nil,
+			// This emoji is very complex and exercises UTF16 surrogate encoding
+			// and composing.
+			{Name: "Test 🏃‍♂️", FirstBlock: 10, LastBlock: 19, Type: PartitionTypeEFISystem, ID: uuid.NewSHA1(testUUID, []byte("test1")), Attributes: AttrNoBlockIOProto},
+			nil,
+			{Name: "Test2", FirstBlock: 20, LastBlock: 49, Type: PartitionTypeEFISystem, ID: uuid.NewSHA1(testUUID, []byte("test2")), Attributes: 0},
+		},
+	}
+	if err := Write(ram0, &g); err != nil {
+		t.Fatalf("Failed to write GPT: %v", err)
+	}
+
+	if err := unix.IoctlSetInt(int(ram0.Fd()), unix.BLKRRPART, 0); err != nil {
+		t.Fatalf("Failed to reread partition table: %v", err)
+	}
+	if _, err := os.Stat("/sys/block/ram0/ram0p2"); err != nil {
+		t.Errorf("Expected ram0p2 to exist, got %v", err)
+	}
+	if _, err := os.Stat("/sys/block/ram0/ram0p4"); err != nil {
+		t.Errorf("Expected ram0p4 to exist, got %v", err)
+	}
+	if _, err := os.Stat("/sys/block/ram0/ram0p1"); err == nil {
+		t.Error("Expected ram0p1 not to exist, but it exists")
+	}
+}