github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/pkg/boot/bootcmd/bootcmd.go (about) 1 // Copyright 2020 the u-root Authors. All rights reserved 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Package bootcmd handles common cleanup functions and flags that all boot 6 // commands should support. 7 package bootcmd 8 9 import ( 10 "log" 11 "os" 12 13 "github.com/u-root/u-root/pkg/boot/menu" 14 "github.com/u-root/u-root/pkg/mount" 15 ) 16 17 // ShowMenuAndBoot handles common cleanup functions and flags that all boot 18 // commands should support. 19 // 20 // mps are mounts to unmount before kexecing. noLoad prints the list of entries 21 // and exits. If noLoad is false, a boot menu is shown to the user. The 22 // user-chosen boot entry will be kexec'd unless noExec is true. 23 func ShowMenuAndBoot(entries []menu.Entry, mps []*mount.MountPoint, noLoad, noExec bool) { 24 if noLoad { 25 log.Print("Not loading menu or kernel. Options:") 26 for i, entry := range entries { 27 log.Printf("%d. %s", i+1, entry.Label()) 28 log.Printf("=> %s", entry) 29 } 30 os.Exit(0) 31 } 32 33 loadedEntry := menu.ShowMenuAndLoad(os.Stdin, entries...) 34 35 // Clean up. 36 for _, mp := range mps { 37 if err := mp.Unmount(mount.MNT_DETACH); err != nil { 38 log.Printf("Failed to unmount %s: %v", mp, err) 39 } 40 } 41 if loadedEntry == nil { 42 log.Fatalf("Nothing to boot.") 43 } 44 if noExec { 45 log.Printf("Chosen menu entry: %s", loadedEntry) 46 os.Exit(0) 47 } 48 // Exec should either return an error or not return at all. 49 if err := loadedEntry.Exec(); err != nil { 50 log.Fatalf("Failed to exec %s: %v", loadedEntry, err) 51 } 52 53 // Kexec should either return an error or not return. 54 log.Fatalf("Kexec should have returned an error or not returned at all.") 55 }