2019-01-12 04:58:27 +00:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
/*
|
2021-03-18 22:40:29 +00:00
|
|
|
Copyright 2021 The Kubernetes Authors.
|
2019-01-12 04:58:27 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2021-03-18 22:40:29 +00:00
|
|
|
package mount
|
2019-01-12 04:58:27 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
"k8s.io/klog/v2"
|
2021-03-18 22:40:29 +00:00
|
|
|
utilexec "k8s.io/utils/exec"
|
2019-01-12 04:58:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ResizeFs Provides support for resizing file systems
|
|
|
|
type ResizeFs struct {
|
2021-03-18 22:40:29 +00:00
|
|
|
exec utilexec.Interface
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewResizeFs returns new instance of resizer
|
2021-03-18 22:40:29 +00:00
|
|
|
func NewResizeFs(exec utilexec.Interface) *ResizeFs {
|
|
|
|
return &ResizeFs{exec: exec}
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Resize perform resize of file system
|
|
|
|
func (resizefs *ResizeFs) Resize(devicePath string, deviceMountPath string) (bool, error) {
|
2021-03-18 22:40:29 +00:00
|
|
|
format, err := getDiskFormat(resizefs.exec, devicePath)
|
2019-01-12 04:58:27 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
formatErr := fmt.Errorf("ResizeFS.Resize - error checking format for device %s: %v", devicePath, err)
|
|
|
|
return false, formatErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// If disk has no format, there is no need to resize the disk because mkfs.*
|
|
|
|
// by default will use whole disk anyways.
|
|
|
|
if format == "" {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
klog.V(3).Infof("ResizeFS.Resize - Expanding mounted volume %s", devicePath)
|
|
|
|
switch format {
|
|
|
|
case "ext3", "ext4":
|
|
|
|
return resizefs.extResize(devicePath)
|
|
|
|
case "xfs":
|
|
|
|
return resizefs.xfsResize(deviceMountPath)
|
2021-03-18 22:40:29 +00:00
|
|
|
case "btrfs":
|
|
|
|
return resizefs.btrfsResize(deviceMountPath)
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
return false, fmt.Errorf("ResizeFS.Resize - resize of format %s is not supported for device %s mounted at %s", format, devicePath, deviceMountPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (resizefs *ResizeFs) extResize(devicePath string) (bool, error) {
|
2021-03-18 22:40:29 +00:00
|
|
|
output, err := resizefs.exec.Command("resize2fs", devicePath).CombinedOutput()
|
2019-01-12 04:58:27 +00:00
|
|
|
if err == nil {
|
|
|
|
klog.V(2).Infof("Device %s resized successfully", devicePath)
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
resizeError := fmt.Errorf("resize of device %s failed: %v. resize2fs output: %s", devicePath, err, string(output))
|
|
|
|
return false, resizeError
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (resizefs *ResizeFs) xfsResize(deviceMountPath string) (bool, error) {
|
|
|
|
args := []string{"-d", deviceMountPath}
|
2021-03-18 22:40:29 +00:00
|
|
|
output, err := resizefs.exec.Command("xfs_growfs", args...).CombinedOutput()
|
2019-01-12 04:58:27 +00:00
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
klog.V(2).Infof("Device %s resized successfully", deviceMountPath)
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
resizeError := fmt.Errorf("resize of device %s failed: %v. xfs_growfs output: %s", deviceMountPath, err, string(output))
|
|
|
|
return false, resizeError
|
|
|
|
}
|
2021-03-18 22:40:29 +00:00
|
|
|
|
|
|
|
func (resizefs *ResizeFs) btrfsResize(deviceMountPath string) (bool, error) {
|
|
|
|
args := []string{"filesystem", "resize", "max", deviceMountPath}
|
|
|
|
output, err := resizefs.exec.Command("btrfs", args...).CombinedOutput()
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
klog.V(2).Infof("Device %s resized successfully", deviceMountPath)
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
resizeError := fmt.Errorf("resize of device %s failed: %v. btrfs output: %s", deviceMountPath, err, string(output))
|
|
|
|
return false, resizeError
|
|
|
|
}
|