Catalog mounting method and computing device
By dynamically adding storage media to the container after it starts up, the problem of data loss caused by insufficient container storage space is solved, and dynamic expansion and persistent data storage are achieved.
Patent Information
- Application Number
- CN202211287023.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-10-20
- Publication Date
- 2025-12-16
- Estimated Expiration
- 2042-10-20
AI Technical Summary
In existing technologies, once the storage space of a container is exhausted, newly generated data within the container cannot be persistently stored, leading to data loss.
After the container starts, the host operating system directory is bound to the container directory by the mount command, and new storage media are dynamically added to the container to achieve dynamic expansion.
It enables dynamic addition of storage space during container operation, preventing data loss, and offers good compatibility, making it suitable for managing multiple containers.
Smart Images

Figure CN115686364B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of computer, and particularly relates to a directory mounting method and a computing device. BACKGROUND
[0002] A container is a virtualization technology in a computer operating system. The technology can enable a process to run in a relatively independent and isolated environment (including an independent file system, a namespace, a resource view, etc.), can simplify the deployment process of software, and can enhance the portability and security of the software.
[0003] At present, in order to realize the persistent storage of data in a container, one or more host directories can be mounted to one or more container directories through a -v parameter or a --mount parameter when the container is created and run through docker run. Then, the persistent storage of data in the container can be realized through the one or more container directories. However, as data is continuously written into the one or more container directories, the available storage space corresponding to the one or more container directories will gradually decrease. When the storage space corresponding to the one or more container directories is used up, newly generated data in the container cannot be persistently stored, and the newly generated data will be lost after the container is destroyed. SUMMARY
[0004] Embodiments of the present application disclose a directory mounting method and a computing device, which can add a new storage medium to a container dynamically after the container is started, realize dynamic expansion, and thus can solve the problem of data loss.
[0005] A first aspect discloses a directory mounting method, which can be applied to a computing device, a module (for example, a chip) in the computing device, a logic module or software capable of realizing all or part of the functions of the computing device. The computing device can include a host and a first container, and the first container runs on the host. The host includes a first storage medium. The directory mounting method is described below by taking application to the computing device as an example. The directory mounting method can include: mounting a first host directory to a first container directory before starting the first container; the first host directory is a directory on a host operating system of the computing device, and the first container directory is a directory of the first container; after starting the first container, mounting the first storage medium based on the first host directory, and storing first data to the first storage medium based on the first container directory; the first data includes data generated in a running process of the first container.
[0006] In the embodiments of the present application, the computing device can mount the first storage medium based on the mounting relationship established before the first container is started (i.e., the mounting relationship between the first host directory and the first container directory), and can store the first data into the first storage medium based on the first container directory after the first container is started (i.e., during the running of the first container). In this way, the new storage medium can be dynamically added to the first container after the first container is started, and the dynamic expansion is realized, so that the above-mentioned data loss problem can be solved. Moreover, when the storage medium currently used by the first container is damaged, the new storage medium can also be dynamically replaced for the first container through this way.
[0007] As a possible implementation, the mounting the first storage medium based on the first host directory includes: mounting a second host directory to a third host directory, the second host directory being a directory on a host operating system of the computing device, and the third host directory being a subdirectory of the first host directory, and the first storage medium being mounted to the second host directory; and the storing the first data into the first storage medium based on the first container directory includes: mounting a second container directory to a third container directory, the second container directory being a subdirectory of the first container directory corresponding to the third host directory, and the third container directory being a directory of the first container; and storing the first data into the first storage medium through the third container directory.
[0008] In the embodiments of the present application, the computing device can mount the second host directory to the third host directory, and can mount the second container directory to the third container directory. Since there is a corresponding mapping relationship between the second container directory and the third host directory, the second host directory can be mounted to the third container directory based on a mounting propagation mechanism. Correspondingly, the computing device can dynamically add the first storage medium for the first container, and can store the first data into the first storage medium through the third container directory. In addition, for different containers, the mounting propagation can be implemented through different subdirectories of the first host directory (such as the third host directory corresponding to the first container) and different subdirectories of the first container directory (such as the second container directory corresponding to the first container), so that the management of multiple different containers can be facilitated, and the influence on the directory mounting of other containers can be avoided.
[0009] As a possible implementation, the mounting the first storage medium based on the first host directory includes: mounting a second host directory to the first host directory, the second host directory being a directory on a host operating system of the computing device, and the first storage medium being mounted to the second host directory; and the storing the first data into the first storage medium based on the first container directory includes: mounting the first container directory to a third container directory, the third container directory being a directory of the first container; and storing the first data into the first storage medium through the third container directory.
[0010] In the embodiments of the present application, the computing device can also directly add the first storage medium to the first container through the first host directory and the first container directory.
[0011] As a possible implementation, the mounting the first storage medium based on the first host directory comprises mounting the first storage medium to the first host directory; and the storing the first data to the first storage medium based on the first container directory comprises storing the first data to the first storage medium through the first container directory.
[0012] In the embodiments of the present application, the computing device can also directly mount the first storage medium to the first host directory, so that the computing device can add the first storage medium to the first container without relying on other directories.
[0013] As a possible implementation, the mounting the first storage medium based on the first host directory comprises mounting the first storage medium to a third host directory, the third host directory being a subdirectory of the first host directory; and the storing the first data to the first storage medium based on the first container directory comprises storing the first data to the first storage medium through a second container directory, the second container directory being a subdirectory corresponding to the third host directory under the first container directory.
[0014] In the embodiments of the present application, the computing device can create a subdirectory (i.e. a third host directory) under the first host directory, and then mount the first storage medium to the third host directory. In this way, different containers on the computing device can be allocated different subdirectories under the first host directory, and then different storage media can be mounted to these subdirectories, so that the directories of different containers can be managed conveniently.
[0015] As a possible implementation, the method further comprises creating and running the first container through a docker run command; and the mounting the first host directory to the first container directory before starting the first container comprises mounting the first host directory to the first container directory through a mounting command before starting the first container.
[0016] As a possible implementation, the mounting the first host directory to the first container directory through the mounting command comprises executing a program file configured in an / etc / sysconfig / docker configuration file to mount the first host directory to the first container directory through the mounting command.
[0017] In the embodiments of the present application, the program file to be executed before starting the first container can be configured through editing a configuration file, and this method has strong realizability and good compatibility.
[0018] As a possible implementation, the mounting command is a mount --bind command.
[0019] In the embodiments of the present application, the mount --bind command can be used, which is a mounting binding command provided by the Linux operating system and has good compatibility.
[0020] As a possible implementation, the mounting the first host directory to the first container directory comprises: obtaining a root directory of the first container; and mounting the first host directory to the first container directory based on the root directory of the first container.
[0021] In the embodiments of the present application, the root directory of the first container can be obtained before the first container is started, and the mounting binding relationship between the first host directory and the first container directory can be established based on the root directory of the first container.
[0022] As a possible implementation, the host further comprises a second storage medium, and the method further comprises: mounting the first host directory to a fourth container directory before a second container is started; the second container is a container created on the computing device, and the fourth container directory is a directory in the second container; after the second container is started, mounting the second storage medium based on the first host directory, and storing second data into the second storage medium based on the fourth container directory; the second data comprises data generated in the running process of the second container.
[0023] In the embodiments of the present application, based on the mounting relationship (i.e., the mounting relationship between the first host directory and the fourth container directory) established before the second container is started, the computing device can mount the second storage medium based on the first host directory and store the second data into the second storage medium based on the fourth container directory after the second container is started. In this way, the second container can be dynamically added with a new storage medium after the second container is started, and dynamic expansion is realized.
[0024] As a possible implementation, the mounting the second storage medium based on the first host directory comprises: mounting a fourth host directory to a fifth host directory, the fourth host directory is a directory on a host operating system of the computing device, the fifth host directory is a subdirectory of the first host directory, and the second storage medium is mounted to the fourth host directory; the storing the second data into the second storage medium based on the fourth container directory comprises: mounting a fifth container directory to a sixth container directory, the fifth container directory is a subdirectory corresponding to the fifth host directory under the fourth container directory, the sixth container directory is a directory in the second container; and the second data is stored into the second storage medium through the sixth container directory.
[0025] In the embodiments of the present application, for different containers, the mounting propagation can be implemented through different subdirectories of the first host directory (for example, the third host directory corresponding to the first container and the fifth host directory corresponding to the second container) and different subdirectories of the first container directory (for example, the second container directory corresponding to the first container and the fifth container directory corresponding to the second container), so that the plurality of different containers can be conveniently managed, and the mounting of the directories of other containers can be avoided from being affected.
[0026] As a possible implementation, the method further comprises: unmounting the mounting path between the second host directory and the third host directory, and unmounting the mounting path between the second container directory and the third container directory.
[0027] In the embodiments of the present application, after the first storage medium is dynamically added for the first container, the mounting path between the second host directory and the third host directory can be unmounted, and the mounting path between the second container directory and the third container directory can be unmounted, so that the isolation effect can be restored, and the third host directory and the second container directory can be prevented from affecting the second host directory and the third container directory.
[0028] As a possible implementation, the first storage medium is a disk, a partition of the disk, or a volume of the disk of the computing device.
[0029] The second aspect discloses a computing device, which comprises a processor, a memory, and a communication interface, the communication interface is used to receive information from other electronic devices outside the computing device and output information to other electronic devices outside the computing device, the processor invokes a computer program stored in the memory to implement the command mounting method provided in the first aspect and any possible implementation of the first aspect.
[0030] The third aspect discloses a computer readable storage medium, the computer readable storage medium stores a computer program or computer instructions, when the computer program or computer instructions are run, the directory mounting method disclosed in the above aspects is implemented.
[0031] The fourth aspect discloses a chip, which comprises a processor, and is used to execute a program stored in a memory, when the program is executed, the chip executes the directory mounting method disclosed in the above aspects.
[0032] As a possible implementation, the memory is located outside the chip.
[0033] The fifth aspect discloses a computer program product, which comprises computer program code, when the computer program code is run, the directory mounting method disclosed in the above aspects is executed.
[0034] It can be understood that the computing device provided by the second aspect, the computer readable storage medium provided by the third aspect, the chip provided by the fourth aspect and the computer program product provided by the fifth aspect are all used to execute the directory mounting method provided by the first aspect and any possible implementation manner in the first aspect. Therefore, the beneficial effects achieved thereby can refer to the beneficial effects in the corresponding method, which will not be described here again. BRIEF DESCRIPTION OF DRAWINGS
[0035] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the drawings needed in the embodiment description will be briefly introduced. Obviously, the drawings in the following description are only some embodiments of the present application, and for those skilled in the art, other drawings can also be obtained without creative labor on the basis of these drawings.
[0036] Figure 1 is a system architecture diagram disclosed by an embodiment of the present application;
[0037] Figure 2 is a flow diagram of a directory mounting method disclosed by an embodiment of the present application;
[0038] Figures 3-6 is a scene diagram of a group of directory mounting disclosed by an embodiment of the present application;
[0039] Figure 7 is a scene diagram of a directory mounting disclosed by an embodiment of the present application;
[0040] Figure 8 is a structure diagram of a computing device disclosed by an embodiment of the present application. DETAILED DESCRIPTION
[0041] The embodiments of the present application disclose a directory mounting method and a computing device, which can dynamically add new storage media to the container after the container is started, realize dynamic expansion, and solve the problem of data loss. The technical solutions in the embodiments of the present application will be described clearly and completely in combination with the drawings in the embodiments of the present application.
[0042] In order to better understand the embodiments of the present application, the related technologies of the embodiments of the present application will be described first.
[0043] Container is a virtualization technology in computer operating system. This technology can make processes run in a relatively independent and isolated environment (including independent file system, namespace, resource view, etc.), which can simplify the deployment process of software and enhance the portability and security of software. Therefore, container technology is widely used in service scenarios of cloud computing field, and container development has gradually become a mainstream application development technology. Among them, Docker is a representative of container technology, which provides a solution for fast and automated deployment of application programs. Using Docker can quickly create and start containers for images where application programs are located. After the container is created and run, the application program is deployed. A physical machine can deploy multiple Docker containers, and each Docker container is isolated from each other. The physical machine can be generally referred to as a host machine.
[0044] The implementation of container mainly depends on two mechanisms of Namespace and Cgroup (control groups), Namespace is responsible for "isolation", and Cgroup is responsible for "limitation".
[0045] Namespace represents the visible range of an identifier. Among them, an identifier can be defined in multiple namespaces, and its meaning in different namespaces is irrelevant. In this way, any identifier can be defined in a new namespace, and they will not conflict with any existing identifier, because the existing definition is in other namespaces. Namespace provides isolation of system resources such as UTS, IPC, Mount, PID, Network, User, etc. Through the isolation ability of Namespace, different containers are not visible to each other, and the application in the container can only access the file system in the container, but different containers can share related resources in the host operating system kernel. At the same time, after the container is started, it generally cannot operate and manage the host operating system. Namespace can also be referred to as name space, namespace, etc.
[0046] Cgroup is a function of Linux kernel, which can be used to limit, control and separate the resources (such as CPU, memory, disk input and output, etc.) of a process group. Through Cgroup, the resources available to the container process can be limited.
[0047] Chroot (change root) is an operation originating from Unix systems, which can change the apparent root directory of a running process and its descendants, for the duration of their execution. A program running in this environment will have its file system root changed to the specified directory, and will be unable to access files outside it.
[0048] It should be noted that, since the container has an independent file system, and the read and write of the data of the container are performed in the storage layer of the container by default, generally, when the container is deleted, the data stored in the container will also be lost, and the data cannot be stored persistently. If it is necessary to realize the persistent storage of the data in the container, a directory on the host operating system can be mounted to a directory in the container when the container is started, so that the data stored in the container directory thereafter can be automatically synchronized to the corresponding host directory, and even if the container is deleted thereafter, the corresponding data can be found in the corresponding host directory.
[0049] In the above method, the mounting of the directory on the host operating system to the directory in the container can be realized by the -v command or the --mount command.
[0050] The format of the -v command can be: docker run -v host directory:container directory.
[0051] In the above command, docker run indicates the creation and running of the container, -v host directory:container directory indicates the mounting of the host directory to the container directory. The host directory can be an absolute path, and if the host directory or the container directory does not exist, it will be automatically created. In the above command, other parameters can also be included, such as the naming of the container through --name, the running of the container in the interactive mode and the allocation of a pseudo input terminal to the container through -it, and the limitation of the read and write permissions through container directory:ro / rw, where ro (readonly) indicates read-only, and rw (read write) indicates read and write.
[0052] The format of the --mount command can be: docker run --mount type=bind,source=host directory,target=container directory. In the above command, --mount type=bind,source=host directory,target=container directory indicates the mounting of the host directory to the container directory, and the mounting type is the bind type.
[0053] It should be noted that when mounting the directory through the docker run command, the container will complete the mounting of the host directory after starting, and will switch to the corresponding namespace to complete the isolation action. Subsequently, due to the isolation capability of the namespace, a new storage directory cannot be dynamically added to the container during the running of the container. If a new storage directory needs to be added, the currently running container can be first destroyed, and then multiple directories can be mounted (such as through multiple -v parameters) when the container is rebuilt.
[0054] It can be seen that in order to mount the host directory to the container, one or more host directories can be mounted to one or more container directories through the -v parameter or the --mount parameter when the container is created and run through the docker run. During the running of the container, the host directory cannot be dynamically mounted to the container. However, in general, when the container is created and run through the docker run, it can not be determined whether the host directory needs to be mounted to the container. Moreover, even if it can be determined that the host directory needs to be mounted to the container, it cannot be determined that the storage capacity of the storage medium that can store data required by the container, and therefore, the storage space allocated to the container can be too large (that is, the storage space corresponding to the host directory mounted to the container is too large), which can cause waste of storage space, or the storage space allocated to the container can be insufficient, which can cause data loss.
[0055] In order to solve the above problems, in the embodiments of the present application, a directory on the host operating system and a directory in the container can be mounted and bound through a mounting command (such as a mount–bind command) before the container is started. Subsequently, if a new storage directory needs to be added to the container during the running of the container, the new storage directory can be dynamically added to the container through the mounting and binding relationship created in advance and the mounting propagation mechanism. In this way, new storage media can be dynamically added to the application in the container according to the needs of the application, and dynamic expansion can be realized.
[0056] In order to better understand the embodiments of the present application, the system architecture used in the embodiments of the present application will be described first.
[0057] Please refer to Figure 1 , Figure 1 is a system architecture diagram disclosed by the embodiments of the present application. As shown in Figure 1 , the system architecture can be the system architecture of the computing device 100, and the computing device 100 can include an operating system 101, a container A 102, a storage medium 1 103, and a storage medium 2 104. The container A 102 can be a process running on the operating system 101.
[0058] The operating system 101 can include a kernel 1011, a host directory 1 1012, and a host directory 2 1013. The host directory 1 1012 can be a mounting point of the storage medium 1 103, and the host directory 2 1013 can be a mounting point of the storage medium 2 104. It should be understood that the kernel is the core of an operating system, and can provide the most basic functions of the operating system. The kernel of the operating system is responsible for managing the processes, memory, device drivers, file systems, network systems, and the like of the system, and is a bridge connecting the application program and the hardware, and determines the performance and stability of the system.
[0059] The storage medium 1 103 and the storage medium 2 104 can be a disk, a partition of the disk, or a volume of the disk. In the embodiment of the present application, the storage space of the storage medium 1 103 can be accessed through the host directory 1 1012, that is, the data stored in the storage medium 1 103 can be accessed, and the storage space of the storage medium 2 104 can be accessed through the host directory 2 1013, that is, the data stored in the storage medium 2 104 can be accessed.
[0060] Specifically, all hardware devices (such as disks / partitions / volumes) in the Linux system can be generally used after being mounted. The mounting here refers to mounting the hardware device to a directory of the host file system, and accordingly, the directory is also called the mounting point corresponding to the hardware device. The mounting can associate a hardware device with a directory in the host file system, and then the corresponding hardware device can be accessed by accessing the directory. For example, after mounting a partition of a disk to a directory of the host file system, the storage space of the corresponding partition (that is, the files on the corresponding partition) can be accessed by accessing the directory (that is, the mounting point).
[0061] The command for mounting the hardware device to a directory of the host file system can be mount device file name mounting point. Since the mounting operation can cause the files in the original directory (that is, the mounting point) to be hidden, in order to avoid causing system abnormalities or even crashes, an empty directory can be generally created as the mounting point.
[0062] When the container A 102 is created and run (e.g., by docker run), the host directory 1 1012 can be mounted to the container directory 1 1022. In this way, the data generated by the application A 1021 in the container A 102 can be saved under the container directory 1 1022, and accordingly, can be synchronized to the host directory 1 1012, so that the persistent storage can be achieved. However, since the storage capacity corresponding to the host directory 1 1012 is limited, the storage capacity of the container directory 1 1022 is also limited. For example, the storage capacity of the storage medium 1 103 is limited, such as 100 GB (gigabyte), and accordingly, the storage capacity corresponding to the host directory 1 1012 and the container directory 1 1022 is also 100 GB.
[0063] During the running of the container A 102, as data is continuously written to the container directory 1 1022, the available storage capacity (i.e., the free storage capacity) of the storage medium 1 103 corresponding to the container directory 1 1022 will gradually decrease. At this time, if new data is generated in the container A 102, the data can not be persistently stored. In the embodiments of the present application, when the available storage capacity of the storage medium 1 103 is insufficient during the running of the container A 102, the host directory 2 1013 can be mounted to the container directory 2 1023 (i.e., a new storage directory is dynamically added), so that the storage medium 2 104 can be dynamically added to the container A 102, and accordingly, the available storage capacity of the container A 102 can be increased.
[0064] In some embodiments, when the container A 102 is created and run, the host directory 1 1012 can not be mounted to the container directory 1 1022. When it is necessary to persistently save the data generated by the application A 1021, the host directory 1 1012 can be dynamically mounted to the container directory 1 1022.
[0065] It should be understood that the directory mounting method provided by the embodiments of the present application can also dynamically mount multiple storage media to one container. For example, Figure 1 As described above, the computing device 100 can also include a storage medium 3 105, which can be mounted to a host directory 3 1014. After the computing device 100 dynamically adds the host directory 2 1013 to the container A 102 (i.e., dynamically adds the storage medium 2 104), the computing device 100 can also mount the host directory 3 1014 to the container directory 3 1024 by using the directory mounting method provided by the embodiments of the present application, so that the storage medium 3 105 can be dynamically added to the container A 102, and accordingly, the available storage capacity of the container A 102 can be increased.
[0066] It should be noted that the host directory 1 1012 and the host directory 2 1013 are directories on the host operating system, i.e., directories in the host file system. The container A 102 can include an independent file system, and the container directory 1 1022 and the container directory 2 1023 are directories in the file system of the container A 102. Therefore, the host directory and the container directory can belong to different file systems.
[0067] It should be understood that the computing device 100 can include a plurality of containers (only one of which is shown in the figure), which can share the kernel of the host operating system. It should also be understood that one host directory can be mounted to the directories of multiple containers, and therefore, mounting the host directory to the container directory can not only achieve persistent storage of data, but also achieve data exchange between different containers, and can achieve data exchange between external processes and containers. Figure 1
[0068] In addition, it should also be understood that the above Figure 1 It should be noted that the computing device 100 can include a host, which is the main body part of the computing device 100, and can include hardware devices such as a central processing unit (CPU) and a memory, and can also include software such as an operating system (OS). The operating system can include one or more of a Linux operating system, a Windows operating system, and a Unix operating system.
[0069] For example, the computing device 100 can also include a container B, a container C, and the like, and can include a storage medium 4, a storage medium 5, and the like. Through the directory mounting method provided by the embodiments of the present application, the computing device 100 can dynamically add a new storage medium (such as the storage medium 4) for the container B during the running of the container B. And the computing device 100 can also dynamically add a new storage medium (such as the storage medium 5) for the container C during the running of the container C.
[0070] It should be noted that the computing device 100 can include a host, which is the main body part of the computing device 100, and can include hardware devices such as a central processing unit (CPU) and a memory, and can also include software such as an operating system (OS). The operating system can include one or more of a Linux operating system, a Windows operating system, and a Unix operating system.
[0071] In some embodiments, the computing device 100 can be a server. The server can be a file server, a domain server, a database server, a mail server, a web server, a multimedia server, a communication server, a terminal server, an infrastructure server, a virtualization server, etc. The server can be tower, rack, blade, cabinet, etc.
[0072] It should be noted that, Figure 1 The system architecture shown is only illustrative and does not constitute a limitation. In other embodiments of the present application, Figure 1 The system architecture shown can include more or fewer components or functional modules than illustrated.
[0073] In the above system architecture, the embodiments of the present application solve the technical problems of data loss and the like by dynamically mounting the host directory during the running of the container. How to specifically implement "dynamically mounting the host directory" in the embodiments of the present application is described in detail below. Please refer to Figure 2 , Figure 2 is a flowchart of a directory mounting method disclosed by the embodiments of the present application. As Figure 2 shown, the directory mounting method can include but is not limited to the following steps:
[0074] 201. Before starting the first container, mount the first host directory to the first container directory.
[0075] In order to dynamically add a new storage directory (i.e. mount a new host directory) for the first container during the running of the first container, the computing device can mount the first host directory to the first container directory before starting the first container.
[0076] Specifically, in one possible implementation, when a container is created and run by a docker run (or when a container is created and run by a configuration file), a reserved processing mechanism is reserved before the container is started, and some customized work can be performed through the reserved processing mechanism. In the reserved processing mechanism before the first container is started, a first host directory (such as / host_bind) on a host operating system and a first container directory (such as / container_bind) in the container can be mounted and bound through a mounting command (such as a mount --bind command). The first host directory can be an empty directory on the host operating system, and can not be a mounting point of any storage medium of the computing device. The first container directory can be an empty directory in the first container. In this way, when the first host directory is mounted to the first container directory, the original files in the first container directory can be prevented from being hidden.
[0077] The mount --bind is a mechanism for implementing mounting binding of the linux operating system, and can bind a new mounting point for a mounting point. After being bound through the mount --bind, the two directories are similar to a hard link, and no matter which directory is read or written will be reflected on the other side. The kernel of the operating system operates the same physical location at the bottom of the system. It should be understood that the mount --bind command is only an example of a mounting command, and does not constitute a limitation.
[0078] In some embodiments, a code file (that is, a program file) that needs to be executed in the reserved processing mechanism before the container is started can be specified by editing an / etc / sysconfig / docker configuration file. Specifically, a parameter --hook-spec="a json file that needs to be executed (that is, a hook configuration file)" can be appended after an OPTIONS variable in the / etc / sysconfig / docker file. For example, --hook-spec= / var / lib / docker / hooks / hook1.json.
[0079] The content of the / var / lib / docker / hooks / hook1.json file can be as follows:
[0080]
[0081] wherein "prestart" means to be executed before the container is started. "path" is used to indicate the program (i.e. code file) to be executed before the container is started, and the path of the above-mentioned program to be executed is " / var / lib / docker / hooks / mount-hook". "args" is used to indicate the program parameters, and the first parameter is generally the program name (i.e. "mount-hook"), and the following parameters are generally the parameters accepted by the program. "env" is used to indicate the environment variable of the container. "timeout" is used to indicate the timeout time in seconds, and the above-mentioned is 120 seconds. It should be understood that after the above-mentioned configuration, the mount-hook program can be executed before each container is started.
[0082] It should be noted that the "poststart" and "poststop" corresponding contents can also be configured in the above-mentioned json file. "poststart" and "poststop" are respectively used after the container is started and after the container is stopped.
[0083] In the embodiment of the application, after the corresponding configuration file is edited, the first host directory on the host operating system can be mounted into the first container directory in the first container by executing the pre-configured program (such as mount-hook) before the first container is started. The first container can be any container created and run on the host operating system.
[0084] Specifically, before the first container is started, the pre-configured program (such as mount-hook) can obtain the root directory and other information of the first container, and at this time, the first container has not switched the storage permission (i.e. has not switched to the corresponding namespace), therefore, the pre-configured program can access the directory (i.e. path) in the first container, at this time, the first host directory on the host operating system can be mounted into the first container directory. For example, at this time, the first host directory can be created on the host operating system, the first container directory can be created in the first container, and then the first host directory can be mounted into the first container directory through the mounting command, and the command can be: mount--bind the root directory of the first container / the first container directory / the first host directory. It should be understood that in some embodiments, the first host directory can also be a directory originally existing on the host operating system, and the first container directory can also be a directory originally existing in the first container.
[0085] 202. After the first container is started, the second host directory is mounted into the third host directory.
[0086] After the first container is started (i.e., during the running of the first container), if it is needed to mount the second host directory on the host operating system to a third container directory within the first container, the second host directory can be mounted to a third host directory first.
[0087] Specifically, the computing device can mount the second host directory to the third host directory through a mount command, which can be mount --bind second host directory third host directory.
[0088] The third host directory can be a subdirectory of the first host directory. For example, the third host directory can be a newly created directory (e.g., a newly created empty directory) under the first host directory, or can be an originally existing directory under the first host directory. The second host directory can be a mount point of the first storage medium. The first storage medium can be any storage medium in the computing device, and can be a disk, a partition of a disk, or a volume of a disk of the computing device. It should be understood that in some embodiments, the third host directory can also be the first host directory.
[0089] It should be understood that since multiple containers can be running on the computing device, and the directory mounting method provided by the embodiments of the present application can dynamically mount host directories for the multiple containers, in order to avoid the directory mounting of the multiple containers affecting each other (i.e., affecting each other when dynamically mounting host directories), for each container, one or more independent subdirectories can be established under the first host directory (e.g., the third host directory under the first host directory corresponding to the first container). In this way, when dynamically mounting host directories for different containers, the host directory specific to the container (i.e., one or more independent subdirectories established under the first host directory for the container) can be used, so that the directory mounting of other containers can be avoided. Moreover, the above-mentioned method facilitates the management of multiple different containers, including the management of the directory mounting of multiple different containers.
[0090] In some embodiments, when one or more independent subdirectories are established under the first host directory for a container, the directory name of the subdirectory can include the identifier of the container. In one possible implementation, the identifier of the container can be a hash value generated by a hash function.
[0091] 203. After the first container is started, mount the second container directory to the third container directory.
[0092] In order to mount the second host directory on the host operating system to the third container directory within the first container, the computing device also needs to mount the second container directory to the third container directory.
[0093] Specifically, the computing device can mount the second container directory to the third container directory by a mount command, which can be specifically: mount --bind second container directory third container directory.
[0094] The second container directory is a subdirectory of the first container directory corresponding to the third host directory. It should be understood that when the third host directory is created under the first host directory, the second container directory corresponding to the third host directory can exist under the first container directory accordingly. Alternatively, when the second container directory is created under the first container directory, the third host directory corresponding to the second container directory can exist under the first host directory accordingly. It should be understood that in some embodiments, the second container directory can also be the first container directory.
[0095] The third container directory can be any directory in the first container. For example, the third container directory can be a newly created empty directory, or can be a directory that originally exists in the first container.
[0096] Through the above processing, the second host directory can be mounted to the third host directory, the second container directory can be mounted to the third container directory, and the second container directory and the third host directory have a corresponding mapping relationship. Therefore, based on the mount propagation mechanism, the second host directory can be mounted to the third container directory, thereby realizing mounting a directory (i.e., the second host directory) on the host operating system to a directory (i.e., the third container directory) in the first container during the running process of the first container, and further realizing expanding the storage capacity of the first container and being able to persistently store more data generated during the running process of the first container. For example, the first data can be stored into the first storage medium through the third container directory. The first data can include data generated during the running process of the first container.
[0097] The mount propagation mechanism can be understood as follows: if a mount point is mounted to a directory, the source directory (device) corresponding to the mount point is also mounted to the directory. For example, / host_bind / new1 is mounted to / host_bind / new2, and / host_bind / new2 can be the mount point corresponding to / host_bind / new1. If / host_bind / new2 is mounted to / host_bind / new3, the source directory / host_bind / new1 corresponding to / host_bind / new2 is also mounted to / host_bind / new3. Moreover, if the mount path of / host_bind / new2 to / host_bind / new3 is cancelled later, / host_bind / new1 is still mounted to / host_bind / new3.
[0098] It can be understood that in some embodiments, the computing device can perform step 203 first and then perform step 202, that is, the computing device can first mount the second container directory to the third container directory, and then mount the second host directory to the third host directory, so that the second host directory can also be mounted to the third container directory based on the mounting propagation mechanism.
[0099] 204. Unmount the mounting paths established in steps 202 and 203.
[0100] Through the above process, the second host directory can be mounted to the third container directory. At this time, the second host directory is also mounted to the third host directory and the second container directory, so in order to avoid the influence of the third host directory and the second container directory on the second host directory and the third container directory, the two mounting paths established in steps 202 and 203 can be cancelled (that is, the mounting path between the second host directory and the third host directory is unmounted, and the mounting path between the second container directory and the third container directory is unmounted), and the isolation effect is restored. Among them, the mounting path can be cancelled (that is, unmounted) by the umount command.
[0101] It should be understood that step 204 is optional, and in some embodiments, the computing device can not perform step 204.
[0102] It can be understood that in some embodiments, the computing device can also directly mount the first storage medium to the first host directory or a subdirectory of the first host directory (such as the third host directory), so that since the first host directory is mounted to the first container directory, the first data can be stored to the first storage medium through the first container directory or a subdirectory of the first container directory (such as the second container directory), thereby realizing adding a new storage medium for the first container and realizing dynamic expansion.
[0103] It can be seen that after the above establishes the binding mounting relationship between the first host directory and the first container directory, based on the binding mounting relationship between the first host directory and the first container directory, a new storage medium can be added for the first container in a variety of different ways, which is not limited by the embodiments of the present application.
[0104] It should be understood that in addition to dynamically adding storage media for the first container, the computing device can also dynamically add storage media for other containers running on the computing device. The following takes the second container on the computing device as an example for description:
[0105] To dynamically add new storage media for the second container during the running of the second container, the computing device can mount the first host directory to a fourth container directory before the second container is started. Then, during the running of the second container, the computing device can mount the second storage media based on the first host directory, and can store the second data to the second storage media based on the fourth container directory.
[0106] The second storage media can be any storage media in the computing device, and specifically can be a disk, a partition of the disk, or a volume of the disk of the computing device. The second container can be a container created on the computing device, and the fourth container directory can be a directory in the second container. The second data can include data generated by the second container during the running of the second container.
[0107] Specifically, the computing device can mount a fourth host directory to a fifth host directory, and can mount a fifth container directory to a sixth container directory, to add the second storage media for the second container. The fourth host directory can be a directory on a host operating system of the computing device, the fifth host directory can be a subdirectory of the first host directory, and the second storage media can be mounted to the fourth host directory. The fifth container directory can be a subdirectory of the fourth container directory corresponding to the fifth host directory, and the sixth container directory can be a directory in the second container.
[0108] Because the fifth container directory and the fifth host directory have a corresponding mapping relationship, based on the mounting propagation mechanism, the fourth host directory can be mounted to the sixth container directory, so that a directory (i.e., the fourth host directory) on the host operating system is mounted to a directory (i.e., the sixth container directory) in the second container during the running of the second container, and the storage capacity of the second container is expanded. Accordingly, the computing device can store the second data to the second storage media through the sixth container directory.
[0109] It should be noted that the first container and the second container are both mounted to the storage media based on the first host directory (e.g., the first container is mounted to the first storage media based on the first host directory, and the second container is mounted to the second storage media based on the first host directory), but in some other embodiments of the present disclosure, the first container and the second container can also be mounted to the storage media based on different host directories. Accordingly, before the first container and the second container are started, the computing device can mount different host directories to a directory in the first container and a directory in the second container, respectively. For example, the computing device can mount a host directory a to a container directory a, and can mount a host directory b to a container directory b. The host directory a and the host directory b are directories on the host operating system, the container directory a is a directory in the first container, and the container directory b is a directory in the second container.
[0110] In the above process, the binding mounting relationship between the first host directory and the first container directory can be created before the container is started through a configuration file. Then, if a new storage directory needs to be added to the first container (i.e., a new host directory is mounted to a directory in the first container) during the running of the container, the binding mounting relationship between the first host directory and the first container directory can be used to achieve this. In this way, the new storage medium can be added to the first container without rebuilding the first container. Moreover, when the storage medium currently used by the first container is damaged, the new storage medium can be dynamically replaced. In addition, the binding mounting of the first host directory and the first container directory can be achieved by configuring a code file, which does not introduce intrusive modifications to the kernel and has good compatibility.
[0111] As follows Figures 3-6 , the dynamic expansion of container A is taken as an example to introduce the application of the method shown in Figure 2 .
[0112] In the reservation processing mechanism before the start of container A, the / host_bind directory on the host operating system and the / container_bind directory in the container can be mounted and bound through a mounting command. Correspondingly, the subdirectory / host_bind / temp of / host_bind and the subdirectory / container_bind / temp of / container_bind also have a corresponding mapping relationship, which can be seen from Figure 3 . Moreover, the / host_temp directory can be created on the host operating system, and then the hard disk A of the computing device can be mounted to the / host_temp directory on the host operating system.
[0113] Then, if a storage medium needs to be added to container A (e.g., / host_temp is mounted to / container_temp) during the running of container A, / host_temp can be mounted to / host_bind / temp through a mounting command first, as shown in Figure 4 . Then, the / container_temp directory can be created in container A, and / container_bind / temp can be mounted to / container_temp through a mounting command, as shown in Figure 5As shown. At this time, since / host_temp is mounted to / host_bind / temp and / container_bind / temp is mounted to / container_temp, / host_temp can be mounted to / container_temp based on the mounting propagation mechanism. Accordingly, the content under / container_temp can be stored into the corresponding hard disk A, thereby realizing dynamic addition of storage media for container A. Accordingly, the application program A in the container can realize persistent storage of data through the / container_temp command. After that, the two mounting paths established before can be cancelled (i.e. the mounting path between / host_temp and / host_bind / temp is unmounted, and the mounting path between / container_bind / temp and / container_temp is unmounted), and the isolation effect is restored, as shown. Figure 6
[0114] Please refer to Figure 7 , and Figures 3-6 different from the above, during the running of container A, if it is needed to add storage media for container A, hard disk A can be directly mounted to the / host_bind / temp directory, so that dynamic addition of storage media for container A can also be realized through the / container_bind / temp directory in the container.
[0115] It should be noted that the related information (i.e. the same information or similar information) and related descriptions in the above different embodiments can be mutually referred.
[0116] It should be understood that the above Figure 2 takes the computing device as an example of the interactive execution subject to illustrate the above processing flow, but the application does not limit the interactive execution subject. For example, Figure 2 the computing device in the above can also be a chip, a chip system or a processor supporting the implementation of the method by the computing device, and can also be a logic module or software capable of realizing all or part of the functions of the computing device.
[0117] Based on the above system architecture, please refer to Figure 8 , Figure 8 is a structural diagram of a computing device disclosed by the embodiments of the application. Wherein, the computing device 800 can include a processor 801, a communication interface 802 and a memory 803. The processor 801, the communication interface 802 and the memory 803 can be connected with each other or connected with each other through a bus 804.
[0118] The memory 803 is configured to store computer programs and data of the computing device 800. The memory 803 can include, but is not limited to, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM), a compact disc read-only memory (CD-ROM), and the like. The communication interface 802 is configured to support the communication of the computing device 800, for example, receiving or sending data.
[0119] The processor 801 can be a central processing unit (CPU), a complex programmable logic device, a general processor, a digital signal processor, an application specific integrated circuit, a field programmable gate array, or other programmable logic device, a transistor logic device, a hardware component, or any combination thereof. The processor can also be a combination of computing functions, such as one or more microprocessor combinations, a combination of a digital signal processor and a microprocessor, and the like.
[0120] In one embodiment, the computing device 800 can be the computing device described above, and the processor 801 can be configured to read the program stored in the memory 803 described above, execute the program described above Figure 2 The operations performed by the computing device or the components in the computing device in the method embodiments shown can be referred to the related description above, and will not be described in detail here.
[0121] It should be noted that, Figure 8 The computing device 800 shown is only one implementation of the embodiments of the present application. In actual applications, the computing device 800 can also include more or fewer components, which are not limited here.
[0122] The embodiments of the present application also disclose a computer readable storage medium, which stores instructions. The instructions are executed to perform the method in the method embodiments described above.
[0123] The embodiments of the present application also disclose a computer program product including instructions. The instructions are executed to perform the method in the method embodiments described above.
[0124] It is apparent that the described embodiments are only some, but not all, of the embodiments of the present application. In this document, the term "embodiment" refers to a specific feature, structure, or characteristic of at least one embodiment of the application. The phraseology "in at least one embodiment" is not used to refer to all embodiments or all alternatives of the application. The phraseology "in at least one embodiment" is used exclusively to refer to at least one embodiment of the application, which is not necessarily the same as any other embodiment or any other alternative. It is clear from the description that embodiments described herein can be combined with other embodiments in a manner known to those skilled in the art. All other embodiments obtained by those skilled in the art based on the embodiments described herein without creative work are within the scope of the present application. In the specification and claims of the present application and the drawings, the terms "first", "second", "third", etc. are used to distinguish different objects, not to describe a particular order. In addition, the terms "comprise", "have" and any variations thereof are intended to cover non-exclusive inclusion. For example, a series of steps or units, or optionally, also includes steps or units not listed, or optionally, also includes other steps or units inherent to the process, method, product or equipment.
[0125] It is understood that only some, but not all, of the relevant parts are shown in the drawings. It should be understood that some exemplary embodiments are described as processes or methods depicted as flowcharts. Although the flowcharts describe the operations (or steps) as sequential processes, many of the operations can be implemented in parallel, concurrently or simultaneously. In addition, the order of the operations can be rearranged. The process can be terminated when its operations are completed, but can also have additional steps not included in the drawings. The process can correspond to a method, function, procedure, subroutine, etc.
[0126] The terms "component", "module", "system", "unit", etc. used in the specification are used to represent computer-related entities, hardware, firmware, combinations of hardware and software, software, or software in execution. For example, a unit can be, but is not limited to, a process running on a processor, a processor, an object, an executable, an execution thread, a program, and / or distributed between two or more computers. In addition, these units can be executed from various computer-readable media having various data structures stored thereon. The units can communicate, for example, according to signals having one or more data packets (e.g., from a second unit interacting with a local system, a distributed system, and / or a network of another unit. For example, the Internet interacts with other systems through signals with other systems.
[0127] The above detailed description of the specific implementation is further detailed for the purpose of the application, technical solutions and beneficial effects, and it should be understood that the above description is only for the specific implementation of the application and is not used to limit the protection scope of the application. Any modification, equivalent replacement, improvement, etc. made on the basis of the technical solutions of the application shall be included in the protection scope of the application.
Claims
1. A directory mounting method characterized by comprising: The method is applied to a computing device, the computing device comprising a host and a first container, the first container running on the host, the host comprising a first storage medium, the method comprising: mounting a first host directory to a first container directory before starting the first container; the first host directory being a directory on an operating system of the host, and the first container directory being a directory of the first container; after starting the first container, mounting the first storage medium based on the first host directory, and storing first data to the first storage medium based on the first container directory; the first data comprising data generated by the first container during running; the mounting the first storage medium based on the first host directory comprises mounting a second host directory to a third host directory, the second host directory being a directory on an operating system of the host, and the third host directory being a subdirectory of the first host directory, and the first storage medium being mounted on the second host directory; the storing first data to the first storage medium based on the first container directory comprises mounting a second container directory to a third container directory, the second container directory being a subdirectory of the first container directory corresponding to the third host directory, and the third container directory being a directory of the first container; and storing first data to the first storage medium through the third container directory.
2. The method of claim 1, wherein, the mounting the first storage medium based on the first host directory comprises: mounting a second host directory to the first host directory, the second host directory being a directory on an operating system of the host, and the first storage medium being mounted on the second host directory; the storing first data to the first storage medium based on the first container directory comprises: mounting the first container directory to a third container directory, the third container directory being a directory of the first container; storing first data to the first storage medium through the third container directory.
3. The method of claim 1, wherein, The method further comprises: creating and running the first container through a docker run command; the mounting the first host directory to the first container directory before starting the first container comprises: mounting the first host directory to the first container directory through a mounting command before starting the first container.
4. The method of claim 3, wherein, the mounting the first host directory to the first container directory through the mounting command comprises: executing a program file configured in an / etc / sysconfig / docker configuration file to mount the first host directory to the first container directory through the mounting command.
5. The method of claim 1, wherein, The host further comprises a second storage medium, and the method further comprises: mounting the first host directory to a fourth container directory before starting a second container; the second container being a container created on the computing device, and the fourth container directory being a directory of the second container; after starting the second container, mounting the second storage medium based on the first host directory, and storing second data to the second storage medium based on the fourth container directory; the second data comprising data generated by the second container during running.
6. The method of claim 5, wherein, mounting the second storage medium based on the first host directory comprises: mounting a fourth host directory to a fifth host directory, the fourth host directory being a directory on an operating system of the host, and the fifth host directory being a subdirectory of the first host directory, and the second storage medium being mounted at the fourth host directory; storing the second data to the second storage medium based on the fourth container directory comprises: mounting a fifth container directory to a sixth container directory, the fifth container directory being a subdirectory under the fourth container directory corresponding to the fifth host directory, and the sixth container directory being a directory of the second container; storing the second data to the second storage medium through the sixth container directory.
7. The method of claim 1, wherein, The method further comprises: unmounting the mounting path between the second host directory and the third host directory, and unmounting the mounting path between the second container directory and the third container directory.
8. The method according to any one of claims 1 to 7, characterized in that, The first storage medium is a disk, a partition of a disk, or a volume of a disk of the computing device.
9. A computing device, comprising: The computing device comprises a processor, a memory, and a communication interface, the communication interface being configured to receive information from and output information to other electronic devices outside the computing device, and the processor being configured to invoke a computer program stored in the memory to implement the method according to any one of claims 1-8.
Citation Information
Patent Citations
Resource allocation method, device, electronic equipment and storage medium
CN110362384A
File processing method and device and storage medium
CN114662102A