Skip to content

Commit

Permalink
Ensure minimum kernel version for thin_ls
Browse files Browse the repository at this point in the history
Ensure that kernel >= 4.4.0 or RHEL/Centos 7 kernel >= 3.10.0-366 exists before starting the thin
pool watcher. Prior versions have a bug in which reserving and releasing the metadata snapshot can
cause thin pool corruption.
  • Loading branch information
Andy Goldstein committed Aug 4, 2016
1 parent c6c06d4 commit d1e2041
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
85 changes: 85 additions & 0 deletions container/docker/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ import (
"fmt"
"path"
"regexp"
"strconv"
"strings"
"sync"
"syscall"

"github.com/blang/semver"
dockertypes "github.com/docker/engine-api/types"
"github.com/google/cadvisor/container"
"github.com/google/cadvisor/container/libcontainer"
Expand Down Expand Up @@ -178,6 +181,14 @@ func startThinPoolWatcher(dockerInfo *dockertypes.Info) (*devicemapper.ThinPoolW
return nil, err
}

kernelVersion, err := getKernelVersion()
if err != nil {
return nil, err
}
if err := ensureThinLsKernelVersion(kernelVersion); err != nil {
return nil, err
}

dockerThinPoolName, err := dockerutil.DockerThinPoolName(*dockerInfo)
if err != nil {
return nil, err
Expand All @@ -197,6 +208,80 @@ func startThinPoolWatcher(dockerInfo *dockertypes.Info) (*devicemapper.ThinPoolW
return thinPoolWatcher, nil
}

func getKernelVersion() (string, error) {
unameOutput := syscall.Utsname{}
err := syscall.Uname(&unameOutput)
if err != nil {
return "", err
}

b := make([]byte, len(unameOutput.Release))
for i, v := range unameOutput.Release {
b[i] = byte(v)
}
return string(b), nil
}

func ensureThinLsKernelVersion(kernelVersion string) error {
// kernel 4.4.0 has the proper bug fixes to allow thin_ls to work without corrupting the thin pool
version_4_4_0 := semver.MustParse("4.4.0")
// RHEL kernel 3.10.0 release >= 366 has the proper bug fixes backported from 4.4.0 to allow
// thin_ls to work without corrupting the thin pool
version_3_10_0 := semver.MustParse("3.10.0")

matches := version_re.FindStringSubmatch(kernelVersion)
if len(matches) < 4 {
return fmt.Errorf("error parsing kernel version %q: expected 4 matches, got %d", kernelVersion, len(matches))
}

sem, err := semver.Make(matches[0])
if err != nil {
return err
}

if sem.GTE(version_4_4_0) {
// kernel 4.4+ - good
return nil
}

// Certain RHEL/Centos 7.x kernels have a backport to fix the corruption bug
if !strings.Contains(kernelVersion, ".el7.") {
// not a RHEL 7.x kernel - won't work
return fmt.Errorf("kernel version 4.4.0 or later is required to use thin_ls - you have %q", kernelVersion)
}

// RHEL/Centos 7.x from here on
if sem.Major != 3 {
// only 3.x kernels *may* work correctly
return fmt.Errorf("RHEL/Centos 7.x kernel version 3.10.0-366 or later is required to use thin_ls - you have %q", kernelVersion)
}

if sem.GT(version_3_10_0) {
// 3.10.1+ - good
return nil
}

if sem.EQ(version_3_10_0) {
// need to check release
releaseRE := regexp.MustCompile(`^[^-]+-([^.]+)\.`)
releaseMatches := releaseRE.FindStringSubmatch(kernelVersion)
if len(releaseMatches) != 2 {
return fmt.Errorf("unable to determine RHEL/Centos 7.x kernel release from %q", kernelVersion)
}

release, err := strconv.Atoi(releaseMatches[1])
if err != nil {
return fmt.Errorf("error parsing release %q: %v", releaseMatches[1], err)
}

if release >= 366 {
return nil
}
}

return fmt.Errorf("RHEL/Centos 7.x kernel version 3.10.0-366 or later is required to use thin_ls - you have %q", kernelVersion)
}

// Register root container before running this function!
func Register(factory info.MachineInfoFactory, fsInfo fs.FsInfo, ignoreMetrics container.MetricSet) error {
client, err := Client()
Expand Down
47 changes: 47 additions & 0 deletions container/docker/factory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2016 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package docker

import "testing"

func TestEnsureThinLsKernelVersion(t *testing.T) {
tests := []struct {
version string
ok bool
}{
{"4.4.0-31-generic", true},
{"4.4.1", true},
{"4.6.4-301.fc24.x86_64", true},
{"3.10.0-327.22.2.el7.x86_64", false},
{"3.10.0-366.el7.x86_64", true},
{"3.10.0.el7.abc", false},
{"3.10.0-abc.el7.blarg", false},
{"3.10.0-367.el7.x86_64", true},
{"3.10.0-366.x86_64", false},
{"3.10.1-1.el7.x86_64", true},
{"2.0.36", false},
{"2.1", false},
{"4.0.0.el7.x86_64", false},
}

for _, test := range tests {
err := ensureThinLsKernelVersion(test.version)
if err != nil && test.ok {
t.Errorf("%s: expected no error, got %v", test.version, err)
} else if err == nil && !test.ok {
t.Errorf("%s: expected an error", test.version)
}
}
}

0 comments on commit d1e2041

Please sign in to comment.