Raspbian 中的 apt 问题,其中 /var 内容已被删除或损坏

Raspbian 中的 apt 问题,其中 /var 内容已被删除或损坏

显然,在生产服务器上出现了一些问题,有人删除了该/var文件夹的内容。

这导致了一些错误,我一直在使用各种服务作为 Web 服务器来解决这些错误。

我遇到的问题apt是不允许我更新、删除或安装软件包。

例如我想重新安装数据库服务器mariadb-server,但它告诉我系统中没有安装该软件包(这是错误的,我亲自安装了它):

root# apt remove mariadb-server
Reading package lists... Done
Building dependency tree
Reading state information... Done
Package 'mariadb-server' is not installed, so not removed
You might want to run 'apt-get -f install' to correct these:
The following packages have unmet dependencies:
 chkconfig : Depends: perl but it is not going to be installed
             Recommends: insserv but it is not going to be installed
 libboost-chrono1.50.0 : Depends: libgcc1 (>= 1:4.4.0) but it is not going to be installed
                         Depends: libstdc++6 (>= 4.4.0) but it is not going to be installed
 libboost-system1.50.0 : Depends: libgcc1 (>= 1:4.4.0) but it is not going to be installed
                         Depends: libstdc++6 (>= 4.4.0) but it is not going to be installed
 libboost-thread1.50.0 : Depends: libgcc1 (>= 1:4.4.0) but it is not going to be installed
                         Depends: libstdc++6 (>= 4.6) but it is not going to be installed
 libc6 : Depends: libgcc1 but it is not going to be installed
 libprotobuf-lite7 : Depends: libgcc1 (>= 1:4.4.0) but it is not going to be installed
                     Depends: libstdc++6 (>= 4.4.0) but it is not going to be installed
                     Depends: zlib1g (>= 1:1.1.4) but it is not going to be installed
 oracle-java8-jdk : Depends: libasound2 (>= 1.0.16)
                    Depends: libgcc1 (>= 1:4.4.0) but it is not going to be installed
                    Depends: libx11-6 but it is not going to be installed
                    Depends: libxext6 but it is not going to be installed
                    Depends: libxi6 but it is not going to be installed
                    Depends: libxrender1 but it is not going to be installed
                    Depends: libxtst6 but it is not going to be installed
                    Recommends: netbase but it is not going to be installed
 redis-server : Depends: init-system-helpers (>= 1.18~) but it is not going to be installed
                Depends: libjemalloc1 (>= 2.1.1) but it is not going to be installed
                Depends: adduser but it is not going to be installed
 redis-tools : Depends: libjemalloc1 (>= 2.1.1) but it is not going to be installed
 watchdog : Depends: debconf (>= 0.5) but it is not going to be installed or
                     debconf-2.0
            Depends: init-system-helpers (>= 1.18~) but it is not going to be installed
            Depends: makedev (>= 2.3.1-24) but it is not going to be installed or
                     udev but it is not going to be installed
            Depends: lsb-base (>= 3.2-14) but it is not going to be installed
E: Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution).

如果我启动告诉我解决未满足的依赖关系的命令:

