Saturday, April 1, 2023

Storage partitions under linux - swap - ramdisk

Partitions

gparted, cfdisk, lsblk, df -h

mkfs.ext4 /dev/sda2 , mkfs.ntfs etc --- format the partition  

sudo fdisk -l     --- view disks
lsblk -f     --- view disks and filesystems
sudo blkid - partition UUID
df -h
df -ih --- innodes

sudo tune2fs -l /dev/sda8
sudo tune2fs -m 1 /dev/sda8
Reserved blocks for root - so when you run out of space you can still login. Set default to 5% of partition size..
*** merge online cu partitia mounted = ok
After you rezise partition size with cfdisk you need to also resize the filesystem:
sudo resize2fs /dev/sdb

Mounting

/mnt : Mount point for a temporarily mounted filesystem - should not have subfolders
/media : Mount point for removeable media

These 3 are required for chroot, 

are interfaces to kernel data structures and not real files on hdd - they stay in ram.

Can check with the command findmnt /sys
/dev - file devices
/proc - running process table + some system info same as sys left for backward compatibility 
/sys - hardware information

/run - virtual ram runtime for udev - needed before /var/run is mounted

/etc - system config files - before changes would be done in source code and recompile
means etcetera - as in the rest of the files of a system

/var - variable stuff like logs
/opt - optional software not installed with system package manager
/usr - libraries, binaries, manuals - all the software

/etc/fstab --- automount at start-up - should at least have /

mount -a --- mount all from fstab

UUID=14D82C19D82BF81E /mnt/d auto nosuid,nodev,nofail,x-gvfs-show 0 0

1. partition id
2. mount point
3. partition type 
4. options

nosuid - specifies that the filesystem cannot contain set userid files. This prevents root escalation and other security issues.
nodev - specifies that the filesystem cannot contain special devices (to prevent access to random device hardware).
nofail - removes the errorcheck.
x-gvfs-show - show the mount option in the file manager. If this is on a GUI-less server, this option won't be necessary.
0 - determines which filesystems need to be dumped (0 is the default).
0 - determine the order in which filesystem checks are done at boot time (0 is the default).

mount /dev/sda1 /mnt

NTFS partition from windows closed without proper shutdown ---> ntfsfix /dev/sda1
If that doesn't work you need to boot a windows os and it will fix it. ntfs is not opensource.

See mounted filesystems and space usage : 
du -sh -- file/folder size

INODES on linux

The file system has an inode table - every file and directory on a disk requires one inode entry.

To have more inode you need to re-format the filesystem and reserve more space for the inode table.

Other filesystems dont allocate a fixed inode table like XFS, JFS, BtrFS or ZFS.

On ext3 and ext4 - the most common used filesystems on linux you get by default 1 inode for every 16kb of space.
using the -i flag to mkfs.ext4 to specify the bytes:inode ratio. 

This problem is most common on servers where you can get a lot of small files.
Apache’s disk cache system (mod_cache_disk) - creates a lot of small files - htcacheclean should clean it.

Swap

Instead of storage swap partition you can have a swap partition in ram.
Of course this will not work for hibernation.

Swap file

dd if=/dev/zero of=/swapfile bs=1M count=3064 status=progress
chmod 600 /swapfile
mkswap -U clear /swapfile
swapon /swapfile
Add it to fstab
/swapfile none swap defaults 0 0 >> /etc/fstab

Ram swap

When ram gets full, data is moved and compressed into this z swap partition.

To enable it, open a cmd and create these files :

sudo su
echo zram > /etc/modules-load.d/zram.conf
echo 'options zram num_devices=1' > /etc/modprobe.d/zram.conf
echo 'KERNEL=="zram0", ATTR{disksize}="512M",TAG+="systemd"' > /etc/udev/rules.d/99-zram.rules

touch /etc/systemd/system/zram.service

[Unit]
Description=Swap with zram
After=multi-user.target

[Service]
Type=oneshot
RemainAfterExit=true
ExecStartPre=/sbin/mkswap /dev/zram0
ExecStart=/sbin/swapon /dev/zram0
ExecStop=/sbin/swapoff /dev/zram0

[Install]
WantedBy=multi-user.target

sudo systemctl enable zram
reboot 

swapiness - sudo nano /etc/sysctl.conf

The default value is 60."
0 - does not disable swap

To use a swap partition instead

# mkswap /dev/sda5
# sync
# swapon /dev/sda5

Ram-disk

For windows :
The only free software that allows for more then 4gb is ImDisk Toolkit

For linux  :
First check how much ram you got : free -h
Make a new directory and mount the ramdisk :

sudo mkdir /mnt/ramdisk
sudo mount -t tmpfs -o size=10g tmpfs /mnt/ramdisk
tmpfs - is mounted in ram
To make it available every time you boot edit /etc/fstab :

tmpfs       /mnt/ramdisk tmpfs   nodev,nosuid,noexec,nodiratime,size=1024M   0 0