From 57f89da69a588e63c4c075a545dd49fdd50eb7ef Mon Sep 17 00:00:00 2001 From: Michael Schmidt Date: Thu, 17 Sep 2015 15:23:04 +0200 Subject: [PATCH] Fix to detect unformatted volumes in CoreOS The `file` command used here to check whether a device is formatted is not available for CoreOS. The effect is that the mounter tries to mount an unformatted volume which fails. This makes it quite tedious to use persistent volumes in CoreOS. This patch replaces the `file` command with `lsblk` which is available in CoreOS. I checked that it's also available on RHEL, Debian, Ubuntu and SLES. --- pkg/util/mount/mount.go | 7 ++++--- pkg/util/mount/safe_format_and_mount_test.go | 10 +++++----- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/pkg/util/mount/mount.go b/pkg/util/mount/mount.go index 83a8b0e7a60..7a9e2d372c3 100644 --- a/pkg/util/mount/mount.go +++ b/pkg/util/mount/mount.go @@ -98,9 +98,10 @@ func (mounter *SafeFormatAndMount) formatAndMount(source string, target string, // diskLooksUnformatted uses 'file' to see if the given disk is unformated func (mounter *SafeFormatAndMount) diskLooksUnformatted(disk string) (bool, error) { - args := []string{"-L", "--special-files", disk} - cmd := mounter.Runner.Command("file", args...) + args := []string{"-nd", "-o", "FSTYPE", disk} + cmd := mounter.Runner.Command("lsblk", args...) dataOut, err := cmd.CombinedOutput() + output := strings.TrimSpace(string(dataOut)) // TODO (#13212): check if this disk has partitions and return false, and // an error if so. @@ -109,7 +110,7 @@ func (mounter *SafeFormatAndMount) diskLooksUnformatted(disk string) (bool, erro return false, err } - return !strings.Contains(string(dataOut), "filesystem"), nil + return output == "", nil } // New returns a mount.Interface for the current system. diff --git a/pkg/util/mount/safe_format_and_mount_test.go b/pkg/util/mount/safe_format_and_mount_test.go index 9833f7ba4f8..26a23ae6e68 100644 --- a/pkg/util/mount/safe_format_and_mount_test.go +++ b/pkg/util/mount/safe_format_and_mount_test.go @@ -65,7 +65,7 @@ func TestSafeFormatAndMount(t *testing.T) { fstype: "ext4", mountErrs: []error{fmt.Errorf("unknown filesystem type '(null)'")}, execScripts: []ExecArgs{ - {"file", []string{"-L", "--special-files", "/dev/foo"}, "ext4 filesystem", nil}, + {"lsblk", []string{"-nd", "-o", "FSTYPE", "/dev/foo"}, "ext4", nil}, }, expectedError: fmt.Errorf("unknown filesystem type '(null)'"), }, @@ -73,7 +73,7 @@ func TestSafeFormatAndMount(t *testing.T) { fstype: "ext4", mountErrs: []error{fmt.Errorf("unknown filesystem type '(null)'")}, execScripts: []ExecArgs{ - {"file", []string{"-L", "--special-files", "/dev/foo"}, "data", nil}, + {"lsblk", []string{"-nd", "-o", "FSTYPE", "/dev/foo"}, "", nil}, {"mkfs.ext4", []string{"-E", "lazy_itable_init=0,lazy_journal_init=0", "-F", "/dev/foo"}, "", fmt.Errorf("formatting failed")}, }, expectedError: fmt.Errorf("formatting failed"), @@ -82,7 +82,7 @@ func TestSafeFormatAndMount(t *testing.T) { fstype: "ext4", mountErrs: []error{fmt.Errorf("unknown filesystem type '(null)'"), fmt.Errorf("Still cannot mount")}, execScripts: []ExecArgs{ - {"file", []string{"-L", "--special-files", "/dev/foo"}, "data", nil}, + {"lsblk", []string{"-nd", "-o", "FSTYPE", "/dev/foo"}, "", nil}, {"mkfs.ext4", []string{"-E", "lazy_itable_init=0,lazy_journal_init=0", "-F", "/dev/foo"}, "", nil}, }, expectedError: fmt.Errorf("Still cannot mount"), @@ -91,7 +91,7 @@ func TestSafeFormatAndMount(t *testing.T) { fstype: "ext4", mountErrs: []error{fmt.Errorf("unknown filesystem type '(null)'"), nil}, execScripts: []ExecArgs{ - {"file", []string{"-L", "--special-files", "/dev/foo"}, "data", nil}, + {"lsblk", []string{"-nd", "-o", "FSTYPE", "/dev/foo"}, "", nil}, {"mkfs.ext4", []string{"-E", "lazy_itable_init=0,lazy_journal_init=0", "-F", "/dev/foo"}, "", nil}, }, expectedError: nil, @@ -100,7 +100,7 @@ func TestSafeFormatAndMount(t *testing.T) { fstype: "ext3", mountErrs: []error{fmt.Errorf("unknown filesystem type '(null)'"), nil}, execScripts: []ExecArgs{ - {"file", []string{"-L", "--special-files", "/dev/foo"}, "data", nil}, + {"lsblk", []string{"-nd", "-o", "FSTYPE", "/dev/foo"}, "", nil}, {"mkfs.ext3", []string{"-E", "lazy_itable_init=0,lazy_journal_init=0", "-F", "/dev/foo"}, "", nil}, }, expectedError: nil,