Advertisement
Guest User

Untitled

a guest
Jul 14th, 2012
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.50 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # optimage.sh <foo.img>
  4. #
  5. # repackages filesystem images for improved compression
  6. # all modifications to the images are non-destructive
  7. # method shown from and explained by [mbm]
  8. #
  9. # created by CEnnis91 (2012)
  10. #
  11.  
  12.  
  13. if [ $(whoami) != "root" ]; then
  14.     echo "Error: You must run as root!"
  15.     exit 1
  16. fi
  17.  
  18. fsimg=$(readlink -f "$1")
  19.  
  20. if [ ! -f "$fsimg" ]; then
  21.     echo "Error: File does not exist!"
  22.     exit 1
  23. fi
  24.  
  25. fstype=$(file "$fsimg" | grep -io "ext[2-4]")
  26.    
  27. if [ -z "$fstype" ]; then
  28.     echo "Error: Invalid file type!"
  29.     exit 1
  30. fi
  31.  
  32.  
  33. fsname=$(basename "$fsimg" | sed 's|^\(.*\)[.].*$|\1|')
  34. blkcount=$(tune2fs -l "$fsimg" | grep "Block count" | sed 's|[^0-9]*\(.*\)|\1|')
  35. blksize=$(tune2fs -l "$fsimg" | grep "Block size" | sed 's|[^0-9]*\(.*\)|\1|')
  36. tmpdir=$(mktemp -d)
  37. tmplog=$(mktemp)
  38.  
  39. exec 3>&1 > $tmplog 2>&1
  40. trap "rm -r $tmpdir" 0 1 2 3 15
  41. trap "echo 'Something went wrong! Read $tmplog for details.' >&3; exit 0" ERR
  42.  
  43. echo "Creating blank image..." >&3
  44. mv "$fsimg" "$fsimg.old"
  45. dd if=/dev/zero of="$fsimg" bs=$blksize count=$blkcount
  46. mke2fs -F -b $blksize -T "$fstype" "$fsimg"
  47.  
  48. mkdir -p "$tmpdir/$fsname" "$tmpdir/old_$fsname"
  49.  
  50. echo "Copying data over..." >&3
  51. mount -o loop "$fsimg" "$tmpdir/$fsname"
  52. mount -o loop "$fsimg.old" "$tmpdir/old_$fsname"
  53. cp -af "$tmpdir/old_$fsname"/* "$tmpdir/$fsname"
  54.  
  55. umount "$tmpdir/$fsname/"
  56. umount "$tmpdir/old_$fsname/"
  57.  
  58. owner=$(ls -al "$fsimg.old" | cut -d " " -f 3-4 | sed 's/ /:/g')
  59. chown "$owner" "$fsimg"
  60.  
  61. echo "Success!" >&3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement