2

I copied a working raspbian buster installation from one SD card to another (raspberry pi 4). I did so by simply calling dd if=/dev/mmcblk0 of=raspbian bs=1M on my Notebook with the existing SD card plugged in. I then did the opposite to copy the image to the new SD card by doing dd if=raspbian of=/dev/mmcblk0 bs=1M. The system boots fine and expanding the root partition worked perfectly.

However, for some reason, I cannot run apt-get update when using the new SD card. This is what apt-get reports:

Get:1 http://archive.raspberrypi.org/debian buster InRelease [25.2 kB]
Get:2 http://raspbian.raspberrypi.org/raspbian buster InRelease [15.0 kB]               
Err:1 http://archive.raspberrypi.org/debian buster InRelease
  Unknown error executing apt-key
Err:2 http://raspbian.raspberrypi.org/raspbian buster InRelease
  Unknown error executing apt-key
Fetched 40.2 kB in 1s (49.8 kB/s)
Reading package lists... Done
W: An error occurred during the signature verification. The repository is not updated and the previous index files will be used. GPG error: http://archive.raspberrypi.org/debian buster InRelease: Unknown error executing apt-key
W: An error occurred during the signature verification. The repository is not updated and the previous index files will be used. GPG error: http://raspbian.raspberrypi.org/raspbian buster InRelease: Unknown error executing apt-key
W: Failed to fetch http://raspbian.raspberrypi.org/raspbian/dists/buster/InRelease  Unknown error executing apt-key
W: Failed to fetch http://archive.raspberrypi.org/debian/dists/buster/InRelease  Unknown error executing apt-key
W: Some index files failed to download. They have been ignored, or old ones used instead.

I tried lots of different things to resolve the issue, but google only shows potential solutions for corrupted gpg key databases. None of those worked and I have the feeling that it's something else.

Do I have to do anything special after creating a copy like that?

Update with info from comment:
apt-get works fine with the previous card. For testing, I copied the image onto the old card, which broke apt-get as well then. So I guess the image is broken in some way?

Update: As I broke the existing SD card with further testing, no amount of dd magic will fix the issue now, I'm afraid. So I need to find a way to fix the issue apt-get and apt-key are having. Resetting the sources did not help.

ifschleife
  • 129
  • 4
  • And if you put previous SD-Card, there is no problem with `apt-get`? – M. Rostami Jan 07 '20 at 22:29
  • apt-get works fine with the previous card. For testing I copied the image onto the old card, which broke apt-get as well then. So I guess the image is broken in some way? Any idea of how to fix this? I don't want to have to do a re-install. – ifschleife Jan 07 '20 at 22:31
  • I realized that the problem happened because of a mistake with cloning. Follow instructions by this link. (Linux part) https://thepihut.com/blogs/raspberry-pi-tutorials/17789160-backing-up-and-restoring-your-raspberry-pis-sd-card – M. Rostami Jan 07 '20 at 22:44

3 Answers3

1

I would clone the SD Card the same way as you did and all suggested tutorials on the answers here also suggest to do it this way. You copied the image back to the original SD Card and getting the error so I also would guess that the image isn't identical to the original SD Card contents. But the original is lost now :-(

With critical images I take some more effort to verify it. You can use

pc ~$ sudo cmp --print-bytes /dev/sda raspbian.img

But I prefer to create a checksum that you can archive with the image:

pc ~$ sudo sha256sum /dev/sda > raspbian.img.sha256
pc ~$ cat raspbian.img.sha256   # example
1e7d4d4c8a36a0a3a14140f6aac2b5b002196e313168aa58864bbcdea56ab17e  /dev/sda

Now take the image and in raspbian.img.sha256 modify /dev/sda to raspbian.img but don't touch the checksum. Verify the image

pc ~$ sha256sum -c raspbian.img.sha256

But as already said this is no longer possible for you because you destroyed the original image. You only can try to reinitialize the apt cache in the hope that will fix the copy. How to do it you can look at Raspberry Pi sudo apt-get update not working.

Ingo
  • 40,606
  • 15
  • 76
  • 189
  • Thanks for the tip on how to fix apt but I tried that myself and it did not help. The error remains the same after re-enabling the sources. – ifschleife Jan 12 '20 at 15:29
1

You shouldn't have copied the non-working image back to the old card for a test. Rather, you should have re-created the backup image from the working card again. There is a good chance that the error would have gone away.

For now, I would try to restore the GPG keys of Raspbian repositories:

sudo apt-key list
sudo apt-key remove XXXXXXX # Get the Raspbian key ID from the list
wget https://archive.raspbian.org/raspbian.public.key -O - | sudo apt-key add -

Either that will work, or you will get a more meaningful error message instead of "Unknown error executing apt-key".

As a last resort solution, see How do I bypass/ignore the gpg signature checks of apt? Note that while you may want to take the risk of installing unsigned packages one time, doing so on a regular basis may eventually get you hacked, so if you can't get apt to run normally, it's strongly advised to start from scratch.

