[Ovirt-devel] [PATCH 1/3] Applied Perry Myers <pmyers at redhat.com> edit-livecd patch

David Huff dhuff at redhat.com
Thu Aug 20 16:29:04 UTC 2009


Old edit-livecd just kept adding to the ext2 fs image which meant
it never decreased in size even if files were removed.

New functionality does multiple passes of ext2fs creation.  First pass
creates a new ext2fs from the image file inside the squashfs.  This
first pass is to allow the user to optionally increase the filesystem
size.  If the -s parameter is omitted the existing size is used and this
step is a nop.  The new ext2 fs is mounted to the ex-rw directory.

After ex-rw is modified by the user (either manually or through supplied
script) it is copied again to ex-min.  This step is done so that the
filesystem is minimized in size (i.e. file removals will actually reduce
the size of the ext2 image and therefore reduce the overall size of the
livecd)

NOTE: There seems to be an approximate 1MB increase in size even on a nop
edit-livecd.  This is not in the ext2 filesystem itself, it's seen in the
squashfs image.  Not sure why this happens.

Signed-off-by: Perry Myers <pmyers at redhat.com>
---
 edit-livecd |  123 ++++++++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 110 insertions(+), 13 deletions(-)

diff --git a/edit-livecd b/edit-livecd
index d69ca9d..e94b1e6 100755
--- a/edit-livecd
+++ b/edit-livecd
@@ -31,7 +31,7 @@ CD=$NODEIMG_DEFAULT
 usage() {
     case $# in 1) warn "$1"; try_h; exit 1;; esac
     cat <<EOF
-Usage: $ME -i LiveCD.iso [-b bootparams] [-p program]
+Usage: $ME -i LiveCD.iso [-b bootparams] [-p program] [-s MB]
   -b BOOTPARAMS  optional parameters appended to the kernel command line
   -i LIVECD.iso  LiveCD ISO to edit (default: $NODEIMG_DEFAULT)
   -o OUTPUT.iso  specify the output file (required)
@@ -42,6 +42,7 @@ Usage: $ME -i LiveCD.iso [-b bootparams] [-p program]
                    the user (in another terminal) to modify the filesystem
                    manually.  Type <enter> when done, and the script
                    re-packages the ISO.
+  -s             Size of ext2 filesystem in MB (default is existing size)
   -h             display this help and exit

 EXAMPLES
@@ -69,14 +70,16 @@ set -e

 CODE=
 OUTPUT_FILE=
+SIZE=

 err=0 help=0
-while getopts :b:hi:o:p: c; do
+while getopts :b:hi:o:p:s: c; do
     case $c in
         i) CD=$OPTARG;;
         b) PARAMS=$OPTARG;;
         o) OUTPUT_FILE=$OPTARG;;
         p) CODE=$OPTARG;;
+        s) SIZE=$OPTARG;;
         h) help=1;;
         '?') err=1; warn "invalid option: \`-$OPTARG'";;
         :) err=1; warn "missing argument to \`-$OPTARG' option";;
@@ -86,9 +89,9 @@ done
 test $err = 1 && { try_h; exit 1; }
 test $help = 1 && { usage; exit 0; }

-# Require "-o OUTPUT_FILE"
+# Require "-o OUTPUT.iso"
 test -z "$OUTPUT_FILE" \
-  && { warn "no output file specified; use -o FILE.iso"; try_h; exit 1; }
+  && { warn "no output file specified; use -o OUTPUT.iso"; try_h; exit 1; }

 # Fail if there are any extra command-line arguments.
 if test $OPTIND -le $#; then
@@ -101,6 +104,8 @@ if [ $( id -u ) -ne 0 ]; then
     die "Must run as root"
 fi

+test selinuxenabled || { echo "selinux not enabled, aborting" ; exit 1 ; }
+
 # Check for some prerequisites.
 # "type" prints "PROG not found" if it's not in $PATH.
 type mkisofs
@@ -137,6 +142,71 @@ mnt() {
     addExit "df | grep $mp > /dev/null 2>&1 && umount -v $mp"
 }

+# Create new ext2 filesystem and copy contents of one into the other
+# Size of ext2 filesystem is same as original size unless size parameter
+# is passed in
+create_ext2fs() {
+    local input_file=$1
+    local input_dir=$2
+    local output_file=$3
+    local output_dir=$4
+    local size_mb=$5
+
+    local size=
+    if [[ -n "$size_mb" ]]; then
+        size=$(( $size_mb * 1024 ))
+    fi
+
+    echo ">>> Mounting old ext3fs"
+    mnt "-t ext2 $input_file -o ro,loop" $input_dir
+    ext_used=$(df $WDIR/$input_dir | tail -1 | awk '{print $3}')
+    ext_max=$(df $WDIR/$input_dir | tail -1 | awk '{print $2}')
+
+    echo ">>> $input_file max_size=$ext_max current_size=$ext_used"
+
+    local new_size=
+    if [[ -z "$size" ]]; then
+        new_size=$ext_max
+    elif [ $size -lt $ext_used ]; then
+        echo "New size of $size is less than the current used size $ext_used"
+        exit 1
+    else
+	new_size=$size
+    fi
+
+    echo ">>> Size selected for new $output_file: $new_size"
+
+    echo ">>> Creating new ext3fs"
+    dd if=/dev/zero of=$output_file bs=1024 count=0 seek=${new_size}
+    mkfs.ext2 -b 4096 -m 1 -L "ovirt-node-image" -F $output_file
+    tune2fs -i 0 -c 0 -Odir_index -ouser_xattr,acl $output_file
+
+    echo ">>> Mounting new ext3fs"
+    mnt "-t ext2 $output_file -o rw,loop" $output_dir
+
+    SELINUX_ENFORCING=$(getenforce)
+    case $SELINUX_ENFORCING in
+        Enforcing) setenforce Permissive ;;
+        Permissive) ;;
+        *) echo "Do not run $0 with selinux in disabled mode" ; exit 1 ;;
+    esac
+
+    echo ">>> Copying data from old ext3fs to new ext3fs"
+    rsync -aX $WDIR/$input_dir/ $WDIR/$output_dir/
+
+    echo -e ">>> $(df $WDIR/$input_dir | tail -1)"
+    echo -e ">>> $(df $WDIR/$output_dir | tail -1)"
+    umount $WDIR/$output_dir
+    umount $WDIR/$input_dir
+
+    if [ "$SELINUX_ENFORCING" = "Enforcing" ]; then
+        setenforce Enforcing
+    fi
+
+    e2fsck -f -y $output_file || :
+    e2fsck -f -y $output_file || :
+}
+
 addExit "rm -rf $WDIR"

 ID_FS_LABEL= # initialize, in case vol_id fails
