lmi-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lmi-commits] [lmi] master 8054c55: Improve chroot destruction


From: Greg Chicares
Subject: [lmi-commits] [lmi] master 8054c55: Improve chroot destruction
Date: Sat, 23 May 2020 18:46:16 -0400 (EDT)

branch: master
commit 8054c5579e5d60df373b9fcd68772605cc4df89e
Author: Gregory W. Chicares <address@hidden>
Commit: Gregory W. Chicares <address@hidden>

    Improve chroot destruction
    
    Deleted 'lmi_destroy_chroot.sh': 'lmi_setup_02.sh' is now preferred.
---
 lmi_destroy_chroot.sh | 37 ---------------------------
 lmi_setup_02.sh       | 70 +++++++++++++++++++++++++++++++++++++++++++++++----
 lmi_setup_02c.sh      | 17 +++++++++++--
 lmi_setup_20.sh       |  2 +-
 4 files changed, 81 insertions(+), 45 deletions(-)

diff --git a/lmi_destroy_chroot.sh b/lmi_destroy_chroot.sh
deleted file mode 100755
index d6a4af8..0000000
--- a/lmi_destroy_chroot.sh
+++ /dev/null
@@ -1,37 +0,0 @@
-#!/bin/sh
-
-# Destroy a chroot for cross-building "Let me illustrate...".
-#
-# Copyright (C) 2016, 2017, 2018, 2019, 2020 Gregory W. Chicares.
-#
-# This program is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License version 2 as
-# published by the Free Software Foundation.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software Foundation,
-# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
-#
-# http://savannah.nongnu.org/projects/lmi
-# email: <address@hidden>
-# snail: Chicares, 186 Belle Woods Drive, Glastonbury CT 06033, USA
-
-. ./lmi_setup_inc.sh
-. /tmp/schroot_env
-
-set -vx
-
-assert_su
-assert_not_chrooted
-
-umount /srv/chroot/"${CHRTNAME}"/var/cache/apt/archives
-umount /srv/chroot/"${CHRTNAME}"/dev/pts
-umount /srv/chroot/"${CHRTNAME}"/proc
-
-rm --one-file-system --recursive --force "$(schroot --chroot="${CHRTNAME}" 
--location)"
-rm /etc/schroot/chroot.d/"${CHRTNAME}".conf
diff --git a/lmi_setup_02.sh b/lmi_setup_02.sh
index 48aede9..df856ac 100755
--- a/lmi_setup_02.sh
+++ b/lmi_setup_02.sh
@@ -1,6 +1,6 @@
 #!/bin/sh
 
-# Create a chroot for cross-building "Let me illustrate...".
+# Destroy any existing chroot named in './lmi_setup_inc.sh'.
 #
 # Copyright (C) 2016, 2017, 2018, 2019, 2020 Gregory W. Chicares.
 #
@@ -29,7 +29,67 @@ set -vx
 assert_su
 assert_not_chrooted
 
