Partitioning for Servers and Multi-User Systems

Instead of cutting it into one giant slice, we can create smaller portions or partitions so everyone gets their fair share without having to fight over the last crumb.

In computer science terms, partitioning is essentially dividing up your hard drive into separate sections for specific purposes. This not only helps with organization but also improves performance and minimizes damage in case of disk corruption. For example, you might create a partition specifically for storing all your important files like photos or documents so they’re easily accessible and don’t get mixed up with other stuff on your computer.

To do this, we use the fdisk command which is essentially a text-based tool that allows us to view and manage disk partitions on our Linux system. First, we list all the current partitions using the ‘l’ option:

#!/bin/bash # This line specifies the interpreter to be used for executing the script

# This script uses the fdisk command to view and manage disk partitions on a Linux system

sudo fdisk -l # This command lists all current partitions on the system with root privileges

This will show you a list of available disks along with their respective sizes and types (e.g. ext4 or swap). To create a new partition, we enter command mode by typing ‘n’:

# This script uses the fdisk command to create a new partition on the /dev/sda disk.
# The user will be prompted to enter commands in command mode.

# Use sudo to run fdisk with root privileges.
sudo fdisk /dev/sda

# Prompt the user to enter a command in command mode.
# The 'n' command is used to create a new partition.
Command (m for help): n

# Prompt the user to select the partition type.
# The available options are primary and extended.
# The number in parentheses indicates the number of existing partitions of each type.
Partition type:
   p   primary (0 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)

# The default option is primary, so the user can simply press Enter to select it.
Select (default p): p

# Prompt the user to enter the partition number.
# The default option is 1, so the user can simply press Enter to select it.
Partition number (1-4, default 1): 1

# Prompt the user to enter the first sector of the partition.
# The default option is 2048, so the user can simply press Enter to select it.
# The '+10M' option creates a partition with a size of 10 megabytes.
First sector (2048-78161590399, default 2048): +10M

In this example, we’re creating a primary partition with the ‘p’ option and specifying that it should start at the first available free sector on our disk (which is usually around 2MB). We then choose to create a new partition of size 10 MB using the ‘+’ sign followed by the desired unit (in this case, megabytes or ‘M’).

Once we’ve created our new partition, we can format it using the appropriate mkfs command:

#!/bin/bash # This line specifies the interpreter to be used for executing the script

# This script is used to format a new partition on our disk

# First, we need to find the first available free sector on our disk
# We can use the 'fdisk' command to list all available disks and their partitions
# Then, we can use the 'df' command to check the available space on each partition
# Finally, we can use the 'grep' command to filter the output and find the first available free sector
# We will store this information in a variable called 'free_sector'
free_sector=$(fdisk -l | df | grep -m 1 "Free" | awk '{print $1}')

# Now, we can use the 'fdisk' command to create a new partition on our disk
# We specify the disk we want to use (in this case, /dev/sda) and the starting sector (which is the first available free sector we found earlier)
# Then, we use the '+' sign to indicate that we want to create a new partition
# Finally, we specify the size of the partition (in this case, 10 MB) and the unit (megabytes or 'M')
# We will store the output of this command in a variable called 'new_partition'
new_partition=$(fdisk /dev/sda <<EOF
n
p
$free_sector
+10M
w
EOF
)

# Once we've created our new partition, we can format it using the appropriate mkfs command
# In this case, we want to use the 'ext4' filesystem, so we use the 'mkfs.ext4' command
# We specify the partition we want to format (in this case, /dev/sda1)
# We use the 'sudo' command to run this command with root privileges
sudo mkfs.ext4 /dev/sda1

This will create a file system on our newly-created partition so that we can start storing files and data right away! And if you ever need to make changes or adjustments, simply enter command mode again using ‘n’ and follow the prompts. It may seem overwhelming at first but trust me, once you get the hang of it, creating partitions will be a breeze!

SICORPS