
At some point I was bored with calculating the VMGUARPAGES and PRIVVMPAGES values for OpenVZ with a calculator and making sure that they were matched for different virtual machines (for example, that physical memory was distributed between machines in a consistent way, and one machine could not eat away physical memory from another and lead her to the swap, for example). And I wrote a small but very convenient “pseudographic” utility, vzmem, which I want to share now. I hope she will come in handy.
How to use dklab_vzmem
Vzmem is a command line utility that allows you to "visually" distribute physical memory between multiple OpenVZ virtual machines so that one machine does not crawl onto another. Those. if you need to increase memory for one of the machines, you can always see where this memory is required to “bite off”: from one of the existing machines or from the pool of free blocks.
Installing vzmem on the server
cd / usr / sbin
wget https://raw.github.com/DmitryKoterov/dklab_vzmem/master/vzmem
chmod + x vzmem
The memory allocation is set in the / etc / vz / conf / MEM-MAP text file, which looks like this:
File / etc / vz / conf / MEM-MAP
300
10003 vps1.example.com ========== 109552K
10004 vps2.example.com ============================== 335503K
20004 vps3.example.com ========== +++++++++++++++ 109552K + 221324K swap
FREE ====================== 513525K
- Each "=" icon symbolizes a piece of physical memory (in this example, 1/300 is its part) and affects the value of the OpenVZ VMGUARPAGES parameter (and also sets the value of SHMPAGES). By moving the "=" icons between different virtual machines, you can reallocate memory (the utility ensures that the total number of blocks remains equal to 300).
- The “+” icons symbolize “additional memory from currently unused blocks or SWAP”. You can add as many as you like (but preferably not very much so that the machine does not go too far into SWAP in critical situations). In total, the number of “=” and “+” icons indicates the value of the PRIVVMPAGES parameter (the same value is also written to OOMGUARPAGES).
- Well, the values in kilobytes after the icons "=" and "+" are calculated automatically - they do not need to be changed by hand.
“And why all this painting?” - you ask. But why: if there is a need to increase the memory of some machine, open any familiar text editor, select the "=" icons, which are not a pity, and use the "cut-paste" method to move them to the desired machine. At the same time, 300 characters remain in the sum of characters. (In mcedit, to which I am used, the key combination is: F3 - the beginning of the selection, F3 - the end of the selection, F6 - transfer of the selected characters to the cursor.)
Apply changes to MEM-MAP
When changes to the MEM-MAP are made, the following command should be executed:
Apply changes to / etc / vz / conf / MEM-MAP
vzmem -a
Total:
- VMGUARPAGES = SHMPAGES = number of characters ("=")
- PRIVVMPAGES = OOMGUARPAGES = number of characters ("=") + number of characters ("+")
Vzmem ensures that there are exactly 300 items in the amount of “=” icons (in this example) and reports an error if this is not the case.
')
Initial MEM-MAP File Creation
To initially create / etc / vz / conf / MEM-MAP based on your OpenVZ configuration, run the command:
Initial creation of the file / etc / vz / conf / MEM-MAP
vzmem -i 300
Instead of 300, you can specify any number that determines how many “blocks” the physical memory should be divided into. Put this number so that it is more convenient to move the icons "=" and "+" from one line to another.
If the memory at the time of the call vzmem -i is already allocated in a contradictory manner, vzmem tries to distribute the number of "=" icons among the machines proportionally. Just in case, check the result after initialization.
PS
The vzmem script takes 200 lines on Perl (
vzmem on GitHub ). If suddenly I invented the bicycle, write in the comments. Other similar in-spirit small utilities for OpenVZ:
vzfirewall ,
vzset ,
vzenter .