Skip to content

docker races with udev on device symlink handling when dm.use_deferred_removal is enabled #24671

Closed

Description

@jizhilong

As I commented on #4036, docker(actually libdevicemapper used by docker) may race with udev on device symlink creating/deleting when dm.use_deferred_removal is turned on , and it eventually leads to a docker start/diff fail.Following is the reproducing procedure:

docker info

Containers: 0
 Running: 0
 Paused: 0
 Stopped: 0
Images: 2
Server Version: 1.11.1
Storage Driver: devicemapper
 Pool Name: docker-252:1-58724678-pool
 Pool Blocksize: 65.54 kB
 Base Device Size: 10.74 GB
 Backing Filesystem: xfs
 Data file: /dev/loop0
 Metadata file: /dev/loop1
 Data Space Used: 883.9 MB
 Data Space Total: 107.4 GB
 Data Space Available: 35.03 GB
 Metadata Space Used: 2.146 MB
 Metadata Space Total: 2.147 GB
 Metadata Space Available: 2.145 GB
 Udev Sync Supported: true
 Deferred Removal Enabled: true
 Deferred Deletion Enabled: true
 Deferred Deleted Device Count: 0
 Data loop file: /var/lib/docker/devicemapper/devicemapper/data
 Metadata loop file: /var/lib/docker/devicemapper/devicemapper/metadata
 Library Version: 1.02.107-RHEL7 (2016-06-09)
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins: 
 Volume: local
 Network: host bridge null
Kernel Version: 3.10.0-327.22.2.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 3.86 GiB
Name: jzl-dev0010
ID: U4ZX:62TO:YRGL:CNDH:6UZN:VNYX:3R2W:ZCWF:4TJS:54BS:O4AK:NLYY
Docker Root Dir: /var/lib/docker
Debug mode (client): false
Debug mode (server): false
Registry: https://index.docker.io/v1/

way to reproduce

make sure all containers removed

# NOTE!!! this command is dangerous, don't execute it on any non-experimental node.
docker rm -f  `docker ps -aq`

get image ready

a busybox image would be sufficient, docker pull busybox

get script ready

A Python script is needed to eagerly detect and open any docker created non-init dm devices:

import os
import time

while True:
    try:
        for line in os.listdir('/dev/mapper'):
            if 'init' in line:continue
            if len(line) >= 80:
                filename=os.path.join('/dev/mapper/',line)
                f = open(filename)
        print 'got it %s' % filename
        time.sleep(3*10**7)
    except Exception:
        pass

make sure all container or image related dm devices removed

run dmsetup info to list all dm devices on your node, and run dmsetup remove <name> to remove all devices whose name starts with docker- and ends with a uuid or <uuid>-init, such as docker-252:1-58724678-3c96b9d15c41b49816eab1faaa0cb6eaa7cdffe5decd45f2bd48ae72f32e16f5

run the script and reproduce the issue

following is one sample of my reproducing logs:

# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
# dmsetup info
Name:              docker-252:1-58724678-pool
State:             ACTIVE
Read Ahead:        8192
Tables present:    LIVE
Open count:        0
Event number:      0
Major, minor:      253, 0
Number of targets: 1

# python opendm.py &
[1] 3757
# cid=`docker create -it busybox sh`
got it /dev/mapper/docker-252:1-58724678-097a341a6a5f17cc86f9d83ebdff35b8da86fd8ab6d614ac8445f3fabe1d6e25
# docker diff $cid
# docker start $cid
Error response from daemon: open /dev/mapper/docker-252:1-58724678-097a341a6a5f17cc86f9d83ebdff35b8da86fd8ab6d614ac8445f3fabe1d6e25: no such file or directory
Error: failed to start containers: d3cbd8f86704136511b5d4cfdafeb1398a46f962ff9af7389ec7368f8472433e

the essential commands are:

python opendm.py &
docker diff $cid
docker start $cid

get the corrupted container back to normal

# kill %1
# dmsetup remove docker-252:1-58724678-097a341a6a5f17cc86f9d83ebdff35b8da86fd8ab6d614ac8445f3fabe1d6e25
[1]+  Terminated              python opendm.py
# ls /dev/dm-*
/dev/dm-0
# ls /dev/mapper/
control  docker-252:1-58724678-pool
# docker start $cid
d3cbd8f86704136511b5d4cfdafeb1398a46f962ff9af7389ec7368f8472433e

the essential operations to recover the container include:

  1. kill the background process python opendm.py
  2. remove the container's dm device.

Activity

  1. jizhilong commented on Jul 15, 2016

    @jizhilong
    ContributorAuthor

    After some digging into code of lvm2 and docker, I got a hypothesis for the cause of the issue:

    1. for dm device removal, libdevicemapper checks the existence of cookie in a dm_task to determine whether or not relying on udev to create/delete device symlink(code is here).
    2. in pkg.devicemapper.RemoveDeviceDeferred, no cookie is set in the dm_task.
    3. since no cookie is set, libdevicemapper will delete the corresponding symlink , no matter the device-removal is a deferred one or direct one.If it is a deferred device-remove, and the device is busy, kernel will not remove it immediately, once the syscall ioctl returns, /dev/dm-xexists, but the symlink under /dev/mapper is deleted by libdevicemapper.
    4. next time docker tries to activate the device, docker only need to cancel the deferred removal, no 'UDEV ADD' event would be generated, neither udevd nor libdevicemapper will create the symlink.
    5. After cancelDeferredRemove, docker will try to mount the device by its symlink, which is already deleted by libdevicemapper for the last call to RemoveDeviceDeferred, thus docker will complain on a failure no such file or directory.
  2. jizhilong commented on Jul 18, 2016

    @jizhilong
    ContributorAuthor
  3. added a commit that references this issue on Aug 24, 2016
    a7e42bf
  4. added a commit that references this issue on Aug 25, 2016
    c7e7694
  5. added 2 commits that reference this issue on Sep 26, 2016
    72f7889
    d5aaaa7
  6. added a commit that references this issue on Oct 30, 2017
    f5436d3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions