Skip to content

Commit

Permalink
handle older bletchley images
Browse files Browse the repository at this point in the history
Summary:
- the format of /etc/issue is slightly unusual
- refuse to upgrade anything older than v2023.07.1 (bricks the BMC)

Test Plan:
```
0 ~/local/openbmc/tools/flashy $ ./build.sh && ./build_dev.sh && go test ./...
ok      github.com/facebook/openbmc/tools/flashy        3.117s
ok      github.com/facebook/openbmc/tools/flashy/checks_and_remediations/bletchley      (cached)
ok      github.com/facebook/openbmc/tools/flashy/checks_and_remediations/common (cached)
ok      github.com/facebook/openbmc/tools/flashy/checks_and_remediations/galaxy100      (cached)
ok      github.com/facebook/openbmc/tools/flashy/checks_and_remediations/wedge100       (cached)
ok      github.com/facebook/openbmc/tools/flashy/checks_and_remediations/yamp   (cached)
?       github.com/facebook/openbmc/tools/flashy/flash_procedure        [no test files]
ok      github.com/facebook/openbmc/tools/flashy/install        0.005s
ok      github.com/facebook/openbmc/tools/flashy/lib/fileutils  (cached)
ok      github.com/facebook/openbmc/tools/flashy/lib/flash      (cached)
ok      github.com/facebook/openbmc/tools/flashy/lib/flash/flashcp      (cached)
ok      github.com/facebook/openbmc/tools/flashy/lib/flash/flashutils   (cached)
ok      github.com/facebook/openbmc/tools/flashy/lib/flash/flashutils/devices   (cached)
?       github.com/facebook/openbmc/tools/flashy/lib/logger     [no test files]
ok      github.com/facebook/openbmc/tools/flashy/lib/step       (cached)
ok      github.com/facebook/openbmc/tools/flashy/lib/utils      (cached)
ok      github.com/facebook/openbmc/tools/flashy/lib/validate   (cached)
ok      github.com/facebook/openbmc/tools/flashy/lib/validate/image     (cached)
ok      github.com/facebook/openbmc/tools/flashy/lib/validate/partition (cached)
?       github.com/facebook/openbmc/tools/flashy/tests  [no test files]
?       github.com/facebook/openbmc/tools/flashy/utilities      [no test files]
0 ~/local/openbmc/tools/flashy $ echo $?
0
```

Reviewed By: williamspatrick

Differential Revision: D45050113

fbshipit-source-id: 5625c2da100ea562c4ce70925dd3a7c9c8cf3aae
  • Loading branch information
doranand authored and facebook-github-bot committed Apr 17, 2023
1 parent 8d3b722 commit 45fe1cf
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 9 deletions.
66 changes: 66 additions & 0 deletions tools/flashy/checks_and_remediations/bletchley/00_check_version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* Copyright 2020-present Facebook. All Rights Reserved.
*
* This program file is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program in a file named COPYING; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*/

package remediations_bletchley

import (
"strconv"

"github.com/facebook/openbmc/tools/flashy/lib/step"
"github.com/facebook/openbmc/tools/flashy/lib/utils"
"github.com/pkg/errors"
)

func init() {
step.RegisterStep(checkVersion)
}

func checkVersion(stepParams step.StepParams) step.StepExitError {
version, err := utils.GetOpenBMCVersionFromIssueFile()
if err != nil {
errMsg := errors.Errorf("Unable to fetch version info: %v", err)
return step.ExitUnsafeToReboot{Err: errMsg}
}

const re = `bletchley-v(?P<year>[0-9]+).(?P<month>[0-9]+)`
versionMap, err := utils.GetRegexSubexpMap(re, version)
if err != nil {
errMsg := errors.Errorf("Unable to parse version info: %v", err)
return step.ExitUnsafeToReboot{Err: errMsg}
}

year, err := strconv.Atoi(versionMap["year"])
if err != nil {
errMsg := errors.Errorf("Unable to parse version info: %v", err)
return step.ExitUnsafeToReboot{Err: errMsg}
}

month, err := strconv.Atoi(versionMap["month"])
if err != nil {
errMsg := errors.Errorf("Unable to parse version info: %v", err)
return step.ExitUnsafeToReboot{Err: errMsg}
}

if year < 2023 || (year == 2023 && month < 07) {
errMsg := errors.Errorf("Cannot upgrade this version: %v", version)
return step.ExitUnsafeToReboot{Err: errMsg}
}

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* Copyright 2020-present Facebook. All Rights Reserved.
*
* This program file is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program in a file named COPYING; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*/

package remediations_bletchley

import (
"testing"

"github.com/facebook/openbmc/tools/flashy/lib/step"
"github.com/facebook/openbmc/tools/flashy/lib/utils"
"github.com/pkg/errors"
)

func TestCheckVersion(t *testing.T) {
// mock and defer restore RunCommand
getOpenBMCVersionFromIssueFileOrig := utils.GetOpenBMCVersionFromIssueFile
defer func() {
utils.GetOpenBMCVersionFromIssueFile = getOpenBMCVersionFromIssueFileOrig
}()
cases := []struct {
name string
version string
err error
want step.StepExitError
}{
{
name: "failed",
version: "bletchley-v2023.02.1",
err: nil,
want: step.ExitUnsafeToReboot{
errors.Errorf("Cannot upgrade this version: bletchley-v2023.02.1"),
},
},
{
name: "succeeded",
version: "bletchley-v2023.07.1",
err: nil,
want: nil,
},
{
name: "garbage",
version: "garbage",
err: nil,
want: step.ExitUnsafeToReboot{
errors.Errorf("Unable to parse version info: No match for regex 'bletchley-v(?P<year>[0-9]+).(?P<month>[0-9]+)' for input 'garbage'"),
},
},
{
name: "error",
version: "",
err: errors.Errorf("fail"),
want: step.ExitUnsafeToReboot{
errors.Errorf("Unable to fetch version info: fail"),
},
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
utils.GetOpenBMCVersionFromIssueFile = func() (string, error) {
return tc.version, tc.err
}
got := checkVersion(step.StepParams{})
step.CompareTestExitErrors(tc.want, got, t)
})
}
}
1 change: 1 addition & 0 deletions tools/flashy/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/facebook/openbmc/tools/flashy/lib/utils"

