From 7e8edd71d4e3a4c689099b283eb1b40c895cba5f Mon Sep 17 00:00:00 2001 From: Michele Cereda Date: Tue, 27 Sep 2022 23:07:50 +0200 Subject: [PATCH] Added howto to mount files as virtual file systems --- knowledge base/dd.md | 36 +++++++++++++++++ .../mount files as virtual file systems.md | 36 +++++++++++++++++ knowledge base/truncate.md | 39 +++++++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 knowledge base/dd.md create mode 100644 knowledge base/mount files as virtual file systems.md create mode 100644 knowledge base/truncate.md diff --git a/knowledge base/dd.md b/knowledge base/dd.md new file mode 100644 index 0000000..30e97da --- /dev/null +++ b/knowledge base/dd.md @@ -0,0 +1,36 @@ +# dd + +Convert and copy a file. + +## TL;DR + +```sh +# Read 512 random Bytes for each iteration and save them . +dd if='/dev/urandom' of='output/file' count=2 bs=512 + +# Read 1000 Bytes for each iteration and save them while watching the progress. +dd if='/input/file' of='output/file' count=4M bs=1k status='progress' + +# Create a 1GiB file with nothing but zeros, ready to mkswap(8) it. +dd if='/dev/zero' of='/swapfile' count=1048576 bs=1024 + +# Make a bootable USB drive from an isohybrid file. +dd if=file.iso of=/dev/usb_drive status=progress + +# Clone a drive to another drive with 4 MiB block. +# Ignore any error and show the progress. +dd if=/dev/source_drive of=/dev/dest_drive bs=4M conv=noerror status=progress + +# Generate a system backup into an IMG file and show the progress: +dd if=/dev/drive_device of=path/to/file.img status=progress + +# Restore a drive from an IMG file and show the progress: +dd if=path/to/file.img of=/dev/drive_device status=progress +``` + +## Sources + +- [cheat.sh] + + +[cheat.sh]: https://cheat.sh/dd diff --git a/knowledge base/mount files as virtual file systems.md b/knowledge base/mount files as virtual file systems.md new file mode 100644 index 0000000..2639e8f --- /dev/null +++ b/knowledge base/mount files as virtual file systems.md @@ -0,0 +1,36 @@ +# Mount files as virtual file systems + +## TL;DR + +```sh +# Create the file. +truncate -s '10G' 'path/to/file' +dd if='/dev/zero' of='path/to/file' bs=4MiB count=250K status='progress' + +# Create the file system on such file. +mkfs.ext4 'path/to/file' + +# Create the mount point. +mkdir 'mount/point' + +# Mount the file system. +sudo mount -t 'ext4' 'path/to/file' 'mount/point' +``` + +Prefer `truncate` to `dd` to let the file expand dynamically and be resized (both larger or smaller) without damaging data with `losetup` and `resize2fs`. + +## Further readings + +- [dd] +- [truncate] + +## Sources + +- [How do I create a file and mount it as a filesystem?] + + +[dd]: dd.md +[truncate]: truncate.md + + +[how do i create a file and mount it as a filesystem?]: https://askubuntu.com/questions/85977/how-do-i-create-a-file-and-mount-it-as-a-filesystem#1402052 diff --git a/knowledge base/truncate.md b/knowledge base/truncate.md new file mode 100644 index 0000000..0102f1b --- /dev/null +++ b/knowledge base/truncate.md @@ -0,0 +1,39 @@ +# truncate + +Shrink or extend the size of a file to the specified size. + +## TL;DR + +```sh +# Empty the contents of files. +truncate -s 0 'file' + +# Set the size of an existing file. +# if the file does not exist, create it anew of the specified size. +truncate -s 100 'file' +truncate --size 5k 'file' +truncate --size 10G 'file' + +# Extend a file's size by 50 MiB and fill it with holes. +# Holes read as zero bytes. +truncate --size +50M 'file' + +# Shrink a file by 2 GiB. +# Removes data from the end of file. +truncate --size -2G 'file' + +# Empty the file's content, but do not create it if existing. +truncate --no-create --size 0 'file' +``` + +## Further readings + +- [GNU's documentation] + +## Sources + +- [cheat.sh] + + +[cheat.sh]: https://cheat.sh/truncate +[gnu's documentation]: https://www.gnu.org/software/coreutils/truncate