root# apt-get -f install
Reading package lists... Done
Building dependency tree
Reading state information... Done
Correcting dependencies... Done
The following extra packages will be installed:
  adduser apt apt-utils base-passwd ca-certificates coreutils debconf debconf-i18n debianutils dpkg e2fslibs e2fsprogs gnupg gnupg-curl gpgv ifupdown init-system-helpers initscripts insserv iproute2 isc-dhcp-client isc-dhcp-common
  krb5-locales libacl1 libalgorithm-c3-perl libapt-inst1.5 libapt-pkg4.12 libarchive-extract-perl libasound2 libasound2-data libatm1 libattr1 libaudit-common libaudit1 libblkid1 libbz2-1.0 libcgi-fast-perl libcgi-pm-perl
  libclass-c3-perl libclass-c3-xs-perl libcomerr2 libcpan-meta-perl libcurl3-gnutls libdata-optlist-perl libdata-section-perl libdb5.3 libdebconfclient0 libdns-export100 libfcgi-perl libffi6 libgcc1 libgcrypt20 libgdbm3 libgmp10
  libgnutls-deb0-28 libgpg-error0 libgpm2 libgssapi-krb5-2 libhogweed2 libidn11 libirs-export91 libisc-export95 libisccfg-export90 libjemalloc1 libk5crypto3 libkeyutils1 libkrb5-3 libkrb5support0 libldap-2.4-2 liblocale-gettext-perl
  liblog-message-perl liblog-message-simple-perl liblzma5 libmodule-build-perl libmodule-pluggable-perl libmodule-signature-perl libmount1 libmro-compat-perl libncurses5 libnettle4 libp11-kit0 libpackage-constants-perl libpam-modules
  libpam-modules-bin libpam0g libparams-util-perl libpcre3 libpng12-0 libpod-latex-perl libpod-readme-perl libreadline6 libregexp-common-perl librtmp1 libsasl2-2 libsasl2-modules libsasl2-modules-db libselinux1 libsemanage-common
  libsemanage1 libsepol1 libslang2 libsmartcols1 libsoftware-license-perl libss2 libssh2-1 libssl1.0.0 libstdc++6 libsub-exporter-perl libsub-install-perl libsystemd0 libtasn1-6 libterm-ui-perl libtext-charwidth-perl libtext-iconv-perl
  libtext-soundex-perl libtext-template-perl libtext-wrapi18n-perl libtinfo5 libusb-0.1-4 libustr-1.0-1 libuuid1 libx11-6 libx11-data libxau6 libxcb1 libxdmcp6 libxext6 libxi6 libxrender1 libxtables10 libxtst6 lsb-base makedev netbase
  openssl passwd perl perl-base perl-modules psmisc raspbian-archive-keyring readline-common redis-server redis-tools rename sensible-utils startpar sysv-rc sysvinit-utils tar tzdata util-linux uuid-runtime x11-common zlib1g
Suggested packages:
  aptitude synaptic wajig dpkg-dev apt-doc python-apt debconf-doc debconf-utils whiptail dialog gnome-utils libterm-readline-gnu-perl libgtk2-perl libnet-ldap-perl libqtgui4-perl libqtcore4-perl gpart parted fuse2fs e2fsck-static
  gnupg-doc libpcsclite1 parcimonie xloadimage imagemagick eog ppp rdnssd net-tools bootchart2 iproute2-doc resolvconf avahi-autoipd libasound2-plugins alsa-utils rng-tools gnutls-bin gpm krb5-doc krb5-user libpam-doc
  libsasl2-modules-otp libsasl2-modules-ldap libsasl2-modules-sql libsasl2-modules-gssapi-mit libsasl2-modules-gssapi-heimdal perl-doc make libb-lint-perl libcpanplus-dist-build-perl libcpanplus-perl libfile-checktree-perl
  libobject-accessor-perl readline-doc bum bootlogd sash bzip2 ncompress xz-utils tar-scripts dosfstools kbd console-tools util-linux-locales
Recommended packages:
  libarchive-tar-perl
The following NEW packages will be installed:
  adduser apt apt-utils base-passwd ca-certificates coreutils debconf debconf-i18n debianutils dpkg e2fslibs e2fsprogs gnupg gnupg-curl gpgv ifupdown init-system-helpers initscripts insserv iproute2 isc-dhcp-client isc-dhcp-common
  krb5-locales libacl1 libalgorithm-c3-perl libapt-inst1.5 libapt-pkg4.12 libarchive-extract-perl libasound2 libasound2-data libatm1 libattr1 libaudit-common libaudit1 libblkid1 libbz2-1.0 libcgi-fast-perl libcgi-pm-perl
  libclass-c3-perl libclass-c3-xs-perl libcomerr2 libcpan-meta-perl libcurl3-gnutls libdata-optlist-perl libdata-section-perl libdb5.3 libdebconfclient0 libdns-export100 libfcgi-perl libffi6 libgcc1 libgcrypt20 libgdbm3 libgmp10
  libgnutls-deb0-28 libgpg-error0 libgpm2 libgssapi-krb5-2 libhogweed2 libidn11 libirs-export91 libisc-export95 libisccfg-export90 libjemalloc1 libk5crypto3 libkeyutils1 libkrb5-3 libkrb5support0 libldap-2.4-2 liblocale-gettext-perl
  liblog-message-perl liblog-message-simple-perl liblzma5 libmodule-build-perl libmodule-pluggable-perl libmodule-signature-perl libmount1 libmro-compat-perl libncurses5 libnettle4 libp11-kit0 libpackage-constants-perl libpam-modules
  libpam-modules-bin libpam0g libparams-util-perl libpcre3 libpng12-0 libpod-latex-perl libpod-readme-perl libreadline6 libregexp-common-perl librtmp1 libsasl2-2 libsasl2-modules libsasl2-modules-db libselinux1 libsemanage-common
  libsemanage1 libsepol1 libslang2 libsmartcols1 libsoftware-license-perl libss2 libssh2-1 libssl1.0.0 libstdc++6 libsub-exporter-perl libsub-install-perl libsystemd0 libtasn1-6 libterm-ui-perl libtext-charwidth-perl libtext-iconv-perl
  libtext-soundex-perl libtext-template-perl libtext-wrapi18n-perl libtinfo5 libusb-0.1-4 libustr-1.0-1 libuuid1 libx11-6 libx11-data libxau6 libxcb1 libxdmcp6 libxext6 libxi6 libxrender1 libxtables10 libxtst6 lsb-base makedev netbase
  openssl passwd perl perl-base perl-modules psmisc raspbian-archive-keyring readline-common rename sensible-utils startpar sysv-rc sysvinit-utils tar tzdata util-linux uuid-runtime x11-common zlib1g