@@ -150,47 +220,73 @@ mnt "-t iso9660 $CD -o loop,ro" cd
 mnt "-t squashfs $WDIR/cd/LiveOS/squashfs.img -o ro,loop" sq

 # create writable copy of the new filesystem for the CD
-cp -pr $WDIR/cd $WDIR/cd-w
+rsync -a $WDIR/cd/ $WDIR/cd-w/

 # create writable copy of the filesystem for the new compressed
 # squashfs filesystem
-cp -pr $WDIR/sq $WDIR/sq-w
+rsync -a $WDIR/sq/ $WDIR/sq-w/
+echo ">>> Old LiveCD Size: $(du -b $CD)"
+echo ">>> Old Squash Size: $(du -b $WDIR/cd/LiveOS/squashfs.img)"
+echo ">>> Old ext3fs Size: $(du -b $WDIR/sq-w/LiveOS/ext3fs.img)"

-# mount root filesystem
-mnt "-t ext2 $WDIR/sq-w/LiveOS/ext3fs.img -o rw,loop" ex
+# Create copy of original ext2fs in case user wants to make
+# the file system larger than the original
+create_ext2fs $WDIR/sq-w/LiveOS/ext3fs.img ex-ro $WDIR/ext3fs-rw.img ex-rw $SIZE
+
+echo ">>> New ext3fs Size: $(du -b $WDIR/ext3fs-rw.img)"
+
+echo ">>> Mounting new ext3fs"
+mnt "-t ext2 $WDIR/ext3fs-rw.img -o rw,loop" ex-rw

 echo ">>> Updating CD content"
 if [ -n "$CODE" ]; then
     (
-      cd $WDIR/ex
+      cd $WDIR/ex-rw
       set +e
       eval "$CODE"
       set -e
     )
 else
     echo "***"
-    echo "*** Pausing to allow manual changes.  Press any key to continue."
+    echo "*** Pausing to allow manual changes."
+    echo "*** Make changes in $WDIR/ex-rw"
+    echo "*** Press any key to continue."
     echo "***"
     read
 fi

+# cleanup temporary and cache locations in the chroot, rhbz#508043
+rm -rf $WDIR/ex-rw/var/cache/yum
+rm -rf $WDIR/ex-rw/tmp
+mkdir $WDIR/ex-rw/tmp
+
 # Try to unmount.  But this is likely to fail, so let the user retry,
-# e.g., if he forgot to "cd" out of $WDIR/ex.
+# e.g., if he forgot to "cd" out of $WDIR/ex-rw.
 while :; do
   echo ">>> Unmounting ext3fs"
-  umount $WDIR/ex && break
+  umount $WDIR/ex-rw && break
   echo ">>> Unmounting the working file system copy failed"
   echo "***"
-  echo "*** Did you forget to 'cd' out of $WDIR/ex?"
+  echo "*** Did you forget to 'cd' out of $WDIR/ex-rw?"
   echo "***"
   echo "*** Press any key to repeat the attempt."
   echo "***"
   read
 done

+# Copy the filesystem again to make sure we minimize the image
+create_ext2fs $WDIR/ext3fs-rw.img ex-rw $WDIR/ext3fs-min.img ex-min $SIZE
+
+echo ">>> Min ext3fs Size: $(du -b $WDIR/ext3fs-min.img)"
+
+# Move the minimized ext2 filesystem into the squash directory
+mv -v $WDIR/ext3fs-min.img $WDIR/sq-w/LiveOS/ext3fs.img
+
 echo ">>> Compressing filesystem"
 mksquashfs $WDIR/sq-w/ $WDIR/cd-w/LiveOS/squashfs.img -noappend

+echo ">>> New Squash Size: $(du -b $WDIR/cd-w/LiveOS/squashfs.img)"
+
 echo ">>> Recomputing MD5 sums"
 ( cd $WDIR/cd-w && find . -type f -not -name md5sum.txt \
     -not -path '*/isolinux/*' -print0 | xargs -0 -- md5sum > md5sum.txt )
@@ -215,6 +311,7 @@ mkisofs \

 echo ">>> Implanting ISO MD5 Sum"
 implantisomd5 --force "$OUTPUT_FILE"
+echo ">>> New LiveCD Size: $(du -b $OUTPUT_FILE)"

 # The trap ... callbacks will unmount everything.
 set +e
-- 
1.6.2.5




More information about the ovirt-devel mailing list