ubuntu, updated

Partitions

Create and delete partitions on a disk with parted, including the non-interactive one-liners.

Before you start

  1. Find the right disk with lsblk. The new disk usually has no partitions and no mount point.
  2. Double-check the name. The commands below erase data on the disk you name.

Do you need a partition at all? For a pure data disk you can also skip it and put LVM or a file system straight on the disk. A partition is still the safer default: other tools and people immediately see that the disk is in use.

Create one partition over the whole disk

sudo parted -s /dev/sdb mklabel gpt                  # new, empty GPT partition table
sudo parted -s /dev/sdb mkpart data ext4 0% 100%     # one partition, whole disk
lsblk /dev/sdb                                       # -> sdb1 appears

ext4 here is only a label; the actual formatting comes next in Format and mount. For LVM, add sudo parted -s /dev/sdb set 1 lvm on.

Several partitions

sudo parted -s /dev/sdb mklabel gpt
sudo parted -s /dev/sdb mkpart logs  ext4 0%    20GiB
sudo parted -s /dev/sdb mkpart data  ext4 20GiB 100%

Interactive

If you prefer menus, both are fine:

ToolStartKey commands
fdisksudo fdisk /dev/sdbg new GPT, n new, d delete, p print, w write, q quit without saving
cfdisksudo cfdisk /dev/sdbarrow keys, menu at the bottom

Nothing is changed until you write (w).

Delete a partition

sudo umount /dev/sdb2                 # 1. unmount it (and remove its line in /etc/fstab)
sudo wipefs -a /dev/sdb2              # 2. optional: remove the file system signature
sudo parted -s /dev/sdb rm 2          # 3. delete partition number 2

If it is an LVM physical volume, remove it from LVM first (see LVM volumes).

Wipe a whole disk

sudo wipefs -a /dev/sdb               # removes partition table and all signatures

The data blocks are still there, but the disk now looks empty. For a secure wipe use shred or the disk’s secure-erase function.

Gotchas

§ home
~/knubertus/home/
1,1 Top