Temporary snapshot in qemu (-snapshot)

Posted by mattkirkey on Fri, 13 Sep 2019 19:34:58 +0200

Original Link: https://my.oschina.net/u/1587810/blog/266447

QEMU supports a temporary snapshot, which makes it easy to experiment with without leaving any traces of use, similar to the Burn After Use mode.

[root@pc qemu]# qemu-system-x86_64 -h|grep "\-snapshot" -A3 -B3 --color
-mtdblock file  use 'file' as on-board Flash memory image
-sd file        use 'file' as SecureDigital card image
-pflash file    use 'file' as a parallel flash image
-snapshot       write to temporary files instead of disk image files
-hdachs c,h,s[,t]
                force hard disk 0 physical geometry and the optional BIOS
                translation (t=none or lba) (usually QEMU can guess them)


With this mode, a temporary file is generated to record write operations to the disk, but the original disk does not actually perform read and write operations.Temporary files are automatically deleted when the virtual machine is running.In this way, we can install a clean system on the original image and start it with qemu plus-snapshot. After the experiment is finished, we don't need to clean the temporary files manually to really shake the palm of your hand.

 

1. Start the operating system

[root@pc images]# qemu  -drive if=virtio,file=centos65_base.img -snapshot --enable-kvm --vnc :8 -m 2048


2. View files opened by qemu

[root@pc ~]# ps -ef|grep qemu|grep snapshot|grep -v grep
root     20890 20129 11 17:29 pts/0    00:00:19 qemu -drive if=virtio,file=centos65_base.img -snapshot --enable-kvm --vnc :8 -m 2048
[root@pc ~]# lsof |grep 20890
qemu      20890      root  cwd       DIR              253,2       4096   21889025 /home/images
qemu      20890      root  rtd       DIR              253,0       4096          2 /
qemu      20890      root  txt       REG              253,0   15198134     941595 /usr/local/bin/qemu-system-x86_64
qemu      20890      root  mem       REG              253,0      13168     921666 /usr/lib64/libXau.so.6.0.0
qemu      20890      root  mem       REG              253,0     124728     921667 /usr/lib64/libxcb.so.1.1.0
qemu      20890      root  mem       REG              253,0      22536    3014680 /lib64/libdl-2.12.so
qemu      20890      root  mem       REG              253,0    1926800    3014662 /lib64/libc-2.12.so
qemu      20890      root  mem       REG              253,0      93320    3014696 /lib64/libgcc_s-4.4.7-20120601.so.1
qemu      20890      root  mem       REG              253,0     599384    3014694 /lib64/libm-2.12.so
qemu      20890      root  mem       REG              253,0     989840     919245 /usr/lib64/libstdc++.so.6.0.13
qemu      20890      root  mem       REG              253,0     560200     921662 /usr/lib64/libpixman-1.so.0.26.2
qemu      20890      root  mem       REG              253,0    1300376     921668 /usr/lib64/libX11.so.6.3.0
qemu      20890      root  mem       REG              253,0     145896    3014663 /lib64/libpthread-2.12.so
qemu      20890      root  mem       REG              253,0     451248     939790 /usr/lib64/libSDL-1.2.so.0.11.3
qemu      20890      root  mem       REG              253,0      17520    3014795 /lib64/libutil-2.12.so
qemu      20890      root  mem       REG              253,0    1068832    3014674 /lib64/libglib-2.0.so.0.2600.1
qemu      20890      root  mem       REG              253,0      20016    3014676 /lib64/libgthread-2.0.so.0.2600.1
qemu      20890      root  mem       REG              253,0      47064    3014670 /lib64/librt-2.12.so
qemu      20890      root  mem       REG              253,0      91096    3014686 /lib64/libz.so.1.2.3
qemu      20890      root  mem       REG              253,0     156928    3014658 /lib64/ld-2.12.so
qemu      20890      root  mem       REG                0,9                  3822 kvm-vcpu (stat: No such file or directory)
qemu      20890      root  DEL       REG                0,4               1210230 /dev/zero
qemu      20890      root  DEL       REG                0,4               1210229 /dev/zero
qemu      20890      root  DEL       REG                0,4               1210218 /dev/zero
qemu      20890      root    0u      CHR              136,0        0t0          3 /dev/pts/0
qemu      20890      root    1u      CHR              136,0        0t0          3 /dev/pts/0
qemu      20890      root    2u      CHR              136,0        0t0          3 /dev/pts/0
qemu      20890      root    3u      REG                0,9          0       3822 [signalfd]
qemu      20890      root    4u      REG                0,9          0       3822 [eventfd]
qemu      20890      root    5r     FIFO                0,8        0t0    1210217 pipe
qemu      20890      root    6w     FIFO                0,8        0t0    1210217 pipe
qemu      20890      root    7u      CHR             10,232        0t0      12550 /dev/kvm
qemu      20890      root    8u      REG                0,9          0       3822 kvm-vm
qemu      20890      root    9r      REG              253,2 5298323456   21889027 /home/images/centos65_base.img
qemu      20890      root   10u      REG                0,9          0       3822 [eventfd]
qemu      20890      root   11u      REG              253,0    8912896    1310990 /var/tmp/vl.xFEzq8 (deleted)
qemu      20890      root   12u      REG                0,9          0       3822 kvm-vcpu
qemu      20890      root   13u     IPv4            1210233        0t0        TCP *:5908 (LISTEN)
qemu      20890      root   14u      REG                0,9          0       3822 [eventfd]
qemu      20890      root   15u     IPv4            1210264        0t0        TCP 10.21.140.44:5908->10.21.140.32:61812 (ESTABLISHED)
qemu      20890      root   16u     IPv4            1210476        0t0        TCP 10.21.140.44:58508->124.40.52.80:http (SYN_SENT)

 

