[Android General] threehappypenguins:
I am a Windows user, and I was having quite the trouble unpacking and repacking system.new.dat files. I tried all sorts of Windows Chinese tools for the job, but no matter what I did, when I put the system.new.dat file and system.transfer.list file back into the flashable zip (even if I made no changes to the system.new.dat file... just unpacked and repacked), I would get an error flashing the file.

I finally got around this by installing Ubuntu 16.04 Xenial Xerus on VirtualBox. You can find a lot of tutorials online about how to do that. I am basing my instructions off of xpirt's tutorial on unpacking and repacking with the tools he provides. But it took me about 2 days to figure it out because I kept running into problems since some of the instructions made assumptions that the user knows how to use Linux. I am very new to Linux. I am not completely sure how to do *everything* from the command line, so if someone wants to add to my instructions to make it easier, please do so. But these are the things I did to figure it out.

So here are the noob-friendly Linux Ubuntu instructions on how to unpack and repack:

Section 1: Getting everything Ready

1. Download your flashable .zip rom. In my case, I downloaded cm-12.1-20150619-UNOFFICIAL-afyonltetmo.zip to my Ubuntu Desktop. You can download it to your Home Directory as well. Doesn't matter.

2. Download sdat2img. I downloaded mine to my Home Directory.

3. Download img2sdat. I also downloaded this to my Home Directory.

4. Extract all 3 zips, by right clicking on them and choosing 'Extract Here.' It will extract them into their own directories. The main directories that you need to pay attention to are /sdat2img-master/ and /img2sdat-master/

5. Download ext4fs either right into the sdat2img-master folder, or anywhere you want and then drag and drop it into the sdat2img-master folder.

6. Open Terminal, and install Python 2.7, if it's not already installed. Put

sudo apt-get install python-2.7 python-pip

Section 2: Beginning the Unpacking Process

*Note, you can copy and paste the codes; however, you can't use keyboard shortcuts in terminal. You have to right click and choose your option.

*Another note: Where you see "user" in the terminal commands, substitute it with your Linux username.

1. In your extracted folder from your flashable zip, right click on system.new.dat and system.transfer.list and choose 'cut.' Then paste them in the sdat2img-master folder.

2. Back in Terminal put

cd sdat2img-master

. That will get you into the right directory.

3. Now put

./sdat2img.py system.transfer.list system.new.dat system.img

. That will create your raw ext4 .img file.

4. You want to create an output directory for all the files that are in system.img. Put

mkdir output

5. Now you can mount system.img, and it will mount it into that output folder. Put

sudo mount -t ext4 -o loop system.img output/

6. You might have the problem that you don't have ownership of all the files (they have little "locks" on them). To gain ownership, put

sudo chown -R user:user /home/user/sdat2img-master/output

. If you still can't get read and write access, just try rebooting your Ubuntu virtual computer. That was the only thing that finally worked for me. You can start modifying stuff as you wish.

Section 3: Compress files back to a raw ext4 image

1. You are now going to need your file_contexts file from your original extract rom zip. COPY (not cut) and paste it into the sdat2img-master folder.

2. You are also going to need your 'make_ext4fs' file. Make sure it is in the sdat2img-master folder. You might have trouble with permissions, so just in case, put

chmod 777 /home/user/sdat2img-master/make_ext4fs

into terminal.

3. In Files (Ubuntu's "File Manager"), in the sdat2img-master folder, right click on your system.img file and go to 'Properties'. Notice the information in "Size." Ignore the GB, but look at the bytes. Mine is 2,411,724,800. Copy that number, but remove the commas. So mine is 2411724800.

4. Next is the actual compressing part. Put

./make_ext4fs -T 0 -S file_contexts -l 2411724800 -a system system_new.img output/

into terminal, but substitute '2411724800' with whatever size you got from the Properties in system.img. You will now how a new file called system_new.img.

Section 4: Converting ext4 (raw image) to img (sparse image)

1. Now you will need to install img2simg. In Terminal, put

sudo apt-get install android-tools-fsutils

2. Now you can put

img2simg system_new.img system_new_sparse.img

You will now have a file called system_new_sparse.img that is ready to be packed in a new dat file.

Section 5: Converting img (sparse image) to dat (sparse data)

1. Cut and paste your system_new_sparse.img into img2sdat-master folder that you made earlier.

2. Now you need to navigate to that folder in terminal. Put

cd /home/user/img2sdat-master

3. Now you can put

./img2sdat.py system_new_sparse.img

to create your new dat file. It will first ask you which Android version it's for. Enter the number corresponding and hit enter.

4. You're now pretty much done. As you'll see, it created 3 files: system.new.dat, system.patch.dat, and system.transfer.list. Just paste these files into your folder where your flashable zip files are, replacing any original ones in there. Make sure you also have your file_contexts file in there as well. Highlight all the files in there, right click, and zip them up. Then name your flashable zip whatever you want and try flashing!