add new partition to your server

Below are the steps I performed to add a new partition to my server.

Initial state

My initial partitioning is as follow:

remy@r12925:~$ sudo fdisk -l
Disk /dev/sda: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 1 392 3148708+ 83 Linux
/dev/sda2 393 1305 7333672+ 83 Linux

remy@r12925:~$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 3.0G 1.8G 1.1G 62% /
tmpfs 228M 8.0K 228M 1% /lib/init/rw
udev 10M 32K 10M 1% /dev
tmpfs 228M 0 228M 0% /dev/shm
/dev/root 4.6M 4.6M 0 100% /initrd
/dev/sda2 6.9G 4.1G 2.5G 63% /home

I’ve got more or less 10Go of space split between / and /home.

Now I want to add an extre 10G0 of space that will be used to store logs into a separate partition (so that they will no longer fill my disk-space and prevent apache, php & mysql from working properly) and to store backup files.

note: on this server, there is no swap space

Next step: we add our new partition

we will do this through the use of ‘fdisk’ tool and use the following commands :

  • ‘p’ to print current partition state and check that everything is as expected
  • ‘n’ to add a new partition
  • ‘q’ to quit in case we did something wrong and we do not want to write changes
  • ‘w’ to write down our changes once we are sure to get what we wanted

Below is an output of all successive commands I wrote so that you can adapt it to your own case:

remy@r12925:~$ sudo fdisk /dev/sda

The number of cylinders for this disk is set to 2610.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): p

Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x000a7b5b

Device Boot Start End Blocks Id System
/dev/sda1 * 1 392 3148708+ 83 Linux
/dev/sda2 393 1305 7333672+ 83 Linux

Command (m for help): n
Command action
e extended
p primary partition (1-4)
e
Partition number (1-4): 3
First cylinder (1306-2610, default 1306):
Using default value 1306
Last cylinder, +cylinders or +size{K,M,G} (1306-2610, default 2610):
Using default value 2610

Command (m for help): p

Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x000a7b5b

Device Boot Start End Blocks Id System
/dev/sda1 * 1 392 3148708+ 83 Linux
/dev/sda2 393 1305 7333672+ 83 Linux
/dev/sda3 1306 2610 10482412+ 5 Extended

Command (m for help): n
Command action
l logical (5 or over)
p primary partition (1-4)
l
First cylinder (1306-2610, default 1306):
Using default value 1306
Last cylinder, +cylinders or +size{K,M,G} (1306-2610, default 2610): +2G

Command (m for help): p

Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x000a7b5b

Device Boot Start End Blocks Id System
/dev/sda1 * 1 392 3148708+ 83 Linux
/dev/sda2 393 1305 7333672+ 83 Linux
/dev/sda3 1306 2610 10482412+ 5 Extended
/dev/sda5 1306 1567 2104483+ 83 Linux

Command (m for help): n
Command action
l logical (5 or over)
p primary partition (1-4)
l
First cylinder (1568-2610, default 1568):
Using default value 1568
Last cylinder, +cylinders or +size{K,M,G} (1568-2610, default 2610):
Using default value 2610

Command (m for help): p

Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x000a7b5b

Device Boot Start End Blocks Id System
/dev/sda1 * 1 392 3148708+ 83 Linux
/dev/sda2 393 1305 7333672+ 83 Linux
/dev/sda3 1306 2610 10482412+ 5 Extended
/dev/sda5 1306 1567 2104483+ 83 Linux
/dev/sda6 1568 2610 8377866 83 Linux

Command (m for help):

————————————————————————–
If there are any mistakes just quit “fdisk” with a “q” and no changes will be saved. This looks right – so lets write our changes with a “w”

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

Loading new partition

As we can see, a warning was issued, partprobe failed to run correctly and as such the new partition was not loaded into the server.

Unfortunately, even after installing ‘kpartx’ and ‘parted’ packages (apt-get install kpartx parted), running partprobe on my own as root failed as well.

remy@r12925:~$ sudo partprobe
Warning: WARNING: the kernel failed to re-read the partition table on /dev/sda (Device or resource busy). As a result, it may not reflect all of your changes until after reboot.

As a consequence, I had to reboot the server completely 🙁

Mounting the new partitions

Know we want to mount our new partitions. We will keep 3G0 for logs and 7Go for backup. We will use the following mount points: /var/log & /var/backups.

We will perform the following steps:

  1. set ext3 as the filesystem for our partitions
  2. mv current content of /var/log & /var/backups into dummy folders (mounting it directly will make content of these folders ‘disappear’)
  3. mount our partitions to /var/log & /var/backups
  4. mv previous content back into their original locations (ie. in the newly mounted partitions)
  5. if everything works as expected, we will add our mount points to /etc/fstab so that partitions will be automatically mounted upon server reboot

Below is the output of the steps I followed for my server so that you can have a look at it and customize it:

remy@r12925:~$ sudo mkfs.ext3 /dev/sda5
mke2fs 1.41.11 (14-Mar-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
131648 inodes, 526120 blocks
26306 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=541065216
17 block groups
32768 blocks per group, 32768 fragments per group
7744 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912

Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 37 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
remy@r12925:~$ sudo mkfs.ext3 /dev/sda6
mke2fs 1.41.11 (14-Mar-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
524288 inodes, 2094466 blocks
104723 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2147483648
64 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 29 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.

——————————————————————————
Filesystem Hierarchy Standard (FHS)
remy@r12925:~$ sudo mkdir /zz_backup /zz_logs
remy@r12925:~$ sudo mv /var/backups/* /zz_backups
remy@r12925:~$ sudo mv /var/log/* /zz_logs

remy@r12925:~$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 3.0G 1.8G 1.1G 62% /
tmpfs 228M 8.0K 228M 1% /lib/init/rw
udev 10M 44K 10M 1% /dev
tmpfs 228M 0 228M 0% /dev/shm
/dev/root 4.6M 4.6M 0 100% /initrd
/dev/sda2 6.9G 4.1G 2.5G 63% /home
/dev/sda5 2.0G 68M 1.9G 4% /var/log
/dev/sda6 7.9G 147M 7.4G 2% /var/backups

remy@r12925:~$ sudo mv /zz_backups/* /var/backups/
remy@r12925:~$ sudo mv /zz_logs/* /var/log/
remy@r12925:~$ sudo rm -R /zz_backups /zz_logs

remy@r12925:~$ sudo vi /etc/fstab
/dev/sda1 / ext3 errors=remount-ro,noatime,nodiratime 0 1
/dev/sda2 /home ext3 defaults,noatime,nodiratime 0 2
proc /proc proc defaults 0 0
sysfs /sys sysfs defaults 0 0
/dev/uba none swap sw 0 0
/dev/sda5 /var/log ext3 defaults,noatime,nodiratime 1 1
/dev/sda6 /var/backups ext3 defaults 1 1
remy@r12925:~$ sudo mount -a

That’s it!

note: in case you mounted two or more partitions to the same mount point (due to a copy & paste error in your /etc/fstab file), do not panic! simply update /etc/fstab, unmount the mount points the number of times necessary for it to no longer be mounted (sudo umount <mount_point>), run “sudo mount -a”

sources

Post a Comment

Your email is never published nor shared. You're allow to say what you want...