Class: LXC::Container

Inherits:
Object
  • Object
show all
Defined in:
ext/lxc/lxc.c,
ext/lxc/lxc.c

Overview

This class contains methods to manage Linux containers.

Instance Method Summary collapse

Constructor Details

#LXC::Container.new(name, config_path = LXC.global_config_item('lxc.lxcpath')) ⇒ Object

Creates a new container instance with the given name, under the given configuration path.



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'ext/lxc/lxc.c', line 255

static VALUE
container_initialize(int argc, VALUE *argv, VALUE self)
{
    char *name, *config_path;
    struct lxc_container *container;
    struct container_data *data;
    VALUE rb_name, rb_config_path;

    rb_scan_args(argc, argv, "11", &rb_name, &rb_config_path);

    name = StringValuePtr(rb_name);
    config_path = NIL_P(rb_config_path) ? NULL : StringValuePtr(rb_config_path);

    container = lxc_container_new(name, config_path);
    if (container == NULL)
        rb_raise(Error, "error creating container %s", name);

    Data_Get_Struct(self, struct container_data, data);
    data->container = container;

    return self;
}

Instance Method Details

#add_device_node(src_path, dst_path = src_path) ⇒ Object

Adds a device node to the container.



392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# File 'ext/lxc/lxc.c', line 392

static VALUE
container_add_device_node(int argc, VALUE *argv, VALUE self)
{
    int ret;
    char *src_path, *dst_path;
    struct container_data *data;
    VALUE rb_src_path, rb_dst_path;

    rb_scan_args(argc, argv, "11", &rb_src_path, &rb_dst_path);
    src_path = NIL_P(rb_src_path) ? NULL : StringValuePtr(rb_src_path);
    dst_path = NIL_P(rb_dst_path) ? NULL : StringValuePtr(rb_dst_path);

    Data_Get_Struct(self, struct container_data, data);

    ret = data->container->add_device_node(data->container, src_path, dst_path);
    if (!ret)
        rb_raise(Error, "unable to add device node");

    return self;
}

#attach(opts = {}, &block) ⇒ Object

Calls block in the context of the attached container. The options may contain the following keys.

  • :flags

  • :namespaces

  • :personality

  • :initial_cwd

  • :uid

  • :gid

  • :env_policy

  • :extra_env_vars

  • :extra_keep_env

  • :stdin

  • :stdout

  • :stderr

  • :wait



617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
# File 'ext/lxc/lxc.c', line 617

static VALUE
container_attach(int argc, VALUE *argv, VALUE self)
{
    int wait;
    long ret;
    pid_t pid;
    lxc_attach_options_t *opts;
    struct container_data *data;
    VALUE block, rb_opts;

    if (!rb_block_given_p())
        rb_raise(Error, "no block given");
    block = rb_block_proc();

    rb_scan_args(argc, argv, "01", &rb_opts);

    wait = 0;
    if (!NIL_P(rb_opts)) {
        VALUE rb_wait;
        Check_Type(rb_opts, T_HASH);
        rb_wait = rb_hash_delete(rb_opts, SYMBOL("wait"));
        if (rb_wait != Qnil && rb_wait != Qfalse)
            wait = 1;
    }
    opts = lxc_attach_parse_options(rb_opts);
    if (opts == NULL)
        rb_raise(Error, "unable to parse attach options");

    Data_Get_Struct(self, struct container_data, data);

    ret = data->container->attach(data->container, lxc_attach_exec,
                                  (void *)block, opts, &pid);
    if (ret < 0)
        goto out;

    if (wait) {
        ret = lxc_wait_for_pid_status(pid);
        /* handle case where attach fails */
        if (WIFEXITED(ret) && WEXITSTATUS(ret) == 255)
            ret = -1;
    } else {
        ret = pid;
    }

out:
    lxc_attach_free_options(opts);
    return LONG2NUM(ret);
}

#cgroup_item(key) ⇒ Object

Returns the value corresponding to the given cgroup item configuration.



955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
# File 'ext/lxc/lxc.c', line 955

