github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/cmds/exp/kconf/kconf.go (about) 1 // Copyright 2021 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 // SPDX-License-Identifier: BSD-3-Clause 6 // 7 8 package main 9 10 import ( 11 "bufio" 12 "compress/gzip" 13 "flag" 14 "fmt" 15 "io" 16 "log" 17 "os" 18 "strings" 19 20 "github.com/mvdan/u-root-coreutils/pkg/boot/bzimage" 21 ) 22 23 const ( 24 cfgfile = "/proc/config.gz" 25 notset = "is not set" 26 ) 27 28 func main() { 29 flag.Usage = func() { 30 fmt.Fprintf(flag.CommandLine.Output(), "Reads kernel config from /proc/config.gz or bzimage, optionally filtering\n") 31 fmt.Fprintf(flag.CommandLine.Output(), "Usage: %s [-k /path/to/bzimage] [-y|-m|-n] [-f filterstr]\n", os.Args[0]) 32 flag.PrintDefaults() 33 } 34 y := flag.Bool("y", false, "show only built-in") 35 m := flag.Bool("m", false, "show only modules") 36 n := flag.Bool("n", false, "show only not configured") 37 f := flag.String("f", "", "filter on config symbol, case insensitive") 38 p := flag.Bool("p", true, "pretty: trim prefix; also suffix if -y/-m/-n") 39 k := flag.String("k", "", "use kernel image rather than /proc/config.gz") 40 flag.Parse() 41 42 var cfgIn io.Reader 43 if len(*k) > 0 { 44 image, err := os.ReadFile(*k) 45 if err != nil { 46 log.Fatal(err) 47 } 48 br := &bzimage.BzImage{} 49 if err = br.UnmarshalBinary(image); err != nil { 50 log.Fatal(err) 51 } 52 cfg, err := br.ReadConfig() 53 if err != nil { 54 log.Fatal(err) 55 } 56 cfgIn = strings.NewReader(cfg) 57 } else { 58 configgz, err := os.Open(cfgfile) 59 if err != nil { 60 log.Fatalf("cannot open %s: %s", cfgfile, err) 61 } 62 defer configgz.Close() 63 gz, err := gzip.NewReader(configgz) 64 if err != nil { 65 log.Fatalf("decompress %s: %s", cfgfile, err) 66 } 67 defer gz.Close() 68 cfgIn = gz 69 } 70 filter := strings.ToUpper(*f) 71 scanner := bufio.NewScanner(cfgIn) 72 for scanner.Scan() { 73 line := scanner.Text() 74 if *y && !strings.HasSuffix(line, "=y") { 75 continue 76 } 77 if *m && !strings.HasSuffix(line, "=m") { 78 continue 79 } 80 if *n && !strings.HasSuffix(line, notset) { 81 continue 82 } 83 if len(filter) > 0 && !strings.Contains(line, filter) { 84 continue 85 } 86 if *p { 87 if *n { 88 line = strings.TrimPrefix(line, "# ") 89 } 90 line = strings.TrimPrefix(line, "CONFIG_") 91 if *y || *m { 92 line = strings.Split(line, "=")[0] 93 } 94 if *n { 95 line = strings.TrimSuffix(line, notset) 96 } 97 } 98 fmt.Println(line) 99 } 100 if err := scanner.Err(); err != nil && err != io.EOF { 101 log.Fatalf("reading %s: %s", cfgfile, err) 102 } 103 }