LoginSignup
2
6

More than 3 years have passed since last update.

partedコマンドを使ってみる

Last updated at Posted at 2020-03-21

lpic101学習中に初めて知ったので、使って覚えてみることにした。
partedコマンドでパーティション管理を見ながら実行。

環境

ホストOS: Ubuntu 16.04.6 LTS
仮想マシン: vagrant 2.2.6, VirtualBox 5.1.38_Ubuntur122592
ゲストOS: CentOS7

パーティションの表示

parted [デバイス名] でfdiskと同じように対話的に動作する。
pコマンドでパーティションテーブルを表示。

pコマンド
[root@localhost ~]# parted /dev/sda
GNU Parted 3.1
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) p                                                                
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sda: 42.9GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  42.9GB  42.9GB  primary  xfs          boot

GPTのパーティションテーブル作成

エラーが出るけど、仮想マシンなので無視。
mklabel gptで作成。

mklabelコマンド
(parted) mklabel gpt
Warning: Partition(s) on /dev/sda are being used.
Ignore/Cancel? Ignore
Warning: The existing disk label on /dev/sda will be destroyed and all data on
this disk will be lost. Do you want to continue?
Yes/No? Yes                                                               
Error: Partition(s) 1 on /dev/sda have been written, but we have been unable to
inform the kernel of the change, probably because it/they are in use.  As a
result, the old partition(s) will remain in use.  You should reboot now before
making further changes.
Ignore/Cancel? Ignore
パーティションテーブルが無いことを確認
(parted) p                                                                
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sda: 42.9GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start  End  Size  File system  Name  Flags

パーティション作成

unit sで表示単位をセクターに切り替え

(parted) unit s
(parted) p                                                                
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sda: 83886080s ←ここ
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start  End  Size  File system  Name  Flags

mkpartで使用するセクターの範囲を指定。

(parted) mkpart                                                           
Partition name?  []? test                                                
File system type?  [ext2]? ext4                                           
Start? 2048s                                                              
End? 83886079s                                                          
Warning: You requested a partition from 2048s to 83886079s (sectors
2048..83886079).
The closest location we can manage is 2047s to 2047s (sectors 2047..2047).
Is this still acceptable to you?
Yes/No? Yes    

指定した範囲のセクターでパーティションができているかと思いきや・・・
Endセクターとファイルシステムの種類が違う!?

(parted) p                                                                
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sda: 83886080s
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start  End        Size       File system  Name  Flags
 1      2048s  83886046s  83883999s  xfs          test

とりあえずそこは置いておいて先に進みます!

フォーマット実行

unit GiBでまず容量の単位をGに戻す。

(parted) unit GiB
(parted) p                                                                
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sda: 40.0GiB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start  End        Size       File system  Name  Flags
 1      0.00GiB  40.0GiB  40.0GiB  xfs          test

OSがマウントできるようにファイルシステムとしてフォーマットする。
→しかし既にマウントされていて、マウントできない。

(parted) q ← partedコマンド終了                                                               
Information: You may need to update /etc/fstab.
[root@localhost ~]# mkfs.ext4 /dev/sda1
mke2fs 1.42.9 (28-Dec-2013)
/dev/sda1 is mounted; will not make a filesystem here!
[root@localhost ~]# umount /dev/sda1
umount: /: target is busy.
        (In some cases useful info about processes that use
         the device is found by lsof(8) or fuser(1))

現在は/dev/sda1しかパーティションがないので、アンマウントもできない。
マウントは前回やって保留中。

終わりに

今回はlpic出題範囲のコマンドを覚えることが最優先なので、とりあえずpartedコマンドの使い方はこれで終わり!

2
6
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
6