resizefs_linux.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // +build linux
  2. /*
  3. Copyright 2017 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package resizefs
  15. import (
  16. "fmt"
  17. "k8s.io/klog"
  18. "k8s.io/kubernetes/pkg/util/mount"
  19. )
  20. // ResizeFs Provides support for resizing file systems
  21. type ResizeFs struct {
  22. mounter *mount.SafeFormatAndMount
  23. }
  24. // NewResizeFs returns new instance of resizer
  25. func NewResizeFs(mounter *mount.SafeFormatAndMount) *ResizeFs {
  26. return &ResizeFs{mounter: mounter}
  27. }
  28. // Resize perform resize of file system
  29. func (resizefs *ResizeFs) Resize(devicePath string, deviceMountPath string) (bool, error) {
  30. format, err := resizefs.mounter.GetDiskFormat(devicePath)
  31. if err != nil {
  32. formatErr := fmt.Errorf("ResizeFS.Resize - error checking format for device %s: %v", devicePath, err)
  33. return false, formatErr
  34. }
  35. // If disk has no format, there is no need to resize the disk because mkfs.*
  36. // by default will use whole disk anyways.
  37. if format == "" {
  38. return false, nil
  39. }
  40. klog.V(3).Infof("ResizeFS.Resize - Expanding mounted volume %s", devicePath)
  41. switch format {
  42. case "ext3", "ext4":
  43. return resizefs.extResize(devicePath)
  44. case "xfs":
  45. return resizefs.xfsResize(deviceMountPath)
  46. }
  47. return false, fmt.Errorf("ResizeFS.Resize - resize of format %s is not supported for device %s mounted at %s", format, devicePath, deviceMountPath)
  48. }
  49. func (resizefs *ResizeFs) extResize(devicePath string) (bool, error) {
  50. output, err := resizefs.mounter.Exec.Run("resize2fs", devicePath)
  51. if err == nil {
  52. klog.V(2).Infof("Device %s resized successfully", devicePath)
  53. return true, nil
  54. }
  55. resizeError := fmt.Errorf("resize of device %s failed: %v. resize2fs output: %s", devicePath, err, string(output))
  56. return false, resizeError
  57. }
  58. func (resizefs *ResizeFs) xfsResize(deviceMountPath string) (bool, error) {
  59. args := []string{"-d", deviceMountPath}
  60. output, err := resizefs.mounter.Exec.Run("xfs_growfs", args...)
  61. if err == nil {
  62. klog.V(2).Infof("Device %s resized successfully", deviceMountPath)
  63. return true, nil
  64. }
  65. resizeError := fmt.Errorf("resize of device %s failed: %v. xfs_growfs output: %s", deviceMountPath, err, string(output))
  66. return false, resizeError
  67. }