Skip to content

Commit 24c1e30

Browse files
alex347holimanfjl
authored
cmd/geth: graceful shutdown if disk is full (#22103)
Adding warnings of free disk space left and graceful shutdown when there is not enough space left. This also adds a flag datadir.minfreedisk which can be used to set the trigger for low disk space, and setting it to zero disables the check. Co-authored-by: Martin Holst Swende <[email protected]> Co-authored-by: Felix Lange <[email protected]>
1 parent 5e9f5ca commit 24c1e30

File tree

6 files changed

+113
-2
lines changed

6 files changed

+113
-2
lines changed

cmd/geth/main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ var (
6565
utils.LegacyBootnodesV5Flag,
6666
utils.DataDirFlag,
6767
utils.AncientFlag,
68+
utils.MinFreeDiskSpaceFlag,
6869
utils.KeyStoreDirFlag,
6970
utils.ExternalSignerFlag,
7071
utils.NoUSBFlag,
@@ -368,7 +369,7 @@ func startNode(ctx *cli.Context, stack *node.Node, backend ethapi.Backend) {
368369
debug.Memsize.Add("node", stack)
369370

370371
// Start up the node itself
371-
utils.StartNode(stack)
372+
utils.StartNode(ctx, stack)
372373

373374
// Unlock any account specifically requested
374375
unlockAccounts(ctx, stack)

cmd/geth/usage.go

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ var AppHelpFlagGroups = []flags.FlagGroup{
3636
configFileFlag,
3737
utils.DataDirFlag,
3838
utils.AncientFlag,
39+
utils.MinFreeDiskSpaceFlag,
3940
utils.KeyStoreDirFlag,
4041
utils.USBFlag,
4142
utils.SmartCardDaemonPathFlag,

cmd/utils/cmd.go

+33-1
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,20 @@ import (
2626
"runtime"
2727
"strings"
2828
"syscall"
29+
"time"
2930

3031
"github.com/ethereum/go-ethereum/common"
3132
"github.com/ethereum/go-ethereum/core"
3233
"github.com/ethereum/go-ethereum/core/rawdb"
3334
"github.com/ethereum/go-ethereum/core/types"
3435
"github.com/ethereum/go-ethereum/crypto"
36+
"github.com/ethereum/go-ethereum/eth"
3537
"github.com/ethereum/go-ethereum/ethdb"
3638
"github.com/ethereum/go-ethereum/internal/debug"
3739
"github.com/ethereum/go-ethereum/log"
3840
"github.com/ethereum/go-ethereum/node"
3941
"github.com/ethereum/go-ethereum/rlp"
42+
"gopkg.in/urfave/cli.v1"
4043
)
4144

4245
const (
@@ -63,14 +66,25 @@ func Fatalf(format string, args ...interface{}) {
6366
os.Exit(1)
6467
}
6568

66-
func StartNode(stack *node.Node) {
69+
func StartNode(ctx *cli.Context, stack *node.Node) {
6770
if err := stack.Start(); err != nil {
6871
Fatalf("Error starting protocol stack: %v", err)
6972
}
7073
go func() {
7174
sigc := make(chan os.Signal, 1)
7275
signal.Notify(sigc, syscall.SIGINT, syscall.SIGTERM)
7376
defer signal.Stop(sigc)
77+
78+
minFreeDiskSpace := eth.DefaultConfig.TrieDirtyCache
79+
if ctx.GlobalIsSet(MinFreeDiskSpaceFlag.Name) {
80+
minFreeDiskSpace = ctx.GlobalInt(MinFreeDiskSpaceFlag.Name)
81+
} else if ctx.GlobalIsSet(CacheFlag.Name) || ctx.GlobalIsSet(CacheGCFlag.Name) {
82+
minFreeDiskSpace = ctx.GlobalInt(CacheFlag.Name) * ctx.GlobalInt(CacheGCFlag.Name) / 100
83+
}
84+
if minFreeDiskSpace > 0 {
85+
go monitorFreeDiskSpace(sigc, stack.InstanceDir(), uint64(minFreeDiskSpace)*1024*1024)
86+
}
87+
7488
<-sigc
7589
log.Info("Got interrupt, shutting down...")
7690
go stack.Close()
@@ -85,6 +99,24 @@ func StartNode(stack *node.Node) {
8599
}()
86100
}
87101

102+
func monitorFreeDiskSpace(sigc chan os.Signal, path string, freeDiskSpaceCritical uint64) {
103+
for {
104+
freeSpace, err := getFreeDiskSpace(path)
105+
if err != nil {
106+
log.Warn("Failed to get free disk space", "path", path, "err", err)
107+
break
108+
}
109+
if freeSpace < freeDiskSpaceCritical {
110+
log.Error("Low disk space. Gracefully shutting down Geth to prevent database corruption.", "available", common.StorageSize(freeSpace))
111+
sigc <- syscall.SIGTERM
112+
break
113+
} else if freeSpace < 2*freeDiskSpaceCritical {
114+
log.Warn("Disk space is running low. Geth will shutdown if disk space runs below critical level.", "available", common.StorageSize(freeSpace), "critical_level", common.StorageSize(freeDiskSpaceCritical))
115+
}
116+
time.Sleep(60 * time.Second)
117+
}
118+
}
119+
88120
func ImportChain(chain *core.BlockChain, fn string) error {
89121
// Watch for Ctrl-C while the import is running.
90122
// If a signal is received, the import will stop at the next batch.

cmd/utils/diskusage.go

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2021 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
// +build !windows
18+
19+
package utils
20+
21+
import (
22+
"fmt"
23+
24+
"golang.org/x/sys/unix"
25+
)
26+
27+
func getFreeDiskSpace(path string) (uint64, error) {
28+
var stat unix.Statfs_t
29+
if err := unix.Statfs(path, &stat); err != nil {
30+
return 0, fmt.Errorf("failed to call Statfs: %v", err)
31+
}
32+
33+
// Available blocks * size per block = available space in bytes
34+
return stat.Bavail * uint64(stat.Bsize), nil
35+
}

cmd/utils/diskusage_windows.go

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2021 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package utils
18+
19+
import (
20+
"fmt"
21+
22+
"golang.org/x/sys/windows"
23+
)
24+
25+
func getFreeDiskSpace(path string) (uint64, error) {
26+
27+
cwd, err := windows.UTF16PtrFromString(path)
28+
if err != nil {
29+
return 0, fmt.Errorf("failed to call UTF16PtrFromString: %v", err)
30+
}
31+
32+
var freeBytesAvailableToCaller, totalNumberOfBytes, totalNumberOfFreeBytes uint64
33+
if err := windows.GetDiskFreeSpaceEx(cwd, &freeBytesAvailableToCaller, &totalNumberOfBytes, &totalNumberOfFreeBytes); err != nil {
34+
return 0, fmt.Errorf("failed to call GetDiskFreeSpaceEx: %v", err)
35+
}
36+
37+
return freeBytesAvailableToCaller, nil
38+
}

cmd/utils/flags.go

+4
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ var (
113113
Name: "datadir.ancient",
114114
Usage: "Data directory for ancient chain segments (default = inside chaindata)",
115115
}
116+
MinFreeDiskSpaceFlag = DirectoryFlag{
117+
Name: "datadir.minfreedisk",
118+
Usage: "Minimum free disk space in MB, once reached triggers auto shut down (default = --cache.gc converted to MB, 0 = disabled)",
119+
}
116120
KeyStoreDirFlag = DirectoryFlag{
117121
Name: "keystore",
118122
Usage: "Directory for the keystore (default = inside the datadir)",

0 commit comments

Comments
 (0)