5장. librbd(Python)


rbd python 모듈은 RBD 이미지에 파일와 유사한 액세스를 제공합니다. 이 기본 제공 툴을 사용하려면 rbdrados 모듈을 가져와야 합니다.

생성 및 이미지에 쓰기

  1. RADOS에 연결하고 IO 컨텍스트를 엽니다.

    cluster = rados.Rados(conffile='my_ceph.conf')
    cluster.connect()
    ioctx = cluster.open_ioctx('mypool')
    Copy to Clipboard Toggle word wrap
  2. 이미지를 생성하는 데 사용하는 :class:rbd.RBD 오브젝트를 인스턴스화합니다.

    rbd_inst = rbd.RBD()
    size = 4 * 1024**3  # 4 GiB
    rbd_inst.create(ioctx, 'myimage', size)
    Copy to Clipboard Toggle word wrap
  3. 이미지에서 I/O를 수행하려면 :class:rbd.Image 오브젝트를 인스턴스화합니다.

    image = rbd.Image(ioctx, 'myimage')
    data = 'foo' * 200
    image.write(data, 0)
    Copy to Clipboard Toggle word wrap

    이 명령은 이미지의 처음 600바이트에 'foo'를 씁니다. 데이터는 :type:unicode - librbd:c:type:char 보다 광범위한 문자를 처리하는 방법을 알 수 없습니다.

  4. 이미지, IO 컨텍스트, RADOS에 대한 연결을 종료합니다.

    image.close()
    ioctx.close()
    cluster.shutdown()
    Copy to Clipboard Toggle word wrap

    안전하려면 각 호출이 별도의 :finally 블록에 있어야합니다.

    import rados
    import rbd
    
    cluster = rados.Rados(conffile='my_ceph_conf')
    try:
        ioctx = cluster.open_ioctx('my_pool')
        try:
            rbd_inst = rbd.RBD()
            size = 4 * 1024**3  # 4 GiB
            rbd_inst.create(ioctx, 'myimage', size)
            image = rbd.Image(ioctx, 'myimage')
            try:
                data = 'foo' * 200
                image.write(data, 0)
            finally:
                image.close()
        finally:
            ioctx.close()
    finally:
        cluster.shutdown()
    Copy to Clipboard Toggle word wrap

    이는 번거로움일 수 있으므로 Rados,Ioctx, Image 클래스는 자동으로 닫히거나 종료되는 컨텍스트 관리자로 사용할 수 있습니다. 컨텍스트 관리자로 이를 사용하면 위의 예는 다음과 같습니다.

    with rados.Rados(conffile='my_ceph.conf') as cluster:
        with cluster.open_ioctx('mypool') as ioctx:
            rbd_inst = rbd.RBD()
            size = 4 * 1024**3  # 4 GiB
            rbd_inst.create(ioctx, 'myimage', size)
            with rbd.Image(ioctx, 'myimage') as image:
                data = 'foo' * 200
                image.write(data, 0)
    Copy to Clipboard Toggle word wrap
맨 위로 이동
Red Hat logoGithubredditYoutubeTwitter

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 문서 정보

Red Hat을 사용하는 고객은 신뢰할 수 있는 콘텐츠가 포함된 제품과 서비스를 통해 혁신하고 목표를 달성할 수 있습니다. 최신 업데이트를 확인하세요.

보다 포괄적 수용을 위한 오픈 소스 용어 교체

Red Hat은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

Theme

© 2025 Red Hat