-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsize.rb
162 lines (127 loc) · 3.71 KB
/
size.rb
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
# frozen_string_literal: true
class Size
@ratio = 4
@block_size = 4096
def initialize(temp_rootfs_dir, extra_inode, extra_size_MB, *rootfs_dev)
@rDir = temp_rootfs_dir.to_s || ""
@eInode = extra_inode.to_i || 0
@eSizeM = extra_size_MB.to_i || 0
@rDev = rootfs_dev[0].to_s || ""
end
class << self
def BtoK(size)
return size / 1024
end
def KtoM(size)
return size / 1024
end
def MtoG(size)
return size / 1024
end
# sudo tune2fs -l /dev/nvme0n1p4
#
# Blocks per group: 32768
# Inodes per group: 8192
#
# 4 = 32768 / 8192
def InodeToBlock(inode)
return inode * @ratio
end
# Block size: 4096
def BlockToB(block)
return block * @block_size
end
def BlockToK(block)
return BtoK(BlockToB(block))
end
def InodeToK(inode)
return BlockToK(InodeToBlock(inode))
end
def BlockToM(block)
return KtoM(BlockToK(block))
end
def InodeToM(inode)
return KtoM(InodeToK(inode))
end
# sudo find . -printf "%h\n" | cut -d / -f -2 | sort | uniq -c | sort -rn
def DirInode(dir)
output = `sudo find #{dir}/.. -printf "%h\n" | cut -d / -f -2 | sort | uniq -c | sort -rn | grep #{dir}`
inode = output.split(" ").first
# puts inode
return inode.to_i
end
def DirSizeK(dir)
output = `sudo du -d0 #{dir}`
size = output.split(" ").first
# puts size
return size.to_i
end
def DirSizeM(dir)
return KtoM(DirSizeK(dir))
end
def DirInodeK(dir)
result = InodeToK(DirInode(dir))
# puts result
return result
end
def DirInodeM(dir)
return KtoM(DirInodeK(dir))
end
def Bigger(num_a, num_b)
return num_a if num_a >= num_b
return num_b
end
end
def GetRatio
if @rDev.empty?
puts "missing disk device ARG"
return false
end
output1 = `sudo tune2fs -l #{@rDev} | grep 'Blocks per group'`
output2 = `sudo tune2fs -l #{@rDev} | grep 'Inodes per group'`
return false if output1.empty? or output2.empty?
blocks = output1.split(" ").last
inodes = output2.split(" ").last
ratio = blocks.to_i.fdiv(inodes.to_i)
# puts ratio
return ratio
end
def GetBlockSize
if @rDev.empty?
puts "missing disk device ARG"
return false
end
output = `sudo tune2fs -l #{@rDev} | grep 'Block size'`
return false if output.empty?
size = output.split(" ").last
# puts size
return size.to_i
end
def GetSize
unless @rDev.empty?
@ratio = self.GetRatio || @ratio
@block_size = self.GetBlockSize || @block_size
end
if @rDir.empty?
puts "missing directory ARG"
return false
end
rInode = Size.DirInode @rDir
rSizeM = Size.DirSizeM @rDir
aInodeM = Size.InodeToM(rInode + @eInode)
aSizeM = rSizeM + @eSizeM
# puts aInodeM
return Size.Bigger(aInodeM.ceil(-1), aSizeM.ceil(-1))
end
end
# dir = "ub-22"
# dev = ""
# s = Size.new dir, 100, 100
# puts s.GetRatio
# puts s.GetBlockSize
# puts s.GetSize
# for n in 1..10
# s = Size.new "", 0, 0, "/dev/mmcblk0p#{n}"
# puts "/dev/mmcblk0p#{n} #{s.GetRatio}"
# end
# sudo ruby size.rb 2>/dev/null