forked from bradfitz/embiggen-disk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
127 lines (112 loc) · 3.03 KB
/
main.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
/*
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.
*/
// The embiggen-disk command live resizes a filesystem and LVM objects
// and partition tables as needed. It's useful within a VM guest to make
// its filesystem bigger when the hypervisor live resizes the underlying
// block device.
package main
// TODO: test/fix on disks with non-512 byte sectors ( /sys/block/sda/queue/hw_sector_size)
import (
"flag"
"fmt"
"log"
"os"
"runtime"
)
var (
dry = flag.Bool("dry-run", false, "don't make changes")
verbose = flag.Bool("verbose", false, "verbose output")
)
func init() {
flag.Usage = usage
}
func usage() {
fmt.Fprintf(os.Stderr, "Usage of embiggen-disk:\n\n")
fmt.Fprintf(os.Stderr, "# embiggen-disk [flags] <mount-point-to-enlarge>\n\n")
flag.PrintDefaults()
os.Exit(1)
}
func fatalf(format string, args ...interface{}) {
log.SetFlags(0)
log.Fatalf(format, args...)
}
func vlogf(format string, args ...interface{}) {
if *verbose {
log.Printf(format, args...)
}
}
func main() {
flag.Parse()
if flag.NArg() != 1 {
usage()
}
if runtime.GOOS != "linux" {
fatalf("embiggen-disk only runs on Linux.")
}
mnt := flag.Arg(0)
e, err := getFileSystemResizer(mnt)
vlogf("getFileSystemResizer(%q) = %#v, %v", mnt, e, err)
if err != nil {
fatalf("error preparing to enlarge %s: %v", mnt, err)
}
changes, err := Resize(e)
if len(changes) > 0 {
fmt.Printf("Changes made:\n")
for _, c := range changes {
fmt.Printf(" * %s\n", c)
}
} else if err == nil {
fmt.Printf("No changes made.\n")
}
if err != nil {
fatalf("error: %v", err)
}
}
// An Resizer is anything that can enlarge something and describe its state.
// An Resizer can depend on another Resizer to run first.
type Resizer interface {
String() string // "ext4 filesystem at /", "LVM PV foo"
State() (string, error) // "534 blocks"
Resize() error // both may be non-zero
DepResizer() (dep Resizer, err error) // can return (nil, nil) for none
}
// Resize resizes e's dependencies and then resizes e.
func Resize(e Resizer) (changes []string, err error) {
s0, err := e.State()
if err != nil {
return
}
dep, err := e.DepResizer()
if err != nil {
return
}
if dep != nil {
changes, err = Resize(dep)
if err != nil {
return
}
}
err = e.Resize()
if err != nil {
return
}
s1, err := e.State()
if err != nil {
err = fmt.Errorf("error after successful resize of %v: %v", e, err)
return
}
if s0 != s1 {
changes = append(changes, fmt.Sprintf("%v: before: %v, after: %v", e, s0, s1))
}
return
}