{"id":585,"date":"2023-10-07T22:32:26","date_gmt":"2023-10-07T13:32:26","guid":{"rendered":"https:\/\/blog.gurees.net\/?p=585"},"modified":"2023-10-07T22:36:35","modified_gmt":"2023-10-07T13:36:35","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%e2%91%a1%e3%82%b3%e3%83%b3%e3%83%86%e3%83%8a%e3%81%ae%e5%a0%b4%e6%89%80%e3%80%81%e3%83%95%e3%82%a1%e3%82%a4","status":"publish","type":"post","link":"https:\/\/blog.gurees.net\/?p=585","title":{"rendered":"LXC\u3092C\u8a00\u8a9e\u304b\u3089\u64cd\u4f5c\u3059\u308b\u2461(\u30b3\u30f3\u30c6\u30ca\u306e\u5834\u6240\u3001\u30d5\u30a1\u30a4\u30eb\u30b7\u30b9\u30c6\u30e0\u3092\u6307\u5b9a)"},"content":{"rendered":"\n<p><a href=\"https:\/\/blog.gurees.net\/?p=581\">LXC\u3092C\u8a00\u8a9e\u304b\u3089\u64cd\u4f5c\u3059\u308b\u2460<\/a>\u3067C\u8a00\u8a9e\u304b\u3089\u30b3\u30f3\u30c6\u30ca\u306e\u4f5c\u6210\u3001\u8d77\u52d5\u3001\u7d42\u4e86\u3001\u524a\u9664\u307e\u3067\u3092\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u30d9\u30fc\u30b9\u3067\u52d5\u304b\u3057\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<p>\u4eca\u5ea6\u306f\u3061\u3087\u3063\u3068\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u5909\u3048\u3066\u3001rootfs\u306e\u8a2d\u5b9a\u3068\u3001\u30b3\u30f3\u30c6\u30ca\u306e\u5834\u6240\u3092\u5909\u3048\u3066\u307f\u307e\u3059\u3002<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>lxc_container_new\u306e\u7b2c2\u5f15\u6570\u304c\u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\u306a\u3069\u306e\u30d1\u30b9\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p>createl\u306e\u7b2c2\u5f15\u6570\u304crootfs\u306e\u30d5\u30a1\u30a4\u30eb\u30bf\u30a4\u30d7\u306b\u306a\u308a\u307e\u3059\u3002NULL\u306b\u3057\u3066\u304a\u304f\u3068dir\u306b\u306a\u308a\u307e\u3059\u3002\u4eca\u56de\u306floop\u30c7\u30d0\u30a4\u30b9(\u30a4\u30e1\u30fc\u30b8\u30d5\u30a1\u30a4\u30eb)\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u7b2c2\u5f15\u6570\u3092\u6307\u5b9a\u3059\u308b\u5834\u5408\u3001\u7b2c3\u5f15\u6570\u306b\u306fbdev_specs\u69cb\u9020\u4f53\u306e\u30dd\u30a4\u30f3\u30bf\u3092\u6307\u5b9a\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002bdev_specs\u69cb\u9020\u4f53\u306f\u30c7\u30d0\u30a4\u30b9\u306e\u8a2d\u5b9a\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p>loop\u30c7\u30d0\u30a4\u30b9\u306e\u5834\u5408\u3001fstype\u306b\u30d5\u30a1\u30a4\u30eb\u30b7\u30b9\u30c6\u30e0\u306e\u7a2e\u5225\u3001fssize\u306b\u30d0\u30a4\u30c8\u5358\u4f4d\u3067\u30d5\u30a1\u30a4\u30eb\u30b7\u30b9\u30c6\u30e0\u306e\u30b5\u30a4\u30ba\u3092\u6307\u5b9a\u3057\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;cstring>\n#include &lt;cstdlib>\n#include &lt;memory>\n#include &lt;lxc\/lxccontainer.h>\n\n#define SIZE_FSTYPE 16\n\nint main() {\n    struct lxc_container* c;\n    int ret = 1;\n    const char* pszDevType = \"loop\";\n    std::unique_ptr&lt;bdev_specs> pstSpecs(new bdev_specs);\n    memset(pstSpecs.get(), 0x00, sizeof(bdev_specs));\n    pstSpecs->fstype = new char[SIZE_FSTYPE];\n\n    strncpy(pstSpecs->fstype, \"ext4\", SIZE_FSTYPE-1);\n    pstSpecs->fssize =        1024*1024*1024*1;\n\n    \/* Setup container struct *\/\n    c = lxc_container_new(\"apicontainer\", \"\/shared\/lxc\");\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\", pszDevType, pstSpecs.get(), 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;\n    if(pstSpecs->fstype != nullptr){\n        delete[] pstSpecs->fstype;\n    }\n    \nout:\n    lxc_container_put(c);\n    return ret;\n}<\/pre>\n\n\n\n<p>\u30b3\u30fc\u30c9\u3092\u4e0a\u8a18\u306e\u3088\u3046\u306b\u66f8\u304d\u63db\u3048\u3066\u3001\u30b3\u30f3\u30d1\u30a4\u30eb\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=\"\"># g++ -llxc lxctest.cpp -o lxctest<\/pre>\n\n\n\n<p>\u5b9f\u884c\u4e2d\u306b\u3001\/shared\/lxc\u3092\u78ba\u8a8d\u3059\u308b\u3068\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306brootfs\u304c1GB\u306e\u30c7\u30a3\u30b9\u30af\u30a4\u30e1\u30fc\u30b8\u306b\u306a\u3063\u3066\u3044\u308b\u306e\u304c\u78ba\u8a8d\u3067\u304d\u308b\u3068\u601d\u3044\u307e\u3059<\/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=\"\"># cd \/shared\/lxc\/apicontainer\n# ll\ntotal 474888\n-rw-r----- 1 nobody nobody        798 Oct  7 21:57 config\n-rw------- 1 nobody nobody 1073741825 Oct  7 21:57 rootdev\ndrwxr-xr-x 2 nobody nobody          6 Oct  7 21:57 rootfs\n<\/pre>\n\n\n\n<p>\u3068\u308a\u3042\u3048\u305a\u306fC++\u304b\u3089\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u6307\u5b9a\u3057\u3064\u3064\u3001\u30b3\u30f3\u30c6\u30ca\u3092\u52d5\u304b\u305b\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<p>\u305f\u3060\u3001C\u8a00\u8a9e\u306eAPI\u3068\u3042\u308b\u3060\u3051\u3042\u3063\u3066\u3001API\u306b\u6e21\u3059\u30d1\u30e9\u30e1\u30fc\u30bf\u304c\u69cb\u9020\u4f53\u306b\u306a\u308b\u3060\u3051\u3042\u3063\u3066\u3001\u3061\u3087\u3063\u3068\u4f7f\u3044\u306b\u304f\u3044\u3067\u3059\u306d\u3002\u30af\u30e9\u30b9\u306b\u30e9\u30c3\u30d1\u30fc\u3057\u305f\u3044\u3067\u3059\u306d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>LXC\u3092C\u8a00\u8a9e\u304b\u3089\u64cd\u4f5c\u3059\u308b\u2460\u3067C\u8a00\u8a9e\u304b\u3089\u30b3\u30f3\u30c6\u30ca\u306e\u4f5c\u6210\u3001\u8d77\u52d5\u3001\u7d42\u4e86\u3001\u524a\u9664\u307e\u3067\u3092\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u30d9\u30fc\u30b9\u3067\u52d5\u304b\u3057\u3066\u307f\u307e\u3057\u305f\u3002 \u4eca\u5ea6\u306f\u3061\u3087\u3063\u3068\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u5909\u3048\u3066\u3001rootfs\u306e\u8a2d\u5b9a\u3068\u3001\u30b3\u30f3\u30c6\u30ca\u306e\u5834\u6240\u3092\u5909\u3048\u3066\u307f\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-585","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\/585","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=585"}],"version-history":[{"count":2,"href":"https:\/\/blog.gurees.net\/index.php?rest_route=\/wp\/v2\/posts\/585\/revisions"}],"predecessor-version":[{"id":596,"href":"https:\/\/blog.gurees.net\/index.php?rest_route=\/wp\/v2\/posts\/585\/revisions\/596"}],"wp:attachment":[{"href":"https:\/\/blog.gurees.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=585"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.gurees.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=585"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.gurees.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=585"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}