aboutsummaryrefslogtreecommitdiffhomepage
path: root/cmd/earlyinit/mount.go
blob: 51d4ecc96bf4f293ae210585274f423ff16659d4 (plain)
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
package main

import (
	"context"
	"errors"
	"os"
	"path/filepath"
	"strconv"
	"syscall"
	"time"

	"hakurei.app/check"
	"hakurei.app/fhs"
	"hakurei.app/internal/kobject"
	"hakurei.app/internal/uevent"
)

// mustMountSystem waits for and mounts a system device matching pattern.
func mustMountSystem(
	ctx context.Context,
	s *kobject.State,
	pattern string,
) {
	c, stop := context.WithTimeout(ctx, 30*time.Second)
	defer stop()

	for {
		var matchErr error
		var systemPath *check.Absolute
		s.Range(c, func(o *kobject.Object, act uevent.KobjectAction) bool {
			if (act != uevent.KOBJ_ADD && act != uevent.KOBJ_CHANGE) ||
				o.Subsystem != "block" ||
				o.Env["DEVTYPE"] != "disk" {
				return true
			}

			if ok, err := filepath.Match(pattern, o.DevPath); err != nil {
				matchErr = err
				return false
			} else if !ok {
				return true
			}

			name, ok := o.Env["DEVNAME"]
			if !ok {
				return true
			}
			systemPath = fhs.AbsDev.Append(name)
			return false
		})
		if c.Err() != nil {
			fatal("devpath", strconv.Quote(pattern), "never appeared")
		}
		if matchErr != nil {
			fatal("cannot match system devpath:", matchErr)
		}
		err := syscall.Mount(
			systemPath.String(),
			"/system/",
			"squashfs",
			0,
			"threads=multi",
		)
		if err == nil {
			break
		}
		if !errors.Is(err, os.ErrNotExist) {
			fatal("cannot mount system:", err)
		}
	}
}