禁用 KVM 的 AppArmor

禁用 KVM 的 AppArmor

我正在嘗試使用以下腳本拍攝 KVM 來賓的外部快照:

DOMAIN=test-snapshots.programster.org
SNAPSHOT_NAME=snap3
STATE_FILE="/media/kvm/test-snapshots/mem-snap.qcow2"
DISK_FILE="/media/kvm/test-snapshots/disk-snap.qcow2"


sudo virsh snapshot-create-as \
--domain $DOMAIN $SNAPSHOT_NAME \
--diskspec vda,file=$DISK_FILE,snapshot=external \
--memspec file=$STATE_FILE,snapshot=external \
--atomic

不幸的是,每當執行時,它都會產生以下錯誤輸出

錯誤:內部錯誤:無法執行 QEMU 指令「交易」:無法開啟「/media/kvm/KVM-Command-Generator/vms/test-snapshots.programster.org.img」:無法開啟「/media/kvm」 /KVM -Command-Generator/vms/test-snapshots.programster.org.img':權限被拒絕:權限被拒絕

我讀到了這可以透過使用 aa-complain 來解決。我按照步驟取得虛擬機器的 ID,即5e1df6be-2cdd-8d7a-a45b-01097c7f44c6.

但是,當我運行時:

sudo aa-complain libvirt-5e1df6be-2cdd-8d7a-a45b-01097c7f44c6

我收到以下錯誤:

Can't find libvirt-5e1df6be-2cdd-8d7a-a45b-01097c7f44c6 in the system path list. If the name of the application
is correct, please run 'which libvirt-5e1df6be-2cdd-8d7a-a45b-01097c7f44c6' as a user with correct PATH
environment set up in order to find the fully-qualified path and
use the full path as parameter.

在嘗試執行快照時,我透過檢查具有以下條目的系統日誌來確保仔細檢查它仍然是一個 apparmor 問題。

Mar  2 02:58:22 kvm kernel: [542687.670005] audit: type=1400 audit(1456887502.702:140): apparmor="STATUS" operation="profile_replace" profile="unconfined" name="libvirt-5e1df6be-2cdd-8d7a-a45b-01097c7f44c6" pid=6824 comm="apparmor_parser"
Mar  2 02:58:22 kvm kernel: [542687.675951] audit: type=1400 audit(1456887502.706:141): apparmor="DENIED" operation="open" profile="libvirt-5e1df6be-2cdd-8d7a-a45b-01097c7f44c6" name="/media/kvm/KVM-Command-Generator/vms/test-snapshots.programster.org.img" pid=8107 comm="qemu-system-x86" requested_mask="r" denied_mask="r" fsuid=118 ouid=118
Mar  2 02:58:22 kvm kernel: [542687.675989] audit: type=1400 audit(1456887502.710:142): apparmor="DENIED" operation="open" profile="libvirt-5e1df6be-2cdd-8d7a-a45b-01097c7f44c6" name="/media/kvm/KVM-Command-Generator/vms/test-snapshots.programster.org.img" pid=8107 comm="qemu-system-x86" requested_mask="r" denied_mask="r" fsuid=118 ouid=118
Mar  2 02:58:22 kvm kernel: [542687.676034] audit: type=1400 audit(1456887502.710:143): apparmor="DENIED" operation="open" profile="libvirt-5e1df6be-2cdd-8d7a-a45b-01097c7f44c6" name="/media/kvm/KVM-Command-Generator/vms/test-snapshots.programster.org.img" pid=8107 comm="qemu-system-x86" requested_mask="r" denied_mask="r" fsuid=118 ouid=118
Mar  2 02:58:23 kvm kernel: [542687.969561] audit: type=1400 audit(1456887503.002:144): apparmor="STATUS" operation="profile_replace" profile="unconfined" name="libvirt-5e1df6be-2cdd-8d7a-a45b-01097c7f44c6" pid=6841 comm="apparmor_parser"

我需要做什麼才能允許 KVM 來賓的外部快照?這可能類似於調整/停用 apparmor,或者也許有更好的解決方案?

額外細節

作業系統:Ubuntu 14.04

輸出uname -a

Linux kvm.programster.org 4.2.0-30-generic #35~14.04.1-Ubuntu SMP Fri Feb 19 14:48:13 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

的輸出aa-status是:

