Project: iSulad (Runtime / Container Runtime)
File: image_storage_driver_design.md

| Author | | | | | | Date | 2020-08-28 | | Email | | The Driver module needs to support overlay2 and devicemapper drivers to achieve the following functions: Initialize the driver; return the driver status; return the metadata information of the driver; cleanup the driver; create a read-only layer; create a read-write layer; delete the layer, obtain the rootfs path of the layer; release the layer; The quota function also needs to be implemented for the overlay2 driver. ```mermaid classDiagram class driver class overlay <<interface>> overlay class devicemapper <<interface>> devicemapper driver : +char *name driver : +char *home driver : +char *backingfs driver : +bool support_dtype driver : +bool support_quota driver : +struct pquotacontrol *quotactrl driver : +struct graphdriver_ops* driver : +init() driver : +create_rw() driver : +create_ro() driver : +rm_layer() driver : +mount_layer() driver : +umount_layer() driver : +exists() driver : +apply_diff() driver : +getlayermetadata() driver : +getdriverstatus() driver : +clean_up() driver : +rm_layer() driver <|-- devicemapper driver <|-- overlay ``` ````c int graphdriverinit(const char *name, const char *isuladroot, char storage_opts, sizet storageopts_len); ```` ````c int graphdrivercreaterw(const char id, const char parent, struct drivercreateopts *create_opts); ```` ````c int graphdrivercreatero(const char id, const char parent, const struct drivercreateopts *create_opts); ```` ````c int graphdriverrmlayer(const char *id) ```` ````c char graphdriver_mount_layer(const char id, const struct drivermountopts *mount_opts) ```` ````c int graphdriverumountlayer(const char *id) ```` ````c bool graphdriverlayerexists(const char *id) ```` ````c int graphdriverapplydiff(const char id, const struct io_read_wrapper content, int64t *layersize) ```` ````c int graphdrivergetlayermetadata(const char *id, jsonmapstringstring *map_info) ```` ````c struct graphdriverstatus *graphdriverget_status(void) ```` ````c int graphdriver_cleanup(void) ```` Driver initialization initialization process: Overlay module initialization process: Devicemapper module initialization process: ````c struct drivercreateopts { char *mount_label; jsonmapstringstring *storageopt; }; ```` According to the incoming ID, parent layer ID and configuration creation options, call the actual driver create_rw interface to realize the function of creating layer layer. When creating the read-write layer, you need to determine whether the quota option is set. If the quota option is not set, the quota value of the default configuration of the daemon will be added as the quota limit of the read-write layer of the container. If the quota option is set and the current file system does not support setting quota, an error will be reported. The current overlay only supports the quota option. If there are other creation options, an error will be reported. ````c struct drivercreateopts { char *mount_label; jsonmapstringstring *storageopt; }; ```` According to the incoming ID, parent layer ID and creation options in configuration, call the actual driver create_ro interface to create a layer read-only layer. When creating a read-only layer, you need to determine whether the quota option is set. If the quota option is set, an error will be

If there are other creation options, an error will be reported. Call the actual driver rm_layer interface according to the incoming ID to delete the corresponding layer. ````c struct drivermountopts { char *mount_label; char options; sizet optionslen; }; ```` Call the actual driver mount_layer interface according to the incoming ID to mount the corresponding layer and return the mounted file system path. Call the actual driver umount_layer interface according to the incoming ID to umount the corresponding layer. Call the actual driver exists interface according to the incoming ID to query whether the corresponding layer exists. ````c struct ioreadwrapper { void *context; ioreadfunc_t read; ioclosefunc_t close; }; ```` Call the actual driver apply_diff interface according to the incoming ID to decompress the data. Overlay driver: When decompressing data, special processing is required for the overlay .whout file. If it is a file starting with .wh., it is marked as deleted and needs to be converted to char data, and the file needs to be skipped for subsequent decompression. For example, after deleting the home directory, the corresponding layer data is decompressed locally, and the corresponding home needs to create a character device with the same name. ````c drwxr-xr-x 4 root root 55 Mar 16 15:52 . drwxrwxrwt. 26 root root 4096 Mar 26 12:02 .. drwxr-xr-x 2 root root 38 Mar 16 12:49 etc c 1 root root 0, 0 Mar 16 15:52 home -rw-r--r-- 1 root root 140543 Mar 13 12:12 index.html dr-xr-x 2 root root 26 Mar 13 12:13 root ```` The decompressed data should be chrooted to the corresponding directory to prevent soft link attacks. Call the actual driver getlayermetadata interface according to the incoming ID to query the metadata of the corresponding layer. The metadata supported by overlay query is as follows: | key | value | | :-- | - | | WorkDir | the work path of the overlay layer | | MergedDir | the work path of the overlay layer | | UpperDir | the diff path of the overlay layer | | LowerDir | the underlying path of the overlay layer, including all the underlying paths, divided by: | ````c struct graphdriver_status { char *driver_name; char *backing_fs; char *status; }; ```` Query the status of the driver. The driver status that supports query is as follows: | key | value | | :- | | | driver_name | driver name | | backing_fs | the name of the file system where storage is located | | status | corresponds to the status of the underlying driver<br />the status return information supported by overlay is:<br />Backing Filesystem<br /> Supports d_type: true | Clean up the resources of the corresponding driver according to the call to the clean_up interface of the underlying driver The overlay driver implements uninstalling the storage home directory