{"id":33,"date":"2024-08-20T10:26:34","date_gmt":"2024-08-20T02:26:34","guid":{"rendered":"https:\/\/cnsn.rr.nu\/?p=33"},"modified":"2024-08-20T10:26:34","modified_gmt":"2024-08-20T02:26:34","slug":"%e7%94%b1img%e9%95%9c%e5%83%8f%e6%8c%82%e8%bd%bd%e5%b0%81%e8%a3%85%ef%bc%8c%e5%b9%b6%e5%8a%a0%e8%bd%bd%e5%88%b0kvm%e4%b8%ad","status":"publish","type":"post","link":"https:\/\/blog.cnsn.fun\/?p=33","title":{"rendered":"\u7531img\u955c\u50cf\u6302\u8f7d\u5c01\u88c5\uff0c\u5e76\u52a0\u8f7d\u5230kvm\u4e2d"},"content":{"rendered":"<h3><strong>\u4e00\u3001\u6302\u8f7d\u5c01\u88c5<\/strong><\/h3>\n<p>1.\u5b89\u88c5\u4f9d\u8d56\u9879\uff1a<\/p>\n<p>\u786e\u4fdd\u4f60\u7684\u7cfb\u7edf\u4e0a\u5b89\u88c5\u4e86QEMU\u548c\u76f8\u5e94\u7684\u5de5\u5177\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5728Ubuntu\u4e0a\u5b89\u88c5\u5b83\u4eec\uff1a<\/p>\n<pre><code>sudo apt update\nsudo apt install qemu-utils<\/code><\/pre>\n<p>2.\u6302\u8f7d\u955c\u50cf\u6587\u4ef6\uff1a<\/p>\n<p>\u4f7f\u7528<code>qemu-nbd<\/code>\u547d\u4ee4\u5c06\u955c\u50cf\u6587\u4ef6\u6302\u8f7d\u5230\u4e00\u4e2a\u76ee\u5f55\u4e2d\uff1a<\/p>\n<pre><code>sudo modprobe nbd max_part=8\nsudo qemu-nbd --connect=\/dev\/nbd0 ubuntu.img<\/code><\/pre>\n<p>\u8fd9\u4f1a\u5c06<code>ubuntu.img<\/code>\u6302\u8f7d\u5230<code>\/dev\/nbd0<\/code>\u3002<\/p>\n<p>3.\u67e5\u770b\u5206\u533a:<\/p>\n<p>\u4f7f\u7528<code>lsblk<\/code>\u6216\u5176\u4ed6\u76f8\u5173\u547d\u4ee4\u67e5\u770b\u6302\u8f7d\u7684\u955c\u50cf\u7684\u5206\u533a\u60c5\u51b5\uff1a<\/p>\n<p><code>lsblk \/dev\/nbd0<\/code><br \/>\n\u8bb0\u4e0b\u5206\u533a\u4fe1\u606f\uff0c\u4ee5\u5907\u540e\u7528\u3002<\/p>\n<p>4.\u6302\u8f7d\u5206\u533a\uff1a  \u5c06\u7279\u5b9a\u5206\u533a\u6302\u8f7d\u5230\u4e00\u4e2a\u76ee\u5f55\u4e2d\uff1a<\/p>\n<p><code> sudo mount \/dev\/nbd0p1 \/mnt<\/code><br \/>\n\u8fd9\u91cc\u7684<code>\/dev\/nbd0p1<\/code>\u53ef\u80fd\u9700\u8981\u66ff\u6362\u4e3a\u4f60\u5b9e\u9645\u7684\u5206\u533a\u3002<\/p>\n<p>5.\u4fee\u6539\u5185\u5bb9\uff1a   <\/p>\n<p>\u5728\u6302\u8f7d\u7684\u76ee\u5f55\u4e2d\u8fdb\u884c\u4efb\u4f55\u4f60\u9700\u8981\u7684\u66f4\u6539\uff0c\u6dfb\u52a0\u6587\u4ef6\u3001\u4fee\u6539\u914d\u7f6e\u7b49\u3002<\/p>\n<p>6.\u5378\u8f7d\u5206\u533a\u548c\u955c\u50cf\uff1a <\/p>\n<p>\u5728\u5b8c\u6210\u4fee\u6539\u540e\uff0c\u786e\u4fdd\u5378\u8f7d\u6302\u8f7d\u7684\u5206\u533a\u5e76\u65ad\u5f00QEMU NBD\u8fde\u63a5\uff1a<\/p>\n<pre><code> sudo umount \/mnt\n sudo qemu-nbd --disconnect \/dev\/nbd0<\/code><\/pre>\n<p>7.\u91cd\u65b0\u5c01\u88c5\u955c\u50cf\uff1a   <\/p>\n<p>\u4f7f\u7528<code>qemu-img<\/code>\u547d\u4ee4\u91cd\u65b0\u5c01\u88c5\u955c\u50cf\u6587\u4ef6\uff1a<\/p>\n<p><code>qemu-img convert -O qcow2 -o compat=1.1 ubuntu.img ubuntu_modified.img<\/code><br \/>\n\u8fd9\u5c06\u751f\u6210\u4e00\u4e2a\u65b0\u7684QCOW2\u683c\u5f0f\u7684\u955c\u50cf\u6587\u4ef6<code>ubuntu_modified.img<\/code>\u3002<\/p>\n<p>\u73b0\u5728\uff0c\u4f60\u5c31\u6210\u529f\u4fee\u6539\u5e76\u91cd\u65b0\u5c01\u88c5\u4e86QCOW2\u955c\u50cf\u6587\u4ef6\u3002\u8bf7\u6ce8\u610f\uff0c\u8fd9\u4e9b\u6b65\u9aa4\u4e2d\u7684\u5177\u4f53\u547d\u4ee4\u53ef\u80fd\u9700\u8981\u6839\u636e\u4f60\u7684\u7cfb\u7edf\u548c\u9700\u6c42\u8fdb\u884c\u9002\u5f53\u7684\u8c03\u6574\u3002\u5e0c\u671b\u8fd9\u4e9b\u6b65\u9aa4\u5bf9\u4f60\u6709\u5e2e\u52a9\uff0c\u5982\u679c\u6709\u4efb\u4f55\u95ee\u9898\uff0c\u8bf7\u968f\u65f6\u5411\u6211\u8be2\u95ee\u3002<\/p>\n<h3><strong>\u4e8c\u3001\u955c\u50cf\u5b89\u88c5<\/strong><\/h3>\n<p>\u5c06\u5236\u4f5c\u597d\u7684QCOW2\u955c\u50cf\u5bfc\u5165\u5230KVM\u5e76\u8fd0\u884c\u5b83\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u5c06\u955c\u50cf\u6587\u4ef6\u590d\u5236\u5230KVM\u4e3b\u673a\uff1a<\/li>\n<\/ol>\n<p>\u5c06\u5236\u4f5c\u597d\u7684QCOW2\u955c\u50cf\u6587\u4ef6\uff08\u6bd4\u5982<code>ubuntu_modified.img<\/code>\uff09\u590d\u5236\u5230KVM\u4e3b\u673a\u7684\u5408\u9002\u4f4d\u7f6e\uff0c\u4f8b\u5982<code>\/var\/lib\/libvirt\/images\/<\/code>\u76ee\u5f55\u4e0b\u3002<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528virt-manager\u521b\u5efa\u865a\u62df\u673a\uff08\u53ef\u9009\uff09\uff1a<\/li>\n<\/ol>\n<p>\u5982\u679c\u4f60\u4f7f\u7528virt-manager\u7ba1\u7406KVM\u865a\u62df\u673a\uff0c\u53ef\u4ee5\u901a\u8fc7virt-manager\u56fe\u5f62\u754c\u9762\u8f7b\u677e\u521b\u5efa\u865a\u62df\u673a\u5e76\u5bfc\u5165\u955c\u50cf\u3002\u6253\u5f00virt-manager\u5e76\u6309\u7167\u5411\u5bfc\u6307\u793a\u64cd\u4f5c\uff0c\u9009\u62e9\u5bfc\u5165\u73b0\u6709\u955c\u50cf\u5e76\u6307\u5b9a\u4f60\u7684QCOW2\u955c\u50cf\u6587\u4ef6\u3002<\/p>\n<ol start=\"3\">\n<li>\u901a\u8fc7\u547d\u4ee4\u884c\u521b\u5efa\u865a\u62df\u673a\uff1a<\/li>\n<\/ol>\n<p>\u5982\u679c\u4f60\u66f4\u559c\u6b22\u4f7f\u7528\u547d\u4ee4\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>virt-install<\/code>\u547d\u4ee4\u521b\u5efa\u865a\u62df\u673a\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u547d\u4ee4\uff1a<\/p>\n<pre><code>virt-install \\\n--name ubuntu-vm \\  # \u865a\u62df\u673a\u540d\u79f0\n--memory 2048 \\     # \u5185\u5b58\u5927\u5c0f\uff08MB\uff09\n--vcpus 2 \\         # \u865a\u62dfCPU\u6570\u91cf\n--disk path=\/var\/lib\/libvirt\/images\/ubuntu_modified.img,format=qcow2 \\  # \u955c\u50cf\u6587\u4ef6\u8def\u5f84\u548c\u683c\u5f0f\n--network bridge=br0 \\  # \u7f51\u7edc\u914d\u7f6e\uff0c\u6839\u636e\u4f60\u7684\u7f51\u7edc\u8bbe\u7f6e\u8c03\u6574\n--graphics spice \\   # \u56fe\u5f62\u663e\u793a\u534f\u8bae\uff08\u53ef\u4ee5\u9009\u62e9\u5176\u4ed6\u9009\u9879\uff09\n--os-type linux \\    # \u64cd\u4f5c\u7cfb\u7edf\u7c7b\u578b\n--os-variant ubuntu20.04 \\  # \u64cd\u4f5c\u7cfb\u7edf\u53d8\u79cd\n--import             # \u5bfc\u5165\u5df2\u6709\u955c\u50cf<\/code><\/pre>\n<p>\u8bf7\u6839\u636e\u4f60\u7684\u5b9e\u9645\u60c5\u51b5\u8c03\u6574\u53c2\u6570\uff0c\u5c24\u5176\u662f\u7f51\u7edc\u914d\u7f6e\u90e8\u5206\u3002<\/p>\n<ol start=\"4\">\n<li>\u542f\u52a8\u865a\u62df\u673a\uff1a<\/li>\n<\/ol>\n<p>\u4f7f\u7528virt-manager\u6216\u8005<code>virsh<\/code>\u547d\u4ee4\u542f\u52a8\u865a\u62df\u673a\uff1a<\/p>\n<p><code>virsh start ubuntu-vm<\/code><br \/>\n\u8fd9\u4f1a\u542f\u52a8\u4f60\u7684\u865a\u62df\u673a\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>virt-manager<\/code>\u6216\u8005<code>virsh console<\/code>\u547d\u4ee4\u6765\u7ba1\u7406\u548c\u76d1\u89c6\u865a\u62df\u673a\u7684\u8fd0\u884c\u72b6\u6001\u3002<\/p>\n<p>\u4ee5\u4e0a\u662f\u5c06QCOW2\u955c\u50cf\u5bfc\u5165\u5230KVM\u5e76\u8fd0\u884c\u7684\u57fa\u672c\u6b65\u9aa4\u3002\u6839\u636e\u4f60\u7684\u9700\u6c42\u548c\u73af\u5883\uff0c\u53ef\u80fd\u9700\u8981\u505a\u4e00\u4e9b\u989d\u5916\u7684\u914d\u7f6e\u548c\u8c03\u6574\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u6302\u8f7d\u5c01\u88c5 1.\u5b89\u88c5\u4f9d\u8d56\u9879\uff1a \u786e\u4fdd\u4f60\u7684\u7cfb\u7edf\u4e0a\u5b89\u88c5\u4e86QEMU\u548c\u76f8\u5e94\u7684\u5de5\u5177\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5728Ubuntu\u4e0a\u5b89 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[18,19],"class_list":["post-33","post","type-post","status-publish","format-standard","hentry","category-linux","tag-kvm","tag-linux"],"_links":{"self":[{"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=\/wp\/v2\/posts\/33","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=33"}],"version-history":[{"count":0,"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=\/wp\/v2\/posts\/33\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=33"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=33"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=33"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}