

How to repartition disk in ubuntu server a comprehensive guide to disk partitioning, resizing, LVM, and filesystem management
Yes, here’s a comprehensive guide to repartitioning a disk on Ubuntu Server. In this post you’ll find a practical, step-by-step plan for planning, backing up, resizing and creating partitions, and using LVM when appropriate. You’ll also get real-world tips, common pitfalls, and a quick reference of commands you’ll actually reach for. This guide is written with admins in mind who want reliable results with minimal downtime, plus a few backup-hardening ideas. Below is a short summary of what you’ll learn, followed by deeper sections, practical steps, and a robust FAQ.
- How to assess your current disk layout and plan changes
- Tools to use fdisk, gdisk, parted, pvcreate, vgcreate, lvextend, resize2fs, xfs_growfs, etc.
- Non-destructive vs destructive repartitioning workflows
- How to resize and extend logical volumes LVM online
- How to create new partitions and filesystems for data directories
- How to migrate data safely and verify integrity
- Common pitfalls and recovery tips
Useful URLs and Resources text only
Ubuntu Documentation – ubuntu.com
GNU Parted – gParted.org
GNU fdisk Manual – man7.org/linux/man-pages/man8/fdisk.8.html
LVM HOWTO – linux.yandex.ru
XFS File System – wiki.archlinux.org/index.php/XFS
Ext4 File System – kernel.org
Rescue from a live USB – ubuntu.com/tutorials/booting-into-a-live-environment
rsync Manual – rsync.samba.org/rsync.html
Filesystem Quotas – man7.org/linux/man-pages/man5/quota.5.html
Planning and prerequisites
- Back up everything. Partitions represent the single fastest path to data loss if something goes wrong.
- Identify the disks and partitions you’ll touch. Use lsblk, blkid, and df -h to map devices to mount points and filesystem types.
- Decide on a strategy:
- Non-destructive expansion: grow an existing partition or LV to use free space.
- Destructive repartitioning: re-create partitions and migrate data.
- Data separation: create new partitions for /data or other mount points to improve organization and backup scope.
- Consider the underlying filesystem. ext4 is common on Ubuntu Server; XFS is favored for large data directories. If you’re using LVM, you can resize LV and filesystem more flexibly.
- If you’re working on a boot disk root, plan for potential downtime. In many server setups, the root filesystem is inside LVM, which makes online resizing possible; otherwise you’ll need a live environment to resize.
Tools you’ll use
- Disk layout and modification: fdisk, gdisk, parted, sgdisk
- Partition resizing: parted recommended for GPT, fdisk MBR/legacy
- LVM management: pvresize, vgextend, lvextend, lvresize, pvcreate, vgcreate, vgscan
- Filesystem resize: resize2fs ext4, e2fsprogs, xfs_growfs XFS
- Data migration and copy: rsync, tar, dump/restore specific setups
- Live environment: Ubuntu Server live USB or other rescue media when resizing root without LVM
Non-destructive vs destructive repartitioning
- Non-destructive:
- Extend an existing partition into unallocated space partition must have space immediately after it, or use a tool that supports moving with care
- Extend a logical volume LV in LVM and then grow the filesystem
- Create a new partition and format it, then migrate data to it
- Destructive:
- Repartition a disk to change partition boundaries significantly and reformat the filesystem
- Requires full data backup and restore
Step-by-step guide: non-destructive expansion when using LVM
This section covers a common scenario: you have a root volume inside LVM and you’ve added a new disk or have unallocated space. You want to extend the root LV non-disruptively.
- Check current layout
- Run: lsblk -f
- Verify which volume group and logical volume hold your root filesystem for example /dev/ubuntu-vg/root
- Prepare unallocated space if you added a disk or resized a partition
- If you added a new disk /dev/sdb: create a new PV on it
- pvcreate /dev/sdb1
- vgextend ubuntu-vg /dev/sdb1
- If you have free space in the same disk after a partition, resize the partition to capture it, then pvresize the PV:
- pvresize /dev/ubuntu-vg/root or pvresize /dev/sda2 depending on your PV
- Extend the logical volume
- lvextend -l +100%FREE /dev/ubuntu-vg/root
- This uses all free extents in the volume group; adjust as needed.
- Resize the filesystem
- If ext4:
- resize2fs /dev/ubuntu-vg/root
- If xfs:
- xfs_growfs /mount/point or xfs_growfs /dev/ubuntu-vg/root -D size if mounted at a non-standard mount
- Verify
- df -h
- lsblk
- Ensure the root filesystem shows the new size and there’s no data loss
Note: If the root filesystem is not on an LV for example, a simple partition mounted at /, you may need to resize the LV anyway if the root partition is enlarged via the disk partition itself. In that case, you might need to boot from a live USB to resize the root partition and filesystem safely.
Step-by-step guide: resizing a non-LVM partition ext4
- Back up and boot plan
- Create a backup plan; consider using rsync to mirror important data to /data or another disk.
- Ensure you have unmounted space or can shrink the partition
- If you can shrink a partition to create unallocated space, you’ll first shrink the filesystem:
- If the partition is not in use or you can do this safely, unmount to shrink.
- For root, you’ll generally need a live environment to shrink the filesystem.
- Shrink the filesystem
- If the partition is ext4 and unmounted:
- e2fsck -f /dev/sda1
- resize2fs /dev/sda1 100G choose a new size
- Resize the partition
- Use parted or fdisk to shrink the partition to the new size:
- parted /dev/sda
- parted resizepart 1 100G
- parted quit
- Verify and mount
- Run: lsblk -f, df -h
- Re-mount if needed and verify data integrity
- Optional: Create a new partition for data
- Create a new primary partition in the now unallocated space
- Format it e.g., mkfs.ext4 /dev/sda2
- Mount it to /data and copy data if needed
Important note: Root and other critical system partitions are tricky to resize while mounted. In most cases, you’ll use a live CD/USB or a rescue environment to safely resize the root partition.
Step-by-step guide: adding a new disk and using it for data or as extra space
- Connect and identify the new disk
- Run: lsblk or sudo fdisk -l
- Identify as /dev/sdb for example
- Create a partition and filesystem
- Partition: sudo fdisk /dev/sdb or gdisk/parted for GPT
- Create a new partition, typically /dev/sdb1
- File system: sudo mkfs.ext4 /dev/sdb1
- Mount and persist
- Create a mount point: sudo mkdir /data
- Mount: sudo mount /dev/sdb1 /data
- Add to /etc/fstab to persist across reboots
- Optional: migrate data
- rsync -aAXv –exclude={“/data/”} /olddata/ /data
- Ensure permissions and ownership are correct
- Expand later
- If you later add more disks, you can extend the volume group if using LVM or create more partitions and mount points.
Common pitfalls and troubleshooting
- Data loss risk: Always back up before resizing. A failed resize can corrupt the filesystem.
- Root partition resizing: Avoid resizing root while running from the same root FS; use a live USB or rescue mode.
- Mismatched UUIDs and fstab: If a partition’s UUID changes, update /etc/fstab accordingly.
- Alignment issues: When creating partitions, align to 1MiB boundaries for best performance.
- LVM metadata misalignment: If lvextend or pvresize fails, re-check PV size and VG state with vgdisplay and pvdisplay.
- Boot issues after changes: Ensure boot loader entries remain valid if you touch the boot disk.
Best practices and performance tips
- Use LVM for flexible growth: It’s easier to extend storage later without heavy downtime.
- Align partitions: Create partitions at 1MiB boundaries to improve performance on modern drives.
- Keep / and /var separate if you expect growth in logs and caches; this makes backups and maintenance easier.
- Regular backups: Maintain a robust backup plan, especially before major repartitioning or resizing.
- Documentation: Keep a record of what you changed and the exact commands you ran to help future maintenance.
Real-world scenarios and case studies
Scenario A: You have a 120GB root disk with 30GB free and want to add 50GB for /var
- Add a new partition on the same disk or add a new disk
- If using LVM, pvcreate on new partition, vgextend, lvextend root or create a new LV for /var
- Resize the filesystem accordingly ext4: resize2fs; if /var is on its own LV, resize after lvextend
Scenario B: You added a 200GB disk and want to use it for /data How to Find the Primary DNS Server The Ultimate Guide: DNS Addresses, Primary vs Secondary DNS, and Troubleshooting
- Create /data and format with ext4
- Mount and back up existing data to /data
- Update /etc/fstab for automatic mounting on boot
- Consider moving large datasets to the new disk and symlink or bind-mount if needed
Scenario C: Shrinking a non-LVM partition to free space for other uses
- Shrink the filesystem first if possible, then shrink the partition
- The new free space can be used to create another partition or extend an LV if using LVM
Quick reference commands
- List disks and partitions: lsblk -f
- Show disk usage: df -h
- Create a new PV on a disk: pvcreate /dev/sdb1
- Extend an LV: lvextend -l +100%FREE /dev/ubuntu-vg/root
- Resize an ext4 filesystem: resize2fs /dev/ubuntu-vg/root
- Grow an XFS filesystem: xfs_growfs /
- Resize a PV to reflect new size: pvresize /dev/sdb1
- Add a disk to an existing VG: vgextend ubuntu-vg /dev/sdb1
- Copy data safely: rsync -aAXv /source/ /destination/
- Mount with persistence: echo ‘/dev/sdb1 /data ext4 defaults,bind 0 0’ >> /etc/fstab example; adjust
Frequently Asked Questions
How can I repartition without losing data on Ubuntu Server?
You should back up, plan carefully, and prefer non-destructive methods like resizing an LV and filesystem with the system online, or using a live environment for root partition changes. Always verify the filesystem integrity after any resize with fsck and ensure you have tested boot/debug in a safe environment before relying on it in production.
Can I resize the root partition while the system is running?
If root is on an LVM logical volume, you can often resize online with less downtime. If not, you’ll typically need to boot from a live USB or rescue environment to resize the root partition safely.
What are the differences between fdisk, parted, and gdisk?
- fdisk is best for MBR disks and basic partitioning; it’s quick but less feature-rich for modern GPT disks.
- parted supports both MBR and GPT and handles larger disks and advanced layouts with more flexible resizing.
- gdisk is the GPT-aware equivalent of fdisk and handles GPT disks with more robust features.
How do I resize an ext4 filesystem?
Use resize2fs to resize the ext4 filesystem after resizing the underlying partition and ensuring filesystem integrity with e2fsck first if needed.
How do I resize an XFS filesystem?
XFS supports online growth with the xfs_growfs command. If the XFS filesystem is on a mounted LV, you can run xfs_growfs on the mounted filesystem; if on a non-root partition, mount it and grow accordingly. Discover How to Find When Someone Changes DNS Server Log and Audit DNS Activity
How do I add a new disk to an existing LVM setup?
Create a PV on the new disk pvcreate /dev/sdb1, extend the VG vgextend volume_group_name /dev/sdb1, then lvextend to grow the LV, and finally resize the filesystem.
Should I always use LVM for Ubuntu Server disks?
LVM provides flexibility and is widely used on servers for easier growth and snapshots. If you don’t need the extra complexity, you can use standard partitions, but you’ll lose some flexibility later.
What if the boot loader stops working after repartitioning?
You can recover via a live USB, chroot into your installed system, verify /boot and boot loader configurations, reinstall GRUB if necessary, and update the boot entries.
How do I safely migrate data to a new partition or disk without downtime?
Use rsync to mirror data to the new location, verify integrity, update fstab and mount points, and perform a minimal downtime cutover by stopping affected services, if possible.
How can I verify that my repartitioning was successful?
Check disk space with df -h, verify partition sizes with lsblk -f, confirm the filesystem type, and ensure data accessibility. Run a file-level check e.g., grep, ls, cat tests to validate proper data access. How to Open SQL Server in Visual Studio 2017 A Step by Step Guide: Connect, LocalDB, SSDT
What backup strategy should I follow before repartitioning?
Back up critical data to an external disk or cloud storage, create a filesystem-level snapshot if available like LVM snapshots, and test restoring a sample dataset to ensure the backup works.
Can I repartition a live server over the network?
Resizing root or boot partitions from a live environment is safer, but some online LV extensions are possible. If you must work remotely, ensure you have console access and a tested rollback plan, along with a dependable backup strategy.
How do I migrate an entire system to a larger disk with minimal downtime?
Clone the disk or use rsync to replicate the system to a larger disk, adjust fstab and bootloader entries, and perform a careful switch during a maintenance window. Always test boot on the new disk in a controlled environment first.
What are best practices for partition alignment?
Align partitions to 1 MiB boundaries 2048 sectors on 512e disks to optimize performance and reduce write amplification, especially on SSDs and modern HDDs.
Is it safe to delete a partition to reclaim space?
Only after you’ve confirmed you’ve backed up the data and have a recovery plan. Deleting a partition risks data loss if you select the wrong device. The Ultimate Guide How To Share A Server In Discord Like A Pro
How do I recover data if a partitioning operation goes wrong?
If you have a backup, recover from backup. If not, you may attempt data recovery tools, but success is not guaranteed. Always keep a known-good backup before partition changes.
If you need more hands-on help with a specific setup for example, exact commands for your current partition map or a command-by-command walkthrough for your scenario, tell me your disk layout output of lsblk -f, df -h, and uname -r, and I’ll tailor the steps to your environment.
Sources:
如何在macbook上轻松安装和使用vpn?新手指南:macOS上VPN安装步骤、隐私保护与速度优化
Proxy microsoft edge: how to configure proxies in Microsoft Edge with VPNs, IP masking, and geo-unblocking How to enable sftp server in ubuntu a comprehensive guide