apparmor module is loaded.
35 profiles are loaded.
34 profiles are in enforce mode.
   /sbin/dhclient
   /usr/bin/evince
   /usr/bin/evince-previewer
   /usr/bin/evince-previewer//sanitized_helper
   /usr/bin/evince-thumbnailer
   /usr/bin/evince-thumbnailer//sanitized_helper
   /usr/bin/evince//sanitized_helper
   /usr/lib/NetworkManager/nm-dhcp-client.action
   /usr/lib/connman/scripts/dhclient-script
   /usr/lib/cups/backend/cups-pdf
   /usr/lib/libvirt/virt-aa-helper
   /usr/lib/lightdm/lightdm-guest-session
   /usr/lib/lightdm/lightdm-guest-session//chromium
   /usr/lib/telepathy/mission-control-5
   /usr/lib/telepathy/telepathy-*
   /usr/lib/telepathy/telepathy-*//pxgsettings
   /usr/lib/telepathy/telepathy-*//sanitized_helper
   /usr/lib/telepathy/telepathy-ofono
   /usr/sbin/cups-browsed
   /usr/sbin/cupsd
   /usr/sbin/tcpdump
   libvirt-0146f0b4-3117-bfae-8142-7fd2680f0e02
   libvirt-1418991d-64ec-9a1f-f9b0-f4c95285c0fa
   libvirt-1ce386c6-c44a-054c-199a-0c44726fe973
   libvirt-271e5909-afe4-57e2-6013-587071919685
   libvirt-5e1df6be-2cdd-8d7a-a45b-01097c7f44c6
   libvirt-701ad939-e103-d41d-e7f1-71f368218604
   libvirt-8a05e9ca-1918-7ec7-37b7-48b7c7e03a6d
   libvirt-a0a8fa52-f59e-2d7f-06d3-7e5080369f1b
   libvirt-be861e30-baf5-9c34-75d6-0142e10cf000
   libvirt-bef3d687-5f3b-217f-29d7-795aaed8a865
   libvirt-c2c962ef-4864-fd32-37d0-3ec0fa773a30
   libvirt-e299551e-d503-7a47-5696-8f28f5c0754d
   libvirt-f8ed2b66-c957-d104-262b-ac3aa63b237f
1 profiles are in complain mode.
   /usr/sbin/libvirtd
17 processes have profiles defined.
16 processes are in enforce mode.
   /usr/lib/telepathy/mission-control-5 (3368) 
   /usr/sbin/cups-browsed (1179) 
   /usr/sbin/cupsd (18585) 
   /usr/sbin/cupsd (18588) 
   libvirt-0146f0b4-3117-bfae-8142-7fd2680f0e02 (2741) 
   libvirt-1418991d-64ec-9a1f-f9b0-f4c95285c0fa (2803) 
   libvirt-1ce386c6-c44a-054c-199a-0c44726fe973 (2867) 
   libvirt-271e5909-afe4-57e2-6013-587071919685 (2470) 
   libvirt-5e1df6be-2cdd-8d7a-a45b-01097c7f44c6 (8107) 
   libvirt-701ad939-e103-d41d-e7f1-71f368218604 (2564) 
   libvirt-8a05e9ca-1918-7ec7-37b7-48b7c7e03a6d (2666) 
   libvirt-a0a8fa52-f59e-2d7f-06d3-7e5080369f1b (2705) 
   libvirt-be861e30-baf5-9c34-75d6-0142e10cf000 (2607) 
   libvirt-bef3d687-5f3b-217f-29d7-795aaed8a865 (2834) 
   libvirt-c2c962ef-4864-fd32-37d0-3ec0fa773a30 (2095) 
   libvirt-f8ed2b66-c957-d104-262b-ac3aa63b237f (2772) 
1 processes are in complain mode.
   /usr/sbin/libvirtd (1562) 
0 processes are unconfined but have a profile defined.

答案1

libvirt 來賓的 apparmor 設定檔是動態建立的。如果要將額外的文件新增至允許清單中,請將所需的文件新增至下列文件的末端:

/etc/apparmor.d/abstractions/libvirt-qemu

  /var/lib/libvirt/images/*.ign r,

答案2

查看所有資訊後,看來您走在正確的道路上。問題是aa-投訴命令需要完整路徑。

解決方案是執行以下命令來指定虛擬機器的完整路徑:

sudo aa-complain /etc/apparmor.d/libvirt/libvirt-5e1df6be-2cdd-8d7a-a45b-01097c7f44c6

答案3

看來在你的情況下,AppArmor 對 libvirtd 的限制太多了。如果您有個人資料/etc/apparmor.d/usr.sbin.libvirtd,您可以停用透過以下方式:

sudo ln -s /etc/apparmor.d/usr.sbin.libvirtd /etc/apparmor.d/disable/usr.sbin.libvirtd

您需要重新啟動才能生效。

答案4

在工作的 kvm 主機從stretch升級到buster之後,我就發生了這種情況。圖像檔案本身是可讀的,投訴是關於 qcow2-backing 文件的。我將“檔案”新增至 /etc/apparmor.d/libvirt/TEMPLATE.qemu (就像在 lxc 範本中一樣)。之後一切似乎都進展順利。

相關內容