[Thincrust-devel] [PATCH] Added whitelisting and blacklisting support. I also removed the old parts of hte
Bryan Kearney
bkearney at redhat.com
Wed Sep 3 18:34:33 UTC 2008
---
Makefile | 2 +
appliance-tools.spec | 1 +
config/aos-f8.ks | 6 +--
config/aos-f9.ks | 6 +--
config/aos-rawhide.ks | 6 +--
config/base-post.ks | 97 ++++++++++--------------------------
tools/image-minimizer | 129 +++++++++++++++++++++++++++++++++++++++++++++++++
7 files changed, 165 insertions(+), 82 deletions(-)
create mode 100755 tools/image-minimizer
diff --git a/Makefile b/Makefile
index 457d7d5..973f466 100644
--- a/Makefile
+++ b/Makefile
@@ -16,6 +16,7 @@ all:
install:
$(INSTALL_PROGRAM) -D tools/appliance-creator $(DESTDIR)/usr/bin/appliance-creator
+ $(INSTALL_PROGRAM) -D tools/image-minimizer $(DESTDIR)/usr/bin/image-minimizer
$(INSTALL_DATA) -D README $(DESTDIR)/usr/share/doc/appliance-tools-$(VERSION)/README
$(INSTALL_DATA) -D COPYING $(DESTDIR)/usr/share/doc/appliance-tools-$(VERSION)/COPYING
mkdir -p $(DESTDIR)/usr/share/appliance-tools/
@@ -26,6 +27,7 @@ install:
uninstall:
rm -f $(DESTDIR)/usr/bin/appliance-creator
+ rm -f $(DESTDIR)/usr/bin/image-minimizer
rm -rf $(DESTDIR)/usr/lib/appliance-creator
rm -rf $(DESTDIR)/usr/share/doc/appliance-tools-$(VERSION)
rm -rf $(DESTDIR)/usr/share/appliance-tools
diff --git a/appliance-tools.spec b/appliance-tools.spec
index 540d912..b7e0bca 100644
--- a/appliance-tools.spec
+++ b/appliance-tools.spec
@@ -39,6 +39,7 @@ rm -rf $RPM_BUILD_ROOT
%doc README
%doc COPYING
%{_bindir}/appliance-creator
+%{_bindir}/imageminimizer
%dir %{_datadir}/appliance-tools
%{_datadir}/appliance-tools/*
%dir %{python_sitelib}/appcreate
diff --git a/config/aos-f8.ks b/config/aos-f8.ks
index bfb0846..d856578 100644
--- a/config/aos-f8.ks
+++ b/config/aos-f8.ks
@@ -31,8 +31,6 @@ repo --name=f8-updates --mirrorlist=http://mirrors.fedoraproject.org/mirrorlist?
%end
#
-# Add custom post scripts after the base post.
+# Image minimization
#
-%post
- #%include base-post.ks
-%end
+%include base-post.ks
diff --git a/config/aos-f9.ks b/config/aos-f9.ks
index 3851911..7e3da1f 100644
--- a/config/aos-f9.ks
+++ b/config/aos-f9.ks
@@ -31,8 +31,6 @@ repo --name=f9-updates --mirrorlist=http://mirrors.fedoraproject.org/mirrorlist?
%end
#
-# Add custom post scripts after the base post.
+# Image minimization
#
-%post
- #%include base-post.ks
-%end
+%include base-post.ks
diff --git a/config/aos-rawhide.ks b/config/aos-rawhide.ks
index eeb65ef..f6cd5f6 100644
--- a/config/aos-rawhide.ks
+++ b/config/aos-rawhide.ks
@@ -30,8 +30,6 @@ repo --name=rawhide --mirrorlist=http://mirrors.fedoraproject.org/mirrorlist?rep
%end
#
-# Add custom post scripts after the base post.
+# Image minimization
#
-%post
- #%include base-post.ks
-%end
+%include base-post.ks
diff --git a/config/base-post.ks b/config/base-post.ks
index e244865..3a19035 100644
--- a/config/base-post.ks
+++ b/config/base-post.ks
@@ -1,70 +1,27 @@
-# from katzj's appliance creator script
-# cut locale archive down to a smaller set; this should be handled
-# automatically based on %packages --installLangs once that's supported
-localedef --list-archive |grep -v en_US | xargs localedef --delete-from-archive
-mv /usr/lib/locale/locale-archive /usr/lib/locale/locale-archive.tmpl
-/usr/sbin/build-locale-archive
-
-# remove some things that mkinitrd doesn't really need in this case
-# we should probably find a way to remove the deps and still have
-# ordering work properly
-rpm -e --nodeps mkinitrd kpartx dmraid mdadm lvm2 tar
-
-# fedora-release-notes is required by fedora-release and is pretty
-# large, but not really needed for an appliance
-rpm -e --nodeps fedora-release-notes
-
-# here, remove a bunch of files we don't need that are just eating up space.
-# it breaks rpm slightly, but it's not too bad
-
-# FIXME: ug, hard-coded paths. This is going to break if we change to F-9
-# or upgrade certain packages. Not quite sure how to handle it better
-
-# Added from pmyers image-minimization patch
-RM="rm -rf"
-# Remove docs and internationalization
-$RM /usr/share/omf
-$RM /usr/share/gnome
-$RM /usr/share/doc
-$RM /usr/share/locale
-$RM /usr/share/libthai
-$RM /usr/share/man
-$RM /usr/share/terminfo
-$RM /usr/share/X11
-$RM /usr/share/i18n
-
-find /usr/share/zoneinfo -regextype egrep -type f ! -regex ".*/EST.*|.*/GMT" -exec $RM {} \;
-
-$RM /usr/lib/locale
-$RM /usr/lib/syslinux
-$RM /usr/lib64/gconv
-$RM /usr/lib64/pango
-$RM /usr/lib64/libpango*
-$RM /etc/pango
-$RM /usr/bin/pango*
-
-# Remove unnecessary kernel modules
-
-MODULES="XXX/lib/modules/*/kernel"
-$RM $MODULES/sound
-
-fs_mods="9p affs autofs autofs4 befs bfs cifs coda configfs cramfs dlm \
- ecryptfs efs exportfs freevxfs fuse gfs2 hfs hfsplus jbd jbd2 \
- jffs jfs minix ncpfs ocfs2 qnx4 reiserfs romfs sysv udf ufs xfs"
-for dir in $fs_mods ; do
- $RM $MODULES/fs/$dir
-done
-
-net_mods="802 8021q 9p appletalk atm ax25 bluetooth dccp decnet \
- ieee80211 ipx irda mac80211 netrom rfkill rose sched \
- sctp tipc wanrouter wireless"
-for dir in $net_mods ; do
- $RM $MODULES/net/$dir
-done
-
-driver_mods="bluetooth firewire i2c isdn media edac"
-for dir in $driver_mods ; do
- $RM $MODULES/drivers/$dir
-done
-
-
+%post --nochroot --interpreter image-minimizer
+ drop /usr/share/omf
+ drop /usr/share/gnome
+ drop /usr/share/doc
+ drop /usr/share/libthai
+ drop /usr/share/man
+ drop /usr/share/terminfo
+ drop /usr/share/X11
+ drop /usr/share/i18n
+
+ # Remove many of the time zones.
+ drop /usr/share/zoneinfo
+ keep /usr/share/zoneinfo/EST
+ keep /usr/share/zoneinfo/UTC
+
+ drop /usr/lib/locale
+ drop /usr/lib/syslinux
+ drop /usr/lib64/gconv
+ drop /usr/lib64/pango
+ drop /usr/lib64/libpango*
+ drop /etc/pango
+ drop /usr/bin/pango*
+
+ # Nix all locale stuff
+ drop /usr/share/locale
+ keep /usr/share/locale/en_US
+%end
diff --git a/tools/image-minimizer b/tools/image-minimizer
new file mode 100755
index 0000000..276f252
--- /dev/null
+++ b/tools/image-minimizer
@@ -0,0 +1,129 @@
+#!/usr/bin/python
+#
+# imageminimizer: removes files on the filesystem
+#
+# Copyright 2007, Red Hat Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 of the License.
+#
+# 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 Library 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+import glob
+import optparse
+import os
+import shutil
+import sys
+
+class ImageMinimizer:
+ filename = ''
+ dryrun = False
+ prefix = None
+ keeps = set()
+ drops = set()
+ visited = set()
+
+ def __init__(self, filename, root, dryrun):
+ self.filename = filename
+ self.prefix = root
+ self.dryrun = dryrun
+
+ # Recursively adds all files and directories.
+ # This is done becuase globbing does not allow
+ # ** for arbitrary nesting.
+ def add_directory(self, files, dirname):
+ self.visited.add(dirname)
+ for root, dirs, items in os.walk(dirname):
+ for dir in dirs:
+ self.visited.add(os.path.join(root, dir))
+ for name in items:
+ files.add(os.path.join(root, name))
+
+ def add_pattern(self, files, pattern):
+ if os.path.isdir(pattern):
+ self.add_directory(files, pattern)
+ else:
+ files.update(glob.glob(pattern))
+
+ # Parses each line in the ifle
+ def parse_line(self, line):
+ tup = line.partition(' ')
+ command = tup[0].lower()
+ pattern = tup[2].strip()
+ if not self.prefix == None:
+ pattern = pattern.lstrip('/')
+ pattern = os.path.join(self.prefix, pattern)
+
+ # Strip out all the comments and blank lines
+ if not (command.startswith('#') or command==''):
+ if command == 'keep':
+ self.add_pattern(self.keeps, pattern)
+ elif command == 'drop':
+ self.add_pattern(self.drops, pattern)
+ else:
+ raise Exception ('Unknown Command: ' + command)
+
+ def remove(self, files):
+ for tag in sorted(files, reverse=True):
+ self.visited.add(os.path.split(tag)[0])
+ if os.path.isdir(tag):
+ self.visited.add(tag)
+ else:
+ if self.dryrun:
+ print 'rm ' + tag
+ else:
+ #print 'rm ' + tag
+ os.remove(tag)
+
+ #remove all empty directory. Every 8k counts!
+ for dir in sorted(self.visited, reverse=True):
+ if len(os.listdir(dir)) == 0:
+ if self.dryrun:
+ print 'rm -rf ' + dir
+ else:
+ #print 'rm -rf ' + dir
+ os.rmdir(dir)
+
+ def filter(self):
+ for line in (open(self.filename).readlines()):
+ self.parse_line(line.strip())
+ final = self.drops.difference(self.keeps)
+ self.remove(final)
+
+
+def parse_options(args):
+ usage = "usage: %prog [options] filename"
+ parser = optparse.OptionParser(usage=usage)
+ parser.set_defaults(root=os.environ.get('INSTALL_ROOT'), dry_run=False)
+
+ parser.add_option("-i", "--installroot", type="string", dest="root",
+ help="Root path to prepend to all patterns. Defaults to INSTALL_ROOT")
+
+ parser.add_option("--dryrun", action="store_true", dest="dryrun",
+ help="If set, no filesystem changes are made.")
+
+ #imgcreate.setup_logging(parser)
+
+ return parser.parse_args()
+
+
+if __name__ == "__main__":
+ try:
+ (options, args) = parse_options(sys.argv[1:])
+ filename = args[0]
+ minimizer = ImageMinimizer(filename, options.root, options.dryrun)
+ minimizer.filter()
+ except SystemExit, e:
+ sys.exit(e.code)
+ except KeyboardInterrupt, e:
+ print >> sys.stderr, _("Aborted at user request")
+ except Exception, e:
+ print e
+ sys.exit(1)
--
1.5.5.1
More information about the Thincrust-devel
mailing list