Dmitry Grigoryev
  • 26,688
  • 4
  • 44
  • 133
  • 1
    Well, of course I shouldn't have copied the non-working image back. But I was (not so) young, lazy and out of disk space ;) Anyway, refreshing the key had no effect, bypassing the signature check also does not work. Everything results in the exact same error that I posted in my original question. I fear a re-install will be necessary :| – ifschleife Jan 13 '20 at 22:40
  • Do you mean that the `apt-key` commands succeed, but `apt-get` fails nevertheless? Are you able to install packages using `dpkg`? In any case, the complexity of recovering a failed `apt` installation is usually significant, so I would just reinstall the system if I were you. – Dmitry Grigoryev Jan 14 '20 at 08:09
  • Yes, that's the weird part. `apt-key` seems to behave fine. In fact the entire system works just fine. the only thing that I know that does not work is `apt-get update` and hence the system can't be updated. I have the feeling that the error about `apt-key` is a red herring and there is something else wrong with apt. But yeah, doing a fresh install seems like the only option. Serves me right for being lazy. – ifschleife Jan 14 '20 at 21:02
-1

The problem is with your procedure of cloning. Follow the instructions.

Assume the SD-Card mounted on /dev/sda. So, put the SD-Card to the system and fire this command to clone:

sudo dd if=/dev/sda of=/home/rpibackup.img  

Unmount the mounted partitions:

sudo umount /dev/sda1
sudo umount /dev/sda2  

Now, put another SD-Card to the system for writing the cloned image. Assume it mounted on /dev/sdb:

sudo dd bs=4M if=/home/rpibackup.img of=/dev/sdb  

Before ejecting the SD card, make sure the system has completed writing to it using the command:

sudo sync

Done.

M. Rostami
  • 4,235
  • 1
  • 15
  • 34
  • 2
    Are you suggesting to use dd on a mounted filesystem? That seems to be the only difference to what I did, or am I misunderstanding something? I indeed forgot to do the sync, so maybe that is the issue. – ifschleife Jan 08 '20 at 22:47
  • Yes. I hope it solve your problem. – M. Rostami Jan 08 '20 at 22:48
  • 2
    @ifschleife You can also append `conv=fsync` to the `dd` command. This will flush the write buffers authomaticaly by `dd`. – Ingo Jan 09 '20 at 08:26
  • 1
    @M.Rostami I asked because I think it's usually a bad idea to use dd on a mounted filesystem, is it not? I think dd even warns about trying that. Either way it's too late for me, as I don't have the original SD card anymore, so I need to find a way to fix the broken system. – ifschleife Jan 12 '20 at 15:32
  • @ifschleife **Did you tried these commands?** `sudo apt-get clean`, `sudo apt-get autoremove`, `sudo apt-get update`. – M. Rostami Jan 13 '20 at 08:03
  • Another suggestion is, `aptitude`. Might be affected. https://packages.debian.org/buster/aptitude – M. Rostami Jan 13 '20 at 08:06
  • @M.Rostami Yes I tried those commands, but they have no effect. And I don't use aptitude. – ifschleife Jan 13 '20 at 22:38
  • @ifschleife "*I don't use aptitude.*", what do you mean? Don't you like it? Or, don't you installed it before? – M. Rostami Jan 13 '20 at 22:49
  • @M.Rostami It's not installed and I don't remember ever having used it. – ifschleife Jan 14 '20 at 20:56
  • `apt-get` and `aptitude` are package manager which are the same if you wanna just install packages, lists, remove, etc. Try install it as a `.deb` file because you can't use it by `apt-get` command. Don't worry about package lists and repository. Just test it. – M. Rostami Jan 14 '20 at 21:06
  • Yes, manually installing packages works fine. I used `dpkg` to reinstall apt itself (with a manually downloaded apt package), didn't help though. The problem is in updating/syncing the repository. – ifschleife Jan 14 '20 at 21:55
  • I didn't mean that reinstall `apt-get` manually by `dpkg`! Try to install `aptitude` to check that "update" and "upgrade" works or not. `wget http://ftp.us.debian.org/debian/pool/main/a/aptitude/aptitude_0.8.11-7_armhf.deb`, then `dpkg -i aptitude_0.8.11-7_armhf.deb`, finally `sudo aptitude update`. – M. Rostami Jan 15 '20 at 07:47
  • I know you didn't mean that, I just wanted to point out that `dpkg` is working as I have used it successfully ;) The problem with installing `aptitude` manually like this is that the system is missing aptitude's dependencies, which also have dependencies and so on. it would take a while to download and install all of those. I will just reinstall the system now and copy over my configs. Thanks for the help. – ifschleife Jan 16 '20 at 22:47
  • @ifschleife My pleasure. If you are curious about that, I suggest you upload the image file to host and add the link to the question, I'll check that for you because I remember I had the same problem as you, unfortunately, because of the lack of time, I had to reinstall and then, I did. – M. Rostami Jan 16 '20 at 22:53