Merging changes from partition 2 into partition 1

samo on altern.org jan-2008: it is possible to do the above even after heavy changes are done to the original filesystem – i.e. lots of work, package additions/removals etc (not at all virgin state) – and still get everything right (and keep the original partition/filesystem instead of making all new). Roughly the steps are as follows (requires some unix skills. Read this entire section before starting and ensure you know what you’re doing):

  • from above, get to the point of not really needing unionfs anymore
  • boot from external usb drive or similar, so /dev/sda1 /dev/sda2 are not used
  • mount them somewhere, i.e. /media/sda1 /media/sda2
  • as said above, all on /media/sda1 is original untouched filesystem, all on /media/sda2 is only your changes:
    • new files are as plain files, changed files are copies of original files
    • removed files/dirs are represented as .wh.thefilenamehere 0-sized files
    • new hard-links (ln -f) are represented as a copy of the file + a hard-link to it
  • step DEL-OPAQUE: find all .wh.__dir_opaque files on sda2 and delete the corresponding dir on sda1
    • Example code to do this would be
    • cd /media/sda2
      find . -name .wh.__dir_opaque -exec sh -c 'F="{}";echo rm -rf "/media/sda1/${F%/*}"' \;
    • this only shows what it will do. Remove the “echo” to actually do it
  • step DEL: find all things matching .wh.* on sda2, and remove the original file/dir they denote (same name less the .wh. prefix, in same dir in /media/sda1)
    • Example code to do this would be
    • cd /media/sda2
      find . -name .wh.\* ! -name .wh.__dir_opaque -exec sh -c 'F="{}";echo rm -rf "/media/sda1/${F/\/.wh.//}"' \;
    • this only shows what it will do. Remove the “echo” to actually do it
    • WARNING: If you mistype the above, it may delete your whole sda1 partition!
    • if you get an error message ’sh: Syntax error: Bad substitution’, you might want to replace -exec sh with -exec bash because your distribution apparently does not use bash as shell.
  • step COPY:
    • cd /media/sda2
      cp -a sbin etc bin usr home lib var opt dev   /media/sda1/
    • put in the above all real dirs, e.g. avoid /proc /sys and similar pseudo-filesystems. Note /dev IS there. This can be also done one by one. Another way is to remove rm -rf sys/ proc/ from the /media/sda2 and then copy -a /media/sda2/* /media/sda1
    • make sure you have enough space on sda1 for all the files on sda2 before you do this. If you’ve added more than ~230Mb of files, you won’t. If not, tar them onto an external drive, resize sda1 to use all the drive space, then untar them onto it.
  • be very careful… This procedure *can* delete your /media/sda1/*
  • backup your whole drive first
  • once you are sure the actual files are gone, you can remove the .wh.* placeholders in /media/sda1
find /media/sda1 -name .wh.\* -exec rm -f "{}" \;

Thanks to VZ for some of these notes

Note on merging partitions 1 and 2: Attempting to resize the /dev/sda1 using parted over original filesystem will fail. The /dev/sda1 ext2 filesystem has some optimization features turned on, which parted does not want to handle. The manual workaround is to resize the partition via fdisk then resize the filesystem via resize2fs:

  • umount /dev/sda1 /dev/sda2
  • fdisk /dev/sda
    • p #to see what it is like
    • d 2 #remove partition 2
    • d 1 #remove partition 1
    • n #new partition 1, use all space as it suggests – something like 1-484
    • t #set its type to 83 (linux)
    • a #make it bootable
    • w #write
  • resize2fs /dev/sda1
  • e2fsck -f /dev/sda1 #force check

Note: This sentence is being put in here to make it easier for those who want to remove default programs from Easy Mode free up space to find this article; should they decide to search the wiki before posting.