static VALUE
container_cgroup_item(VALUE self, VALUE rb_key)
{
    int len1, len2;
    char *key, *value;
    struct container_data *data;
    struct lxc_container *container;
    VALUE ret;

    Data_Get_Struct(self, struct container_data, data);
    container = data->container;

    key = StringValuePtr(rb_key);
    len1 = container->get_cgroup_item(container, key, NULL, 0);
    if (len1 < 0)
        rb_raise(Error, "invalid cgroup entry for %s", key);

    value = malloc(sizeof(char) * len1 + 1);
    if (value == NULL)
        rb_raise(rb_eNoMemError, "unable to allocate cgroup value");

    len2 = container->get_cgroup_item(container, key, value, len1 + 1);
    if (len1 != len2) {
        free(value);
        rb_raise(Error, "unable to read cgroup value");
    }
    ret = rb_str_new2(value);
    free(value);

    return ret;
}

#clear_configObject

Clears the container configuration.



672
673
674
675
676
677
678
679
# File 'ext/lxc/lxc.c', line 672

static VALUE
container_clear_config(VALUE self)
{
    struct container_data *data;
    Data_Get_Struct(self, struct container_data, data);
    data->container->clear_config(data->container);
    return self;
}

#clear_config_item(key) ⇒ Object

Clears the container configuration item key.



687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
# File 'ext/lxc/lxc.c', line 687

static VALUE
container_clear_config_item(VALUE self, VALUE rb_key)
{
    int ret;
    char *key;
    struct container_data *data;

    key = StringValuePtr(rb_key);

    Data_Get_Struct(self, struct container_data, data);

    ret = data->container->clear_config_item(data->container, key);
    if (!ret)
        rb_raise(Error, "unable to clear config item %s", key);

    return self;
}

#clone(clone_name, opts = {}) ⇒ Object

Clones the container, returning a new one with the given name. The options hash may contain the following keys:

  • :config_path

  • :flags

  • :bdev_type

  • :bdev_data

  • :new_size

  • :hook_args



719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
# File 'ext/lxc/lxc.c', line 719

static VALUE
container_clone(int argc, VALUE *argv, VALUE self)
{
    int flags;
    uint64_t new_size;
    char *name, *config_path, *bdev_type, *bdev_data;
    char **hook_args;
    struct lxc_container *container, *new_container;
    struct container_data *data;
    VALUE rb_name, rb_opts;
    VALUE rb_flags, rb_config_path, rb_bdev_type, rb_bdev_data;
    VALUE rb_new_size, rb_hook_args;
    VALUE rb_args[2];

    rb_scan_args(argc, argv, "11", &rb_name, &rb_opts);

    name = StringValuePtr(rb_name);

    config_path = NULL;
    flags       = 0;
    bdev_type   = NULL;
    bdev_data   = NULL;
    new_size    = 0;
    hook_args   = NULL;

    rb_config_path = Qnil;

    if (!NIL_P(rb_opts)) {
        Check_Type(rb_opts, T_HASH);
        rb_config_path = rb_hash_aref(rb_opts, SYMBOL("config_path"));
        if (!NIL_P(rb_config_path))
            config_path = StringValuePtr(rb_config_path);

        rb_flags = rb_hash_aref(rb_opts, SYMBOL("flags"));
        if (!NIL_P(rb_flags))
            flags = NUM2INT(rb_flags);

        rb_bdev_type = rb_hash_aref(rb_opts, SYMBOL("bdev_type"));
        if (!NIL_P(rb_bdev_type))
            bdev_type = StringValuePtr(rb_bdev_type);

        rb_bdev_data = rb_hash_aref(rb_opts, SYMBOL("bdev_data"));
        if (!NIL_P(rb_bdev_data))
            bdev_data = StringValuePtr(rb_bdev_data);

        rb_new_size = rb_hash_aref(rb_opts, SYMBOL("new_size"));
        if (!NIL_P(rb_bdev_data))
            new_size = NUM2ULL(rb_new_size);

        rb_hook_args = rb_hash_aref(rb_opts, SYMBOL("hook_args"));
        if (!NIL_P(rb_hook_args))
            hook_args = ruby_to_c_string_array(rb_hook_args);
    }

    Data_Get_Struct(self, struct container_data, data);
    container = data->container;

    new_container = container->clone(container, name, config_path,
                                     flags, bdev_type, bdev_data, new_size,
                                     hook_args);

    if (hook_args)
        free_c_string_array(hook_args);

    if (new_container == NULL)
        rb_raise(Error, "unable to clone container");

    lxc_container_put(new_container);

    rb_args[0] = rb_name;
    rb_args[1] = rb_config_path;
    return rb_class_new_instance(2, rb_args, Container);
}

#config_file_nameObject

Returns the name of the container’s configuration file.



284
285
286
287
288
289
290
291
292
293
294
# File 'ext/lxc/lxc.c', line 284

