trešdiena, 2017. gada 20. decembris

How to extend linux vm drive and partition and recreate swap file without gparted and without gui

Preface.

 HDD which is actually bunch of files in vm, emulating HDD are extended via VMwareVCenter. I need to increase my primary partition in Ubuntu Linux VMware virtual machine. In other words  "/" must be extended.  In this example parted and fdisk which are preinstalled in all modern linux systems will be used. 

How to extend root partition /

First we need to extend device. In my situation there are problem- after device /dev/sda1 are /dev/sda2 so I need firstly to move /dev/sda2 to the end of HDD partition.
Fdisk shows I can increase /dev/sda2 but cannot /dev/sda1
root@Log:/# fdisk -l
Disk /dev/sda: 450 GiB, 483183820800 bytes, 943718400 sectors
Device     Boot     Start       End   Sectors   Size Id Type
/dev/sda1  *         2048 615234375 615232328 293.4G 83 Linux
/dev/sda2       617187328 629145599  11958272   5.7G 82 Linux swap / Solaris
As you can see there are 943 mil sectors but only 629 occupied.
So I first need to move swap to the end of disk.

Move swap partition

All commands must be typed as root user, in ubuntu type sudo su or type sudo before each command. Before deleting swap partition, stop all active applications like database and web servers. After moving swap partition must be at the end of disk /dev/sda
Type fdisk /dev/sda and then type
p to print info about partitions
d to delete swap partition
2 number of partition
n to create new partition
p to make partition primary
2 number of partition
calculate sectors, partition take, subtract from the last sector and provide to numbers First sector of partition and last sector. Some spreadsheet could help.



t to change filesystem type
2 number of partition
82 as partition type Linux swap
p to check all are fine swap partition now are at the end of disk
w to write changes to disk
mkswap /dev/sda2 Make new partiton a swap partition
swapon/dev/sda2 To swap on new partition
free -h to check new swap partition is in place
nano /etc/fstab to change UUID to the new value
shutdown -r now to rebot server
See the shell:
root@Log:/# fdisk /dev/sda
Welcome to fdisk (util-linux 2.27.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): p
Disk /dev/sda: 450 GiB, 483183820800 bytes, 943718400 sectors
Disk identifier: 0x29720c3b
Device     Boot     Start       End   Sectors   Size Id Type
/dev/sda1  *         2048 615234375 615232328 293.4G 83 Linux
/dev/sda2       617187328 629145599  11958272   5.7G 82 Linux swap / Solaris
Command (m for help): d
Partition number (1,2, default 2): 2
Partition 2 has been deleted.
Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2): 2
First sector (615234376-943718399, default 615235584): 931760000
Last sector, +sectors or +size{K,M,G,T,P} (931760000-943718399, default 943718399):
Created a new partition 2 of type 'Linux' and of size 5.7 GiB.
Command (m for help): p
Disk /dev/sda: 450 GiB, 483183820800 bytes, 943718400 sectors
Disk identifier: 0x29720c3b
Device     Boot     Start       End   Sectors   Size Id Type
/dev/sda1  *         2048 615234375 615232328 293.4G 83 Linux
/dev/sda2       931760000 943718399  11958400   5.7G 83 Linux
Command (m for help): t
Partition number (1,2, default 2): 2
Partition type (type L to list all types): 82
Changed type of partition 'Linux' to 'Linux swap / Solaris'.
Command (m for help): p
Disk /dev/sda: 450 GiB, 483183820800 bytes, 943718400 sectors
Disklabel type: dos
Disk identifier: 0x29720c3b
Device     Boot     Start       End   Sectors   Size Id Type
/dev/sda1  *         2048 615234375 615232328 293.4G 83 Linux
/dev/sda2       931760000 943718399  11958400   5.7G 82 Linux swap / Solaris
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Re-reading the partition table failed.: 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).
root@Log:/home/ubuntu# sudo mkswap /dev/sda2
Setting up swapspace version 1, size = 5.7 GiB (6122696704 bytes)
no label, UUID=276f45a9-1705-49e8-b2fa-43df728f61ea
root@Log:/home/ubuntu# sudo swapon /dev/sda2
root@Log:/home/ubuntu# free -h
              total        used        free      shared  buff/cache   available
