# Linux Partitions

## Create Partition \[^1]

This tutorial is copied from the tutorial from Satish Tiwary [**slashroot**.in](https://www.slashroot.in/how-create-new-partition-linux-0). Don't give me any credit.

{% hint style="info" %}
We will use the program `fdisk` for the following steps.
{% endhint %}

### Show Partitions

This following command will show all partition and the disk (with information about the size).

```bash
fdisk -l
```

### Create new partition

Select the disk you want to use, here we are using `/dev/sda`

```bash
fdisk /dev/sda
```

1. Then hit `n` for creating a new partition.
2. Select the partition number (just hit enter for the default)
3. Select the first sector, just hit enter
4. Then select the last sector. Here you can set the size of the partition. We are going to `+100G` which is 100 Gigabyte.
5. Then use insert `w` to write the changes, save them and exit

Use partprobe to update the changes of the partition table without restart.

```bash
partprobe
```

### Adding a file system for the partition

We will create the file system `ext4` for the partition `/dev/sda5`

```
mkfs.ext4 /dev/sda5
```

### Mount the partition to a directory

Create a new partition

```
mkdir /new
```

Mount the parition (`/dev/sda5`) to the new directory.

```
mount /dev/sda5 /new
```

To check if it's mounted you can use `df -h`

### Make the file system permanent

Append to the file `/etc/fstab`

{% code title="/etc/fstab" %}

```
/dev/sda5     /new     ext4    defaults  0 0
```

{% endcode %}

## Resize Partitions \[^2]

This tutorial is copied from the tutorial from Søren Løvborg [askubuntu.com](https://askubuntu.com/questions/24027/how-can-i-resize-an-ext-root-partition-at-runtime). Don't give me any credit.

{% hint style="warning" %}
This is only to enlarge the parition, shrinking won't work
{% endhint %}

### Delete the to resized partition

We first delete the partition you want to resize.

We select the device, where the partition is located, in this example it is `/dev/sda`.

```
fdisk /dev/sda
```

> Please copy the sector information about the parition. Then you can start the new created parition where the deleted one started. This means no data will be lost.

1. Type `p` to show the partitions
2. Type `d` to delete a partition
3. Select the partition number you want to resize (for example the last one)

Then we are going to create the partition again

1. Type `n` to create a new partition
2. Type select the partition number (for us we just hit enter)
3. Select the first sector (hit enter for default)
4. Select the last sector (here you can increase or decrease the partition)
5. remove the ext signature
6. Type `w` to save the changes

Use partprobe to update the changes

```
partprobe
```

### Resize the file system

Resize the file system, for this example we use the resized partition `/dev/sda3`

```
resize2fs /dev/sda3
```

### List of resources

* \[^1]: **slashroot**.in - *Satish Tiwary* - <https://www.slashroot.in/how-create-new-partition-linux-0>
* \[^2]: **askubuntu**.com- [*Søren Løvborg*](https://askubuntu.com/users/53746/s%C3%B8ren-l%C3%B8vborg) - <https://askubuntu.com/questions/24027/how-can-i-resize-an-ext-root-partition-at-runtime>