static VALUE
container_config_file_name(VALUE self)
{
    char *config_file_name;
    struct container_data *data;

    Data_Get_Struct(self, struct container_data, data);
    config_file_name = data->container->config_file_name(data->container);

    return rb_str_new2(config_file_name);
}

#config_item(key) ⇒ Object

Returns the value corresponding to the given configuration item.



993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
# File 'ext/lxc/lxc.c', line 993

static VALUE
container_config_item(VALUE self, VALUE rb_key)
{
    int len1, len2, mlines;
    char *key, *value;
    struct container_data *data;
    struct lxc_container *container;
    VALUE rb_config;

    Data_Get_Struct(self, struct container_data, data);
    container = data->container;

    key = StringValuePtr(rb_key);
    len1 = container->get_config_item(container, key, NULL, 0);
    if (len1 < 0)
        rb_raise(Error, "invalid configuration key: %s", key);

    value = malloc(sizeof(char) * len1 + 1);
    if (value == NULL)
        rb_raise(rb_eNoMemError, "unable to allocate configuration value");

    len2 = container->get_config_item(container, key, value, len1 + 1);
    if (len1 != len2) {
        free(value);
        rb_raise(Error, "unable to read configuration file");
    }
    rb_config = rb_str_new2(value);
    mlines = value[len2-1] == '\n';
    free(value);

    /* Return a list in case of multiple lines */
    return mlines ? rb_str_split(rb_config, "\n") : rb_config;
}

#config_pathObject

Returns the configuration path for the container.



1033
1034
1035
1036
1037
1038
1039
# File 'ext/lxc/lxc.c', line 1033

static VALUE
container_config_path(VALUE self)
{
    struct container_data *data;
    Data_Get_Struct(self, struct container_data, data);
    return rb_str_new2(data->container->get_config_path(data->container));
}

#config_path=(path) ⇒ Object

Sets the container configuration path.



1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
# File 'ext/lxc/lxc.c', line 1377

static VALUE
container_set_config_path(VALUE self, VALUE rb_path)
{
    int ret;
    char *path;
    struct container_data *data;

    path = StringValuePtr(rb_path);
    Data_Get_Struct(self, struct container_data, data);

    ret = data->container->set_config_path(data->container, path);
    if (!ret)
        rb_raise(Error, "unable to set configuration path to %s", path);

    return self;
}

#console(opts = {}) ⇒ Object

Accesses the container’s console. The options hash may contain the following keys.

  • :tty_num

  • :stdin_fd

  • :stdout_fd

  • :stderr_fd

  • :escape



806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
# File 'ext/lxc/lxc.c', line 806

static VALUE
container_console(int argc, VALUE *argv, VALUE self)
{
    int ret;
    int tty_num = -1, stdin_fd = 0, stdout_fd = 1, stderr_fd = 2, escape = 1;
    struct container_data *data;
    struct lxc_container *container;
    VALUE rb_opts;

    rb_scan_args(argc, argv, "01", &rb_opts);
    switch (TYPE(rb_opts)) {
    case T_HASH:
        tty_num   = NUM2INT(rb_hash_aref(rb_opts, SYMBOL("tty_num")));
        stdin_fd  = NUM2INT(rb_hash_aref(rb_opts, SYMBOL("stdin_fd")));
        stdout_fd = NUM2INT(rb_hash_aref(rb_opts, SYMBOL("stdout_fd")));
        stderr_fd = NUM2INT(rb_hash_aref(rb_opts, SYMBOL("stderr_fd")));
        escape    = NUM2INT(rb_hash_aref(rb_opts, SYMBOL("escape")));
        break;
    default:
        rb_raise(rb_eArgError, "options must be a hash");
    }

    Data_Get_Struct(self, struct container_data, data);
    container = data->container;

    ret = container->console(container, tty_num, stdin_fd, stdout_fd, stderr_fd,
                             escape);
    if (ret != 0)
        rb_raise(Error, "unable to access container console");

    return self;
}

#console_fd(tty_num = nil) ⇒ Object

Returns an IO object referring to the container’s console file descriptor.



845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
# File 'ext/lxc/lxc.c', line 845

static VALUE
container_console_fd(int argc, VALUE *argv, VALUE self)
{
    int ret, tty_num, master_fd;
    struct container_data *data;
    VALUE rb_tty_num;
    VALUE rb_io_args[1];

    rb_scan_args(argc, argv, "01", &rb_tty_num);
    tty_num = NIL_P(rb_tty_num) ? -1 : NUM2INT(rb_tty_num);

    Data_Get_Struct(self, struct container_data, data);

    ret = data->container->console_getfd(data->container, &tty_num, &master_fd);
    if (ret < 0)
        rb_raise(Error, "unable to allocate tty");

    rb_io_args[0] = INT2NUM(master_fd);
    return rb_class_new_instance(1, rb_io_args, rb_cIO);
}

#controllable?Boolean

Returns:

  • (Boolean)


296
297
298
299
300
301
302
303
304
305
306
# File 'ext/lxc/lxc.c', line 296

static VALUE
container_controllable_p(VALUE self)
{
    int controllable;
    struct container_data *data;

    Data_Get_Struct(self, struct container_data, data);
    controllable = data->container->may_control(data->container);

    return controllable ? Qtrue : Qfalse;
}

#create(template, bdevtype = nil, flags = 0, args = []) ⇒ Object

Creates a structure for the container according to the given template. This usually consists of downloading and installing a Linux distribution inside the container’s rootfs.

The flags argument is an OR of LXC_CREATE_* flags.



876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
# File 'ext/lxc/lxc.c', line 876

static VALUE
container_create(int argc, VALUE *argv, VALUE self)
{
    int ret, flags;
    char *template;
    char *bdevtype;
    char **args = { NULL };
    struct container_data *data;
    struct lxc_container *container;
    VALUE rb_template, rb_bdevtype, rb_flags, rb_args;

    rb_scan_args(argc, argv, "13", &rb_template, &rb_bdevtype, &rb_flags, &rb_args);

    template = StringValuePtr(rb_template);
    bdevtype = NIL_P(rb_bdevtype) ? NULL : StringValuePtr(rb_bdevtype);
    flags = NIL_P(rb_flags) ? 0 : NUM2INT(rb_flags);
    if (!NIL_P(rb_args))
        args = ruby_to_c_string_array(rb_args);

    Data_Get_Struct(self, struct container_data, data);
    container = data->container;
    ret = container->create(container, template, bdevtype, NULL, flags, args);

    if (!NIL_P(rb_args))
        free_c_string_array(args);

    if (!ret)
        rb_raise(Error, "unable to create container");

    return self;
}

#defined?Boolean

Returns:

  • (Boolean)


308
309
310
311
312
313
314
315
316
317
318
# File 'ext/lxc/lxc.c', line 308

static VALUE
container_defined_p(VALUE self)
{
    int defined;
    struct container_data *data;

    Data_Get_Struct(self, struct container_data, data);
    defined = data->container->is_defined(data->container);

    return defined ? Qtrue : Qfalse;
}

#destroyObject

Destroys the container.



914
915
916
917
918
919
920
921
922
923
924
925
926
# File 'ext/lxc/lxc.c', line 914

static VALUE
container_destroy(VALUE self)
{
    int ret;
    struct container_data *data;

    Data_Get_Struct(self, struct container_data, data);

    ret = data->container->destroy(data->container);
    if (!ret)
        rb_raise(Error, "unable to destroy container");
    return self;
}

#freezeObject

Freezes the container.



934
935
936
937
938
939
940
941
942
943
944
945
946
947
# File 'ext/lxc/lxc.c', line 934

static VALUE
container_freeze(VALUE self)
{
    int ret;
    struct container_data *data;

    Data_Get_Struct(self, struct container_data, data);

    ret = data->container->freeze(data->container);
    if (!ret)
        rb_raise(Error, "unable to freeze container");

    return self;
}

#init_pidObject

Returns the PID of the container’s init process from the host’s point of view.



327
328
329
330
331
332
333
334
335
336
337
338
# File 'ext/lxc/lxc.c', line 327

static VALUE
container_init_pid(VALUE self)
{
    pid_t pid;
    struct container_data *data;

    Data_Get_Struct(self, struct container_data, data);
    pid = data->container->init_pid(data->container);
    if (pid < 0)
        return Qnil;
    return INT2NUM(pid);
}

#interfacesObject

Returns the list of network interfaces of the container.



1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
# File 'ext/lxc/lxc.c', line 1085

