github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/data/systemd/snapd.core-fixup.sh (about)

     1  #!/bin/sh
     2  
     3  set -e
     4  
     5  if ! grep -q "ID=ubuntu-core" /etc/os-release; then
     6      # this code is only relevant on ubuntu-core devices
     7      #
     8      # this script will only run via systemd if /writable/system-data
     9      # exists however we still add this check here in case people run
    10      # it manually
    11      exit 0
    12  fi
    13  
    14  # No fix-ups yet on UC20
    15  if grep -q snapd_recovery_mode= /proc/cmdline; then
    16      exit 0
    17  fi
    18  
    19  # Workaround https://forum.snapcraft.io/t/5253
    20  #
    21  # We see sometimes corrupted uboot.env files created by fsck.vfat.
    22  # On the fat filesystem they are indistinguishable because one
    23  # has a fat16 name UBOOT.ENV (and not lfn (long-file-name)) but
    24  # the other has a "uboot.env" lfn name and a FSCK0000.000 FAT16
    25  # name. The only known workaround is to remove all dupes and put
    26  # one file back in place.
    27  if [ "$(find /boot/uboot -name uboot.env | wc -l)" -gt 1 ]; then
    28      echo "Corrupted uboot.env file detected"
    29      # Ensure we have one uboot.env to go back to. Note that it does
    30      # not matter which one we pick (we can't choose anyway, we get
    31      # whatever the kernel gives us). The key part is that there is
    32      # only a single one after this script finishes. The bootloader
    33      # logic will recover in any case.
    34      cp -a /boot/uboot/uboot.env /boot/uboot/uboot.env.save
    35      # now delete all dupes
    36      while ls /boot/uboot/uboot.env 2>/dev/null; do
    37          rm -f /boot/uboot/uboot.env
    38      done
    39      # and move the saved one into place
    40      mv /boot/uboot/uboot.env.save /boot/uboot/uboot.env
    41  
    42      # ensure we sync the fs
    43      sync
    44  fi
    45  
    46  
    47  # The code below deals with incorrect permissions that happened on
    48  # some buggy ubuntu-image versions.
    49  #
    50  # This needs to run only once so we can exit here.
    51  if [ -f /var/lib/snapd/device/ownership-change.after ]; then
    52      exit 0
    53  fi
    54  
    55  # store important data in case we need it later
    56  if [ ! -f /var/lib/snapd/device/ownership-change.before ]; then
    57      mkdir -p /var/lib/snapd/device
    58      find /etc/cloud /var/lib/cloud /var/lib/snapd -printf '%M %U %G %p\n' > /var/lib/snapd/device/ownership-change.before.tmp || true
    59      find  /writable/system-data /writable/system-data/var /writable/system-data/var/lib /writable/system-data/boot /writable/system-data/etc -maxdepth 0 -printf '%M %U %G %p\n' >> /var/lib/snapd/device/ownership-change.before.tmp || true
    60      mv /var/lib/snapd/device/ownership-change.before.tmp /var/lib/snapd/device/ownership-change.before
    61  fi
    62      
    63  # cleanup read/write files and directories (CVE-2017-10600)
    64  for i in /etc/cloud /var/lib/cloud /var/lib/snapd ; do
    65    # restore ownership to root:root
    66    find "$i" \( -type f -o -type d -o -type l \) -a \( \! -uid 0 -o \! -gid 0 \) -print0 | \
    67      xargs -0 --no-run-if-empty chown -c --no-dereference root:root -- || true
    68  done
    69  
    70  # cleanup a few /writable directories without descending
    71  for i in /writable/system-data /writable/system-data/var /writable/system-data/var/lib /writable/system-data/boot /writable/system-data/etc ; do
    72    # restore ownership to root:root
    73    find "$i" -maxdepth 0 \( \! -uid 0 -o \! -gid 0 -o -type l \) -print0 | \
    74      xargs -0 --no-run-if-empty chown -c --no-dereference root:root -- || true
    75  done
    76  
    77  # store permissions after manipulation, this is also used as the stamp file
    78  # for the systemd service to ensure it is only run once
    79  find /etc/cloud /var/lib/cloud /var/lib/snapd -printf '%M %U %G %p\n' > /var/lib/snapd/device/ownership-change.after.tmp
    80  # Note: this find will fail on UC20 seeding because there is no
    81  # /writable/system-data/boot
    82  find  /writable/system-data /writable/system-data/var /writable/system-data/var/lib /writable/system-data/boot /writable/system-data/etc -maxdepth 0 -printf '%M %U %G %p\n' >> /var/lib/snapd/device/ownership-change.after.tmp
    83  mv /var/lib/snapd/device/ownership-change.after.tmp /var/lib/snapd/device/ownership-change.after
    84  
    85  # ensure things are really on disk
    86  sync