The following packages will be upgraded:
  redis-server redis-tools
2 upgraded, 153 newly installed, 0 to remove and 3 not upgraded.
11 not fully installed or removed.
Need to get 37.1 MB of archives.
After this operation, 120 MB of additional disk space will be used.
Do you want to continue? [Y/n]
Get:1 http://archive.raspberrypi.org/debian/ jessie/main libasound2-data all 1.0.28-1+rpi3 [65.3 kB]
Get:2 http://archive.raspberrypi.org/debian/ jessie/main libasound2 armhf 1.0.28-1+rpi3 [320 kB]
Get:3 http://mirrordirector.raspbian.org/raspbian/ jessie/main libgcc1 armhf 1:4.9.2-10+deb8u2 [39.5 kB]
Get:4 http://archive.raspberrypi.org/debian/ jessie/main x11-common all 1:7.7+16 [251 kB]
[...]
Get:152 http://mirrordirector.raspbian.org/raspbian/ jessie/main libterm-ui-perl all 0.42-1 [19.1 kB]
Get:153 http://mirrordirector.raspbian.org/raspbian/ jessie/main libtext-soundex-perl armhf 3.4-1+b2 [13.3 kB]
Get:154 http://mirrordirector.raspbian.org/raspbian/ jessie/main psmisc armhf 22.21-2 [117 kB]
Get:155 http://mirrordirector.raspbian.org/raspbian/ jessie/main rename all 0.20-3 [12.4 kB]
Fetched 37.1 MB in 2min 29s (249 kB/s)
Reading changelogs... Done
E: Cannot get debconf version. Is debconf installed?
debconf: apt-extracttemplates failed: No such file or directory
Extracting templates from packages: 19%E: Cannot get debconf version. Is debconf installed?
debconf: apt-extracttemplates failed: No such file or directory
Extracting templates from packages: 38%E: Cannot get debconf version. Is debconf installed?
debconf: apt-extracttemplates failed: No such file or directory
Extracting templates from packages: 58%E: Cannot get debconf version. Is debconf installed?
debconf: apt-extracttemplates failed: No such file or directory
Extracting templates from packages: 77%E: Cannot get debconf version. Is debconf installed?
debconf: apt-extracttemplates failed: No such file or directory
Extracting templates from packages: 96%E: Cannot get debconf version. Is debconf installed?
debconf: apt-extracttemplates failed: No such file or directory
Extracting templates from packages: 100%
dpkg: regarding .../libgcc1_1%3a4.9.2-10+deb8u2_armhf.deb containing libgcc1:armhf, pre-dependency problem:
 libgcc1 pre-depends on multiarch-support
  multiarch-support is unpacked, but has never been configured.

dpkg: error processing archive /var/cache/apt/archives/libgcc1_1%3a4.9.2-10+deb8u2_armhf.deb (--unpack):
 pre-dependency problem - not installing libgcc1:armhf
Errors were encountered while processing:
 /var/cache/apt/archives/libgcc1_1%3a4.9.2-10+deb8u2_armhf.deb

E: Sub-process /usr/bin/dpkg returned an error code (1)

这是mariadb-server已安装的证明(错误是由于删除了/var,所以我想重新安装它):

root# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111 "Connection refused")

正如您所看到的,两者apt都会mariadb-server向我抛出目录中缺少内容的错误/var。所以我进入了一个循环,我不知道如何继续解决这个问题。

答案1

我将参考以下内容文章Debian 维基帖子对于我的回答。

首先,在工作的 Raspbian 系统上创建一个debootstrap环境。按照来自的指示Debian 维基您只需要完成以下内容:

mkdir /debootstrap
debootstrap stable /debootstrap http://deb.debian.org/debian/

现在,您正在使用 Raspbian,因此您可能需要添加正确的 gpg-key,正如这里指出的,并参考 Raspbian Deb Mirror。像这样的东西:http://archive.raspbian.org/raspbian。我将包括一条链接跨平台构建指南说明您是在 amd64 系统而不是 Raspberry Pi 上执行此操作。

重要提示:如果出现提示,请勿安装grub到 MBR!这可能会破坏您现有的安装!

接下来,在损坏的系统上获取所有已安装程序的列表。最初我建议使用dpkg --get-selections,但是正如用户 AB 指出的那样,这会失败。dpkg引用各种/var目录来工作,如果您/var丢失了,这当然会失败。用户AB指出您可以引用 的内容/usr/share/doc/和相关符号链接。我将首先查看可用的内容,然后尝试installed.txt根据您的结果构建一个。

cd /usr/share/doc && for i in *; do echo $i install >> /home/user/installed.txt; done

将其结果installed.txt与您的预期进行比较。另一个要检查的地方是/usr/share/man.该目录包含联机帮助页,但仅适用于安装联机帮助页的软件包。如果您有一个已知的可用 Raspberry Pi 来进行比较,它的配置与损坏的 Raspberry Pi 相同,您可以installed.txt使用 Raspberry Pi 的输出进行构建dpkg --get-selections

installed.txt从丢失的主机复制/var到修复/var主机。scprysnc将在这里工作,但此时您确实需要确认ssh损坏的主机和进行修复的主机之间有一个工作配置。如果这两台主机之间没有有效ssh配置,您将无法复制/var.在继续之前,任何ssh问题都需要解决。

现在,将此文件移至/deboostrap环境中并准备在此 chroot 内安装所有相同的软件。

cp installed.txt /debootstrap/
chroot /bootstrap/
mount -t proc none /proc
dpkg --set-selections < installed.txt

如果需要,请确保您的sources.list匹配。debootstrap默认情况下仅包括main.

apt update
apt install dselect
dselect update
apt-get dselect-upgrade

如果你无法建立一个好的installed.txt

您可能需要简单地在debootstrapchroot 中手动安装所有必需的软件包。这是良好的设置文档或基础设施即代码派上用场的地方。apt install package1 package2 package3 ... packageN在 chroot 中运行以尽可能/var接近原始版本/vardselect您应该按照以下步骤操作,而不是安装:

chroot /bootstrap/
mount -t proc none /proc
apt update
apt install package1 package2 package3 ... packageN

此时,您的debootstrap环境应该与目标、损坏的主机的环境相匹配。这里就是需要用到rsync复制/var过来的地方。rsync -A应该足够了。正如我所指出的关联,您可能需要允许PermitRootLogin without-password您的sshd_configforrsync工作。

我还将直接引用 Alephnull.uk 的 Pete Donnell 必须运行的最后步骤和注意事项:

...我必须mariadb-server使用 . 在损坏的服务器上重新安装软件包apt-get install --reinstall mariadb-server mariadb-server-10.1 mariadb-server-core-10.1

下一步是恢复您的用户数据。这将特定于您使用的结构,因此我无法提供相关说明。完成此操作后,请在工作服务器(或者可能是您的引导程序)中检查文件的权限,/var/lib/var/log检查它们是否正确。服务器和引导环境之间的系统帐户的用户 ID 很可能会有所不同。

现在您应该使用 来检查已安装的各种服务service --status-all。并非所有服务都必须启用,我再次建议与工作服务器进行比较。尝试重新启动所有当前未运行但应该运行的服务。如果所有权限均正确并且相关用户数据(如果有)已恢复,则服务应成功启动。如果没有,请使用 s 检查 systemd 状态ystemctl status <service-name>,使用 s 检查 systemd 日志,journalctl -xe并使用 来检查服务的日志文件/var/log。这些应该为您提供足够的信息来追踪任何剩余的问题。

debootstrap如果您有幸拥有第二台工作主机,我强烈建议您花时间比较环境和损坏的主机。您需要确认所有权限均正确且服务运行正常。只有完成此操作后,我才会重新启动主机并确认一切都恢复了。

在尝试任何命令之前,请仔细阅读我提供的每个链接。如果我的帖子有任何错误、误解或问题,请告诉我,我将努力纠正它们。

祝你好运!

相关内容