static VALUE
container_interfaces(VALUE self)
{
    int i, num_interfaces;
    char **interfaces;
    struct container_data *data;
    VALUE rb_interfaces;

    Data_Get_Struct(self, struct container_data, data);

    interfaces = data->container->get_interfaces(data->container);
    if (!interfaces)
        return rb_ary_new();

    for (num_interfaces = 0; interfaces[num_interfaces]; num_interfaces++)
        ;

    rb_interfaces = rb_ary_new2(num_interfaces);
    for (i = 0; i < num_interfaces; i++)
        rb_ary_store(rb_interfaces, i, rb_str_new2(interfaces[i]));

    free_c_string_array(interfaces);

    return rb_interfaces;
}

#ip_addressesObject

Returns the list of IP addresses of the container.



1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
# File 'ext/lxc/lxc.c', line 1117

static VALUE
container_ips(int argc, VALUE *argv, VALUE self)
{
    int i, num_ips, scope;
    char *interface, *family;
    char **ips;
    struct container_data *data;
    VALUE rb_ips, rb_interface, rb_family, rb_scope;

    rb_scan_args(argc, argv, "03", &rb_interface, &rb_family, &rb_scope);
    interface = NIL_P(rb_interface) ? NULL : StringValuePtr(rb_interface);
    family    = NIL_P(rb_family)    ? NULL : StringValuePtr(rb_family);
    scope     = NIL_P(rb_scope)     ? 0    : NUM2INT(rb_scope);

    Data_Get_Struct(self, struct container_data, data);

    ips = data->container->get_ips(data->container, interface, family, scope);
    if (ips == NULL)
        return rb_ary_new();

    for (num_ips = 0; ips[num_ips]; num_ips++)
        ;

    rb_ips = rb_ary_new2(num_ips);
    for (i = 0; i < num_ips; i++)
        rb_ary_store(rb_ips, i, rb_str_new2(ips[i]));

    free_c_string_array(ips);

    return rb_ips;
}

#keys(key) ⇒ Object

Returns a list of valid sub-keys for the given configuration key.



1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
# File 'ext/lxc/lxc.c', line 1047

static VALUE
container_keys(VALUE self, VALUE rb_key)
{
    int len1, len2;
    char *key, *value;
    struct container_data *data;
    struct lxc_container *container;
    VALUE rb_keys;

    Data_Get_Struct(self, struct container_data, data);
    container = data->container;

    key = StringValuePtr(rb_key);
    len1 = container->get_keys(container, key, NULL, 0);
    if (len1 < 0)
        rb_raise(Error, "invalid configuration key: %s", key);

    value = malloc(sizeof(char) * len1 + 1);
    if (value == NULL)
        rb_raise(rb_eNoMemError, "unable to allocate configuration value");

    len2 = container->get_keys(container, key, value, len1 + 1);
    if (len1 != len2) {
        free(value);
        rb_raise(Error, "unable to read configuration keys");
    }
    rb_keys = rb_str_new2(value);
    free(value);

    return value[len2-1] == '\n' ?  rb_str_split(rb_keys, "\n") : rb_keys;
}

#load_config(config_path = nil) ⇒ Object

Loads the container’s configuration.



1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
# File 'ext/lxc/lxc.c', line 1155

static VALUE
container_load_config(int argc, VALUE *argv, VALUE self)
{
    int ret;
    char *path;
    struct container_data *data;
    VALUE rb_path;

    rb_scan_args(argc, argv, "01", &rb_path);
    path = NIL_P(rb_path) ? NULL : StringValuePtr(rb_path);

    Data_Get_Struct(self, struct container_data, data);

    ret = data->container->load_config(data->container, path);
    if (!ret)
        rb_raise(Error, "unable to load configuration file");

    return self;
}

#nameObject

Returns the name of the container.



346
347
348
349
350
351
352
353
354
# File 'ext/lxc/lxc.c', line 346

static VALUE
container_name(VALUE self)
{
    struct container_data *data;

    Data_Get_Struct(self, struct container_data, data);

    return rb_str_new2(data->container->name);
}

#rebootObject

Reboots the container.



1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
# File 'ext/lxc/lxc.c', line 1181

static VALUE
container_reboot(VALUE self)
{
    int ret;
    struct container_data *data;

    Data_Get_Struct(self, struct container_data, data);

    ret = data->container->reboot(data->container);
    if (!ret)
        rb_raise(Error, "unable to reboot container");

    return self;
}

#remove_device_node(src_path, dst_path = src_path) ⇒ Object

Removes a device node from the container.



1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
# File 'ext/lxc/lxc.c', line 1202

