-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlinuxTmpfsRamdisk.py
More file actions
445 lines (351 loc) · 14.3 KB
/
linuxTmpfsRamdisk.py
File metadata and controls
445 lines (351 loc) · 14.3 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
"""
Linux tmpfs ramdisk implementation
@author: Roy Nielsen
"""
#--- Native python libraries
import os
import re
import pwd
import sys
import traceback
from tempfile import mkdtemp
from time import time
#--- non-native python libraries in this source tree
from lib.run_commands import RunWith
from lib.loggers import CyLogger
from lib.loggers import LogPriority as lp
from commonRamdiskTemplate import RamDiskTemplate, NotValidForThisOS, BadRamdiskArguments
from lib.libHelperExceptions import SystemToolNotAvailable, UserMustBeRootError
###############################################################################
class RamDisk(RamDiskTemplate):
"""
http://www.cyberciti.biz/tips/what-is-devshm-and-its-practical-usage.html
In this example, remount /dev/shm with 8G size as follows:
# mount -o remount,size=8G /dev/shm
To be frank, if you have more than 2GB RAM + multiple Virtual machines,
this hack always improves performance. In this example, you will give you
tmpfs instance on /disk2/tmpfs which can allocate 5GB RAM/SWAP in 5K inodes
and it is only accessible by root:
# mount -t tmpfs -o size=5G,nr_inodes=5k,mode=700 tmpfs /disk2/tmpfs
Where,
-o opt1,opt2 : Pass various options with a -o flag followed by a comma
separated string of options. In this examples, I used the
following options:
remount : Attempt to remount an already-mounted filesystem. In this
example, remount the system and increase its size.
size=8G or size=5G : Override default maximum size of the
/dev/shm filesystem. he size is given in bytes,
and rounded up to entire pages. The default is half
of the memory. The size parameter also accepts a
suffix % to limit this tmpfs instance to that
percentage of your pysical RAM: the default, when
neither size nor nr_blocks is specified, is
size=50%. In this example it is set to 8GiB or 5GiB.
The tmpfs mount options for sizing ( size,
nr_blocks, and nr_inodes) accept a suffix k, m or
g for Ki, Mi, Gi (binary kilo, mega and giga) and
can be changed on remount.
nr_inodes=5k : The maximum number of inodes for this instance. The
default is half of the number of your physical RAM pages,
or (on a machine with highmem) the number of lowmem RAM
pages, whichever is the lower.
mode=700 : Set initial permissions of the root directory.
tmpfs : Tmpfs is a file system which keeps all files in virtual memory.
---------------------------------------------------------------------------
Another link:
http://www.jamescoyle.net/how-to/943-create-a-ram-disk-in-linux
Exerpt:
mount -t [TYPE] -o size=[SIZE],opt2=[opt2],opt3=[opt3] [FSTYPE] [MOUNTPOINT]
Substitute the following attirbutes for your own values:
[TYPE] is the type of RAM disk to use; either tmpfs or ramfs.
[SIZE] is the size to use for the file system. Remember that ramfs does not
have a physical limit and is specified as a starting size.
[FSTYPE] is the type of RAM disk to use; either tmpfs, ramfs, ext4, etc.
Example:
mount -t tmpfs -o size=512m tmpfs /mnt/ramdisk
"""
def __init__(self, size, mountpoint, logger,
mode=700, uid=None, gid=None,
fstype="tmpfs", nr_inodes=None, nr_blocks=None):
"""
"""
super(RamDisk, self).__init__(size, mountpoint, logger)
#####
# The passed in size of ramdisk should be in 1Mb chunks
self.module_version = '20160224.032043.009191'
self.logger = logger
if not sys.platform.startswith("linux"):
raise NotValidForThisOS("This ramdisk is only viable for a Linux.")
if fstype in ["tmpfs", "ramfs"]:
self.fstype = fstype
if fstype == "tmpfs":
self.myRamdiskDev = "/dev/tmpfs"
else:
raise BadRamdiskArguments("Not a valid argument for " + \
"'fstype'...")
if not os.geteuid() == 0:
raise UserMustBeRootError("You must be root, or have elevated with sudo to use this software...")
if isinstance(mode, int):
self.mode = mode
else:
self.mode = 700
if not isinstance(uid, int):
self.uid = os.getuid()
else:
self.uid = uid
if not isinstance(gid, int):
self.gid = os.getgid()
else:
self.gid = gid
if isinstance(nr_inodes, basestring):
self.nr_inodes = nr_inodes
else:
self.nr_inodes = None
if isinstance(nr_blocks, basestring):
self.nr_blocks = nr_blocks
else:
self.nr_blocks = None
#####
# Initialize the mount and umount command paths...
self.mountPath = ""
self.umountPath = ""
self.getCmds()
#####
# Initialize the RunWith helper for executing shelled out commands.
self.runWith = RunWith(self.logger)
#self.runWith.getNlogReturns()
self.success = self._mount()
self.logger.log(lp.DEBUG, "Finishing linux ramdisk init...")
###########################################################################
def getCmds(self):
"""
Acquire the paths for mount and umount on the system...
@author: Roy Nielsen
"""
success = False
paths = ["/bin", "/usr/bin", "/sbin", "/usr/sbin", "/usr/local/bin", "/user/local/sbin"]
#####
# Look for the mount command
mountFound = False
for path in paths:
possibleFullPath = os.path.join(path, "mount")
if os.path.exists(possibleFullPath):
self.mountPath = possibleFullPath
mountFound = True
if not mountFound:
raise SystemToolNotAvailable("Cannot find mount command...")
#####
# Look for the umount command
umountFound = False
for path in paths:
possibleFullPath = os.path.join(path, "umount")
if os.path.exists(possibleFullPath):
self.umountPath = possibleFullPath
umountFound = True
if not umountFound:
raise SystemToolNotAvailable("Cannot find umount command...")
#####
# Figure out if this method was successfull or not.
if mountFound and umountFound:
success = True
return success
###########################################################################
def buildCommand(self):
"""
Build a command based on the "fstype" passed in.
For more options on the tmpfs filesystem, check the mount manpage.
@author: Roy Nielsen
"""
command=None
if self.fstype == "ramfs":
command = [self.mountPath, "-t", "ramfs"]
elif self.fstype == "tmpfs":
options = ["size=" + str(self.diskSize) + "m"]
options.append("uid=" + str(self.uid))
options.append("gid=" + str(self.gid))
options.append("mode=" + str(self.mode))
"""
try:
options.append(self.nr_inodes)
except AttributeError:
pass
try:
options.append("nr_blocks=" + str(self.nr_blocks))
except AttributeError:
pass
"""
command = [self.mountPath, "-t", "tmpfs", "-o",
",".join(options), "tmpfs", self.mntPoint]
self.logger.log(lp.DEBUG, "command: " + str(command))
#/bin/mount -t tmpfs -o size=500m,uid=0,gid=0,mode=700 /tmp/tmp0gnLNt
return command
###########################################################################
def _format(self) :
"""
One can't really format a tmpfs disk, so this will mimic a format
by unmounting an recreating the disk.
@author: Roy Nielsen
"""
success = False
successOne = self.umount()
successTwo = self._mount()
if successOne and successTwo:
success = True
return success
###########################################################################
def _mount(self) :
"""
Mount the disk
@author: Roy Nielsen
"""
success = False
if not os.path.exists(self.mntPoint):
os.makedirs(self.mntPoint)
self.logger.log(lp.DEBUG, "Created mount point")
elif os.path.exists(self.mntPoint) and not os.path.isdir(self.mntPoint):
# Cannot use mkdtmp here because it will make the directory on
# the root filesystem instead of the ramdisk, then it will try
# to link across filesystems which won't work
tmpdir = self.mntPoint + "." + str(time())
os.rename(self.mntPoint, tmpdir)
os.mkdir(self.mntPoint)
command = self.buildCommand()
self.logger.log(lp.WARNING, "Command: " + str(command))
self.runWith.setCommand(command)
output, error, returncode = self.runWith.communicate()
self.logger.log(lp.DEBUG, "output : " + str(output))
self.logger.log(lp.DEBUG, "error : " + str(error))
self.logger.log(lp.DEBUG, "returncode: " + str(returncode))
if not error:
success = True
self.logger.log(lp.DEBUG, "Damn it Jim! The Damn Thing worked!!!")
self.getNlogData()
return success
###########################################################################
def remount(self, size=0, mountpoint="", mode=700, uid=None, gid=None,
nr_inodes=None, nr_blocks=None):
"""
Use the tmpfs ability to be remounted with different options
If bad input is given, the previous values will be used.
@author: Roy Nielsen
"""
#####
# Input Validation:
#####
# tmpfs is the only viable ramdisk that handles remounting ok.
# this includes mouting tmpfs with msdos, ext2,3,4, etc.
if not self.fstype == "tmpfs":
raise BadRamdiskArguments("Can only use 'remount' with " + \
"tmpfs...")
if size and isinstance(size, int):
self.diskSize = size
if mountpoint and isinstance(mountpoint, type.string):
self.mntPoint = mountpoint
if mode and isinstance(mode, int):
self.mode = mode
if uid and isinstance(uid, int):
self.uid = uid
if gid and isinstance(gid, int):
self.gid = gid
if nr_inodes and isinstance(nr_inodes, (int, long)):
self.nr_inodes = nr_inodes
if nr_blocks and isinstance(nr_blocks, (int, long)):
self.nr_blocks = nr_blocks
self.buildCommand()
self._mount()
###########################################################################
def unmount(self) :
"""
Unmount the disk
@author: Roy Nielsen
"""
success = False
command = [self.umountPath, self.mntPoint]
self.runWith.setCommand(command)
self.runWith.communicate()
retval, reterr, retcode = self.runWith.getNlogReturns()
if not reterr:
success = True
return success
###########################################################################
def umount(self):
"""
Unmount the disk
@author: Roy Nielsen
"""
success = False
success = self.unmount()
return success
###########################################################################
def detach(self) :
"""
Unmount the disk
@author: Roy Nielsen
"""
success = False
success = self.umount()
return success
###########################################################################
def __isMemoryAvailable(self):
"""
Check to make sure there is plenty of memory of the size passed in
before creating the ramdisk
Must be over-ridden to provide OS/Method specific functionality
@author: Roy Nielsen
"""
#mem_free = psutil.phymem_usage()[2]
#print "Memory free = " + str(mem_free)
success = False
return success
###########################################################################
def getVersion(self):
"""
Getter for the version of the ramdisk
@author: Roy Nielsen
"""
return self.module_version
###############################################################################
def detach(mnt_point="", logger=False):
"""
Mirror for the unmount function...
@author: Roy Nielsen
"""
success = umount(mnt_point, logger)
return success
###############################################################################
def umount(mnt_point="", logger=False):
"""
Unmount the ramdisk
@author: Roy Nielsen
"""
success = False
if mnt_point:
paths = ["/bin", "/usr/bin", "/sbin", "/usr/sbin", "/usr/local/bin", "/user/local/sbin"]
#####
# Look for the umount command
umountFound = False
umountPath = ""
for path in paths:
possibleFullPath = os.path.join(path, "umount")
if os.path.exists(possibleFullPath):
umountPath = possibleFullPath
umountFound = True
if not umountFound:
raise SystemToolNotAvailable("Cannot find umount command...")
#####
# Run the umount command...
runWith = RunWith(logger)
command = [umountPath, mnt_point]
runWith.setCommand(command)
runWith.communicate()
retval, reterr, retcode = runWith.getNlogReturns()
if not reterr:
success = True
return success
def unmount(mnt_point="", logger=False):
'''
mirror functioin for umount
'''
success = False
success = umount(mnt_point, logger)
return success