Disk Management



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

sudo blkid          # get label, UUID and type


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Make a Bootable USB Drive


• All data will be wiped

# Write ISO to your USB flash drive
sudo dd bs=4M if=/path/to/iso of=/dev/sdX status=progress && sync

# bs                block size
# if                specify the source
# of                specify the destination
# status=progress   (optional) show progress
# sync              clear the cache


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Format a bootable flash drive to normal



# List device names
sudo fdisk -l

# Clean flash drive
sudo wipefs --all /dev/sdb

# Create a new partition
sudo cfdisk /dev/sdb # select dos type > New > Write > Quit
# Type 83 - Linux; other: c - W95 FAT32 (LBA)

# Create file system
sudo mkfs.vfat -n 'label' /dev/sdb1 # format partition 1 as FAT sys

# If the above fails, overwrite drive with 0s. Then use cfdisk utility (see above)
sudo umount /dev/sdb
sudo dd if=/dev/zero of=/dev/sdb # takes a long time


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Swap


Swap partition is more reliable than swap file but the size can't be changed
Swap file is for home use and the size can be changed easily at any time
• Rule of thumb: swap size

RAMSwap
< 1G2G
2-4G2-4G
8G4G
> 8G2-4G


# Check swap size
sudo swapon
free -h


• Create/ Resize swap file

sudo fallocate -l 1G /swapfile
# -l        specify size of swap file

# For F2FS or XFS, use dd command
sudo dd if=/dev/zero of=/swapfile bs=1M count=1024


sudo chmod 600 /swapfile
sudo mkswap /swapfile       # format
sudo swapon /swapfile       # activate the swap
sudo swapon --show          # verify


◇ To make the change permanent, add the following to /etc/fstab

/swapfile none swap defaults 0 0


• Remove swap file

sudo swapoff -a /swapfile   # de-activate
sudo rm -f /swapfile
# Also remove the entry from /etc/fstab


• Swapiness
◇ Swapiness defines how often the kernel use RAM instead of swap (default = 60)

# Check swapiness
cat /proc/sys/vm/swapiness

# Change swapiness
sudo nano /etc/sysctl.conf

vm.swappiness=10 # add this line to sysctl.conf


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Encrypt Storage Devices



• Backup data in target device first. This will overwrite and erase all data

# Install cryptsetup
sudo apt install cryptsetup

# List block devices (partition info)
lsblk

# Encrypt the drive
sudo cryptsetup --verbose --verify-passphrase luksFormat /dev/sdb1

# Open encrypted drive
sudo cryptsetup luksOpen /dev/sdb1 sdb1

# Once the encrypted drive is opened, it's mapped to /dev/mapper/sdb1
sudo fdisk -l

# Create file system on it
sudo mkfs.ext4 /dev/mapper/sdb1

# If you don't run your system in the parition, you can get rid of space reserved by ext4
sudo tune2fs -m 0 /dev/mapper/sdb1


• To use the encrypted volume

sudo mkdir /mnt/encrypted
sudo mount /dev/mapper/sdb1 /mnt/encrypted


• By default, root is required to access encrypted parition but you can change it with chown command

sudo chown -R `whoami`:users /mnt/encrypted


• When you're done with the encrypted volume, unmount it and close the mapped device

sudo umount /dev/mapper/sdb1
sudo cryptsetup luksClose sdb1


• To use the encrypted volume next time

lsblk
sudo cryptsetup luksOpen /dev/sdb1 sdb1
sudo mount /dev/mapper/sdb1 /mnt/encrypted


• Auto mounted encrypted partitions at boot. Applicable only on LUKS encryption

lsblk                               # Get name of encrypted device
sudo cryptsetup luksUUID /dev/sdb1  # Get uuid of encrypted device
sudo nano /etc/crypttab

# Add the following line to /etc/crypttab
sdb1 /dev/disk/by-uuid/fd3c01ad-0e59-4bc1-9bda-7c61e00b36cf none luks
# none - no key files used
# You can use key files if your system partition is encrypted 

sudo mkdir /mnt/encrypted_sdb1      # Define mount point
sudo nano /etc/fstab

# Add the following line to /etc/fstab
/dev/mapper/sdb1 /mnt/encrypted_sdb1 ext4 defaults 0 2

Index