Thanks for replying, rich.

here are some informations i gathered. the guest is linux (centos) and i find out the guest's file system is 'xfs' using `df -hT`:

```
$ df -hT
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs       8.0G  1003M  7.1G   13% /
devtmpfs                devtmpfs  484M     0  484M    0% /dev
tmpfs                   tmpfs     496M     0  496M    0% /dev/shm
tmpfs                   tmpfs     496M  6.8M  489M    2% /run
tmpfs                   tmpfs     496M     0  496M    0% /sys/fs/cgroup
/dev/vda1               xfs      1014M  132M  883M   14% /boot
tmpfs                   tmpfs     100M     0  100M    0% /run/user/0
```

and sgdisk version is 0.8.10:

```
$ sgdisk --version
GPT fdisk (sgdisk) version 0.8.10
```

also, i tried the command you gave. it came out the same output:

```
$ virt-rescue --ro -d 138093b9b33345c38e58efa014036bd8
><rescue> sgdisk /dev/sdb -i 1
Invalid partition data!
```

but strange things happened when i tried some other reports' suggestions:

https://listman.redhat.com/archives/libguestfs/2016-February/msg00145.html
https://listman.redhat.com/archives/libguestfs/2018-November/msg00026.html

i did these things:

1. set LIBGUESTFS_BACKEND to direct;
2. recompile the `libguestfs-1.40.2-10.el7.src.rpm` without the `XXXX-RHEL-7-Reject-use-of-libguestfs-winsupport-features-.patch `;
3. run the `virt-rescue` command again.

```
$ virt-rescue --ro -d 138093b9b33345c38e58efa014036bd8
><rescue> sgdisk /dev/sdb -i 1
Creating new GPT entries.
Partition #1 does not exist.
```

and the command works somehow. apparently, i still dont understand what happened indeed. and i have read about the official explanation of LIBGUESTFS_BACKEND, is this something to do with libvirt? is it a bug or something?

https://libguestfs.org/libguestfs-test-tool.1.html#trying-out-with-without-libvirt


------------------ Original ------------------
From:  "Richard W.M. Jones";<rjones@redhat.com>;
Send time: Thursday, Mar 18, 2021 7:12 PM
To: "Ç®·Ç·²"<b3ale693592@foxmail.com>;
Cc: "libguestfs"<libguestfs@redhat.com>;
Subject:  Re: [Libguestfs] error invalid partition data while using libguestfs-tools


On Thu, Mar 18, 2021 at 04:10:51PM +0800, Ç®·Ç·² wrote:
> I'm trying to upload a file to guest os by using libguestfs api. but a
> partition error occurs during the `virt-copy-in` command. it seems its some
> problems with the `sgdisk` command, but i have no idea how to fix this. is this
> some problems with the host os or the guest os? or maybe is the version of
> libguestfs? btw, my host os is centos 7.6 with libguestfs 1.40.
>
> ```
> $ virt-copy-in -d 138093b9b33345c38e58efa014036bd8 1.txt /root/
> libguestfs: error: inspect_os: sgdisk: Invalid partition data!
> $ cat /etc/redhat-release
> CentOS Linux release 7.6.1810 (Core)
> $ yum list installed | grep libguestfs
> libguestfs.x86_64                     1:1.40.2-10.el7            @base
> ...
> ```
>
> and after turning on LIBGUESTFS_DEBUG and LIBGUESTFS_TRACE flag, i found out
> the error was happening which calling `guestfs_inspect_os()` function. but i
> still dont know how to fix this. here are part of the output:
>
> ```
> ...
> calling: settle
> commandrvf: stdout=n stderr=y flags=0x0
> commandrvf: udevadm --debug settle
> calling: settle
> command: sfdisk '--print-id' '/dev/sdb' '1'
> [    2.400992]  sdb: sdb1 sdb2
> command: sfdisk returned 0
> command: sfdisk: stdout:
> 0
> commandrvf: stdout=n stderr=y flags=0x0
> commandrvf: udevadm --debug settle
> calling: settle
> commandrvf: stdout=n stderr=y flags=0x0
> commandrvf: udevadm --debug settle
> calling: settle
> command: fold-stdout-on-stderr sgdisk '/dev/sdb' '-i' '1'
> [    2.427088]  sdb: sdb1 sdb2
> command: sgdisk returned 2
> command: sgdisk: stderr:
> Invalid partition data!

The error comes from sgdisk running on the guest, so it is a bug in
sgdisk or maybe a problem with the guest filesystem itself.

The closest I can find is this report from a few years ago:

https://listman.redhat.com/archives/libguestfs/2018-May/msg00009.html

What is the guest?  And what version of sgdisk are you using?

You could also try something like:

  $ virt-rescue --ro -d 138093b9b33345c38e58efa014036bd8

  ><rescue> sgdisk /dev/sdb -i 1

Rich.

--
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
virt-builder quickly builds VMs from scratch
http://libguestfs.org/virt-builder.1.html