Linux partition table

From Ever changing code
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

The headline it should be Working with Partition Table in Linux but Google search phrases take precedences as these are more human language much often than we think. Vote if you disagree!

List partitions

sudo blkid -o list         # read more http://linux.101hacks.com/unix/blkid/
device      fs_type label    mount point     UUID
----------------------------------------------------------------------------------
/dev/sda1   ext4             /               4a5d5028-062d-4a4b-9d1a-c4df9708ef86
/dev/sda5   swap             <swap>          bc8348c5-c188-4627-9df4-32bba94c9c8b
lsblk 
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0  55.9G  0 disk 
├─sda1   8:1    0  54.4G  0 part /
├─sda2   8:2    0     1K  0 part 
└─sda5   8:5    0   1.5G  0 part [SWAP]
sudo fdisk -l
Disk /dev/sda: 60.0 GB, 60011642880 bytes
255 heads, 63 sectors/track, 7296 cylinders, total 117210240 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000f12f1

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048   114083839    57040896   83  Linux
/dev/sda2       114085886   117209087     1561601    5  Extended
/dev/sda5       114085888   117209087     1561600   82  Linux swap / Solaris
sudo sfdisk -ls
/dev/sda:  58605120

Disk /dev/sda: 7296 cylinders, 255 heads, 63 sectors/track
Warning: extended partition does not start at a cylinder boundary.
DOS and Linux will interpret the contents differently.
Units = cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0

   Device Boot Start     End   #cyls    #blocks   Id  System
/dev/sda1   *      0+   7101-   7102-  57040896   83  Linux
/dev/sda2       7101+   7295-    195-   1561601    5  Extended
/dev/sda3          0       -       0          0    0  Empty
/dev/sda4          0       -       0          0    0  Empty
/dev/sda5       7101+   7295-    195-   1561600   82  Linux swap / Solaris
  • -l : List the partitions of a device.
  • -s : List the size of a partition.
  • -u or -uS or -uB or -uC or -uM : Accept or report in units of sectors (blocks, cylinders, megabytes, respecpively). The default is cylinders, at least when the geometry is known


Create a single optimal partition

This can be useful for provisioning EBS disks on AWS instances.

# Provision an entire disk
parted -a optimal /dev/xvdb
partition table, partition map
(parted) mklabel msdos
Warning: The existing disk label on /dev/xvdb will be destroyed and all data on this disk will be lost. Do you want to continue?
Yes/No? Yes
(parted) mkpart primary ext2 0% 100%
(parted) set 1 lvm on
# | 'mklabel msdos' must be one of these supported disk labels: 
# | bsd, loop (raw disk access), gpt, mac, msdos, pc98, sun. “disk label” same thing as 

# Create a filesystem (format disk)
sudo mkfs.ext4 /dev/xvdb1

# Mount
sudo mount /dev/xvdb1 -f ext4 /mnt/xvdb1

Sync disk after partition altering

The OS must be "forced" to re-read the partition table once changes have been done. A reboot does this and hence you see fdisk changes after a reboot.

$ partprobe
# partprobe is part of parted package

Permanent mount options /etc/fstab

fstab is a configuration file that contains information of all the partitions and storage devices in your computer. /etc/fstab contains information of where your partitions and storage devices should be mounted and how.

       COL 1             COL 2          COL 3   COL 4               COL 5 6   
|----------------------|-------------|-------|-------------------------|-|-|
LABEL=cloudimg-rootfs   /              ext4   defaults,discard          0 0
/dev/fd0                /media/floppy  auto   rw,noauto,user,sync       0 0
/dev/cdrom              /media/cdrom   auto   ro,noauto,user,exec       0 0 
/dev/xvdb1              /mnt/xvdb1     ext4   defaults                  0 0
  • 1st column - device
  • 2nd column - default mount point. It tells where to mount device mount /dev/fd0 if mounting point is not specified
  • 3rd column - filesystem type. Value auto means that the file system type is detected automatically usually used for /dev/fd0 and /dev/cdrom0 as the type may vary
  • 4th column - mount options
    • auto - device will be mounted automatically at boot or when you issue mount -a
    • noauto - device needs to be mouted explicitly
    • user - allows normal user mount device
    • nouser - only root can mount the device
    • exec - lets you execute binaries; it is default option
    • noexec - doesn't allow execute binaries
    • ro - mount the file system read-only
    • rw - mount as read-write filesystem; it is default option
    • sync and async - check wiki as it is more advanced; async is the default
    • defaults - uses the default options that are rw, suid, dev, exec, auto, nouser, and async
  • 5th column - dump - backup utility, advanced option, 0 means dump ignores the filesystem
  • 6th column - fsck - determine in which order the file systems should be checked, 0 means fsck won't chech the file system

References