Vzmem: visual consistent memory allocation for OpenVZ

    At some point, I was tired of calculating the values ​​of VMGUARPAGES and PRIVVMPAGES for OpenVZ with a calculator and making sure that they were consistent for different virtual machines (for example, that physical memory was distributed between machines in a consistent way, and one machine couldn’t “eat up” physical memory from another and take her to a swap, for example). And I wrote a small but very convenient “pseudographic” utility, vzmem, which I now want to share. I hope she comes in handy for someone.

    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 the memory for any of the machines, you can always see where this memory is to be "bitten off" from: from one of the existing machines or from a pool of free blocks.
    Install 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 text file / etc / vz / conf / MEM-MAP, which looks something like this:
    File / etc / vz / conf / MEM-MAP
    10003 vps1.example.com =========== 109552K
    10004 vps2.example.com =============================== 335503K
    20004 vps3.example.com ========== ++++++++++++++++ 109552K + 221324K swap
    FREE ======================== 513525K
    1. Each "=" symbolizes a piece of physical memory (in this example, 1/300 of it) and affects the value of the OpenVZ VMGUARPAGES parameter (and also sets the SHMPAGES value). By moving the "=" icons between different virtual machines, you can redistribute the memory (the utility makes sure that the total number of blocks remains equal to 300).
    2. The “+” symbols symbolize “additional memory from currently unused blocks or SWAP”. They can be added as many as you want (but preferably not very much so that the machine does not go into SWAP too much in critical situations). In total, the number of "=" and "+" icons indicates the value of the PRIVVMPAGES parameter (the same value is recorded in OOMGUARPAGES).
    3. Well, the values ​​in kilobytes after the "=" and "+" icons 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 a 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 remains in the sum of characters. (In mcedit, which I'm used to, the key combination is: F3 - start of selection, F3 - end of selection, F6 - move selected characters to the cursor position.)

    Applying Changes to MEM-MAP

    When changes have been made to the MEM-MAP, run the following command:
    Applying Changes to / etc / vz / conf / MEM-MAP
    vzmem -a
    • VMGUARPAGES = SHMPAGES = number of digits ("=")
    • PRIVVMPAGES = OOMGUARPAGES = number of digits ("=") + number of digits ("+")
    Vzmem makes sure that the total number of "=" icons is exactly 300 pieces (in this example) and reports an error if this is not the case.

    Initial creation of a MEM-MAP file

    To initially create / etc / vz / conf / MEM-MAP based on your OpenVZ configuration, run the command:
    Initial creation of the / etc / vz / conf / MEM-MAP file
    vzmem -i 300
    Instead of 300, you can specify any number that determines how many "blocks" should be divided into physical memory. Set this number so that it is more convenient to move the "=" and "+" icons from one line to another.

    If the memory at the time of the call to vzmem -i was already allocated in a contradictory way, vzmem tries to distribute the number of "=" icons between the machines in a proportional way. Just in case, check the result after initialization.

    The vzmem script takes 200 lines on Perl ( vzmem on GitHub ). If suddenly I invented a bicycle, write in the comments. Other similar in spirit small utilities for OpenVZ: vzfirewall , vzset , vzenter .

    Also popular now: