Cheatsheet for preparing and a new disk and using it with:
This section presents how to use each tool, in a given order, but depending on the use case you may want to skip some layers, or apply them in a different order. In particular:
gparted
, then go directly to the next one to create the LVM volumes directly on the raw device.gpt
partition table with gparted
gparted
:boot
and esp
flagsmount –bind
/mnt/{dev,proc,sys}, mount -t efivarfs efivarfs /mnt/sys/firmware/efi/efivars
chroot /mnt
grub-install –root-directory=/ –boot-directory=/boot –efi-directory=/boot/efi –bootloader-id=<os-name>
efibootmgr -v
(and remove an entry with efibootmgr -b <0005> -B
gparted
to create the LVM volumes on this partition.gparted
pvcreate <device-name>
(device can be the whole device if not a boot device, or a partition).pvdisplay
or pvs
.Cannot use <device-name>: device is partitioned
, you need to remove existing traces of partition table or filesystem with the command wipefs –all <device-name>
vgcreate <vgroup-name!> <device-name>
.vgdisplay
or vgs
.lvcreate -n <lvolume-name!> [-L <absolute-size>] [-l <relative-size>] <vgroup-name>
.lvdisplay
or lvs
<absolute-size>
: 200G
, 3T
, …<relative-size>
: +100%FREE
issue_discards
option to 1 in /etc/lvm/lvm.conf
.cryptsetup luksFormat -c aes-xts-plain64 -h sha256 -s 512 <volume-name>
<volume-name>
is the device or partition name if not using LVM, or /dev/mapper/<vgroup-name>-<lvolume-name>
if using it.cryptsetup luksOpen <volume-name> <evolume-name!>
cryptsetup –allow-discards –persistent refresh <evolume-name>
(check security implications though). Check with cryptsetup luksDump <volume-name>
. If you enabled it by mistake (for instance on a non-SSD), you can disable it with cryptsetup –persistent refresh <evolume-name>
(it resets flags).ext4
for a default robust journaled filesystembtrfs
: modern filesystem based on copy-on-write (instead of journal), offering more features (integrity checks, subvolumes, snapshots / deduplication, compression, encryption, RAID, …), but also some drawbacks (slightly less stable, requires more resources, wasted allocated data, …). It is a good choice for a work volume (because integrity checks and snapshots are really useful), but less obvious for backup volumes (because native deduplication is less performant with moved files and modified files, especially if not using btrfs-send
, so it is more efficient to use a deduplicating backup software, which will also handle integrity checks).zfs
: similar to btrfs
, using both copy-on-write and a journal (for improved performance with synchonous writes), more mature and sligthly more stable, but not included in kernel due to licensing (though easy to use).mkfs.<fs> <evolume-name>
ext4
, if not the system partition, you can remove the 5% reserved for root
: tune2fs -r 0 /dev/mapper/<evolume-name>
mount /dev/mapper/<evolume-name> /mnt/<evolume-name>
btrfs
, you can enable compression: btrfs property set <fs-root> compression <algo>
with <algo>
equal to lzo
(fastest) or zstd
(compromise). Note that this syntax does not support configuring levels, nor forcing compression to disable heuristics. For that you have to use instead a mount option in the previous step: compress=zstd:1
(default is :3
) or compress-force=lzo
.Some filesystems such as BTRFS and ZFS allow to create subvolumes.
btrfs subvolume create <subvolume-path>
(directory must not exist, -p
for creating parents).btrfs subvolume list <fs-root>
and btrfs subvolume show <subvolume-path>
cryptsetup luksOpen <volume-name> <evolume-name>
mount /dev/mapper/<evolume-name> /mnt/<evolume-name>
umount /dev/mapper/<evolume-name>
cryptsetup luksClose <evolume-name>
vgchange -an <groupe-name
if using LVM and the disk will be removedbtrfs subvolume snapshot -r <input-subvolume-path> <output-snapshot-path>
(<output-snapshot-path> is typically <input-subvolume-path>/.snapshots/<date>) (just remove -r
for a read-write snapshot).btrfs subvolume delete <snapshot-path>
btrfs quota enable <subvolume>
and then btrfs qgroup show <subvolume>
source]vgrename <old-name> <new-name>
lvrename <group> <old-name> <new-name>
cryptsetup luksChangeKey
pvck <device>
btrfs check <mount-point>
to verify the structural integrity of the filesystembtrfs scrub <mount-point>
to verify the data integritycryptsetup luksHeaderBackup /dev/DEVICE –header-backup-file /path/to/backupfile
vgcfgbackup -f /path/to/backup/file vg01
btrfs filesystem usage <subvolume>
(option -g
to display GB only).btrfs
:btrfs filesystem defrag
for defragmenting files.-czstd
(inherits level specified at mount).btrfs filesystem balance -dusage=<percentage>
for defragmenting free space (only data chunks less full than <percentage>
will be compacted).filefrag -v <file>
to analyze the fragmentation of a file, and list all extents.btrfs
:compsize <subvolume-path>
in order to get statistics about quantity of compressed files, and compression ratio.compsize <file-path>
in order to get compression details about a specific file.lsblk –discard
, and check for non-zero values in columns DISC-GRAN (DISCard GRANularity) and DISC-MAX (DISCard MAX bytes).cryptsetup <…> –allow-discards
fstrim
utils from the util-linux package.fstrim –verbose <mount-point>
for a single volume, or fstrim –verbose -A
for all mounted filesystems listed in /etc/fstab
and the root filesystem inferred from the kernel command line.systemctl start fstrim.timer
* Resize the LVM logical volume: lvresize -L <absolute-size> <lvolume-name>
<absolute-size>
can also be an increment, e.g. +50G
* Open the volume with Luks: cryptsetup luksOpen <lvolume-name> <evolume-name>
* Resize the filesystem:
e2fsck -f <evolume-name> ; resize2fs <evolume-name>
btrfs filesystem resize max /mnt/<evolume-name>
TODO
Everyone has personal data that nothing could recreate (pictures, emails, creations, …), or global data and configuration that it would take a lot of time to recreate. However you can lose some of them or all of them in several situations: hard drive crash, hard drive corruption, computer theft, computer destruction (fire…).
My advice:
borg init -e <encryption> [--append-only] .
<encryption>
can be:none
to disable it, for instance on an already encrypted volumerepokey
to enable it (or repokey-blake2
to use Blake2 instead of Sha256, which is often faster)authenticated
to disable encryption but still enable authentication (or authenticated-blake2
to use Blake2)–append-only
means that no data can be removed with borg, archives can only be added. It can be used to protect an online repository against malware.borg create <repo>::<!archive> <path> --stats --progress --compression auto,zstd,12 --chunker-params 15,23,19,4095 --noctime -x --exclude-caches
–compression
: it can make sense to adjust the compression level depending on your computer speed and your storage speed, so that compression does not slow down the backup, but still save as much space as possible under this constraint. However it is not always easy to find an universal value (data that compress very well are mostly limited by the input storage speed, while data that compress less well are mostly limited by the output storage speed). You have roughly the choice between LZ4 (very quick), LZMA (very high compression ratio), and ZSTD (wide-range) in between.–chunker-params
: this is also an important but a bit complicated tuning. Originally default value was creating small chunks causing huge cache and memory usage, so they switched to much larger chunks, but which can be too large for some applications (for instance when modifying only metadata of an image file, we want to deduplicate the data), so I came with this compromise 15,23,19,4095
.borg info <repo>
borg list <repo>
borg info <repo>::<archive>
borg diff <repo>::<archive1> <archive2>
borg mount <repo>::<archive> <mountpoint>
restic init --repo .
–password-file
.restic --repo <repo> --verbose --compression auto --ignore-ctime backup <path>
–compression
: unlike Borg, there is only choicies auto
, max
and off
restic –repo <repo> snapshots
to list snapshotsThe BTRFS filesystem allows to perform some sorts of backups:
rsync
for instance, then moved files and modified files will not be deduplicated (because they are sent again by rsync and won't be recognized).btrfs send -p <parent-src-snapshot> <src-snapshot> | pv | btrf receive <target-snapshot>
(<parent-source-snapshot>
must have been sent already).Sources:
The goal is threefold:
Different approaches:
Different methods could be combined, for instance 2 or 3 plus 4. But 3 managed by the password manager is probably unbeatable.
Ideally, for increased safety, the data to be obtained is always encrypted with a key that the designated persons possess.
What to transmit?
Notes:
By default ext3 reserves 5% of disk space to super-user. The intent is to let to critical applications the ability to write to the disk when it is full, but it has no use for a data partition, you just waste 5% of your partition.
You can check and remove these reserved blocks with the following commands:
tune2fs -l /dev/sda1 | grep Reserved tune2fs -r 0 /dev/sda1
First unmount your partition and remount it read-only.
extundelete –restore-file Documents/file.dat /dev/sda4
: the easiest solution if there are only a few files and you know their name. Accepts not unmounting the partition, works generally ok if you do it immediately after removing the files.testdisk
(photorec) is great to recover files on a mobile storage device because it works with any filesystem (finds signatures in data so no need of journal), and find all deleted files on the partition.ext3grep
ext3grep <partition> --restore-file <filename> # filename => file ; works great, but only for one file at a time... ext3grep <partition> --restore-all --deleted --after=1270639550 # dates -> files ext3grep <partition> --histogram=dtime --deleted --after=1270639000 --before=1270640000 # => dates ext3grep <partition> --ls --inode 2 # filenames => inodes (navigating in directories with inodes) ext3grep <partition> --search Libs/jafar/modules/ # filename,dates -> blocks ext3grep <partition> --restore-inode <inode> # inodes => files
Notes: “restore-all” failed while building stage2 cache with error “ext3grep: init_directories.cc:535: void init_directories(): Assertion `lost_plus_found_directory_iter != all_directories.end()' failed.”. However doing a “ls inode” created this stage2 cache, and afterwards “restore-all” worked… but just restored everything on the disk even not deleted files/dirs, not taking into account the “after” option… But manually editing the stage2 cache to only keep files/dirs you want to restore then “restore-all” worked perfectly!
ext4magic
In case the MBR/partition table of you disk is damaged.
You should always keep a backup of your partition table !
The first way is to store the output of p command of fdisk
.
You can also do a dump of the MBR and EBR:
dd if=/dev/sda of=sda.dd bs=512 count=1 # full MBR dump sfdisk -d /dev/sda > sda.sfdisk # MBR and EBR partition tables
Out of curiosity, the file
command is able to interpret the content of your MBR dump:
file sda.dd
If you have the output of the p command of fdisk
, then you can manually recreate the partition table with fdisk
with the same information. As long as you don't mount or format, modifying the partition table with fdisk
doesn't modify the partitions data.
If you have a full dump of MBR and EBR, you can automatically restore it:
dd if=sda.dd of=/dev/sda sfdisk /dev/sda < sda.sfdisk
To restore the MBR without the partition table:
dd if=sda.dd of=/dev/sda bs=446 count=1
To restore only the partition table:
dd if=sda.dd of=/dev/sda bs=1 skip=446 count=66
If you don't have a copy of your partition info, don't panic, some software can recover them by searching for the partitions in the disk content (but it has to be formatted as a standard filesystem, ie not encrypted):