diff --git a/po/POTFILES-ml b/po/POTFILES-ml index cfe5a3eac..2dff10fbe 100644 --- a/po/POTFILES-ml +++ b/po/POTFILES-ml @@ -69,6 +69,7 @@ sparsify/utils.ml sysprep/main.ml sysprep/sysprep_operation.ml sysprep/sysprep_operation_abrt_data.ml +sysprep/sysprep_operation_backup_files.ml sysprep/sysprep_operation_bash_history.ml sysprep/sysprep_operation_blkid_tab.ml sysprep/sysprep_operation_ca_certificates.ml diff --git a/sysprep/Makefile.am b/sysprep/Makefile.am index e52f0d38b..c9ce3b01d 100644 --- a/sysprep/Makefile.am +++ b/sysprep/Makefile.am @@ -29,6 +29,7 @@ EXTRA_DIST = \ # Filenames sysprep_operation_.ml in alphabetical order. operations = \ abrt_data \ + backup_files \ bash_history \ blkid_tab \ ca_certificates \ @@ -179,6 +180,7 @@ TESTS = \ if ENABLE_APPLIANCE TESTS += \ test-virt-sysprep.sh \ + test-virt-sysprep-backup-files.sh \ test-virt-sysprep-passwords.sh if HAVE_FUSE diff --git a/sysprep/sysprep_operation_backup_files.ml b/sysprep/sysprep_operation_backup_files.ml new file mode 100644 index 000000000..603b5db5f --- /dev/null +++ b/sysprep/sysprep_operation_backup_files.ml @@ -0,0 +1,94 @@ +(* virt-sysprep + * Copyright (C) 2012-2016 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; either version 2 of the License, or + * (at your option) any later version. + * + * 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 Street, Fifth Floor, Boston, MA 02110-1301 USA. + *) + +open Printf + +open Sysprep_operation +open Common_gettext.Gettext +open Common_utils +open Visit +open Fnmatch + +module G = Guestfs + +let unix_whitelist = List.sort compare [ + "/etc"; + "/root"; + "/srv"; + "/tmp"; + "/var"; +] +let unix_whitelist_as_pod = + String.concat "\n" (List.map ((^) " ") unix_whitelist) + +let globs = List.sort compare [ + "*.bak"; + "*~"; +] +let globs_as_pod = String.concat "\n" (List.map ((^) " ") globs) + +let backup_files_perform (g : Guestfs.guestfs) root side_effects = + (* Unix-like? If so that only operate on the unix_whitelist + * filesystems, else operate on everything. + *) + let fses = + if unix_like (g#inspect_get_type root) then unix_whitelist + else ["/"] in + + List.iter ( + fun fs -> + if g#is_dir ~followsymlinks:false fs then ( + visit g#ocaml_handle fs ( + fun dir filename { G.st_mode = mode } _ -> + match dir, filename, mode with + (* Ignore root directory and non-regular files. *) + | _, None, _ -> () + | _, Some _, mode when not (is_reg mode) -> () + | dir, Some filename, _ -> + (* Check the filename against all of the globs, and if it + * matches any then delete it. + *) + let matching glob = fnmatch glob filename [FNM_NOESCAPE] in + if List.exists matching globs then ( + let path = full_path dir (Some filename) in + g#rm_f path + ) + ) + ) + ) fses + +let op = { + defaults with + name = "backup-files"; + enabled_by_default = true; + heading = s_"Remove editor backup files from the guest"; + pod_description = Some ( + sprintf (f_"\ +The following files are removed from anywhere in the guest +filesystem: + +%s + +On Linux and Unix operating systems, only the following filesystems +will be examined: + +%s") globs_as_pod unix_whitelist_as_pod); + perform_on_filesystems = Some backup_files_perform; +} + +let () = register_operation op diff --git a/sysprep/test-virt-sysprep-backup-files.sh b/sysprep/test-virt-sysprep-backup-files.sh new file mode 100755 index 000000000..114e1d8be --- /dev/null +++ b/sysprep/test-virt-sysprep-backup-files.sh @@ -0,0 +1,64 @@ +#!/bin/bash - +# libguestfs virt-sysprep test script +# Copyright (C) 2016 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; either version 2 of the License, or +# (at your option) any later version. +# +# 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 Street, Fifth Floor, Boston, MA 02110-1301 USA. + +export LANG=C +set -e + +# Test removal of editor backup files. + +if [ "$(guestfish get-backend)" = "uml" ]; then + echo "$0: skipping test because uml backend does not support qcow2" + exit 77 +fi + +if [ ! -s ../test-data/phony-guests/fedora.img ]; then + echo "$0: skipping test because there is no phony Fedora test image" + exit 77 +fi + +rm -f test-backup-files.qcow2 +rm -f test-backup-files-before +rm -f test-backup-files-after + +# Add some backup files to the Fedora image. +guestfish -- \ + disk-create test-backup-files.qcow2 qcow2 -1 \ + backingfile:../test-data/phony-guests/fedora.img \ + backingformat:raw +guestfish --format=qcow2 -a test-backup-files.qcow2 -i <<'EOF' +# /bin and /usr are not on the whitelist, so these file shouldn't be deleted. +touch /bin/test~ +touch /usr/share/test~ +find / | cat > test-backup-files-before +touch /etc/fstab.bak +touch /etc/resolv.conf~ +EOF + +# Run virt-sysprep backup-files operation only. + +virt-sysprep -x --format qcow2 -a test-backup-files.qcow2 \ + --enable backup-files + +# Check the file list is the same as above. +guestfish --format=qcow2 -a test-backup-files.qcow2 -i find / > test-backup-files-after + +diff -u test-backup-files-before test-backup-files-after + +rm test-backup-files.qcow2 +rm test-backup-files-before +rm test-backup-files-after