Skip to content

Commit

Permalink
grub: Add support for the search command
Browse files Browse the repository at this point in the history
This adds support for the grub `search` command as defined by the grub
manual. This allows the kernel and initrd to be loaded from a file
system other from the boot drive. CentOS exercises this command and has
been added as a unit test.

The following options are included:

* `search --file`: Set root to the file system which contains the given
  file.
* `search --fs-label`: Set root to the file system with the given label.
* `search --fs-uuid`: Set root to the file system with the given UUID.

The aliases `search.file`, search.fs_label` and `search.fs_uuid` are
also supported.

Signed-off-by: Ryan O'Leary <[email protected]>
  • Loading branch information
rjoleary committed Oct 30, 2020
1 parent 3cdafb3 commit 2e02e4f
Show file tree
Hide file tree
Showing 39 changed files with 759 additions and 126 deletions.
48 changes: 46 additions & 2 deletions pkg/boot/grub/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,44 @@ import (
"testing"

"github.com/u-root/u-root/pkg/boot/boottest"
"github.com/u-root/u-root/pkg/mount"
"github.com/u-root/u-root/pkg/mount/block"
)

// fakeDevices returns a list of fake block devices and a pool of mount points.
// The pool is pre-populated so that Mount is never called.
func fakeDevices() (block.BlockDevices, *mount.Pool, error) {
// For some reason, Glob("testdata_new/*/") does not work here.
files, err := ioutil.ReadDir("testdata_new")
if err != nil {
return nil, nil, err
}
dirs := []string{}
for _, f := range files {
if f.IsDir() {
dirs = append(dirs, filepath.Join("testdata_new", f.Name()))
}
}

devices := block.BlockDevices{}
mountPool := &mount.Pool{}
for _, dir := range dirs {
// TODO: Also add LABEL to BlockDev
fsUUID, _ := ioutil.ReadFile(filepath.Join(dir, "UUID"))
devices = append(devices, &block.BlockDev{
Name: dir,
FSType: "test",
FsUUID: strings.TrimSpace(string(fsUUID)),
})
mountPool.Add(&mount.MountPoint{
Path: dir,
Device: dir,
FSType: "test",
})
}
return devices, mountPool, nil
}

// Enable this to generate new configs.
func DISABLEDTestGenerateConfigs(t *testing.T) {
tests, err := filepath.Glob("testdata_new/*.json")
Expand All @@ -24,7 +60,11 @@ func DISABLEDTestGenerateConfigs(t *testing.T) {
for _, test := range tests {
configPath := strings.TrimRight(test, ".json")
t.Run(configPath, func(t *testing.T) {
imgs, err := ParseLocalConfig(context.Background(), configPath)
devices, mountPool, err := fakeDevices()
if err != nil {
t.Fatal(err)
}
imgs, err := ParseLocalConfig(context.Background(), configPath, devices, mountPool)
if err != nil {
t.Fatalf("Failed to parse %s: %v", test, err)
}
Expand All @@ -51,7 +91,11 @@ func TestConfigs(t *testing.T) {
t.Errorf("Failed to read test json '%v':%v", test, err)
}

imgs, err := ParseLocalConfig(context.Background(), configPath)
devices, mountPool, err := fakeDevices()
if err != nil {
t.Fatal(err)
}
imgs, err := ParseLocalConfig(context.Background(), configPath, devices, mountPool)
if err != nil {
t.Fatalf("Failed to parse %s: %v", test, err)
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/boot/grub/echo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"testing"

"github.com/u-root/u-root/pkg/curl"
"github.com/u-root/u-root/pkg/mount"
"github.com/u-root/u-root/pkg/mount/block"
)

var update = flag.Bool("run-bash", false, "run bash and update golden file")
Expand Down Expand Up @@ -109,7 +111,8 @@ func TestGrubTests(t *testing.T) {
Scheme: "file",
Path: "./testdata",
}
c := newParser(wd, curl.DefaultSchemes)
mountPool := &mount.Pool{}
c := newParser(wd, block.BlockDevices{}, mountPool, curl.DefaultSchemes)
c.W = &b

script, err := ioutil.ReadFile(file)
Expand Down
Loading

0 comments on commit 2e02e4f

Please sign in to comment.