{"id":581,"date":"2023-10-07T11:28:34","date_gmt":"2023-10-07T02:28:34","guid":{"rendered":"https:\/\/blog.gurees.net\/?p=581"},"modified":"2023-10-07T22:32:41","modified_gmt":"2023-10-07T13:32:41","slug":"lxc%e3%82%92c%e8%a8%80%e8%aa%9e%e3%81%8b%e3%82%89%e6%93%8d%e4%bd%9c%e3%81%99%e3%82%8b","status":"publish","type":"post","link":"https:\/\/blog.gurees.net\/?p=581","title":{"rendered":"LXC\u3092C\u8a00\u8a9e\u304b\u3089\u64cd\u4f5c\u3059\u308b\u2460"},"content":{"rendered":"\n<p><a href=\"https:\/\/blog.gurees.net\/?p=567\">CentOS Stream9\u3067LXC\u3092\u52d5\u304b\u3059\u2460<\/a>\u3067\u53d6\u308a\u5408\u3048\u305aCentOS\u4e0a\u3067\u30b3\u30f3\u30c6\u30ca\u304c\u52d5\u304f\u3068\u3053\u308d\u307e\u3067\u8a2d\u5b9a\u3067\u304d\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<p>\u6b21\u306fC\u8a00\u8a9e\u306eAPI\u3092\u4f7f\u3063\u3066\u307f\u3088\u3046\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>\u30b3\u30f3\u30d1\u30a4\u30eb\u306b\u5fc5\u8981\u306a\u300c<em>lxc<\/em>&#8211;<em>devel<\/em>&nbsp;\u300d\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002\u5408\u308f\u305b\u3066\u30b3\u30f3\u30d1\u30a4\u30e9\u95a2\u4fc2\u3082\u5165\u308c\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># dnf install gcc g++ make lxc-devel -y<\/pre>\n\n\n\n<p>\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u3068\u3001API\u306b\u4f7f\u3046\u30d8\u30c3\u30c0\u30d5\u30a1\u30a4\u30eb\u306f\u300c\/usr\/include\/lxc\/lxccontainer.h\u300d\u306b\u3001\u30e9\u30a4\u30d6\u30e9\u30ea\u306f\u300c\/usr\/lib64\/liblxc.so\u300d\u306b\u7f6e\u304b\u308c\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u516c\u5f0f\u306e\u30b5\u30f3\u30d7\u30eb\u3092\u30d9\u30fc\u30b9\u306b\u3001CentOS Stream9\u306e\u30b3\u30f3\u30c6\u30ca\u3092\u4f5c\u308b\u3088\u3046\u306b\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#include &lt;stdio.h>\n#include &lt;lxc\/lxccontainer.h>\n\nint main() {\n    struct lxc_container* c;\n    int ret = 1;\n\n    \/* Setup container struct *\/\n    c = lxc_container_new(\"apicontainer\", NULL);\n    if (!c) {\n        fprintf(stderr, \"Failed to setup lxc_container struct\\n\");\n        goto out;\n    }\n\n    if (c->is_defined(c)) {\n        fprintf(stderr, \"Container already exists\\n\");\n        goto out;\n    }\n\n    \/* Create the container *\/\n    if (!c->createl(c, \"download\", NULL, NULL, LXC_CREATE_QUIET,\n                    \"-d\", \"centos\", \"-r\", \"9-Stream\", \"-a\", \"amd64\", NULL)) {\n        fprintf(stderr, \"Failed to create container rootfs\\n\");\n        goto out;\n    }\n\n    \/* Start the container *\/\n    if (!c->start(c, 0, NULL)) {\n        fprintf(stderr, \"Failed to start the container\\n\");\n        goto out;\n    }\n\n    \/* Query some information *\/\n    printf(\"Container state: %s\\n\", c->state(c));\n    printf(\"Container PID: %d\\n\", c->init_pid(c));\n\n    \/* Stop the container *\/\n    if (!c->shutdown(c, 30)) {\n        printf(\"Failed to cleanly shutdown the container, forcing.\\n\");\n        if (!c->stop(c)) {\n            fprintf(stderr, \"Failed to kill the container.\\n\");\n            goto out;\n        }\n    }\n\n    \/* Destroy the container *\/\n    if (!c->destroy(c)) {\n        fprintf(stderr, \"Failed to destroy the container.\\n\");\n        goto out;\n    }\n\n    ret = 0;\nout:\n    lxc_container_put(c);\n    return ret;\n}<\/pre>\n\n\n\n<p>\u4e0a\u8a18\u306e\u30b3\u30fc\u30c9\u3092lxctest.cpp\u3068\u3057\u3066\u4fdd\u7ba1\u3057\u3001\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3067\u30d3\u30eb\u30c9\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># gcc -llxc lxctest.cpp -o lxctest<\/pre>\n\n\n\n<p>\u30d3\u30eb\u30c9\u3067\u304d\u305f\u3089\u5b9f\u884c\u3057\u307e\u3059\u3002\u30b7\u30e3\u30c3\u30c8\u30c0\u30a6\u30f3\u304c\u6307\u5b9a\u306e30sec\u4ee5\u5185\u306b\u3067\u304d\u306a\u304b\u3063\u305f\u307f\u305f\u3044\u3067\u3059\u304c\u3001\u3068\u308a\u3042\u3048\u305a\u306f\u52d5\u3044\u305f\u307f\u305f\u3044\u3067\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># .\/lxctest\nContainer state: RUNNING\nContainer PID: 3538\nFailed to cleanly shutdown the container, forcing.<\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>CentOS Stream9\u3067LXC\u3092\u52d5\u304b\u3059\u2460\u3067\u53d6\u308a\u5408\u3048\u305aCentOS\u4e0a\u3067\u30b3\u30f3\u30c6\u30ca\u304c\u52d5\u304f\u3068\u3053\u308d\u307e\u3067\u8a2d\u5b9a\u3067\u304d\u307e\u3057\u305f\u3002 \u6b21\u306fC\u8a00\u8a9e\u306eAPI\u3092\u4f7f\u3063\u3066\u307f\u3088\u3046\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33,32,19,25],"tags":[],"class_list":["post-581","post","type-post","status-publish","format-standard","hentry","category-c-c","category-container","category-linux","category-programming"],"_links":{"self":[{"href":"https:\/\/blog.gurees.net\/index.php?rest_route=\/wp\/v2\/posts\/581","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.gurees.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.gurees.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.gurees.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.gurees.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=581"}],"version-history":[{"count":8,"href":"https:\/\/blog.gurees.net\/index.php?rest_route=\/wp\/v2\/posts\/581\/revisions"}],"predecessor-version":[{"id":593,"href":"https:\/\/blog.gurees.net\/index.php?rest_route=\/wp\/v2\/posts\/581\/revisions\/593"}],"wp:attachment":[{"href":"https:\/\/blog.gurees.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=581"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.gurees.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=581"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.gurees.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=581"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}