我们有时希望传输文件到qemu里,那如何做了?官方给的答复:

Simple: Treat the virtual machine as if it was a real machine and
transfer files to and from it.

More to the point, the following are the most common methods:

A) If the virtual machine runs Windows, OS/2 or Linux, create a Samba
network share on the Linux host (or anywhere else), put files on it,
and access the share over the network from the virtual machine. Some
qemu versions include options to automate this.

B) On the Linux host create a disk image of a floppy or CD with the
desired files, then use qemu commands to virtually insert the disk
image file in the virtual floppy or CD drive. Again there are tools
to automate this.

C) Run a file transfer tool such as rsync, ssh, ftp etc. on the virtual
machine and the Linux host and use it to transfer files.

D) With the virtual machine not running, loop mount its virtual hard
drive on the Linux host, and copy files to and from it.

我们来看下比较简单的D)方法,可以理解为移动硬盘(-hda),具体操作如下:

Step1: 创建磁盘镜像

这里是256MB disk:

dd if=/dev/zero of=disk.img bs=1M count=256

Step2: 格式化磁盘

这里我们用ext4格式:

mkfs.ext4 disk.img

Step3: 挂载磁盘

mkdir disk_mnt //建个挂载点
sudo mount disk.img disk_mnt/

看下内容ls -l disk_mnt/是空的:

total 12
drwx------ 2 root root 12288 11月 6 11:57 lost+found

ok, cp你想cp的东东。。。然后用sudo umount disk_mnt卸载。接下来我们就挂到qemu上。

Step4: 挂载磁盘到qemu

就是在末尾加上-hda disk.img, 进入后能看到如下磁盘信息:

[root@x86 ]# ls -l /dev/sda 
brw-rw---- 1 0 0 8, 0 Nov 6 04:14 /dev/sda
[root@x86 ]# dmesg | grep sda
[ 2.471753] sd 0:0:0:0: [sda] 524288 512-byte logical blocks: (268 MB/256 MiB)
[ 2.484847] sd 0:0:0:0: [sda] Write Protect is off
[ 2.486421] sd 0:0:0:0: [sda] Mode Sense: 00 3a 00 00
[ 2.488286] sd 0:0:0:0: [sda] Write cache: enabled, read cache: enabled, doesn't support DPO or FUA
[ 2.548810] sd 0:0:0:0: [sda] Attached SCSI disk

ok, now 我们把这个硬盘挂上去。

[root@x86 ]# mount -t ext4 /dev/sda mnt/

then, just use it.

Step5: 自动挂载

上面的退出qemu后就丢失了,因为fstab没有修改,我们回到之前制作rootfs那里,rootfs/ext4/fstab里添加:

/dev/sda        /mnt            ext4    defaults                0       0

then重新制作ramdisk.gz即可。

refer

https://blog.csdn.net/geshifei/article/details/119869405