Mem:           5.8G        2.7G        1.8G        8.5M        1.3G        2.9G
Swap:          5.7G          0B        5.7G
root@Log:/home/ubuntu# swapon --show
NAME      TYPE      SIZE USED PRIO
/dev/sda2 partition 5.7G   0B   -1

root@Log:/home/ubuntu# nano /etc/fstab
# swap was on /dev/sda5 during installation
UUID=276f45a9-1705-49e8-b2fa-43df728f61ea  none            swap    sw              0       0

Increase disk logical partition

Run parted, type
print all to see partitions
print free to ensure free space is now after partition 1 so we can increase it
resizepart 1
Yes
477 GB
print all
root@Log:~# parted
GNU Parted 3.2
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print all
Model: VMware Virtual disk (scsi)
Disk /dev/sda: 483GB
Disk Flags:
Number  Start   End    Size    Type     File system  Flags
 1      1049kB  315GB  315GB   primary  ext4         boot
 2      477GB   483GB  6123MB  primary
(parted) print devices
/dev/sda (483GB)
(parted) print free
Model: VMware Virtual disk (scsi)
Disk /dev/sda: 483GB
Sector size (logical/physical): 512B/512B
Disk Flags:
Number  Start   End     Size    Type     File system  Flags
        32.3kB  1049kB  1016kB           Free Space
 1      1049kB  315GB   315GB   primary  ext4         boot
        315GB   477GB   162GB            Free Space
 2      477GB   483GB   6123MB  primary
(parted) resizepart 1
Warning: Partition /dev/sda1 is being used. Are you sure you want to continue?
Yes/No? Yes
End?  [315GB]? 477GB
(parted) print all
Model: VMware Virtual disk (scsi)
Disk /dev/sda: 483GB
Disk Flags:
Number  Start   End    Size    Type     File system  Flags
 1      1049kB  477GB  477GB   primary  ext4         boot
 2      477GB   483GB  6123MB  primary
root@Log:~# df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            2.9G     0  2.9G   0% /dev
tmpfs           597M  8.3M  589M   2% /run
/dev/sda1       289G  251G   26G  91% /
tmpfs           3.0G     0  3.0G   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           3.0G     0  3.0G   0% /sys/fs/cgroup
tmpfs           597M     0  597M   0% /run/user/1000

As you can notice /dev/sda1 is 477 GB but root partition / is still 289 GB

Increase linux OS root partition

Run resize2fs /dev/sda1
root@Log:~# resize2fs /dev/sda1
resize2fs 1.42.13 (17-May-2015)
Filesystem at /dev/sda1 is mounted on /; on-line resizing required
old_desc_blocks = 19, new_desc_blocks = 28
The filesystem on /dev/sda1 is now 116454822 (4k) blocks long.
root@Log:~# df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            2.9G     0  2.9G   0% /dev
tmpfs           597M  8.3M  589M   2% /run
/dev/sda1       438G  251G  168G  60% /
tmpfs           3.0G     0  3.0G   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           3.0G     0  3.0G   0% /sys/fs/cgroup
tmpfs           597M     0  597M   0% /run/user/1000

Root partition is now 444 GB.

root@Log:~# fdisk -l
Disk /dev/sda: 450 GiB, 483183820800 bytes, 943718400 sectors
Disk identifier: 0x29720c3b
Device     Boot     Start       End   Sectors   Size Id Type
/dev/sda1  *         2048 931640625 931638578 444.2G 83 Linux
/dev/sda2       931760000 943718399  11958400   5.7G 82 Linux swap / Solaris

Terminology

There are misunderstanding in linux between disks, devices and partitions.
fdisk thinks about partitions as disks. /dev/sda actually are primary partition. Diskpart thinks devices are partitions. So its hard to google out extend partition because you first need to extend disk, only then you can extend  partition or in other terms first extend physical partition and then extend logical partition.
/dev/sda-|-/dev/sda1-|-/
              |                   |-/etc
              |                   |-/var         
              |--/dev/sda2  swap
  I even found http://www.linfo.org/confusing_terminology.html
logical partition   This can be confusing because a partition is defined as a logical division of a HDD, but a logical partition is any partition that is carved out of an extended partition. An extended partition is a primary partition that is designated to be available for carving up into additional partitions in order to increase the number of partitions to more than the maximum of four primary partitions.

Nav komentāru:

Ierakstīt komentāru