|
| 1 | +/* |
| 2 | + Copyright The containerd Authors. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package platforms |
| 18 | + |
| 19 | +import ( |
| 20 | + "bufio" |
| 21 | + "bytes" |
| 22 | + "fmt" |
| 23 | + "os" |
| 24 | + "runtime" |
| 25 | + "strings" |
| 26 | + |
| 27 | + "github.com/containerd/containerd/errdefs" |
| 28 | + "golang.org/x/sys/unix" |
| 29 | +) |
| 30 | + |
| 31 | +// getMachineArch retrieves the machine architecture through system call |
| 32 | +func getMachineArch() (string, error) { |
| 33 | + var uname unix.Utsname |
| 34 | + err := unix.Uname(&uname) |
| 35 | + if err != nil { |
| 36 | + return "", err |
| 37 | + } |
| 38 | + |
| 39 | + arch := string(uname.Machine[:bytes.IndexByte(uname.Machine[:], 0)]) |
| 40 | + |
| 41 | + return arch, nil |
| 42 | +} |
| 43 | + |
| 44 | +// For Linux, the kernel has already detected the ABI, ISA and Features. |
| 45 | +// So we don't need to access the ARM registers to detect platform information |
| 46 | +// by ourselves. We can just parse these information from /proc/cpuinfo |
| 47 | +func getCPUInfo(pattern string) (info string, err error) { |
| 48 | + |
| 49 | + cpuinfo, err := os.Open("/proc/cpuinfo") |
| 50 | + if err != nil { |
| 51 | + return "", err |
| 52 | + } |
| 53 | + defer cpuinfo.Close() |
| 54 | + |
| 55 | + // Start to Parse the Cpuinfo line by line. For SMP SoC, we parse |
| 56 | + // the first core is enough. |
| 57 | + scanner := bufio.NewScanner(cpuinfo) |
| 58 | + for scanner.Scan() { |
| 59 | + newline := scanner.Text() |
| 60 | + list := strings.Split(newline, ":") |
| 61 | + |
| 62 | + if len(list) > 1 && strings.EqualFold(strings.TrimSpace(list[0]), pattern) { |
| 63 | + return strings.TrimSpace(list[1]), nil |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + // Check whether the scanner encountered errors |
| 68 | + err = scanner.Err() |
| 69 | + if err != nil { |
| 70 | + return "", err |
| 71 | + } |
| 72 | + |
| 73 | + return "", fmt.Errorf("getCPUInfo for pattern %s: %w", pattern, errdefs.ErrNotFound) |
| 74 | +} |
| 75 | + |
| 76 | +// getCPUVariantFromArch get CPU variant from arch through a system call |
| 77 | +func getCPUVariantFromArch(arch string) (string, error) { |
| 78 | + |
| 79 | + var variant string |
| 80 | + |
| 81 | + arch = strings.ToLower(arch) |
| 82 | + |
| 83 | + if arch == "aarch64" { |
| 84 | + variant = "8" |
| 85 | + } else if arch[0:4] == "armv" && len(arch) >= 5 { |
| 86 | + //Valid arch format is in form of armvXx |
| 87 | + switch arch[3:5] { |
| 88 | + case "v8": |
| 89 | + variant = "8" |
| 90 | + case "v7": |
| 91 | + variant = "7" |
| 92 | + case "v6": |
| 93 | + variant = "6" |
| 94 | + case "v5": |
| 95 | + variant = "5" |
| 96 | + case "v4": |
| 97 | + variant = "4" |
| 98 | + case "v3": |
| 99 | + variant = "3" |
| 100 | + default: |
| 101 | + variant = "unknown" |
| 102 | + } |
| 103 | + } else { |
| 104 | + return "", fmt.Errorf("getCPUVariantFromArch invalid arch: %s, %w", arch, errdefs.ErrInvalidArgument) |
| 105 | + } |
| 106 | + return variant, nil |
| 107 | +} |
| 108 | + |
| 109 | +// getCPUVariant returns cpu variant for ARM |
| 110 | +// We first try reading "Cpu architecture" field from /proc/cpuinfo |
| 111 | +// If we can't find it, then fall back using a system call |
| 112 | +// This is to cover running ARM in emulated environment on x86 host as this field in /proc/cpuinfo |
| 113 | +// was not present. |
| 114 | +func getCPUVariant() (string, error) { |
| 115 | + |
| 116 | + variant, err := getCPUInfo("Cpu architecture") |
| 117 | + if err != nil { |
| 118 | + if errdefs.IsNotFound(err) { |
| 119 | + //Let's try getting CPU variant from machine architecture |
| 120 | + arch, err := getMachineArch() |
| 121 | + if err != nil { |
| 122 | + return "", fmt.Errorf("failure getting machine architecture: %v", err) |
| 123 | + } |
| 124 | + |
| 125 | + variant, err = getCPUVariantFromArch(arch) |
| 126 | + if err != nil { |
| 127 | + return "", fmt.Errorf("failure getting CPU variant from machine architecture: %v", err) |
| 128 | + } |
| 129 | + } else { |
| 130 | + return "", fmt.Errorf("failure getting CPU variant: %v", err) |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + // handle edge case for Raspberry Pi ARMv6 devices (which due to a kernel quirk, report "CPU architecture: 7") |
| 135 | + // https://www.raspberrypi.org/forums/viewtopic.php?t=12614 |
| 136 | + if runtime.GOARCH == "arm" && variant == "7" { |
| 137 | + model, err := getCPUInfo("model name") |
| 138 | + if err == nil && strings.HasPrefix(strings.ToLower(model), "armv6-compatible") { |
| 139 | + variant = "6" |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + switch strings.ToLower(variant) { |
| 144 | + case "8", "aarch64": |
| 145 | + variant = "v8" |
| 146 | + case "7", "7m", "?(12)", "?(13)", "?(14)", "?(15)", "?(16)", "?(17)": |
| 147 | + variant = "v7" |
| 148 | + case "6", "6tej": |
| 149 | + variant = "v6" |
| 150 | + case "5", "5t", "5te", "5tej": |
| 151 | + variant = "v5" |
| 152 | + case "4", "4t": |
| 153 | + variant = "v4" |
| 154 | + case "3": |
| 155 | + variant = "v3" |
| 156 | + default: |
| 157 | + variant = "unknown" |
| 158 | + } |
| 159 | + |
| 160 | + return variant, nil |
| 161 | +} |
0 commit comments