回覆回應

parted

最後更新: 2020-03-11

介紹

 

parted 是 Linux 上修改 partion 的利器來.

它可以建立(mkpart), 刪除(rm), 檢查(check) Partion.

而且還可以 更改容量(resize) 及 救回(rescue) 誤刪的 partion !!!

注意: resize partion  唔等於 resize file system !!

目錄

 

 


自助

help <cmd>

help set

 


Tips

Modern HDD starting after about 1TB can use 4kB physical sectors.

Its important for performance that filesystems are aligned on 4kB boundaries,

as the drive will do read/modify/write to fake 512B physical sectors.

-a alignment-type

alignment-type:

  • optimal
  • minimal
  • cylinder

用 parted 的 "optimal" 或 "minimal" 就沒有 align 的問題

P.S.

 fdisk -b 4096 /dev/sda

switched off DOS compatibility of fdisk ("c") and changed view mode to sectors ("u")

HardDisk Format 的要求:

Partitions:

Make sure that all partitions start on a multiple of 8 sectors (8x 512B = 4KB) and

that partition sizes are multiples of 8 sectors.

所以:

The first partition starting at sector 63 is done automatically by the partitioning program to optimize read/write performance.

FS:

The file system defaults on most Linux systems are to use blocks of 4KB data.

Typically no specific actions are needed to configure block sizes for WD Advanced Format drives.

 


Usage

 

查看本機上所有的 block device 的資訊

parted -l

Model: WDC WD800JB-00FSA0 (ide)
Disk /dev/hda: 80.0GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type      File system  Flags
 1      32.3kB  8003MB  8003MB  primary   ext3         boot
 2      8003MB  10.0GB  1999MB  primary   linux-swap
 3      10.0GB  20.0GB  10.0GB  primary   ext3
 4      20.0GB  80.0GB  60.0GB  extended
 5      20.0GB  40.0GB  20.0GB  logical                lvm
 6      40.0GB  60.0GB  20.0GB  logical                lvm
 7      60.0GB  80.0GB  20.0GB  logical   ext3

進入 interactive mode

parted

在 interactive mode 看本看資料

print all = parted -l

print free

print <n>

<n> 是指 print 時的 Number, 如

Model: ST362021 C " " " " " " " (scsi)
Disk /dev/sda: 60.0GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type     File system  Flags
 1      32.3kB  30.0GB  30.0GB  primary  ntfs
 2      30.0GB  60.0GB  30.0GB  primary

 


建立 Partition Table(mklabel)

 

mklabel  <label-type>

label-type:

  • loop (raw disk access)
  • msdos
  • gpt

建立 Partition(mkpart)

 

mkpart [...]

Creates a new partition, without creating a new file system on that partition.

You may specify a file system type,

to set the appropriate partition code in the partition table for the new partition.

 

i.e 在 Device 上建立4個 partiion

選擇要進行操作的 Device

select DEVICE

i.e. select /dev/sda

建立第 1 個 partiion

(parted) mkpart

Partition type?  primary/extended? primary
File system type?  [ext2]? ext3
Start? 0%
End? 33%

Start 及 END 的表達方式可以是"容量"或"%", 如 4GB or 10%

P.S.

File system type:

swap = linux-swap

建立第 2 個

(parted) mkpart

Partition type?  primary/extended? primary
File system type?  [ext2]? ext3
Start? 15%
End? 100%
Warning: You requested a partition from 9003MB to 60.0GB.
The closest location we can manage is 30.0GB to 60.0GB.  Is this still
acceptable to you?
Yes/No?yes

建立第 3 個

Default 單位 megabytes

Start=34s   <-- (units in sectors)

End=Total sectors - 34s

建立第 4 個

mkpart primary 2200 100%

當沒有 partation table 時:

(parted) mkpart
Error: /dev/sda: unrecognised disk label

unrecognised disk label

 


Unit

 

"s" (sectors),

"B" (bytes),

"MB"  <- megabyte (10^n)

"MiB" <- mebibyte (2^n)

"%" (percentage of device size),

"compact"  (megabytes for input, and a human-friendly form for output)

 


Set Flags

 

set minor flag state

  • boot
  • root
  • swap
  • hidden
  • raid
  • lvm
  • lba

Example:

set 1 boot on

set 1 raid off

說明

lba

this flag can be enabled to tell MS DOS, MS Windows 9x and MS Windows ME based operating systems to use Linear (LBA) mode.

 


Rescue 測試

刪除 Number 為 1 的 partion

rm 1

!!!!!!!! 沒有再問一次的 !!!!!!!!

拯救:

(parted) rescue
Start? 0
End? 100%

searching for file systems... 0%        (time left 179:30)
Information: A ntfs primary partition was found at 32.3kB -> 60.0GB.  Do you
want to add it to the partition table?
Yes/No/Cancel?yes

 


Resize

 

方案 1:

parted /dev/sdc

(parted) unit s

(parted) print

Number  Start  End         Size        Type     File system  Flags
 1      2048s  488386559s  488384512s  primary  ext4         boot

(parted) rm 1

(parted) mkpart pri ext4 2048s 100%

方案 2:

# Error: The resize command has been removed in parted 3.0

(parted) resize 1 0.00B 100%

Example

假設要 resize lamp 這個 Partition

mount | grep lamp      # 看看它的 device 是什麼

/dev/sdd6 on /var/lib/lxc/lamp/rootfs type btrfs 
    (rw,noatime,space_cache,subvolid=264,subvol=/squeeze)

umount  /dev/sdd6     # 必須 umount 它

# 查看原來 partition 的位置

parted /dev/sdd

(parted) unit s

(parted) print

 6      23633920s   101562367s   77928448s    logical   btrfs
 5      179689472s  273436671s   93747200s    logical   ext4

# 刪除及建立

(parted) rm 6

(parted) mkpart log btrfs 23633920s 131562367s

 7      23633920s   131562367s   107928448s   logical   btrfs   lba
 5      179689472s  273436671s   93747200s    logical   ext4

mount -a                    # mount by UUID

cd /var/lib/lxc/lamp/rootfs

btrfs fi resize max .     # resize fs (BTRFS)

df -h | grep lamp        # checking

/dev/sdd7   52G   36G   15G  71% /var/lib/lxc/lamp/rootfs

 


parted 錯地方 (Device, Partition)

 

parted /dev/sdc1

(parted) unit s

Number  Start  End         Size        File system  Flags
 1      0s     488384511s  488384512s  ext4

parted /dev/sdc

(parted) unit s

(parted) print

Number  Start  End         Size        Type     File system  Flags
 1      2048s  488386559s  488384512s  primary  ext4         boot

 


 

 

回應

此欄位內容將保密,不會公開顯示出來。