static VALUE
container_remove_device_node(int argc, VALUE *argv, VALUE self)
{
    int ret;
    char *src_path, *dst_path;
    struct lxc_container *container;
    struct container_data *data;
    VALUE rb_src_path, rb_dst_path;

    rb_scan_args(argc, argv, "11", &rb_src_path, &rb_dst_path);
    src_path = StringValuePtr(rb_src_path);
    dst_path = NIL_P(rb_dst_path) ? NULL : StringValuePtr(rb_dst_path);

    Data_Get_Struct(self, struct container_data, data);
    container = data->container;

    ret = container->remove_device_node(container, src_path, dst_path);
    if (!ret)
        rb_raise(Error, "unable to remove device node");

    return self;
}

#rename(new_name) ⇒ Object

Renames the container and returns a new LXC::Container instance of the container with the new name.



1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
# File 'ext/lxc/lxc.c', line 1232

static VALUE
container_rename(VALUE self, VALUE rb_name)
{
    int ret;
    char *name;
    struct container_data *data;
    VALUE rb_args[2];

    name = StringValuePtr(rb_name);
    Data_Get_Struct(self, struct container_data, data);

    ret = data->container->rename(data->container, name);
    if (!ret)
        rb_raise(Error, "unable to rename container");

    rb_args[0] = rb_name;
    rb_args[1] = Qnil;
    return rb_class_new_instance(2, rb_args, Container);
}

#running?Boolean

Returns:

  • (Boolean)


356
357
358
359
360
361
362
363
364
365
366
# File 'ext/lxc/lxc.c', line 356

static VALUE
container_running_p(VALUE self)
{
    int running;
    struct container_data *data;

    Data_Get_Struct(self, struct container_data, data);
    running = data->container->is_running(data->container);

    return running ? Qtrue : Qfalse;
}

#running_config_item(key) ⇒ Object

Returns the value corresponding to the given configuration item from a running container.



1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
# File 'ext/lxc/lxc.c', line 1259

static VALUE
container_running_config_item(VALUE self, VALUE rb_key)
{
    char *key, *value;
    struct container_data *data;
    struct lxc_container *container;
    VALUE rb_value;

    Data_Get_Struct(self, struct container_data, data);
    container = data->container;

    key = StringValuePtr(rb_key);
    value = container->get_running_config_item(container, key);
    if (value == NULL)
        rb_raise(Error, "unable to read running configuration item: %s", key);

    rb_value = rb_str_new2(value);
    free(value);

    return rb_value;
}

#save_config(*args) ⇒ Object



1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
# File 'ext/lxc/lxc.c', line 1281

static VALUE
container_save_config(int argc, VALUE *argv, VALUE self)
{
    int ret;
    char *path;
    struct container_data *data;
    VALUE rb_path;

    rb_scan_args(argc, argv, "01", &rb_path);
    path = NIL_P(rb_path) ? NULL : StringValuePtr(rb_path);

    Data_Get_Struct(self, struct container_data, data);

    ret = data->container->save_config(data->container, path);
    if (!ret)
        rb_raise(Error, "unable to save configuration file");

    return self;
}

#set_cgroup_item(key, value) ⇒ Object

Sets the value of a cgroup configuration item.



1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
# File 'ext/lxc/lxc.c', line 1307

static VALUE
container_set_cgroup_item(VALUE self, VALUE rb_key, VALUE rb_value)
{
    int ret;
    char *key, *value;
    struct container_data *data;

    key = StringValuePtr(rb_key);
    value = StringValuePtr(rb_value);

    Data_Get_Struct(self, struct container_data, data);

    ret = data->container->set_cgroup_item(data->container, key, value);
    if (!ret)
        rb_raise(Error, "unable to set cgroup item %s to %s", key, value);

    return self;
}

#set_config_item(key, value) ⇒ Object

Sets the value of a configuration item.



1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
# File 'ext/lxc/lxc.c', line 1332

static VALUE
container_set_config_item(VALUE self, VALUE rb_key, VALUE rb_value)
{
    int ret;
    char *key, *value;
    struct container_data *data;

    Data_Get_Struct(self, struct container_data, data);

    key = StringValuePtr(rb_key);
    switch (TYPE(rb_value)) {
    case T_STRING: {
        value = StringValuePtr(rb_value);
        ret = data->container->set_config_item(data->container, key, value);
        if (!ret) {
            rb_raise(Error, "unable to set configuration item %s to %s",
                     key, value);
        }
        return self;
    }
    case T_ARRAY: {
        size_t i;
        size_t len = RARRAY_LEN(rb_value);
        for (i = 0; i < len; i++) {
            VALUE rb_entry = rb_ary_entry(rb_value, i);
            char *entry = StringValuePtr(rb_entry);
            ret = data->container->set_config_item(data->container, key, entry);
            if (!ret) {
                rb_raise(Error, "unable to set configuration item %s to %s",
                        key, entry);
            }
        }
        return self;
    }
    default:
        rb_raise(Error, "configuration value must be either string or array");
    }
}