3. Notice/var/tmp/vl.xFEzq8, shown as deleted

 

[root@pc ~]# ls /var/tmp/vl.xFEzq8
ls: cannot access /var/tmp/vl.xFEzq8: No such file or directory


4. Look at the space

[root@pc ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_pc-lv_root
                       50G  4.2G   43G   9% /
tmpfs                 3.9G  236K  3.9G   1% /dev/shm
/dev/sda2             485M   68M  393M  15% /boot
/dev/sda1             200M  260K  200M   1% /boot/efi
/dev/mapper/vg_pc-lv_home
                      860G   13G  803G   2% /home


5. Write 5G in Guest,

dd if=/dev/zero of=test bs=1M size=5120

6. Look at the space again

[root@pc ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_pc-lv_root
                       50G  9.2G   43G  20% /
tmpfs                 3.9G  236K  3.9G   1% /dev/shm
/dev/sda2             485M   68M  393M  15% /boot
/dev/sda1             200M  260K  200M   1% /boot/efi
/dev/mapper/vg_pc-lv_home
                      860G   13G  803G   2% /home

Since host does not mount / var partition separately, it will actually be placed under / var/tmp


7. Turn the virtual machine on and see the space again

[root@pc ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_pc-lv_root
                       50G  4.2G   43G   9% /
tmpfs                 3.9G  236K  3.9G   1% /dev/shm
/dev/sda2             485M   68M  393M  15% /boot
/dev/sda1             200M  260K  200M   1% /boot/efi
/dev/mapper/vg_pc-lv_home
                      860G   13G  803G   2% /home

At this time, it was found that the space was completely released after shutting down.

8. Inside block.c:bdrv_open_common, it is found that he was actually unlink at open time, unlink was deleted from the file system but not from the disk file.This is a security consideration for qemu.

#ifndef _WIN32
    if (bs->is_temporary) {
        assert(bs->filename[0] != '\0');
        unlink(bs->filename);
    }
#endif

9. Temporary files are actually placed inside / tmp/var

int get_tmp_filename(char *filename, int size)
{
#ifdef _WIN32
    char temp_dir[MAX_PATH];
    /* GetTempFileName requires that its output buffer (4th param)
       have length MAX_PATH or greater.  */
    assert(size >= MAX_PATH);
    return (GetTempPath(MAX_PATH, temp_dir)
            && GetTempFileName(temp_dir, "qem", 0, filename)
            ? 0 : -GetLastError());
#else
    int fd;
    const char *tmpdir;
    tmpdir = getenv("TMPDIR");
    if (!tmpdir) {
        tmpdir = "/var/tmp";
    }
    if (snprintf(filename, size, "%s/vl.XXXXXX", tmpdir) >= size) {
        return -EOVERFLOW;
    }
    fd = mkstemp(filename);
    if (fd < 0) {
        return -errno;
    }
    if (close(fd) != 0) {
        unlink(filename);
        return -errno;
    }
    return 0;
#endif
}

The temporary snapshot of qemu has now been completed~


Reprinted at: https://my.oschina.net/u/1587810/blog/266447

Topics: snapshot vnc lsof