How to format and copy disks

How to format and copy disks in GNU/Linux(R)

Discover disk devices and their IDs.

The following commands usually help.

lsblk
blkid

Discover file system structure.

Some of the following commands may help.

ls -l
chattr
du
ncdu .
df

Fill a disk with zeros.

Use the following command to fill the disk "/dev/sdb" with zeros:

dd if=/dev/zero of=/dev/sdb

Create a non-encrypted FAT32 disk.

Create a partition map with "fdisk".
Type in:
"m" (display help), "o" (create new empty MBR partition table),
"p" (display the state of the partition table),
"n" (create a new partition),
[Enter for default], [default], [default],
"t" (change partition type),
"L" (list all available options),
"c" (W95 FAT32 (LBA)),
"w" (write and exit).

Format the partition with the command like the following:

mkfs.fat -F 32 -n PHOTOS /dev/sdb1

Create an encrypted disk.

Discover the list of the device names:

lsblk

Let us assume the target disk is listed as "/dev/sdb". Create a partition map for "/dev/sdb":

fdisk /dev/sdb
Type in:
m
g
p
n
[Enter for default], [default], [default]
w

Encrypt the partition:

cryptsetup luksFormat --type luks2 /dev/sdb1

Unlock the encryption partition:

cryptsetup open /dev/sdb1 sdb1x

Format the partition:

mke2fs -t ext4 /dev/mapper/sdb1x

Lock the encryption partition:

cryptsetup close sdb1x

Create an non-encrypted disk.

Discover the list of the device names:

lsblk

Create a partition map:

fdisk /dev/sdb
Type in:
m
g
p
n
[Enter for default], [default], [default]
w

Format the partition (use "ext4" or "ext2"; "ext4" works for Linux; "ext2" works for Linux and OpenBSD):

mke2fs -t ext2 /dev/sdb1

Mount a non-encrypted FAT32 disk.

Make use of the following mount options (not tested):

fmask=0177,check=s,

Mount an encrypted disk.

Unlock the encryption partition:

cryptsetup open /dev/sdb1 sdb1x

Mount the partition to the "/mountpoint":

mount /dev/mapper/sdb1x /mountpoint

Unmount an encrypted disk.

Unmount the "/mountpoint":
umount /mountpoint

Lock the encrypted partition:

cryptsetup close sdb1x

Mount an non-encrypted disk.

Mount the partition to the "/mountpoint":

mount /dev/sdb1 /mountpoint

Unmount an non-encrypted disk.

Unmount the "/mountpoint":

umount /mountpoint

Persistent mounting of an external drive.

Make a directory for the drive mounting, for example "/backup":

mkdir /backup

Change the directory to "immutable", so it can be only used for mounting and not for storing files:

chattr +i /backup

Run "blkid" to list the partitions, and put a line into "/etc/fstab" using the PARTUUID values without the quotes, e.g.:

PARTUUID=2bu4f252-948d-42k0-aad5-e527a4ea3ga8  /external               ext2    defaults,nofail,noatime  0       2

Or in case of an encrypted disk, run "blkid" to list the partitions, and put a line into "/etc/fstab" using the UUID values without the quotes, e.g.:

UUID=  /external               ext2    defaults,nofail,noatime  0       2

Mount an OpenBSD disk.

Take the following command as an example.

mount -r -t ufs -o ufstype=44bsd /dev/sdb /external

Copy disk (non-optical) to a file as an image.

Discover the filename of the disk device (described elsewhere). Take the following command as an example.

dd if=/dev/sdb of=disk_image.iso bs=4M status=progress

Copy an optical disc (CD/DVD) as a file.

Discover the filename of the disk device (described separately).

In order to only copy actual data from the disc and not the empty blocks filling it up, first retrieve its block/sector count and size (2048 most of the time):

isosize -x /dev/sr0

Take the following command as an example.

dd if=/dev/sr0 of=discmage.iso bs=[sector_size] count=[sector_count] status=progress

Copy a disk image file to a disk.

Discover Linux device name of the disk device:

lsblk

Let us assume, the Linux device name is "dev/sdb" for all written below.

Unmount the disk if mounted, e.g. "/dev/sdb1":

umount /dev/sdb1

Copy the data to the disk:

dd if=image.img of=/dev/sdb bs=4M oflag=sync status=progress

Make sure file buffers are flushed:

sync

Use an image file as disk.

Mount an image file containing a single filesystem, such as ISO image of an optical disc:

mount /my_archive_photos.ISO /photomnt -o loop

Unmount it to finish work with it:

umount /photomnt

Mount an image file of a multiple partition disk with something like this:

losetup -Pf /my_backup_hdd_image
mount /dev/loop0p1 /linuxmnt
mount /dev/loop0p2 /linuxmnt/boot

Unmount it to finish work with it:

umount /linuxmnt/boot
umount /linuxmnt
losetup -d /loop0

Trademark notices

Apple(R), MacBook(R), MacBook Air(R), MacBook Pro(R), iTunes(R), iBooks(R), App Store(R), iCloud(R), iMessage(R), FaceTime(R), Safari(R), FileVault(R), MagSafe(R), Touch Bar(TM), and macOS(R) are registered trademarks of Apple Inc. in the United States and other countries.
Linux(R) is registered trademark of Linus Torvalds in the United States and other countries.


Copyright (c) 2022 Leonid Dorogin