#shutdown(timeout = -1) ⇒ Object

Shuts down the container, optionally waiting for timeout seconds. If timeout is -1, wait as long as necessary for the container to shutdown.



1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
# File 'ext/lxc/lxc.c', line 1402

static VALUE
container_shutdown(int argc, VALUE *argv, VALUE self)
{
    int ret, timeout;
    struct container_data *data;
    VALUE rb_timeout;

    rb_scan_args(argc, argv, "01", &rb_timeout);
    timeout = NIL_P(rb_timeout) ? -1 : NUM2INT(rb_timeout);

    Data_Get_Struct(self, struct container_data, data);

    ret = data->container->shutdown(data->container, timeout);
    if (!ret)
        rb_raise(Error, "unable to shutdown container");

    return self;
}

#snapshot(path = nil) ⇒ Object

Creates a snapshot of the container. Returns the snapshot name.



1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
# File 'ext/lxc/lxc.c', line 1427

static VALUE
container_snapshot(int argc, VALUE *argv, VALUE self)
{
    int ret;
    char *path;
    char new_name[20];
    struct container_data *data;
    VALUE rb_path;

    rb_scan_args(argc, argv, "01", &rb_path);
    path = NIL_P(rb_path) ? NULL : StringValuePtr(rb_path);

    Data_Get_Struct(self, struct container_data, data);

    ret = data->container->snapshot(data->container, path);
    if (ret < 0)
        rb_raise(Error, "unable to snapshot container");

    ret = snprintf(new_name, 20, "snap%d", ret);
    if (ret < 0 || ret >= 20)
        rb_raise(Error, "unable to snapshot container");

    return rb_str_new2(new_name);
}

#snapshot_destroy(name) ⇒ Object

Destroys the given snapshot.



1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
# File 'ext/lxc/lxc.c', line 1458

static VALUE
container_snapshot_destroy(VALUE self, VALUE rb_name)
{
    int ret;
    char *name;
    struct container_data *data;

    name = StringValuePtr(rb_name);

    Data_Get_Struct(self, struct container_data, data);

    ret = data->container->snapshot_destroy(data->container, name);
    if (!ret)
        rb_raise(Error, "unable to destroy snapshot");

    return self;
}

#snapshot_listObject

Returns a list of existing snapshots for the container.



1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
# File 'ext/lxc/lxc.c', line 1482

static VALUE
container_snapshot_list(VALUE self)
{
    int i, num_snapshots;
    struct lxc_snapshot *snapshots;
    struct container_data *data;
    VALUE rb_snapshots;

    Data_Get_Struct(self, struct container_data, data);

    num_snapshots = data->container->snapshot_list(data->container, &snapshots);
    if (num_snapshots < 0)
        rb_raise(Error, "unable to list snapshots");

    rb_snapshots = rb_ary_new2(num_snapshots);
    for (i = 0; i < num_snapshots; i++) {
        VALUE attrs = rb_ary_new2(4);
        rb_ary_store(attrs, 0, rb_str_new2(snapshots[i].name));
        rb_ary_store(attrs, 1, rb_str_new2(snapshots[i].comment_pathname));
        rb_ary_store(attrs, 2, rb_str_new2(snapshots[i].timestamp));
        rb_ary_store(attrs, 3, rb_str_new2(snapshots[i].lxcpath));
        snapshots[i].free(&snapshots[i]);
        rb_ary_store(rb_snapshots, i, attrs);
    }

    return rb_snapshots;
}

#snapshot_restore(name, new_name = nil) ⇒ Object

Restores the given snapshot.



1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
# File 'ext/lxc/lxc.c', line 1516

static VALUE
container_snapshot_restore(int argc, VALUE *argv, VALUE self)
{
    int ret;
    char *name, *new_name;
    struct container_data *data;
    VALUE rb_name, rb_new_name;

    rb_scan_args(argc, argv, "11", &rb_name, &rb_new_name);
    name = StringValuePtr(rb_name);
    new_name = NIL_P(rb_new_name) ? NULL : StringValuePtr(rb_new_name);

    Data_Get_Struct(self, struct container_data, data);

    ret = data->container->snapshot_restore(data->container, name, new_name);
    if (!ret)
        rb_raise(Error, "unable to restore snapshot");

    return self;
}

