Add files to VFAT image without mounting - linux

I want to create a new VFAT image and add a few files to it.
# Create file of 1MB size:
dd if=/dev/zero of=my-image.fat count=1 bs=1M
# Format file as VFAT:
mkfs.vfat ./my-image.fat
Now I want to add the files ./abc, ./def and ./ghi to the image.
How do I do that without mount -o loop or fusermount?
I only want to write to a new, empty, pristine VFAT image.
I don't need deleting appending or any "complicated" operations.
I tried 7z -a because 7zip can read VFAT images, but it does not know how to write to it.

I want to do the exact same thing as part of an image build for an embedded system. It's really annoying that the entire build, which takes ~3hrs, could be completely unattended except for the final steps which required a password in order to mount a VFAT image. Fortunately, I found a set of tools which solve the problem.
You want mcopy provided by GNU mtools.
Mtools is a collection of utilities to access MS-DOS disks from GNU and Unix without mounting them.
It also supports disk images such as VFAT image files.
As an example, the following command will copy the file hello.txt from your current directory into the subdirectory subdir of the VFAT file system in ~/images/fat_file.img:
mcopy -i ~/images/fat_file.img hello.txt ::subdir/hello.txt
There are more useful inclusions in mtools, such as mdir and mtype which are great for inspecting your image file without having to mount it.
mdir -i ~/images/fat_file.img ::
mdir -i ~/images/fat_file.img ::subdir
mtype -i ~/imags/fat_file.img ::subdir/hello.txt

What you want is basically impossible. You can't just "stuff" some file data onto the end of a disk image and have those files magically "appear" within the image. Feel free to stuff in the data, but there's more to a filesystem than just the data. You have to EXACTLY replicate the metadata operations that the file system handles for you, e.g. updating the FAT tables.
In other words, you'd have to build the ENTIRE FAT filesystem handling code in your own code. Which is utterly ludicrous. Just mount the image, use normal file operations on that mounted file system, then dismount it again. Boom, done.

Related

No space left on device- Ubuntu Issues

I am getting this error "OSError: [Errno 28] No space left on device" when I am writing files in a directory. I am downloading images programmatically from different sources and creating directories according to day wise. Its working well on windows though.
While checking inodes I got this
I tried different solution like deleting the deleting the junk file and tmp folder but still no success.
What could be the issue?
inodes don't directly correlate with disk usage. Better use df -h to actually see, if your drive is full.
But as you get the error: Yep, it's full. Up to the brim.
You probably have some data some where that uses all that precious storage. Check your home directory with du -hs * | sort -h. This can take a moment, but it will show you the size of all files and directories in the current workdir (and sort it too).
Also directories to check would be /opt, /var and /tmp. Don't randomly delete stuff in /var though, if you don't know what you are doing.
I've listed /tmp here too, because you don't have listed it as a mount of the type tmpfs. You should fix that probably.

du linux command size greater than df

I am using Digi embedded linux module which is having 8MB flash and 16MB RAM.
My partition table is as below:
SO, I got 4.4MB for rootfs. And 2MB for UserFS.
When I run ‘df -ah’, I get following output.
However, when I run ‘du -sh’ on root, I have 4M in /lib and 3M in /usr. Both are under root. However, the root is only 4.4M.
I have checked for symbolic link and can confirm that the files are physically present on /lib and /usr.
I deleted some of the library files(netsnmp) under /lib, which was close to 2M, but the available size on /dev/root only increased by ~390K(from 408K to 792K).
This suggests that the /lib/libnetsnmp* were stored somewhere else. I am not sure where those files were saved. Any ideas?
Also, please note that the rootfs image size is 4M. And this is shown correctly in df -ah command on /dev/root filesystem.
JFFS2 has transparent compression built in if I recall correctly. Executables compress pretty well.
if the file is in use. you can't delete it really.
you can use lsof | grep deleted to find them.
Probably it is due to the existence of hard-links in the root filesystem. Each hard-link will be shown as a normal file, but all hard-links will point to the same inode, so physically there is only one copy of the file in the hard-disk. You can see a good definition of soft-link and hard-link in this link.
EDIT: You can search for hard-links using this command (taken from this answer):
find . -samefile /path/to/file

Recover a text file in linux

My project c source code file is corrupted while making the tgz of the file. I wanted to make *.tgz of 4 files. The file names are common.c common.h myfile.c and myfile.h. I mistyped the tar command. I used the following tar command by mistake
tar -cvf common.* myfile.* project.tgz
This has corrupted the common.c file. Is there any way to overcome this error?
If the file is really important, than umount the related block device, and you can look into it with the strings command. If the file contains something relatively unique, you can grep it, and if you have a little chance you can save the most of the file.
This can work with mounted block devices, but the chance of loosing the data is higher. But it works only under system's where you can access the block device directly.

Recover files deleted with rsync -avz --delete

Is it possible to recover files deleted with rsync -avz --delete?
If it is, what are some suggested tools to do so?
I am assuming you ran rsync on some unix system.
If you don't have a backup of your file system,
then its a long tedious process recovering deleted files from unix file system.
High level steps :
find partition where your file resided
create image of entire partition % dd if=/partition of=partition.img ..
(this assumes you have enough space to store this somewhere locally in a different partition, or you can copy it over to different system % dd if=/partition | ssh otherhost "dd of=partition.img")
open the img file in hex edit
(this assumes you know the contents of the files that you've lost and can identify them when you see the content.)
note the byte offset and length of your file
use grep -b to extract the contents of your missing file.
enjoy!
I wasn't able to get extundelete to work, so I ended up using photorec + find/grep in order to recover my important files.

How to recover deleted files in linux filesystem (a bit faster)?

If I launch the following command to recover lost file on linux:
grep -a -B 150 -A 600 "class SuperCoolClass" /dev/sda10 > /tmp/SuperCoolClass.repair
Do I really need the "-a"? We need to recover from "sda10" some erased files (sabotage) and we have a bunch of them to recover and I believe removing the -a would be faster.
I believe the files to be on disk but not in binary.
thx
The file you are working on is /dev/sda10 which grep would assume to contain binary data. In order to treat it as text (which you are looking for) you need the -a otherwise grep will just print Binary file /dev/sda10 matches
In addition since the task is IO rather than CPU bound it would not be a big performance gain in any case.
In the future it's quite easy to test something like this by yourself:
create dummy 10Mb disk: dd if=/dev/zero of=testfs bs=1024 count=10000
create filesystem: mkfs.ext4 testfs
mount via loopback: mount -o loop ./testfs /mnt/test/
copy some stuff on the dummy filesystem
unmount: umount /mnt/test
run grep on the test file with different options
EDIT
it just occurred to me that maybe you are looking for the command '/usr/bin/strings' instead
something like:
extract all printable strings from ruined disk: /usr/bin/strings -a /dev/sda10 > /tmp/recovery
grep on the text only many times for different strings: grep "whatever" /tmp/recovery > /tmp/recovery.whatever
To recover a text file (only a text file) you accidently deleted / overwrote (provided you remember a phrase in that text file)
Ensure the safety of files by unmounting the directory with
umount /home/johndoe.
Find which hard disk partition the folder is at, say sda3
Switch to terminal as root.
Run
grep -a -A800 -B800 'search this phrase' /dev/sda3 | strings>recovery_log.txt
This will take a while. You can go through the file recovery_log.txt using any text editor, even while the command is running.

Resources