Debian Codegraph @

resizefs_linux.go

raw
1 //go:build linux
2
3 /*
4 Copyright 2021 The Kubernetes Authors.
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 */
18
19 package mount
20
21 import (
22 "fmt"
23 "strconv"
24 "strings"
25
26 "k8s.io/klog/v2"
27 utilexec "k8s.io/utils/exec"
28 )
29
30 const (
31 blockDev = "blockdev"
32 )
33
34 // ResizeFs Provides support for resizing file systems
35 type ResizeFs struct {
36 exec utilexec.Interface
37 }
38
39 // NewResizeFs returns new instance of resizer
40 func NewResizeFs(exec utilexec.Interface) *ResizeFs {
41 return &ResizeFs{exec: exec}
42 }
43
44 // Resize perform resize of file system
45 func (resizefs *ResizeFs) Resize(devicePath string, deviceMountPath string) (bool, error) {
46 format, err := getDiskFormat(resizefs.exec, devicePath)
47 if err != nil {
48 formatErr := fmt.Errorf("ResizeFS.Resize - error checking format for device %s: %v", devicePath, err)
49 return false, formatErr
50 }
51
52 // If disk has no format, there is no need to resize the disk because mkfs.*
53 // by default will use whole disk anyways.
54 if format == "" {
55 return false, nil
56 }
57
58 klog.V(3).Infof("ResizeFS.Resize - Expanding mounted volume %s", devicePath)
59 switch format {
60 case "ext3", "ext4":
61 return resizefs.extResize(devicePath)
62 case "xfs":
63 return resizefs.xfsResize(deviceMountPath)
64 case "btrfs":
65 return resizefs.btrfsResize(deviceMountPath)
66 }
67 return false, fmt.Errorf("ResizeFS.Resize - resize of format %s is not supported for device %s mounted at %s", format, devicePath, deviceMountPath)
68 }
69
70 func (resizefs *ResizeFs) extResize(devicePath string) (bool, error) {
71 output, err := resizefs.exec.Command("resize2fs", devicePath).CombinedOutput()
72 if err == nil {
73 klog.V(2).Infof("Device %s resized successfully", devicePath)
74 return true, nil
75 }
76
77 resizeError := fmt.Errorf("resize of device %s failed: %v. resize2fs output: %s", devicePath, err, string(output))
78 return false, resizeError
79 }
80
81 func (resizefs *ResizeFs) xfsResize(deviceMountPath string) (bool, error) {
82 args := []string{"-d", deviceMountPath}
83 output, err := resizefs.exec.Command("xfs_growfs", args...).CombinedOutput()
84
85 if err == nil {
86 klog.V(2).Infof("Device %s resized successfully", deviceMountPath)
87 return true, nil
88 }
89
90 resizeError := fmt.Errorf("resize of device %s failed: %v. xfs_growfs output: %s", deviceMountPath, err, string(output))
91 return false, resizeError
92 }
93
94 func (resizefs *ResizeFs) btrfsResize(deviceMountPath string) (bool, error) {
95 args := []string{"filesystem", "resize", "max", deviceMountPath}
96 output, err := resizefs.exec.Command("btrfs", args...).CombinedOutput()
97
98 if err == nil {
99 klog.V(2).Infof("Device %s resized successfully", deviceMountPath)
100 return true, nil
101 }
102
103 resizeError := fmt.Errorf("resize of device %s failed: %v. btrfs output: %s", deviceMountPath, err, string(output))
104 return false, resizeError
105 }
106
107 func (resizefs *ResizeFs) NeedResize(devicePath string, deviceMountPath string) (bool, error) {
108 // Do nothing if device is mounted as readonly
109 readonly, err := resizefs.getDeviceRO(devicePath)
110 if err != nil {
111 return false, err
112 }
113
114 if readonly {
115 klog.V(3).Infof("ResizeFs.needResize - no resize possible since filesystem %s is readonly", devicePath)
116 return false, nil
117 }
118
119 format, err := getDiskFormat(resizefs.exec, devicePath)
120 if err != nil {
121 formatErr := fmt.Errorf("ResizeFS.Resize - error checking format for device %s: %v", devicePath, err)
122 return false, formatErr
123 }
124
125 // If disk has no format, there is no need to resize the disk because mkfs.*
126 // by default will use whole disk anyways.
127 if format == "" {
128 return false, nil
129 }
130
131 switch format {
132 case "ext3", "ext4", "xfs":
133 // For ext3/ext4/xfs, recommendation received from linux filesystem folks is to let
134 // resize2fs/xfs_growfs do the check for us. So we will not do any check here.
135 return true, nil
136 case "btrfs":
137 deviceSize, err := resizefs.getDeviceSize(devicePath)
138 if err != nil {
139 return false, err
140 }
141 blockSize, fsSize, err := resizefs.getBtrfsSize(devicePath)
142 klog.V(5).Infof("Btrfs size: filesystem size=%d, block size=%d, err=%v", fsSize, blockSize, err)
143 if err != nil {
144 return false, err
145 }
146 if deviceSize <= fsSize+blockSize {
147 return false, nil
148 }
149 return true, nil
150 default:
151 return false, fmt.Errorf("could not parse fs info of given filesystem format: %s. Supported fs types are: xfs, ext3, ext4", format)
152 }
153 }
154
155 func (resizefs *ResizeFs) getDeviceSize(devicePath string) (uint64, error) {
156 output, err := resizefs.exec.Command(blockDev, "--getsize64", devicePath).CombinedOutput()
157 outStr := strings.TrimSpace(string(output))
158 if err != nil {
159 return 0, fmt.Errorf("failed to read size of device %s: %s: %s", devicePath, err, outStr)
160 }
161 size, err := strconv.ParseUint(outStr, 10, 64)
162 if err != nil {
163 return 0, fmt.Errorf("failed to parse size of device %s %s: %s", devicePath, outStr, err)
164 }
165 return size, nil
166 }
167
168 func (resizefs *ResizeFs) getBtrfsSize(devicePath string) (uint64, uint64, error) {
169 output, err := resizefs.exec.Command("btrfs", "inspect-internal", "dump-super", "-f", devicePath).CombinedOutput()
170 if err != nil {
171 return 0, 0, fmt.Errorf("failed to read size of filesystem on %s: %s: %s", devicePath, err, string(output))
172 }
173
174 blockSize, totalBytes, _ := resizefs.parseBtrfsInfoOutput(string(output), "sectorsize", "total_bytes")
175
176 if blockSize == 0 {
177 return 0, 0, fmt.Errorf("could not find block size of device %s", devicePath)
178 }
179 if totalBytes == 0 {
180 return 0, 0, fmt.Errorf("could not find total size of device %s", devicePath)
181 }
182 return blockSize, totalBytes, nil
183 }
184
185 func (resizefs *ResizeFs) parseBtrfsInfoOutput(cmdOutput string, blockSizeKey string, totalBytesKey string) (uint64, uint64, error) {
186 lines := strings.Split(cmdOutput, "\n")
187 var blockSize, blockCount uint64
188 var err error
189
190 for _, line := range lines {
191 tokens := strings.Fields(line)
192 if len(tokens) != 2 {
193 continue
194 }
195 key, value := strings.ToLower(strings.TrimSpace(tokens[0])), strings.ToLower(strings.TrimSpace(tokens[1]))
196
197 if key == blockSizeKey {
198 blockSize, err = strconv.ParseUint(value, 10, 64)
199 if err != nil {
200 return 0, 0, fmt.Errorf("failed to parse block size %s: %s", value, err)
201 }
202 }
203 if key == totalBytesKey {
204 blockCount, err = strconv.ParseUint(value, 10, 64)
205 if err != nil {
206 return 0, 0, fmt.Errorf("failed to parse total size %s: %s", value, err)
207 }
208 }
209 }
210 return blockSize, blockCount, err
211 }
212
213 func (resizefs *ResizeFs) getDeviceRO(devicePath string) (bool, error) {
214 output, err := resizefs.exec.Command(blockDev, "--getro", devicePath).CombinedOutput()
215 outStr := strings.TrimSpace(string(output))
216 if err != nil {
217 return false, fmt.Errorf("failed to get readonly bit from device %s: %w: %s", devicePath, err, outStr)
218 }
219 switch outStr {
220 case "0":
221 return false, nil
222 case "1":
223 return true, nil
224 default:
225 return false, fmt.Errorf("failed readonly device check. Expected 1 or 0, got '%s'", outStr)
226 }
227 }