Container management method and system, electronic device, and storage medium
By establishing kernel file nodes in the kernel space and starting management processes, communication between containers and the host machine is realized, ensuring that all containers are in a consistent state and resources are released before the entire machine is restarted or shut down, thus solving the problems of inconsistent container states and unreleased resources in existing technologies.
Patent Information
- Application Number
- PCT/CN2024/127211
- Authority / Receiving Office
- WO · WO
- Patent Type
- Applications
- Current Assignee / Owner
- Priority Date
- 2024-05-20
- Filing Date
- 2024-10-25
- Publication Date
- 2025-11-27
AI Technical Summary
In existing technologies, container startup interfaces cannot achieve full machine restart or shutdown, and there are issues with inconsistent container states or unreleased resources during full machine restart.
A kernel file node is created in the kernel space. Both the host machine and the container mount this node and start a management process. After receiving a control request, the target container writes the control type. The host machine management process listens and sets the control types of other containers. Only after all containers have performed the corresponding operations is the entire machine restarted or shut down.
It achieves container state consistency and resource release, ensuring a smooth restart or shutdown process for the entire machine and avoiding resource leaks and state inconsistencies.
Smart Images

Figure CN2024127211_27112025_PF_FP_ABST
Abstract
Description
Container management method, system, electronic device and storage medium
[0001] Reference of Related Applications
[0002] The present disclosure claims the full right of priority of the Chinese Invention Patent Application No. 202410642618.1, entitled “A container management method, system, electronic device and storage medium” and filed with the State Intellectual Property Office of the People’s Republic of China on May 20, 2024, and incorporates it in its entirety by reference.
[0003] TECHNICAL FIELD
[0004] The present disclosure generally relates to the field of computer technology, and more particularly to a container management method, system, electronic device and storage medium.
[0005] BACKGROUND
[0006] A container is a virtualization technology for resource isolation running on a host. Multiple containers are often started in the host, and the whole machine needs to be restarted or shut down during system maintenance, update or fault handling. However, in the related art, the single restart or shutdown of the container is usually realized by clicking the restart button on the start interface of the container, and there is no solution to realize the whole machine restart or shutdown through a single container.
[0007] SUMMARY
[0008] In one aspect, the present disclosure provides a container management method, comprising:
[0009] establishing a kernel file node in the kernel space;
[0010] controlling the host and the plurality of containers to mount the kernel file node, and starting a management process in the host and all the containers; wherein the management process is a process for reading and writing the kernel file node;
[0011] if a target container receives a control request, writing the control type of the control request into the kernel file node, and controlling the target container to perform a container restart operation or a container exit operation corresponding to the control request;
[0012] if the management process of the host listens to the control type written in the kernel file node, and the control type is a whole machine restart or whole machine shutdown, setting the control type of other containers in the kernel file node by using the management process of the host, so that the management process of the other containers performs an operation corresponding to the control request after listening to the control type written by the host;
[0013] controlling the host to perform a whole machine restart operation or a whole machine shutdown operation.
[0014] In some embodiments, after the target container receives the control request, further comprising:
[0015] writing the control type of the control request into a file system node; wherein the file system node is a node in a kernel space, and the host machine and all the containers are mounted with the file system node;
[0016] Correspondingly, controlling the target container to perform a container restart operation or a container exit operation corresponding to the control request, comprises:
[0017] terminating all child processes of the init process of the target container, and recycling mounting resources of all the child processes;
[0018] controlling the init process of the target container to exit; and
[0019] reading the control type from the file system node, and performing a container restart operation or a container exit operation corresponding to the control request according to the control type.
[0020] In some embodiments, performing a container restart operation or a container exit operation corresponding to the control request according to the control type, comprises:
[0021] if the control type is container restart, restarting the init process of the target container;
[0022] if the control type is container exit or system shutdown, controlling the container process of the target container to exit;
[0023] if the control type is system restart, restarting the init process of the target container after the host machine performs a system restart operation.
[0024] In some embodiments, before controlling the host machine to perform a system restart operation or a system shutdown operation, further comprising:
[0025] judging whether container processes of all the containers have exited;
[0026] if yes, entering the step of controlling the host machine to perform a system restart operation or a system shutdown operation.
[0027] In some embodiments, judging whether container processes of all the containers have exited, comprises:
[0028] reading container state information in the kernel file node; wherein the container state information is used to store a state of each container; and
[0029] Based on the container status information, determine whether the container processes of all the containers have exited.
[0030] In some implementations, after setting the control type of other containers in the kernel file node using the host machine's management process, the method further includes:
[0031] Start a timer to record the waiting time;
[0032] Accordingly, after controlling the host machine to perform a system reboot or system shutdown operation, the method further includes:
[0033] If a container process fails to exit and the waiting time exceeds the threshold, the process proceeds to control the host machine to perform a system restart or system shutdown.
[0034] In some implementations, the container management method further includes:
[0035] If the management process of the host machine detects that the control type written in the kernel file node is container restart or container exit, then the monitoring thread is started, and the monitoring thread is used to determine whether the target container has completed the container restart operation or container exit operation within a preset time.
[0036] If the target container fails to complete the container restart operation or container exit operation within the first preset time, the management process of the host machine sets the control type of all the containers in the kernel file node, so that the management process of all the containers will perform the container restart operation after listening to the control type written by the host machine.
[0037] Control the host machine to perform a complete machine restart operation.
[0038] On the other hand, this disclosure also provides a container management system, which includes:
[0039] The node creation module is configured to create kernel file nodes in the kernel space;
[0040] The process startup module is configured to control the host machine and multiple containers to mount the kernel file node, and to start a management process in the host machine and all the containers; wherein the management process is a process used to read and write the kernel file node;
[0041] The container control module is configured to, if the target container receives a control request, write the control type of the control request into the kernel file node, and control the target container to perform the container restart operation or container exit operation corresponding to the control request;
[0042] The whole machine control module is configured to, if the management process of the host machine listens to the control type written in the kernel file node as whole machine restart or whole machine shutdown, set the control type of other containers in the kernel file node by the management process of the host machine, so that the management process of the other containers executes the operation corresponding to the control request after listening to the control type written by the host machine; and is further configured to control the host machine to execute the whole machine restart operation or the whole machine shutdown operation.
[0043] In another aspect, the disclosure also provides a storage medium having a computer program stored thereon, the computer program being executed to implement the container management method of the disclosure.
[0044] In another aspect, the disclosure also provides an electronic device including a memory and a processor, the memory having a computer program stored therein, the processor invoking the computer program in the memory to implement the container management method of the disclosure.
[0045] In some embodiments, a kernel file node is established in the kernel space, and the host machine and the plurality of containers are controlled to mount the kernel file node, and a management process is started in the host machine and the containers, so that the host machine and the containers can communicate through the kernel file node. When a target container receives a control request, the target container writes the control type of the control request into the kernel file node, and performs a corresponding operation according to the execution of the control request, such as container restart or exit. When the management process of the host machine listens to the control type written in the kernel file node as whole machine restart or whole machine shutdown, the management process of the host machine sets the control type of other containers in the kernel file node, so that the management process of the other containers also executes a corresponding operation after listening to the control type written by the host machine. After the plurality of containers all execute the operation corresponding to the control request, the situation of inconsistent container state or resource not released can be avoided, so the host machine can be controlled to execute the whole machine restart operation or the whole machine shutdown operation, thereby achieving the whole machine restart or the whole machine shutdown based on the containers.
[0046] BRIEF DESCRIPTION OF DRAWINGS
[0047] In order to more clearly illustrate the embodiments of the disclosure, the drawings needed in the embodiments will be briefly introduced below. Obviously, the drawings in the following description are only some embodiments of the disclosure, and other drawings can be obtained by those skilled in the art without creative labor.
[0048] FIG. 1 is a flowchart of a container management method provided by an embodiment of the disclosure;
[0049] FIG. 2 is a schematic diagram of a start management system structure of three containers provided by an embodiment of the disclosure;
[0050] Figure 3 is a flowchart of a container management process in an Android operating system according to an embodiment of the present disclosure; and
[0051] Figure 4 is a structural diagram of a container management system according to an embodiment of the present disclosure.
[0052] Detailed description
[0053] To make the objects, technical solutions, and advantages of the embodiments of the present disclosure clearer, the technical solutions in the embodiments of the present disclosure will be described clearly and completely below with reference to the drawings in the embodiments of the present disclosure. Obviously, the described embodiments are only some but not all of the embodiments of the present disclosure. Based on the embodiments in the present disclosure, all other embodiments obtained by a person of ordinary skill in the art without creative work fall within the protection scope of the present disclosure.
[0054] Please refer to Figure 1, which is a flowchart of a container management method according to an embodiment of the present disclosure.
[0055] The specific steps can include:
[0056] S101: establishing a kernel file node in a kernel space;
[0057] S102: controlling a host machine and a plurality of containers to mount the kernel file node, and starting a management process in the host machine and all the containers;
[0058] S103: if a target container receives a control request, writing a control type of the control request into the kernel file node, and controlling the target container to perform a container restart operation or a container exit operation corresponding to the control request;
[0059] S104: if the management process of the host machine listens to a control type written in the kernel file node and the control type is a whole machine restart or a whole machine shutdown, setting a control type of other containers in the kernel file node by using the management process of the host machine, so that the management process of the other containers performs an operation corresponding to the control request after listening to the control type written by the host machine;
[0060] S105: controlling the host machine to perform a whole machine restart operation or a whole machine shutdown operation.
[0061] In some embodiments, the present embodiment can be applied to electronic devices such as smart phones, tablets, computers, etc., and the virtual memory of the electronic devices includes a user space and a kernel space. The present disclosure can establish a kernel file node in the kernel space, and each operating system can have its corresponding kernel file node, for example, in a Linux system, the kernel file node is a dev node used to store a directory of hardware device information.
[0062] In some embodiments, the host machine provides a virtualization environment for running containers, and multiple containers can run on the host machine. The present disclosure can control the host machine and the containers to mount the kernel file node in the kernel. The present disclosure can also start a management process in the host machine (i.e., the environment of the host machine) and the container, and the management process is a process for reading and writing the kernel file node. The host machine and each container can read and write the kernel file node through the respective management processes.
[0063] In some embodiments, step S103 is established on the basis of the target container receiving a control request. The target container can be any container running on the host machine. The control request can be a request generated by a child process inside the target container, or a request input by a user through the start interface of the target container.
[0064] In some embodiments, the target container can write the control type of the control request to the kernel file node in the kernel space. The control type is information used to describe the control object and the control method. The control object can be the whole machine (host machine) or the container (i.e., a single container), and the control method can be reboot or shutdown.
[0065] In some embodiments, after receiving the control request, the target container can also be controlled to perform a container restart operation or a container exit operation corresponding to the control request. Specifically, the operation corresponding to the target request can be the operation corresponding to the control method in the control type of the control request. The sub-step of the whole machine restart operation includes container restart, and the whole machine shutdown operation includes container exit. Therefore, in this step, the target container can perform a container restart operation or a container shutdown operation corresponding to the target request.
[0066] In some embodiments, the management process of the host machine can read the information in the kernel file node to listen to the control type written in the kernel file node. If the control type is a whole machine restart or a whole machine shutdown, the other containers in the host machine except the target container need to be notified to restart or exit. In some embodiments, the management process of the host machine can set the control type of the other containers in the kernel file node; the management process of the other containers also listens to the kernel file node, and if the management process detects the control type written by the host machine, the other containers can perform the operation corresponding to the control type of the control request, i.e., container restart or container exit.
[0067] In some embodiments, the present embodiment can set a corresponding read-write field for each container in the kernel file node, and the management process of the container only has read-write permission for the read-write field corresponding to itself, and the management process of the host machine has read-write permission for all read-write fields. If the target container receives a control request, the control type of the control request is written in the read-write field corresponding to the target container in the kernel file node; if the management process of the host machine listens to the control type written in the read-write field corresponding to the target container, the same control type is written in the read-write field corresponding to the other containers in the kernel file node by the management process of the host machine; if the management process of the other containers listens to the control type written in the read-write field corresponding to the target container, the operation corresponding to the control type is performed.
[0068] In some embodiments, after the containers of the host machine (including the target container and the other containers) perform the whole machine restart operation or the whole machine shutdown operation corresponding to the control request, the host machine can be controlled to perform the whole machine restart operation or the whole machine shutdown operation. In some embodiments, if the control type is a whole machine restart, the host machine can be controlled to perform the whole machine restart operation; if the control type is a whole machine shutdown, the host machine can be controlled to perform the whole machine shutdown operation.
[0069] In some embodiments, a kernel file node is established in the kernel space, and the host machine and the plurality of containers are controlled to mount the kernel file node. A management process is also started in the host machine and the containers, and the management process enables the host machine and the containers to communicate through the kernel file node. When the target container receives a control request, the target container writes the control type of the control request into the kernel file node, and performs a corresponding operation according to the execution of the control request, such as container restart or container exit. When the management process of the host machine listens to the control type written in the kernel file node, the management process of the host machine sets the control type of other containers in the kernel file node, so that the management process of other containers also performs a corresponding operation after listening to the control type written by the host machine. After the plurality of containers perform the operation corresponding to the control request, the inconsistent state of the containers or the un-released resources can be avoided, and thus the host machine can be controlled to perform a whole machine restart operation or a whole machine shutdown operation, thereby achieving the whole machine restart or the whole machine shutdown based on the containers.
[0070] As a further introduction to the embodiment corresponding to FIG. 1, a file system node can also be established in the kernel space, and each operating system can have a corresponding file system node. For example, in a Linux system, the file system node is a sys node for storing system kernel information. In the Linux system, a sysfs pseudo file system is mounted to the / sys node, and by accessing the files under the sys node, various system kernel information can be obtained.
[0071] In some embodiments, an operation of establishing a file system node can also exist, and an operation of controlling the host machine and the plurality of containers to mount the file system node can also exist. On this basis, after the target container receives a control request, the control type of the control request can also be written into the file system node. The file system node is a node in the kernel space, and the host machine and all the containers mount the file system node.
[0072] In some embodiments, the target container can be controlled to perform an operation corresponding to a control request in the following manner:
[0073] Step A1: terminate all child processes by using the init process of the target container, and recycle the mounting resources of all the child processes;
[0074] Step A2: control the init process of the target container to exit;
[0075] Step A3: read the control type from the file system node, and perform a container restart operation or a container exit operation corresponding to the control request according to the control type.
[0076] In some embodiments, the init process (process with PID 1) is the first process of the user space started by the system, and all user processes are hatched by the init process. In some embodiments, the init process can be sent a termination signal, and then the init process is responsible for terminating all its child processes (i.e., processes started and managed by the init process).
[0077] In some embodiments, a termination signal can be sent to the init process in step A1 to request the init process to terminate all child processes; and a resource recycling signal can be sent to the init process after all child processes are terminated to recycle the mounting resources of the child processes.
[0078] The container can mount some file systems or devices during runtime, and these mounted resources need to be properly recycled after the container is stopped to avoid resource leakage. Recycling the mounted resources usually includes operations such as unmounting file systems and releasing device files.
[0079] In some embodiments, the init process can be controlled to exit by sending a specific system signal, and since the init process is the parent process of all other processes, all processes in the target container will be terminated when the init process exits.
[0080] In some embodiments, the control type is written in the file system node, and the target container can read the control type from the mounted file system node after the init process exits, and then perform a container restart operation or a container exit operation corresponding to the control request according to the control type.
[0081] In some embodiments, performing a container restart operation or a container exit operation corresponding to the control request according to the control type includes the following ways:
[0082] Way (1): If the control type is container restart, the init process of the target container can be restarted. The original init process has been exited before this step, and a new init process instance is created to restart the container.
[0083] Way (2): If the control type is container exit or whole machine shutdown, the container process of the target container can be controlled to exit. When the control type is container exit or whole machine shutdown, this operation mode can ensure that all resources in the container are properly released when the container exits or the whole machine shuts down, avoiding resource leakage.
[0084] Method (3): If the control type is a full system reboot, the init process of the target container can be restarted after the host machine performs a full system reboot. The original init process has already exited before this step. After the host machine performs a full system reboot, the init process of the target container can be restarted, thereby restoring the container's running state. Through the above operations, it can be ensured that the target container can automatically resume operation even if the host machine reboots.
[0085] The above methods ensure that the lifecycle of the target container can be effectively controlled, and that all resources can be properly reclaimed when the target container ends, avoiding resource leaks and potential security issues.
[0086] In some implementations, after the management process of other containers detects the control type written by the host machine, the other containers can perform the following operations to implement the operation corresponding to the control request:
[0087] Step B1: Write the control type of the control request to the file system node;
[0088] Step B2: Terminate all child processes using the init process of the other containers and reclaim all mounted resources of the child processes;
[0089] Step B3: Control the exit of the init process of the other containers; and
[0090] Step B4: Read the control type from the file system node, and execute the container restart operation or container exit operation corresponding to the control request according to the control type.
[0091] In some implementations, if the control type is a complete shutdown, the container processes of the other containers are controlled to exit; if the control type is a complete reboot, the init processes of the other containers are restarted after the host machine performs a complete reboot.
[0092] In some implementations, before controlling the host machine to perform a system restart or shutdown operation, it can be determined whether all container processes of the containers have exited; if so, the step of controlling the host machine to perform a system restart or shutdown operation is then initiated.
[0093] In some implementations, the kernel file node can record the status of each container, so it can be determined whether the container processes of all the containers have exited by reading the container status information in the kernel file node; wherein the container status information is used to store the status of each container; and it is determined whether the container processes of all the containers have exited based on the container status information.
[0094] In some embodiments, after the management process of the host machine sets the control type of other containers in the kernel file node, a timer can be started to record the waiting time.
[0095] In some embodiments, after the host machine is controlled to perform a system restart operation or a system shutdown operation, if there are container processes that have not exited and the waiting time is greater than a threshold, the step of controlling the host machine to perform a system restart operation or a system shutdown operation is entered. In this way, the host machine can be controlled to perform a system restart operation or a system shutdown operation after all container processes of all containers have exited or the waiting time is greater than a threshold.
[0096] In some embodiments, if the management process of the host machine listens to the control type written in the kernel file node and the control type is container restart or container exit, a monitoring thread is started, and the monitoring thread is used to determine whether the target container completes the container restart operation or the container exit operation within a preset time. If the target container does not complete the container restart operation or the container exit operation within the first preset time, the management process of the host machine is used to set the control type of all containers in the kernel file node, so that all management processes of the containers perform a container restart operation after listening to the control type written by the host machine; the host machine is controlled to perform a system restart operation.
[0097] In some embodiments, the above process is performed by starting a monitoring thread that determines whether the target container completes the restart operation or the container exit operation within a preset time. If the target container does not complete the container restart operation or the container exit operation within the first preset time, it means that the target container may have a fault, and at this time, all containers can be controlled to restart and the host machine can be controlled to perform a system restart operation.
[0098] The following describes the process described in the above embodiments by starting more than three containers on an Android in practical applications.
[0099] Taking starting container one, container two and container three on an Android system as an example, the Android starting part outside the container is a host environment host. The restart process of container one is as follows: when a restart button is clicked on the starting interface of container one, sys.powerctl = reboot, xx is written, which triggers the init process of container one to execute the restart process. Subsequently, after the kernel receives the restart instruction reboot, it judges whether the init process of container one is the No. 1 process. Since the init process of container one is not the No. 1 process, the kernel exits it. After container one detects that its init process has exited, the init process is executed again, thereby completing the separate restart process of container one. The whole machine starting process in the related art is as follows: the host environment is switched through adb / serial port, the reboot, xx instruction is executed in adb / serial port, the instruction is written into sys.powerctl to trigger the init process of the host to execute the restart process. After the kernel receives the restart instruction reboot, it starts to release the currently occupied resources and performs the restart operation.
[0100] The above technology has the following defects: the container cannot be restarted as a whole machine, the container cannot execute the recovery factory setting of the Android system, and cannot enter the fastboot mode for execution; during the whole machine restart process, the container is not executed correctly, but is killed as a service of the host environment; the container separate restart condition is not monitored and processed.
[0101] In order to solve the problems of container restart as a whole machine and correct resource release, the present disclosure provides a three-container starting management solution based on an Android system, please refer to FIG. 2, which is a schematic diagram of the starting management system structure of three containers according to an embodiment of the present disclosure. The figure shows the kernel space kernel and the user space Host Userspace, the kernel space includes the sys node and the dev node, the kernel space includes container one, container two and container three, container one, container two, container three and the host environment run a management process (also known as a starting management process).
[0102] In some embodiments, a dev node can be created in the kernel space, which is used to process restart type messages and container state messages. The host environment and the three containers are all mounted with the dev device. By reading and writing the dev / power_xxx file device node, communication is realized between the host and the containers and between the containers. The sys kernel file device node is created to save the type of container restart.
[0103] In some embodiments, a user space process can be started to start a management process responsible for read-write processing of the dev node, which is started in the host environment and the three container environments, and logical differentiation is made in the code for different containers, and the management process is used as a container restart / shutdown process in the host environment. When the container is issued to restart, the restart type is written to the dev device node and the sys device node. After the init exit process inside the container is executed, the container receives a signal of the init process exit, and according to the restart type stored in the sys, the init is re-executed or the entire container system is exited to release the container resources. The management process started in the host environment listens to the messages in the dev, and if the restart type is a whole machine restart, a restart message is written to the dev, and the start management process in the container receives the restart message and issues a restart attribute to execute the restart of the container system. When the container system exits, the state of the container exit is written to the dev node. The host environment start management process determines that the three containers are exited according to the dev state message, and issues a restart type to execute the restart of the Android system in the host environment. After the host environment start management process listens to the restart type message of the dev, a thread is started to monitor whether the restart is completed, and a timeout time is set. After the timeout, the whole machine is restarted according to the restart type.
[0104] In some embodiments, the containers can restart or shut down the whole machine, the host can manage the restart strategy of the three containers, and the host can monitor the restart state of the containers / whole machine.
[0105] Referring to FIG. 3, which is a container management flowchart in an Android operating system according to an embodiment of the present disclosure, the following steps are included: a process inside container one initiates a restart or shutdown process; and a restart or shutdown type is written to a file device node. If only container one is restarted or shut down, the container obtains the device node restart or shutdown type according to an init exit signal, and restarts the init process or container one exits. If the Android system needs to be restarted, the host management process processes the message, writes the device node to shut down container two and container three, receives the state message of the container one, container two and container three exit completion, and the host restarts or shuts down according to the restart type.
[0106] In some embodiments, based on the Android and container restart flow, a kernel device node and a user space process are created, the user space process reads and writes and listens to the messages of the kernel device node and processes them, to monitor the container restart flow and process the restart sequence and strategy of the three containers.
[0107] If the present solution is adopted, the container one can execute the Android factory reset in the following manner: the container one initiates a reboot request in the adb environment. The adbd process issues the sys.powerctrl=reboot,recovery instruction to the init process of the container one. After receiving the instruction, the init process of the container one starts to execute the reboot operation and writes the reboot,recovery information into the dev node. The startup management service in the host environment starts a timeout thread for monitoring. When the startup management service of the host reads the information of the dev node, it issues the reboot instruction to the container two and the container three. After receiving the exit signal of the init process, the container one, the container two and the container three judge according to the reboot type written previously and execute the exit operation respectively. In the exit process, each container writes its state information into the dev node. The power management service in the host environment monitors the exit state of the container. When it is judged that all the containers have exited, the timeout thread started previously is ended and the sys.powerctrl=reboot,recovery instruction is issued to the init process of the host. The init process of the host calls the syscall to the kernel to execute the reboot operation, thereby completing the whole system reboot process.
[0108] If the present solution is adopted, the container one can implement the timeout processing of the individual reboot in the following manner: the container one initiates a reboot request in the adb environment. Then, the adbd process issues the sys.powerctrl=reboot instruction to the init process of the container one. After receiving the instruction, the init process of the container one starts to execute the reboot operation and writes the reboot information into the dev node. In the host environment, the power management service triggers the read operation through the poll mechanism and reads the message of the reboot type. After that, it starts the timeout detection thread for the reboot of the container one to monitor the reboot progress of the container one. The init process of the container one then calls the syscall to the kernel to execute the reboot operation. In the reboot process, the system judges that the init process of the container one is not the No. 1 process, so it executes the exit operation of the init process of the container one. After receiving the exit signal of the init process, the container one judges according to the reboot type written previously. Since it is judged as reboot, the container one decides to restart the init process to complete the reboot process. When the container one is successfully rebooted, a flag bit is written into the dev node. The power management service in the host environment reads the information of the dev node and ends the timeout detection thread started previously after judging that the container one has been successfully rebooted.
[0109] If the present solution is adopted, the host machine can execute the whole machine shutdown in the following way: in the adb environment of the host machine, a reboot operation is initiated. Then, the adbd process writes the reboot,host information into the dev node. The startup management service running in container one, container two and container three issues the sys.powerctrl=reboot instruction to the init process of the respective container after listening to the message. The startup management service in the host environment starts a timeout thread for monitoring the progress of the whole reboot process. After receiving the exit signal of the respective init process, container one, container two and container three judge according to the reboot type set in advance and execute the exit operation accordingly. In the exit process, each container writes its state information into the dev node for the host environment to read and judge. After confirming that all containers have successfully exited by reading the dev node, the power management service of the host environment ends the previously started timeout thread and issues the sys.powerctrl=reboot,recovery instruction to the init process of the host machine. Finally, the init process of the host machine calls the syscall to the kernel to execute the reboot operation, thereby completing the whole system reboot process.
[0110] Referring to FIG. 4, FIG. 4 is a structural schematic diagram of a container management system provided by an embodiment of the present disclosure. The system can include:
[0111] The node establishing module 401 is configured to establish a kernel file node in the kernel space;
[0112] The process starting module 402 is configured to control the host machine and the plurality of containers to mount the kernel file node, and start a management process in the host machine and all the containers; wherein the management process is a process for reading and writing the kernel file node;
[0113] The container control module 403 is configured to, if a target container receives a control request, write the control type of the control request into the kernel file node, and control the target container to execute a container restart operation or a container exit operation corresponding to the control request; and
[0114] The whole machine control module 404 is configured to, if the management process of the host machine listens to the control type written in the kernel file node and the control type is a whole machine restart or a whole machine shutdown, set the control type of other containers in the kernel file node by using the management process of the host machine, so that the management process of the other containers executes an operation corresponding to the control request after listening to the control type written by the host machine; and is further configured to control the host machine to execute a whole machine restart operation or a whole machine shutdown operation.
[0115] In some embodiments, a kernel file node is established in the kernel space, and the host machine and the plurality of containers are controlled to mount the kernel file node, and a management process is started in the host machine and the containers, through which the host machine and the containers can communicate through the kernel file node. When the target container receives the control request, the target container writes the control type of the control request into the kernel file node, and performs corresponding operations according to the execution of the control request, such as container restart or exit. When the management process of the host machine listens to the control type written in the kernel file node, the management process of the host machine sets the control type of other containers in the kernel file node, so that the management process of other containers also performs corresponding operations after listening to the control type written by the host machine. After the plurality of containers perform the operations corresponding to the control request, the inconsistent state of the containers or the un-released resources can be avoided, so that the host machine can perform the whole machine restart operation or the whole machine shutdown operation, thereby realizing the whole machine restart or the whole machine shutdown based on the containers.
[0116] In some embodiments, the container management system further comprises:
[0117] a type writing module, configured to write the control type of the control request into a file system node; wherein the file system node is a node in the kernel space, and the host machine and all the containers mount the file system node;
[0118] Correspondingly, the process in which the container control module 403 controls the target container to perform the container restart operation or the container exit operation corresponding to the control request comprises: terminating all child processes by using the init process of the target container, and recycling the mounting resources of all the child processes; controlling the init process of the target container to exit; reading the control type from the file system node, and performing the container restart operation or the container exit operation corresponding to the control request according to the control type.
[0119] In some embodiments, the process in which the container control module 403 performs the container restart operation or the container exit operation corresponding to the control request according to the control type comprises: if the control type is container restart, restarting the init process of the target container; if the control type is container exit or whole machine shutdown, controlling the container process of the target container to exit; and if the control type is whole machine restart, restarting the init process of the target container after the host machine performs the whole machine restart operation.
[0120] In some embodiments, the container management system further comprises:
[0121] The judging module is configured to judge whether the container processes of all the containers have exited; if yes, the host computer is controlled to perform a whole-machine restart operation or a whole-machine shutdown operation.
[0122] In some embodiments, the process in which the judging module judges whether the container processes of all the containers have exited comprises: reading container state information in the kernel file node; wherein the container state information is used to store the state of each container; and judging whether the container processes of all the containers have exited according to the container state information.
[0123] In some embodiments, the container management system further comprises:
[0124] The timing module is configured to start a timer to record a waiting duration after the management process of the host computer sets the control type of other containers in the kernel file node.
[0125] Correspondingly, the whole-machine control module 404 is further configured to, after controlling the host computer to perform a whole-machine restart operation or a whole-machine shutdown operation, if there is a container process that has not exited and the waiting duration is greater than a threshold value, control the host computer to perform a whole-machine restart operation or a whole-machine shutdown operation.
[0126] In some embodiments, the whole-machine control module 404 is further configured to, if the management process of the host computer listens to the control type written in the kernel file node and the control type is container restart or container exit, start a monitoring thread and use the monitoring thread to judge whether the target container completes the container restart operation or the container exit operation within a preset time; and further configured to, if the target container does not complete the restart operation or the container exit operation within the first preset time, use the management process of the host computer to set the control type of all the containers in the kernel file node, so that the management processes of all the containers perform a container restart operation after listening to the control type written by the host computer; and further configured to control the host computer to perform a whole-machine restart operation.
[0127] Since the embodiments of the system part correspond to the embodiments of the method part, the embodiments of the system part are described in the description of the embodiments of the method part, which are not described here in detail.
[0128] The disclosure also provides a storage medium having a computer program stored thereon, which, when executed, can implement the container management method described in the disclosure. The storage medium can include a U disk, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk, and various media that can store program codes.
[0129] The present disclosure also provides an electronic device comprising a memory and a processor, wherein the memory stores a computer program, and the processor invokes the computer program in the memory to implement the container management method as described above. Of course, the electronic device can also comprise various network interfaces, power supplies, and other components.
[0130] The various embodiments described in the specification are presented in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same or similar parts between the various embodiments can be mutually referred to. For the system disclosed in the embodiments, since it corresponds to the method disclosed in the embodiments, the description is relatively simple, and the relevant parts can be referred to the method part. It should be noted that, for those of ordinary skill in the art, some improvements and modifications can be made to the present disclosure without departing from the principles of the present disclosure, and these improvements and modifications also fall within the protection scope of the present disclosure.
[0131] It should also be noted that, in the specification, the relationship terms such as first and second are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or sequence between the entities or operations. Moreover, the terms "include", "contain" or any other variants thereof are intended to cover non-exclusive inclusion, so that the process, method, article or device including a series of elements not only includes those elements, but also includes other elements not explicitly listed or inherent to such process, method, article or device. Without more limitations, the element defined by the statement "including a" does not exclude the presence of other identical elements in the process, method, article or device including the element.
Claims
1.A container management method, comprising: establishing a kernel file node in a kernel space; controlling a host and a plurality of containers to mount the kernel file node and start a management process in the host and all the containers; wherein the management process is a process for reading and writing the kernel file node; if a target container receives a control request, writing a control type of the control request into the kernel file node and controlling the target container to perform a container restart operation or a container exit operation corresponding to the control request; if the management process of the host listens to the control type written into the kernel file node and the control type is a whole machine restart or a whole machine shutdown, setting, by the management process of the host, control types of other containers in the kernel file node, so that the management processes of the other containers perform operations corresponding to the control request after listening to the control type written by the host; controlling the host to perform a whole machine restart operation or a whole machine shutdown operation. 2.The container management method of claim 1, wherein after the target container receives the control request, further comprising: writing the control type of the control request into a file system node; wherein the file system node is mounted by the host and all the containers in the kernel space; correspondingly, controlling the target container to perform the container restart operation or the container exit operation corresponding to the control request, comprising: terminating all child processes by an init process of the target container and recycling mounting resources of all the child processes; controlling the init process of the target container to exit; reading the control type from the file system node and performing the container restart operation or the container exit operation corresponding to the control request according to the control type. 3.The container management method of claim 2, wherein performing the container restart operation or the container exit operation corresponding to the control request according to the control type, comprising: if the control type is a container restart, restarting the init process of the target container; if the control type is a container exit or a whole machine shutdown, controlling container processes of the target container to exit; if the control type is a whole machine restart, restarting the init process of the target container after the host performs the whole machine restart operation. 4.The container management method of any one of claims 1 to 3, wherein before controlling the host to perform the whole machine restart operation or the whole machine shutdown operation, further comprising: judging whether container processes of all the containers have exited; if yes, entering the step of controlling the host to perform the whole machine restart operation or the whole machine shutdown operation. 5.The container management method of claim 4, wherein judging whether container processes of all the containers have exited, comprising: reading container state information in the kernel file node; wherein the container state information is used to store states of each of the containers; and judging whether container processes of all the containers have exited according to the container state information. 6.The container management method of claim 4 or 5, wherein after setting the control type of other containers in the kernel file node by the management process of the host computer, further comprising: starting a timer to record a waiting duration; correspondingly, after controlling the host computer to perform a whole machine restart operation or a whole machine shutdown operation, further comprising: if there is a container process that has not exited and the waiting duration is greater than a threshold, entering the step of controlling the host computer to perform a whole machine restart operation or a whole machine shutdown operation. 7.The container management method of any one of claims 1 to 6, further comprising: if the management process of the host computer listens to the control type written in the kernel file node being container restart or container exit, starting a monitoring thread, and using the monitoring thread to determine whether the target container completes the container restart operation or the container exit operation within a preset time; if the target container does not complete the restart operation or the container exit operation within the first preset time, setting the control type of all the containers in the kernel file node by the management process of the host computer, so that the management processes of all the containers perform a container restart operation after listening to the control type written by the host computer; controlling the host computer to perform a whole machine restart operation. 8.A container management system, comprising: a node establishing module configured to establish a kernel file node in a kernel space; a process starting module configured to control a host computer and a plurality of containers to mount the kernel file node, and start a management process in the host computer and all the containers; wherein the management process is a process for reading and writing the kernel file node; a container control module configured to, if a target container receives a control request, write a control type of the control request into the kernel file node, and control the target container to perform a container restart operation or a container exit operation corresponding to the control request; a whole machine control module configured to, if the management process of the host computer listens to the control type written in the kernel file node being a whole machine restart or a whole machine shutdown, set the control type of other containers in the kernel file node by the management process of the host computer, so that the management processes of the other containers perform an operation corresponding to the control request after listening to the control type written by the host computer; and further configured to control the host computer to perform a whole machine restart operation or a whole machine shutdown operation. 9.An electronic device comprising a memory and a processor, wherein the memory stores a computer program, and the processor invokes the computer program in the memory to implement the container management method of any one of claims 1 to 7. 10.A storage medium, wherein the storage medium stores computer executable instructions, and the computer executable instructions are loaded and executed by a processor to implement the container management method of any one of claims 1 to 7.
Citation Information
Patent Citations
Container-based mobile terminal shutdown method and device
CN105550024A
Control method and device of in-vehicle infotainment system
CN117539593A
Container management method and system, electronic equipment and storage medium
CN118605998A
Systems and methods for container management
US11113090B1