# افزایش حجم هارد دیسک با استفاده از Lvm

**URL:** <https://forum.mysup.ir/t/topic/423>\
**Category:** کانفیگ سخت‌افزار\
**Tags:** simotel\
**Created:** [ژانویه 2، 2025، 11:38ق.ظ UTC](https://forum.mysup.ir/t/topic/423 "2025-01-02T11:38:52Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![shokri](https://forum.mysup.ir/user_avatar/forum.mysup.ir/shokri/32/948_2.png) [@shokri](https://forum.mysup.ir/u/shokri)\
**Post date:** [ژانویه 2، 2025، 11:38ق.ظ UTC](https://forum.mysup.ir/t/topic/423/1 "2025-01-02T11:38:52Z")

</div>

### مراحل افزایش اندازه Logical Volume:

در ابتدای مراحل حجم هارد درخواستی خود را از طریق ماشین مجازی به صورت مجزا از هارد قبلی یک دیسک جدید اختصاص و تعریف میکنیم سپس:

1. **ایجاد پارتیشن جدید بر روی هارد دیسک**
2. **اضافه کردن پارتیشن جدید به عنوان Physical Volume**
3. **اضافه کردن Physical Volume به Volume Group**
4. **اختصاص فضای جدید از Volume Group به Logical Volume**
5. **تغییر اندازه سیستم‌فایل**

### پیش‌نیازها:

قبل از هرگونه تغییر در پارتیشن‌ها و پیکربندی LVM، **از داده‌های خود بکاپ بگیرید**. این فرآیند نسبتاً ساده است اما اگر اشتباهی رخ دهد، جبران آن بسیار سخت خواهد بود.

### مراحل :

#### 1. بررسی وضعیت فعلی هارد سیستم:

ابتدا برای بررسی وضعیت دیسک‌ها و پارتیشن‌ها از دستور `fdisk -l` استفاده می‌کنیم:

```auto
# fdisk -l

```

نتیجه مشابه زیر خواهد بود:

```auto
# fdisk -l

Disk /dev/sda: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000d78cd

   Device Boot Start End Blocks Id System
/dev/sda1 * 1 64 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 64 3917 30944256 8e Linux LVM

Disk /dev/mapper/vg_vmware-lv_root: 29.6 GB, 29569843200 bytes
255 heads, 63 sectors/track, 3594 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/mapper/vg_vmware-lv_swap: 2113 MB, 2113929216 bytes
255 heads, 63 sectors/track, 257 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

```

سپس برای بررسی Volume Groupهای ایجاد شده ی قبلی و مشاهده نام از دستور زیر استفاده کنیم:

```auto
# df -lh

```

در اینجا، Volume Group `vg_vmware` ایجاد شده است که شامل دو Logical Volume به نام‌های `lv_root` و `lv_swap` است را در خروجی دستور به ما میدهد.

```auto
[root@Simotel~]# df -lh
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_vmware-lv_root
                       28G 15G 12G 57% /
tmpfs 495M 0 495M 0% /dev/shm
/dev/sda1 485M 53M 407M 12% /boot

```

#### 2. **ایجاد پارتیشن جدید بر روی هارد دیسک:**

در این مرحله، باید پارتیشن جدیدی بر روی هارد دیسک ایجاد کنیم و از آن به‌عنوان Physical Volume استفاده کنیم. برای این کار از دستور `fdisk` استفاده می‌کنیم:

```auto
# fdisk /dev/sda

```

سپس یک پارتیشن جدید ایجاد می‌کنیم:

```auto
# fdisk /dev/sda

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): n
Command action
   e extended
   p primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (3917-7832, default 3917): 
Using default value 3917
Last cylinder, +cylinders or +size{K,M,G} (3917-7832, default 7832): 
Using default value 7832

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

```

سپس با دستور fdisk -l میتوان پارتیشن جدید را مشاهده نمود:

```auto
# fdisk -l

Disk /dev/sda: 64.4 GB, 64424509440 bytes
255 heads, 63 sectors/track, 7832 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000d78cd

   Device Boot Start End Blocks Id System
/dev/sda1 * 1 64 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 64 3917 30944256 8e Linux LVM
/dev/sda3 3917 7832 31453260 83 Linux

```

#### 3. **تبدیل پارتیشن جدید به Physical Volume:**

بعد از ایجاد پارتیشن، باید آن را به‌عنوان یک Physical Volume جدید به LVM معرفی کنیم:

```auto
# pvcreate /dev/sda3

```

خروجی دستور:

```auto
# pvcreate /dev/sda3
  Physical volume "/dev/sda3" successfully created

```

برای تایید، می‌توانیم از دستور `pvdisplay` استفاده کنیم:

```auto
# pvdisplay

```

خروجی دستور مشابه زیر خواهد بود:

```auto
# pvdisplay
  --- Physical volume ---
  PV Name /dev/sda2
  VG Name vg_vmware
  PV Size 29.51 GiB / not usable 3.00 MiB
  Allocatable yes (but full)
  PE Size 4.00 MiB
  Total PE 7554
  Free PE 0
  Allocated PE 7554
  PV UUID rxHFrq-a1p1-8woW-1Vr2-4C6w-zLQt-IdTgFk

  "/dev/sda3" is a new physical volume of "30.00 GiB"
  --- NEW Physical volume ---
  PV Name /dev/sda3
  VG Name               
  PV Size 30.00 GiB
  Allocatable NO
  PE Size 0   
  Total PE 0
  Free PE 0
  Allocated PE 0
  PV UUID JRdnMd-4bm1-ahFI-6FtR-Opzl-4SBL-OXmKFQ

```

#### 4. **اضافه کردن Physical Volume به Volume Group:**

ابتدا باید نام VG-NAME را با دستور زیر مشاهده کنیم و سپس به همین Volume Group اضافه کنیم:

```auto
# vgdisplay | grep Name
  VG Name vg_vmware

```

حالا باید پارتیشن جدید را به Volume Group اضافه کنیم. برای این کار از دستور `vgextend` استفاده می‌کنیم:

```auto
# vgextend vg_vmware /dev/sda3
  Volume group "vg_vmware" successfully extended

```

برای بررسی وضعیت جدید Volume Group از دستور `vgdisplay` استفاده می‌کنیم:

```auto
# vgdisplay

```

خروجی دستور مشابه زیر خواهد بود:

```auto
# vgdisplay
  --- Volume group ---
  VG Name vg_vmware
  System ID             
  Format lvm2
  Metadata Areas 2
  Metadata Sequence No 4
  VG Access read/write
  VG Status resizable
  MAX LV 0
  Cur LV 2
  Open LV 2
  Max PV 0
  Cur PV 2
  Act PV 2
  VG Size 59.50 GiB
  PE Size 4.00 MiB
  Total PE 15232
  Alloc PE / Size 7554 / 29.51 GiB
  Free PE / Size 7678 / 29.99 GiB
  VG UUID NIQaYT-WSin-0FSk-LBeE-g7RP-9cOV-ru5Vza

```

در اینجا مشاهده می‌کنیم که Volume Group `vg_vmware` به فضای جدید 30GB دسترسی پیدا کرده است.

#### 5. **افزایش اندازه Logical Volume:**

در ابتدا با دستور زیر مسیر logical volume را مشاهده میکنیم و سپس فضای volume group را به logical volume اضافه میکنیم:

```auto
# lvdisplay | grep Path
  LV Path /dev/vg_vmware/lv_root
  LV Path /dev/vg_vmware/lv_swap

```

حال که فضای جدید به Volume Group اضافه شد، می‌توانیم از این فضا برای افزایش اندازه Logical Volume استفاده کنیم. فرض کنید که می‌خواهیم تمام فضای آزاد موجود در Volume Group را به Logical Volume `lv_root` اضافه کنیم:

```auto
# lvextend -l +100%FREE /dev/vg_vmware/lv_root
خروجی دستور: 
  Extending logical volume lv_root to 57.53 GiB
  Logical volume lv_root successfully resized

```

#### 6. **تغییر اندازه سیستم‌فایل:**

در نهایت، باید سیستم‌فایل موجود در Logical Volume را برای استفاده از فضای جدید تغییر اندازه دهیم. این کار را می‌توان با دستور `resize2fs` انجام داد:

```auto
# resize2fs /dev/vg_vmware/lv_root

```

خروجی مشابه زیر خواهد بود:

```auto
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg_vmware/lv_root is mounted on /; on-line resizing required
old desc_blocks = 2, new_desc_blocks = 4
Performing an on-line resize of /dev/vg_vmware/lv_root to 15081472 (4k) blocks.
The filesystem on /dev/vg_vmware/lv_root is now 15081472 blocks long

```

#### 7. **بررسی فضای جدید هارد دیسک:**

برای بررسی فضای جدید در Logical Volume و سیستم‌فایل، از دستور `df -h` استفاده می‌کنیم:

```auto
# df -h

```

خروجی مشابه زیر خواهد بود:

```auto
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_vmware-lv_root
                       57G 15G 40G 27% /
tmpfs 495M 0 495M 0% /dev/shm
/dev/sda1 485M 53M 407M 12% /boot

```

در اینجا مشاهده می‌کنید که اندازه Logical Volume `lv_root` به 57GB افزایش یافته است.

این فرآیند شامل ایجاد پارتیشن جدید، تبدیل آن به Physical Volume، اضافه کردن آن به Volume Group، افزایش اندازه Logical Volume و تغییر اندازه سیستم‌فایل بود. امیدواریم این راهنما برای شما مفید واقع شده باشد.

اگر سوالی دارید یا با مشکلی مواجه شدید، می‌توانید در ادامه سوالات خود را مطرح کنید.