-# First, destroy any chroot left by a prior run.
-grep "${CHRTNAME}" /proc/mounts | cut -f2 -d" " | xargs --no-run-if-empty 
umount
-rm -rf /srv/chroot/"${CHRTNAME}"
-rm /etc/schroot/chroot.d/"${CHRTNAME}".conf || echo "None?"
+# umount expected mounts, then list any that seem to have been missed.
+#
+# It might seem snazzier to extract the relevant field of
+#   grep "${CHRTNAME}" /proc/mounts
+# and pipe it into
+#   xargs umount
+# but that would do something astonishing if two chroots (one nested
+# and the other not) have mounted /proc thus:
+#   proc /srv/chroot/"${CHRTNAME}"/proc
+#   proc /srv/chroot/centos7lmi/srv/chroot/"${CHRTNAME}"/proc
+# and only the non-nested one is intended to be destroyed.
+#
+# The 'findmnt' invocation is elaborated thus:
+#   -r
+# [see "column -t" below];
+#   o SOURCE,TARGET
+# because the natural order is {real-name, pseudonym}:
+#   eric blair alias george orwell
+# cf. /proc/self/mountstats:
+#   device proc mounted on /srv/chroot/"${CHRTNAME}"/proc
+# and thus, for commands, {existing, to-be-created}:
+#   cp existing_file copied_file
+#   ln -s original_name link_name
+#   mount /dev/sda1 /
+# but by default 'findmnt' reverses that order (its other default
+# fields are generally less interesting for chroot bind mounts);
+#   | grep "${CHRTNAME}"
+# because 'findmnt -T' doesn't find substrings;
+#   | sed -e's,^[/A-Za-z0-9_-]*[[]\([^]]*\)[]],\1,'
+# because in output like
+#   /dev/sdb5[/srv/cache_for_lmi]
+#   /dev/mapper/VolGroup00-1v_root[/srv/cache_for_lmi]
+# the part in brackets is the more interesting, while the "/dev..."
+# part is distracting (and the '--nofsroot' option would discard the
+# more interesting part)--yet bear in mind that the part in brackets
+# may be incomplete if a leading component like '/srv' is mounted on
+# a distinct device (and '--canonicalize' wouldn't fix that); and
+#   | column -t
+# along with '-r' because '-l' does a poor job of columnization.
+
+umount /srv/chroot/"${CHRTNAME}"/var/cache/apt/archives
+umount /srv/chroot/"${CHRTNAME}"/dev/pts
+umount /srv/chroot/"${CHRTNAME}"/proc
+
+findmnt -ro SOURCE,TARGET \
+  | grep "${CHRTNAME}" \
+  | sed -e's,^[/A-Za-z0-9_-]*[[]\([^]]*\)[]],\1,' \
+  | column -t
+
+# Use '--one-file-system' because it was designed for this use case:
+#   https://lists.gnu.org/archive/html/bug-coreutils/2006-10/msg00332.html
+# | This option is useful when removing a build "chroot" hierarchy
+#
+# Use 'schroot --location' rather than /srv/chroot/"$CHRTNAME" because
+# chroots need not be located in /srv .
+
+rm --one-file-system --recursive --force \
+  "$(schroot --chroot="${CHRTNAME}" --location)"
+
+# schroot allows configuration files in /etc/schroot/chroot.d/ only.
+
+rm /etc/schroot/chroot.d/"${CHRTNAME}".conf
+
+# These commands fail harmlessly if the chroot doesn't already exist.
diff --git a/lmi_setup_02c.sh b/lmi_setup_02c.sh
index 24762f1..cc37a4a 100755
--- a/lmi_setup_02c.sh
+++ b/lmi_setup_02c.sh
@@ -1,6 +1,6 @@
 #!/bin/sh
 
-# Create a chroot for cross-building "Let me illustrate...".
+# Destroy all existing centos chroots, brutally.
 #
 # Copyright (C) 2016, 2017, 2018, 2019, 2020 Gregory W. Chicares.
 #
@@ -29,7 +29,20 @@ set -vx
 assert_su
 assert_not_chrooted
 
-# First, destroy any chroot left by a prior run.
+# This brutal approach assumes that only one centos chroot exists.
+# That's a workable assumption if a unique centos chroot is used
+# only to emulate an inconvenient corporate redhat server. See
+# 'lmi_setup_02.sh' for a more thoughtful approach.
+#
+# The grep command conveniently finds not only mounts created to
+# support the centos chroot itself, but also mounts created to
+# support a debian chroot within the centos chroot.
+
+if [ "greg" != "$(id -un)" ]; then
+   echo "This script would eradicate all your centos chroots--beware."
+   exit 1
+fi
+
 grep centos /proc/mounts | cut -f2 -d" " | xargs --no-run-if-empty umount
 rm -rf /srv/chroot/centos7lmi
 rm /etc/schroot/chroot.d/centos7lmi.conf
diff --git a/lmi_setup_20.sh b/lmi_setup_20.sh
index a9443d2..5655233 100755
--- a/lmi_setup_20.sh
+++ b/lmi_setup_20.sh
@@ -69,7 +69,7 @@ mount -t proc -o rw,nosuid,nodev,noexec,relatime proc /proc
 # devpts /srv/chroot/${CHRTNAME}/dev/pts devpts 
rw,nosuid,noexec,relatime,mode=600,ptmxmode=000 0 0
 # proc /srv/chroot/${CHRTNAME}/proc proc rw,nosuid,nodev,noexec,relatime 0 0
 #
-# If the chroot is ever to be eradicated, use 'lmi_destroy_chroot.sh',
+# If the chroot is ever to be eradicated, use 'lmi_setup_02.sh',
 # which, crucially, unmounts before removing.
 #
 # As an alternative, configure the chroot with "type=directory"



reply via email to

[Prev in Thread] Current Thread [Next in Thread]