github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/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/mvdan/u-root-coreutils/pkg/boot/menu" 14 "github.com/mvdan/u-root-coreutils/pkg/mount" 15 ) 16 17 // ShowMenuAndBoot handles common cleanup functions and flags that all boot 18 // commands should support. 19 // 20 // mountPool is unmounted 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, mountPool *mount.Pool, 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(true, entries...) 34 35 // Clean up. 36 if mountPool != nil { 37 if err := mountPool.UnmountAll(mount.MNT_DETACH); err != nil { 38 log.Printf("Failed in UnmountAll: %v", 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 }