forked from bradfitz/embiggen-disk
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fs.go
223 lines (199 loc) · 5.15 KB
/
fs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
//go:build linux
/*
Copyright 2018 Google Inc.
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 main
import (
"bufio"
"bytes"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
"github.com/skiffos/embiggen-disk/findmnt"
"golang.org/x/sys/unix"
)
func getFileSystemResizer(mnt string) (Resizer, error) {
fs, err := statFS(mnt)
if err != nil {
return nil, err
}
var cmd *exec.Cmd
switch fs.fstype {
case "ext2", "ext3", "ext4":
cmd = exec.Command("resize2fs", fs.dev)
// pass EXT2FS_PRETEND_... variables
cmd.Env = os.Environ()
return fsResizer{fs, cmd}, nil
case "xfs":
cmd = exec.Command("xfs_growfs", "-d", fs.mnt)
return fsResizer{fs, cmd}, nil
case "btrfs":
cmd = exec.Command("btrfs", "filesystem", "resize", "max", fs.mnt)
return fsResizer{fs, cmd}, nil
}
return nil, fmt.Errorf("unsupported filesystem type %q", fs.fstype)
}
type fsResizer struct {
fs fsStat
cmd *exec.Cmd
}
func (e fsResizer) String() string {
return fmt.Sprintf("%s filesystem at %s", e.fs.fstype, e.fs.mnt)
}
func (e fsResizer) DepResizer() (Resizer, error) {
// TODO: use /proc/devices instead and stat the thing to
// figure out what it is, rather than using its name.
dev := e.fs.dev
if dev == "/dev/root" {
return nil, errors.New("unexpected device /dev/root from statFS")
}
if (strings.HasPrefix(dev, "/dev/sd") ||
strings.HasPrefix(dev, "/dev/vd") ||
strings.HasPrefix(dev, "/dev/mmcblk") ||
strings.HasPrefix(dev, "/dev/nvme")) &&
devEndsInNumber(dev) {
vlogf("fsResizer.DepResizer: returning partitionResizer(%q)", dev)
return partitionResizer(dev), nil
}
if strings.HasPrefix(dev, "/dev/mapper") ||
strings.HasPrefix(filepath.Base(dev), "dm-") {
return lvResizer(dev), nil
}
return nil, fmt.Errorf("don't know how to resize block device %q", dev)
}
func (e fsResizer) Resize() error {
if *dry {
fmt.Printf("[dry-run] would've run %v %v\n", e.cmd.Path, e.cmd.Args)
return nil
}
out, err := e.cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("running %v %v: %v, %s", e.cmd.Path, e.cmd.Args, err, out)
}
return nil
}
func (e fsResizer) State() (string, error) {
st, err := statFS(e.fs.mnt)
if err != nil {
return "", err
}
return fmt.Sprintf("%v blocks", st.statfs.Blocks), nil
}
type fsStat struct {
mnt string
dev string
fstype string
statfs unix.Statfs_t
}
func statFS(mnt string) (fs fsStat, err error) {
fs, err = statFSFindmnt(mnt)
if err != nil || fs.dev != "" {
return fs, err
}
err = unix.Statfs(mnt, &fs.statfs)
if err != nil {
return
}
mounts, err := os.ReadFile("/proc/mounts")
if err != nil {
return
}
bs := bufio.NewScanner(bytes.NewReader(mounts))
for bs.Scan() {
f := strings.Fields(bs.Text())
if len(f) < 3 {
continue
}
if f[0] == "rootfs" {
// See https://github.com/google/embiggen-disk/issues/6
continue
}
if f[1] == mnt {
if fs.dev == "/dev/root" {
dev, err := findDevRoot()
if err != nil {
return fs, fmt.Errorf("failed to map /dev/root to real device: %v", err)
}
fs.dev = dev
}
fs.mnt = mnt
fs.dev = f[0]
fs.fstype = f[2]
return fs, err
}
}
return fs, errors.New("mount point not found")
}
// statFSFindmnt uses findmnt to stat the filesystem.
func statFSFindmnt(mnt string) (fs fsStat, err error) {
cmd := exec.Command(
"findmnt",
"--noheadings",
"--output", "FSTYPE,SOURCE",
"-J",
mnt,
)
vlogf("statFSFindmnt: running(%s)", cmd.String())
out, err := cmd.Output()
if err != nil {
return fs, fmt.Errorf("error running findmnt: %v", execErrDetail(err))
}
var output findmnt.Output
if len(out) > 2 {
err = (&output).UnmarshalJSON(out)
if err != nil {
return fs, fmt.Errorf("error parsing findmnt output: %v", err)
}
}
fsInfo := &output.Filesystems[0]
if len(fsInfo.Fstype) != 0 {
fs.mnt = mnt
fs.fstype = fsInfo.Fstype
fs.dev = fsInfo.Source
}
if fs.dev == "" {
return fs, errors.New("mount point not found")
}
return fs, nil
}
// findDevRoot finds which block device (e.g. "/dev/nvme0n1p1") patches the device number of /dev/root.
func findDevRoot() (string, error) {
fis, err := os.ReadDir("/dev")
if err != nil {
return "", err
}
dev := map[string]uint64{}
for _, fi := range fis {
fii, err := fi.Info()
if err != nil {
continue
}
if fii.Mode()&os.ModeDevice == 0 || fii.Mode()&os.ModeCharDevice != 0 {
continue
}
dev[fi.Name()] = uint64(fii.Sys().(*syscall.Stat_t).Rdev)
}
wantDevnum, ok := dev["root"]
if !ok {
return "", errors.New("/dev/root not found in /dev")
}
for baseName, devNum := range dev {
if devNum == wantDevnum && baseName != "root" {
return "/dev/" + baseName, nil
}
}
return "", errors.New("no block device in /dev had device number like /dev/root")
}