Module: LinuxStat::Mounts

Defined in:
lib/linux_stat/mounts.rb

Class Method Summary collapse

Class Method Details

.device_stat(dev = root) ⇒ Object

device_stat(dev = root)

Not to confuse this method with devices_stat() which shows all devices

It shows all the block devices corresponding to mount points and data from LinuxStat::FS.stat(arg)

For example:

LinuxStat::Mounts.device_stat('/dev/sda2')

=> {"/dev/sda2"=>{:mountpoint=>"/", :total=>119981191168, :free=>35298562048, :available=>35298562048, :used=>84682629120, :percent_used=>70.58, :percent_free=>29.42, :percent_available=>29.42}}

The return type is Hash. But if the status isn’t available, it will return an empty Hash.



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
# File 'lib/linux_stat/mounts.rb', line 179

def device_stat(dev = root)
	# Code duplication is fine if it gives maximum performance
	m = {}
	mounts.each do |x|
		x.strip!

		unless x.empty?
			_x = x.split
			next if _x[0] != dev

			total, free, available, used = fs_info(_x[1])

			m.merge!({
				mountpoint: _x[1],

				total: total,
				free: free,
				available: available,
				used: used,

				percent_used: used.*(100).fdiv(total).round(2),
				percent_free: free.*(100).fdiv(total).round(2),
				percent_available: available.*(100).fdiv(total).round(2),
			})

			break
		end
	end
	m
end

.devices_statObject

devices_stat

Not to confuse this method with device_stat(dev) which shows only one device’s info

It shows all the block devices corresponding to mount points and data from LinuxStat::FS.stat(arg)

For example:

LinuxStat::Mounts.devices_stat

=> {"proc"=>{:mountpoint=>"/proc", :total=>0, :free=>0, :available=>0, :used=>0, :percent_used=>NaN, :percent_free=>NaN, :percent_available=>NaN}, "/dev/sdb1"=>{:mountpoint=>"/run/media/sourav/5c2b7af7-d4c3-4ab4-a035-06d18ffc8e6f", :total=>31466008576, :free=>2693931008, :available=>2693931008, :used=>28772077568, :percent_used=>91.44, :percent_free=>8.56, :percent_available=>8.56}}

The return type is Hash. But if the status isn’t available, it will return an empty Hash.



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
# File 'lib/linux_stat/mounts.rb', line 137

def devices_stat
	# Code duplication is fine if it gives maximum performance
	m = {}
	mounts.each do |x|
		x.strip!

		unless x.empty?
			_x = x.split
			total, free, available, used = fs_info(_x[1])

			m.merge!(_x[0] => {
				mountpoint: _x[1],

				total: total,
				free: free,
				available: available,
				used: used,

				percent_used: used.*(100).fdiv(total).round(2),
				percent_free: free.*(100).fdiv(total).round(2),
				percent_available: available.*(100).fdiv(total).round(2),
			})
		end
	end
	m
end

.listObject

Reads /proc/mounts and returns the output splitted with n.

In other words, it’s same as running IO.readlines(‘/proc/mounts’).each(&:strip!)

It returns an Array. If the info isn’t available or /proc/mounts is not readable, it will return an empty Array.



11
12
13
# File 'lib/linux_stat/mounts.rb', line 11

def list
	mounts
end

.list_devicesObject

Reads /proc/mounts and returns list of devices.

It returns an Array. If the info isn’t available or /proc/mounts is not readable, it will return an empty Array.



20
21
22
# File 'lib/linux_stat/mounts.rb', line 20

def list_devices
	mounts.map { |x| x.split(?\s.freeze).first }
end

.list_devices_mount_pointObject

list_devices_mount_point()

It shows all the block devices corresponding to mount points.

For example:

LinuxStat::Mounts.list_devices_mount_point

=> {"proc"=>"/proc", "sys"=>"/sys", "dev"=>"/dev", "run"=>"/run", "/dev/sda2"=>"/", "securityfs"=>"/sys/kernel/security", "tmpfs"=>"/run/user/1000", "devpts"=>"/dev/pts", "cgroup2"=>"/sys/fs/cgroup/unified", "cgroup"=>"/sys/fs/cgroup/perf_event", "pstore"=>"/sys/fs/pstore", "none"=>"/sys/fs/bpf", "systemd-1"=>"/proc/sys/fs/binfmt_misc", "debugfs"=>"/sys/kernel/debug", "mqueue"=>"/dev/mqueue", "hugetlbfs"=>"/dev/hugepages", "tracefs"=>"/sys/kernel/tracing", "configfs"=>"/sys/kernel/config", "fusectl"=>"/sys/fs/fuse/connections", "gvfsd-fuse"=>"/run/user/1000/gvfs", "/dev/sdb1"=>"/run/media/sourav/5c2b7af7-d4c3-4ab4-a035-06d18ffc8e6f", "binfmt_misc"=>"/proc/sys/fs/binfmt_misc"}

The return type is Hash. But if the status isn’t available or the device isn’t mounted, it will return an empty String.



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/linux_stat/mounts.rb', line 109

def list_devices_mount_point
	m = {}
	mounts.each do |x|
		x.strip!

		unless x.empty?
			_x = x.split
			m.merge!(_x[0] => _x[1])
		end
	end
	m
end

.mount_point(dev = root) ⇒ Object

mount_point(dev = root)

Where device = block device.

The default argument is the root block device.

It helps you find the mountpoint of a block device. For example:

LinuxStat::Mounts.mount_point('/dev/sdb1')

=> "/run/media/sourav/5c2b7af7-d4c3-4ab4-a035-06d18ffc8e6f"

The return type is String. But if the status isn’t available or the device isn’t mounted, it will return an empty String.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/linux_stat/mounts.rb', line 80

def mount_point(dev = root)
	m = ''
	mounts.each do |x|
		x.strip!

		unless x.empty?
			_x = x.split
			if _x[0] == dev
				m.replace(_x[1])
				break
			end
		end
	end
	m
end

.rootObject

Reads /proc/mounts and returns partition name of the device mounted at /.

It returns a String. But if the info isn’t available or /proc/mounts is not readable, it will return an empty frozen String.



29
30
31
# File 'lib/linux_stat/mounts.rb', line 29

def root
	find_root[0].to_s
end

.root_fsObject

Reads /proc/mounts and returns the file system of the device mounted at /.

It returns a String. But if the info isn’t available or /proc/mounts is not readable, it will return an empty frozen String.



38
39
40
# File 'lib/linux_stat/mounts.rb', line 38

def root_fs
	find_root[2].to_s
end

.root_mount_optionsObject

Reads /proc/mounts and returns the options used for mounting /.

It returns a String. But if the info isn’t available or /proc/mounts is not readable, it will return an empty frozen string.



47
48
49
# File 'lib/linux_stat/mounts.rb', line 47

def root_mount_options
	find_root[3].to_s
end

.tmpfsObject

Reads /proc/mounts and finds all tmpfs.

It returns a Hash But if the info isn’t available or /proc/mounts is not readable, it will return an empty Hash.



56
57
58
59
60
61
62
# File 'lib/linux_stat/mounts.rb', line 56

def tmpfs
	ret = {}
	mounts.each { |x|
		ret.merge!({x.split[1] => x}) if x.start_with?('tmpfs '.freeze)
	}
	ret
end