Added howto to mount files as virtual file systems

This commit is contained in:
Michele Cereda
2022-09-27 23:07:50 +02:00
parent 24a0ae76bf
commit 7e8edd71d4
3 changed files with 111 additions and 0 deletions

36
knowledge base/dd.md Normal file
View File

@@ -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

View File

@@ -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

View File

@@ -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