#start(opts = {}) ⇒ Object

Starts the container. The options hash may contain the following keys.

  • :use_init

  • :daemonize

  • :close_fds

  • :args



1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
# File 'ext/lxc/lxc.c', line 1548

static VALUE
container_start(int argc, VALUE *argv, VALUE self)
{
    int ret, use_init, daemonize, close_fds;
    char **args;
    struct container_data *data;
    VALUE rb_use_init, rb_daemonize, rb_close_fds, rb_args, rb_opts;

    use_init = 0;
    daemonize = 1;
    close_fds = 0;
    args = NULL;
    rb_args = Qnil;

    rb_scan_args(argc, argv, "01", &rb_opts);
    if (!NIL_P(rb_opts)) {
        Check_Type(rb_opts, T_HASH);
        rb_use_init = rb_hash_aref(rb_opts, SYMBOL("use_init"));
        use_init = (rb_use_init != Qnil) && (rb_use_init != Qfalse);

        rb_daemonize = rb_hash_aref(rb_opts, SYMBOL("daemonize"));
        daemonize = (rb_daemonize != Qnil) && (rb_daemonize != Qfalse);

        rb_close_fds = rb_hash_aref(rb_opts, SYMBOL("close_fds"));
        close_fds = (rb_close_fds != Qnil) && (rb_close_fds != Qfalse);

        rb_args = rb_hash_aref(rb_opts, SYMBOL("args"));
        args = NIL_P(rb_args) ? NULL : ruby_to_c_string_array(rb_args);
    }

    Data_Get_Struct(self, struct container_data, data);

    data->container->want_close_all_fds(data->container, close_fds);
    data->container->want_daemonize(data->container, daemonize);
    ret = data->container->start(data->container, use_init, args);

    if (!NIL_P(rb_args))
        free_c_string_array(args);

    if (!ret)
        rb_raise(Error, "unable to start container");

    return self;
}

#stateObject

Returns a symbol representing the state of the container.



374
375
376
377
378
379
380
381
382
383
384
# File 'ext/lxc/lxc.c', line 374

static VALUE
container_state(VALUE self)
{
    struct container_data *data;
    VALUE rb_state;

    Data_Get_Struct(self, struct container_data, data);
    rb_state = rb_str_new2(data->container->state(data->container));

    return rb_str_intern(rb_funcall(rb_state, rb_intern("downcase"), 0));
}

#stopObject

Stops the container.



1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
# File 'ext/lxc/lxc.c', line 1599

static VALUE
container_stop(VALUE self)
{
    int ret;
    struct container_data *data;

    Data_Get_Struct(self, struct container_data, data);

    ret = data->container->stop(data->container);
    if (!ret)
        rb_raise(Error, "unable to stop container");

    return self;
}

#unfreezeObject

Thaws a frozen container.



1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
# File 'ext/lxc/lxc.c', line 1620

static VALUE
container_unfreeze(VALUE self)
{
    int ret;
    struct container_data *data;

    Data_Get_Struct(self, struct container_data, data);

    ret = data->container->unfreeze(data->container);
    if (!ret)
        rb_raise(Error, "unable to unfreeze container");

    return self;
}

#wait(state, timeout = -1) ⇒ Object

Waits for timeout seconds (or as long as necessary if timeout is -1) until the container’s state becomes state.



1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
# File 'ext/lxc/lxc.c', line 1642

static VALUE
container_wait(int argc, VALUE *argv, VALUE self)
{
    int ret, timeout;
    char *state;
    struct container_data *data;
    VALUE rb_state_str, rb_state, rb_timeout;

    rb_scan_args(argc, argv, "11", &rb_state, &rb_timeout);

    rb_state_str = rb_funcall(rb_state, rb_intern("to_s"), 0);
    rb_state_str = rb_funcall(rb_state_str, rb_intern("upcase"), 0);
    state = StringValuePtr(rb_state_str);

    timeout = NIL_P(rb_timeout) ? -1 : NUM2INT(rb_timeout);

    Data_Get_Struct(self, struct container_data, data);

    ret = data->container->wait(data->container, state, timeout);
    if (!ret)
        rb_raise(Error, "error waiting for container");

    return self;
}