Skip to content

Instantly share code, notes, and snippets.

@ThinGuy
Created November 1, 2017 09:01
Show Gist options
  • Save ThinGuy/4befa0a7db030fd2c479238677628673 to your computer and use it in GitHub Desktop.
Save ThinGuy/4befa0a7db030fd2c479238677628673 to your computer and use it in GitHub Desktop.
Removing swap partitions and extend freespace to root filesystem. For servers with large RAM footprint where installer automatically created swap partition equal to the size of ram.
For LVM
--------
1) add your ssh key to root's account
e.g. sudo su - root
ssh-import-id <lp-id>
2) ssh into box as root. Don't do this via sudo!
3) get volume ids of what you want to remove and what you want to add it to
e.g. cat /etc/fstab
3.a) Comment out the lines that contain volume ids of that you are going to remove (i.e. /home or swap)
4) if swap, Comment out volume id from /etc/initramfs-tools/conf.d/resume
5) if swap, run swapoff -a
6.a) If working with /home, backup home directory
e.g. tar -czvf /tmp/home-backup.tgz .
6.a) if home, unmount it
e.g. umount -f /home
6.c) if busy, run lsof against each volume id and kill running processes
e.g. lsof /dev/mapper/myserver--aa--a01--vg-home
7) for each volume group, run lvremove <volume id>
e.g. lvremove /dev/mapper/myserver--aa--a01--vg-swap_1
e.g. lvremove /dev/mapper/myserver--aa--a01--vg-home
8) Add free space to vgroot
e.g. lvextend -l +100%FREE -r /dev/mapper/myserver--aa--a01--vg-root
9) Observe free space
e.g df -h
10 ) if you removed /home, restore contents
e.g tar -xzvf /tmp/home-backup.tgz -C /home
11) Remember to remove your public keys from root's .ssh/authorized_keys
For Non-LVM
--------
1) add your ssh key to root's account
e.g. sudo su - root
ssh-import-id <lp-id>
2) ssh into box as root. Don't do this via sudo!
3) get device id and partition of what you want to remove and what you want to add it to (i.e. /dev/sda4)
e.g. cat /etc/fstab
3.a) Comment out the lines that contain the UUID of that you are going to remove (i.e. /home or swap)
4) if swap, Comment out UUID of device from /etc/initramfs-tools/conf.d/resume
5) if swap, run swapoff -a
6.a) If working with /home, backup home directory
e.g. tar -czvf /tmp/home-backup.tgz .
6.a) if home, unmount it
e.g. umount -f /home
6.c) if busy, run lsof against each volume id and kill running processes
e.g. lsof /dev/mapper/myserver--aa--a01--vg-home
7) Run fdisk /dev/sdX -l and copy that off somewhere (where X is the device we are working with)
e.g. fdisk /dev/sda -l
8) Run fdisk /dev/sdX (where X is the device we are working with)
e.g. ~# fdisk /dev/sda
Welcome to fdisk (util-linux 2.27.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
8.a) p to print the partitions
Command (m for help): p
Disk /dev/sda: 894.2 GiB, 960139976704 bytes, 1875273392 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 131072 bytes / 262144 bytes
Disklabel type: gpt
Disk identifier: F5F3FC22-CBAF-4830-88F6-FF3775135DC0
Device Start End Sectors Size Type
/dev/sda1 2048 1050623 1048576 512M EFI System
/dev/sda2 1050624 264921087 263870464 125.8G Linux filesystem
/dev/sda3 264921088 1875271679 1610350592 767.9G Linux swap
8.b) d to delete the partition you want remove
Command (m for help): d
Partition number (1-3, default 3):3
Partition 3 has been deleted.
8.c) d to delete the existing partition that we are adding to
Command (m for help): d
Partition number (1,2, default 2): 2
Partition 2 has been deleted.
8.d) n to create new partition, First sector EXACTLY where old first sector started
Command (m for help): n
Partition number (2-128, default 2):
First sector (1050624-1875273358, default 1050624): 1050624
8.e) Last sector EXACTLY where removed partition ended
Last sector, +sectors or +size{K,M,G,T,P} (1050624-1875273358, default 1875273358): 1875271679
Created a new partition 2 of type 'Linux filesystem' and of size 893.7 GiB.
8.f) w to write new partition information
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Re-reading the partition table failed.: 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).
9) run partprobe
e.g. partprobe
10) Expand partion with free space from old partition with resize2fs
e.g. resize2fs /dev/sda2
resize2fs 1.42.13 (17-May-2015)
Filesystem at /dev/sda2 is mounted on /; on-line resizing required
old_desc_blocks = 8, new_desc_blocks = 56
The filesystem on /dev/sda2 is now 234277632 (4k) blocks long.
11) Observe free space
e.g df -h
12 ) if you removed /home, restore contents
e.g tar -xzvf /tmp/home-backup.tgz -C /home
13) Remember to remove your public keys from root's .ssh/authorized_keys
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment