[lxc-devel] [PATCH] introduce lxcapi_rename for renaming containers

S.Çağlar Onur caglar at 10ur.org
Mon Dec 16 20:55:07 UTC 2013


On Mon, Dec 16, 2013 at 2:58 PM, Serge Hallyn <serge.hallyn at ubuntu.com> wrote:
> Quoting Stéphane Graber (stgraber at ubuntu.com):
>> On Mon, Dec 16, 2013 at 02:37:11PM -0500, S.Çağlar Onur wrote:
>> > Hi Serge,
>> >
>> > On Mon, Dec 16, 2013 at 9:47 AM, Serge Hallyn <serge.hallyn at ubuntu.com> wrote:
>> > > Quoting S.Çağlar Onur (caglar at 10ur.org):
>> > >> lxcapi_rename implemented as a convenience function as lately
>> > >> I find myself in a need to rename a container due to a
>> > >> typo in its name. I could have started over but didn't want
>> > >> to spend more time (to installing extra packages and changing
>> > >> their configuration) on it.
>> > >>
>> > >> c->clone() followed by c->destroy() did the trick for me and I
>> > >> though it could be helpful to the other people, so here it is.
>> > >>
>> > >> Signed-off-by: S.Çağlar Onur <caglar at 10ur.org>
>> > >
>> > > Looks ok, but should you be specifying LXC_CLONE_COPYHOOKS |
>> > > LXC_CLONE_KEEPMACADDR  as clone flags?
>> >
>> > I thought I should to be keeping the container intact as much as I
>> > can, assuming something might be using those values (like
>> > redhat/centos/fedora ifcfg-* files where you could specify HWADDR)
>>
>> I'm not sure about LXC_CLONE_COPYHOOKS but LXC_CLONE_KEEPMACADDR should
>> actually go towards your goal of having the copy be as close to the
>> original as possible (without it, the mac address would be changed).
>
> Yeah.  Çağlar, lemme know if you agree.

Sure, you can drop LXC_CLONE_COPYHOOKS or alternatively I can send a
new patch tomorrow (I'll be offline rest of the day)

>> >
>> > >> ---
>> > >>  src/lxc/lxccontainer.c | 33 +++++++++++++++++++++++++++++++++
>> > >>  src/lxc/lxccontainer.h | 10 ++++++++++
>> > >>  2 files changed, 43 insertions(+)
>> > >>
>> > >> diff --git a/src/lxc/lxccontainer.c b/src/lxc/lxccontainer.c
>> > >> index 6326724..1af8d62 100644
>> > >> --- a/src/lxc/lxccontainer.c
>> > >> +++ b/src/lxc/lxccontainer.c
>> > >> @@ -2596,6 +2596,38 @@ out:
>> > >>       return NULL;
>> > >>  }
>> > >>
>> > >> +static bool lxcapi_rename(struct lxc_container *c, const char *newname)
>> > >> +{
>> > >> +     struct bdev *bdev;
>> > >> +     struct lxc_container *newc;
>> > >> +     int flags = LXC_CLONE_KEEPMACADDR | LXC_CLONE_COPYHOOKS;
>> > >> +
>> > >> +     if (!c || !c->name || !c->config_path)
>> > >> +             return false;
>> > >> +
>> > >> +     bdev = bdev_init(c->lxc_conf->rootfs.path, c->lxc_conf->rootfs.mount, NULL);
>> > >> +     if (!bdev) {
>> > >> +             ERROR("Failed to find original backing store type");
>> > >> +             return false;
>> > >> +     }
>> > >> +
>> > >> +     newc = lxcapi_clone(c, newname, c->config_path, flags, NULL, bdev->type, 0, NULL);
>> > >> +     bdev_put(bdev);
>> > >> +     if (!newc) {
>> > >> +             lxc_container_put(newc);
>> > >> +             return false;
>> > >> +     }
>> > >> +
>> > >> +     if (newc && lxcapi_is_defined(newc))
>> > >> +             lxc_container_put(newc);
>> > >> +
>> > >> +     if (!lxcapi_destroy(c)) {
>> > >> +             ERROR("Could not destroy existing container %s", c->name);
>> > >> +             return false;
>> > >> +     }
>> > >> +     return true;
>> > >> +}
>> > >> +
>> > >>  static int lxcapi_attach(struct lxc_container *c, lxc_attach_exec_t exec_function, void *exec_payload, lxc_attach_options_t *options, pid_t *attached_process)
>> > >>  {
>> > >>       if (!c)
>> > >> @@ -3139,6 +3171,7 @@ struct lxc_container *lxc_container_new(const char *name, const char *configpath
>> > >>       c->wait = lxcapi_wait;
>> > >>       c->set_config_item = lxcapi_set_config_item;
>> > >>       c->destroy = lxcapi_destroy;
>> > >> +     c->rename = lxcapi_rename;
>> > >>       c->save_config = lxcapi_save_config;
>> > >>       c->get_keys = lxcapi_get_keys;
>> > >>       c->create = lxcapi_create;
>> > >> diff --git a/src/lxc/lxccontainer.h b/src/lxc/lxccontainer.h
>> > >> index 3e1b492..b7c5313 100644
>> > >> --- a/src/lxc/lxccontainer.h
>> > >> +++ b/src/lxc/lxccontainer.h
>> > >> @@ -326,6 +326,16 @@ struct lxc_container {
>> > >>                       struct bdev_specs *specs, int flags, ...);
>> > >>
>> > >>       /*!
>> > >> +      * \brief Rename a container
>> > >> +      *
>> > >> +      * \param c Container.
>> > >> +      * \param newname New name to be used for the container.
>> > >> +      *
>> > >> +      * \return \c true on success, else \c false.
>> > >> +      */
>> > >> +     bool (*rename)(struct lxc_container *c, const char *newname);
>> > >> +
>> > >> +     /*!
>> > >>        * \brief Request the container reboot by sending it \c SIGINT.
>> > >>        *
>> > >>        * \param c Container.
>> > >> --
>> > >> 1.8.3.2
>> > >>
>> > >> _______________________________________________
>> > >> lxc-devel mailing list
>> > >> lxc-devel at lists.linuxcontainers.org
>> > >> http://lists.linuxcontainers.org/listinfo/lxc-devel
>> > > _______________________________________________
>> > > lxc-devel mailing list
>> > > lxc-devel at lists.linuxcontainers.org
>> > > http://lists.linuxcontainers.org/listinfo/lxc-devel
>> >
>> >
>> >
>> > --
>> > S.Çağlar Onur <caglar at 10ur.org>
>> > _______________________________________________
>> > lxc-devel mailing list
>> > lxc-devel at lists.linuxcontainers.org
>> > http://lists.linuxcontainers.org/listinfo/lxc-devel
>>
>> --
>> Stéphane Graber
>> Ubuntu developer
>> http://www.ubuntu.com
>
>
>
>> _______________________________________________
>> lxc-devel mailing list
>> lxc-devel at lists.linuxcontainers.org
>> http://lists.linuxcontainers.org/listinfo/lxc-devel
>
> _______________________________________________
> lxc-devel mailing list
> lxc-devel at lists.linuxcontainers.org
> http://lists.linuxcontainers.org/listinfo/lxc-devel



-- 
S.Çağlar Onur <caglar at 10ur.org>


More information about the lxc-devel mailing list