// all packages containing steps must be included to successfully run install
_ "github.com/facebook/openbmc/tools/flashy/checks_and_remediations/bletchley"
_ "github.com/facebook/openbmc/tools/flashy/checks_and_remediations/common"
_ "github.com/facebook/openbmc/tools/flashy/checks_and_remediations/galaxy100"
_ "github.com/facebook/openbmc/tools/flashy/checks_and_remediations/wedge100"
Expand Down
17 changes: 13 additions & 4 deletions tools/flashy/lib/utils/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,17 +291,18 @@ var SystemdAvailable = func() (bool, error) {
// in old images.
var GetOpenBMCVersionFromIssueFile = func() (string, error) {
const rVersion = "version"
etcIssueVersionRegEx := fmt.Sprintf(`^Open ?BMC Release (?P<%v>[^\s]+)`, rVersion)
const re = `^(?P<facebook>facebook )?open ?bmc(?P<release> release)? ?(?P<%v>[^\s]+)`
etcIssueVersionRegEx := fmt.Sprintf(re, rVersion)

etcIssueBuf, err := fileutils.ReadFile(etcIssueFilePath)
if err != nil {
return "", errors.Errorf("Error reading %v: %v",
etcIssueFilePath, err)
}
etcIssueStr := string(etcIssueBuf)
etcIssueStr := strings.ToLower(string(etcIssueBuf))

// handle ancient galaxy100 linecard release with missing version info
if strings.HasPrefix(etcIssueStr, "OpenBMC Release \n") {
if strings.HasPrefix(etcIssueStr, "openbmc release \n") {
return "unknown-v1", nil
}

Expand All @@ -315,8 +316,16 @@ var GetOpenBMCVersionFromIssueFile = func() (string, error) {
etcIssueFilePath, err)
}

// really old releases don't have the build name
// greedy matching will cause corrupt /etc/issue to yield "release"
version := etcIssueMap[rVersion]
if version == "release" {
// does not match regex
return "",
errors.Errorf("Unable to get version from %v: missing version info",
etcIssueFilePath)
}

// really old releases don't have the build name
if version[0] == 'v' && !strings.Contains(version, "-") {
version = "unknown-" + version
}
Expand Down
16 changes: 11 additions & 5 deletions tools/flashy/lib/utils/system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,16 +600,22 @@ func TestGetOpenBMCVersionFromIssueFile(t *testing.T) {
etcIssueContents: `OpenBMC Release`,
etcIssueReadErr: nil,
want: "",
wantErr: errors.Errorf("Unable to get version from /etc/issue: %v",
"No match for regex '^Open ?BMC Release (?P<version>[^\\s]+)' for input 'OpenBMC Release'"),
wantErr: errors.Errorf("Unable to get version from /etc/issue: missing version info"),
},
{
name: "openbmc wrong case ",
etcIssueContents: `openbmc Release wedge100-v2020.07.1`,
etcIssueReadErr: nil,
want: "",
wantErr: errors.Errorf("Unable to get version from /etc/issue: %v",
"No match for regex '^Open ?BMC Release (?P<version>[^\\s]+)' for input 'openbmc Release wedge100-v2020.07.1'"),
want: "wedge100-v2020.07.1",
wantErr: nil,
},
{
name: "early bletchley",
etcIssueContents: `Facebook OpenBMC bletchley-v2023.02.1 \\n \\l
`,
etcIssueReadErr: nil,
want: "bletchley-v2023.02.1",
wantErr: nil,
},
}
for _, tc := range cases {
Expand Down

0 comments on commit 45fe1cf

Please sign in to comment.