Skip to content

Instantly share code, notes, and snippets.

@alsyundawy
Forked from iambryancs/move-var-part-ubuntu.md
Last active May 21, 2024 19:41
Show Gist options
  • Save alsyundawy/dc9a1401b1847f3346266bf490ad5822 to your computer and use it in GitHub Desktop.
Save alsyundawy/dc9a1401b1847f3346266bf490ad5822 to your computer and use it in GitHub Desktop.
Move /var to another partition in Ubuntu

Move /var to another partition in Ubuntu

Create partition with fdisk or gdisk

fdisk -l
create a new GPT partition table by typing
gdisk /dev/sda
GPT fdisk (gdisk) version 1.0.5

Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present

Found valid GPT with protective MBR; using GPT.

create a new partition by typing n followed by the partition number 1

Command (? for help): n
Partition number (1-128, default 1): 1

and agree to all further prompts

finally, type “w” to write the changes to the drive and confirm by typing “Y”
First sector (34-11721045134, default = 2048) or {+-}size{KMGTP}:
Last sector (2048-11721045134, default = 11721045134) or {+-}size{KMGTP}:
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300):

Command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING PARTITIONS!!

Do you want to proceed? (Y/N): Y
OK; writing new GUID partition table (GPT) to /dev/sdb.
The operation has completed successfully.

Create fs

Assuming target fs is ext4 and device is /dev/sdb1

mkfs.ext4 /dev/sdb1

Create temp mount point

mkdir /var2

Mount partition to temp mount point

mount /dev/sdb1 /var2

Copy current /var content to new

rsync -a /var/ /var2

Get UUID

Run blkid and copy the UUID of /dev/sdb1

Add /etc/fstab

UUID="<UUID from previous step>" /var ext4 defaults 0 2

Post

You can cleanup the old /var to free up space by mounting / to another location using a LiveCD.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment