sexta-feira, 10 de junho de 2016

Resizing EXT4 partition on IDE Disks in XEN Project Virtual Machine


This tutorial will show you how to resize the virtual IDE Disk on XEN Project (not XenServer). This works on virtual IDE Disk but not for Virtual Disks.
In the follow example, I have a 4GB virtual IDE disk, and we'll resize to 5GB. 

Requirements:
  • Xen Project virtual machine
  • Virtual IDE Disk
  • Partition with EXT4 filesystem
  • A partition where reside all the files, wich will be resized, and a swap partition (primary or extended)


1) Open virtual machine Manager, open virtual machine Details, and annotate the IDE Disk Source Path.



2) Shutdown the virtual machine

3) For safety, backup your virtual harddisk

  # cp disk0.raw disk0.raw.backup 

4) Increase the virtual IDE disk in the hypervisor:


  Before: -rwxr-xr-x  1 root root 4.0G Jun  9 09:16 disk0.raw



  # dd if=/dev/zero bs=1G count=1 >> disk0.raw


  After: -rwxr-xr-x  1 root root 5.0G Jun  9 09:16 disk0.raw
 
Note: To increase the disk space more, change the "count" value.

5) Yet in the hypervisor, annotate the "Start" value of the first partition.


In this case, disk0.raw1 is the operational system partition, this is the partition that will be increased.
The partition disk0.raw2 and disk0.raw5 is the swap partition.

 6) Yet in the hypervisor, with FDISK delete all the partitions from the virtual disk and create again with more space. The first partition must start in the same sector.



# fdisk disk0.raw

Verify the partitions:

Command: p

Delete the partitions:

Command: d → 2 → d → 1  
Create the new primary partition with the new size:


Command: n → p → 1 → 2048 → +4800M → enter


Note: 2048 is the start of first partition, annotated in the item 5, +4800M is the new partition size. Leave some space for the swap partition.


Rebuild the extended partition for swap

Rebuild the external partition for swap:
Command: n → e → 2 → enter → enter
Create the logical partition for swap:
Command: n → l → enter → enter 
Change the partition type for swap filesystem:
Command: t → 5 → 82


Verify the partitions:


command: p  


command: w


7) Start the Virtual Machine. The next commands will be executed inside the Virtual Machine.

Verify the current free space in the virtual machine:


We still have only 4GB of Disk Size and 124MB of free space.


Resize the partition using RESIZE2FS

Note: Online resizing is only possible with EXT4 filesystem.

Verify the new Disk Size:

Now your virtual disk have 4.7GB and 997MB of free space.

8) Fix the swap partition:

The swap partition was deleted and recreated, so you need to reconfigure again.

Format the swap partition:


Annotate the UUID of the swap partition and fix it in the /etc/fstab 


Enable the swap partition:



Verify if the swap is configured: 


Done!

References:


segunda-feira, 6 de junho de 2016

Fast LINUX LVM (Logical Volume Manager) Tutorial

This tutorial will teach you how to work with LVM (Logical Volume Group) using regular 
files to simulate harddisks in LINUX, this way you can learn how it works without the need of real harddisks.
LVM is very usefull because you can join many harddisks into a virtual device (also known as Logical Volume), that will appear on /dev/volume_group_name/logical_volume_name that will contains the sum of the all harddisks spaces, so you don't need to have a  mounting point for each harddisk. LVM also allows you to add news harddisks to the volume group and resize the space on the fly. 


CREATING THE LOGICAL VOLUME


1) Create the fake disk1 and map to /dev/loop1 (to simulate a harddisk)

  # dd if=/dev/zero of=fakedisk1 bs=1M count=100
  # losetup /dev/loop1 fakedisk1

Now fakedisk1 and /dev/loop1 is the same thing, but /dev/loop1 will looks like a harddisk device to Linux.

2) Create fake disk2 and map to /dev/loop2 (to simulate another harddisk)

  # dd if=/dev/zero of=fakedisk2 bs=1M count=100
  # losetup /dev/loop2 fakedisk2

3) Creating a PV (Physical Volume), this command destroys any data on /dev/loop1 (fakedisc1) and /dev/loop2 (fakedisck2), but they are already empty files, so don't worry. In a real disk you must take care. This command format the disks so can be used with LVM.

  # pvcreate /dev/loop1 
  # pvcreate /dev/loop2

4) Create the VG (Volume Group)

  # vgcreate testVG /dev/loop1 /dev/loop2

5) Create the Logical Volume (LV) using all the harddisks free spaces: 

  # lvcreate -l 100%FREE -n testLV testVG

6) Format the LV (Logical Volume) with any filesystem you like
Example, formatting with ext4 filesystem

  # mkfs.ext4 /dev/testVG/testLV

7) Mount the filesystem

  # mkdir /mnt/testlvm
  # mount /dev/testVG/testLV /mnt/testlvm


8) Show the mounting point 
# df
Sist. Arq.                  Tam   Usad 
/dev/mapper/testVG-testLV   188M  9.8M    178M   6% /mnt/testlvm

You may notice the logical volume has 200MB, the sum of two 100MB disks.


VERIFYING LVM INFORMATION


# Show the Physical Volumes (PV)  and wich group belongs:

  # pvdisplay
  --- Physical volume ---
  PV Name               /dev/loop1
  VG Name               testVG
  PV Size               100.00 MiB / not usable 4.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              24
  Free PE               0
  Allocated PE          24
  PV UUID               vV9Pq0-XfD1-TYZU-Vd9c-tLC5-zYhc-0vP1ah

  --- Physical volume ---

  PV Name               /dev/loop2
  VG Name               testVG
  PV Size               100.00 MiB / not usable 4.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              24
  Free PE               0
  Allocated PE          24
  PV UUID               EPo5Bj-bgVL-79dQ-IhzB-pzEK-f1IR-3sX0Vz

# Show the Volume Group (VG) detailed information:

  # vgdisplay
  --- Volume group ---
  VG Name               testVG
  System ID
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               192.00 MiB
  PE Size               4.00 MiB
  Total PE              48
  Alloc PE / Size       48 / 192.00 MiB
  Free  PE / Size       0 / 0
  VG UUID               k4JsiS-vE1X-pt5B-rDb7-MXdz-J581-WpFd4q

# Show Logical Volume (LV) information:

  # lvdisplay

  --- Logical volume ---

  LV Path                /dev/testVG/testLV

  LV Name                testLV

  VG Name                testVG

  LV UUID                vtJOn2-Od3X-M8O2-0iIB-arE6-CHiG-V1ZYtT

  LV Write Access        read/write

  LV Creation host, time aegeus, 2016-06-06 19:38:41 -0300

  LV Status              available

  # open                 1

  LV Size                192.00 MiB

  Current LE             48

  Segments               2

  Allocation             inherit

  Read ahead sectors     auto

  - currently set to     256

  Block device           254:0

ADDING A NEW DISK TO THE VOLUME GROUP

1) Create a third fake disk:

  # dd if=/dev/zero of=fakedisk3 bs=1M count=100

  # losetup /dev/loop3 fakedisk3

2) Create the Physical Volume

  # pvcreate /dev/loop3

3) Add the new disk to the existing Volume Group

  # vgextend testVG /dev/loop3

4) Verify the unallocated free space in volume group:


  # pvscan
  PV /dev/loop1   VG testVG   lvm2 [96.00 MiB / 0    free]
  PV /dev/loop2   VG testVG   lvm2 [96.00 MiB / 0    free]
  PV /dev/loop3   VG testVG   lvm2 [96.00 MiB / 96.00 MiB free]
  Total: 3 [288.00 MiB] / in use: 3 [288.00 MiB] / in no VG: 0 

5) Increase the logical volume space: 

  # lvextend /dev/testVG/testLV /dev/loop3

6) Verify there is no more free space:

  # pvscan

  PV /dev/loop1   VG testVG   lvm2 [96.00 MiB / 0    free]
  PV /dev/loop2   VG testVG   lvm2 [96.00 MiB / 0    free]
  PV /dev/loop3   VG testVG   lvm2 [96.00 MiB / 0    free]

7) Expand the filesystem

  # resize2fs /dev/testVG/testLV

8) Verify that your partition now have more free space:

# df
Sist. Arq.                 Tam    Usad    Dispon. Uso% Montado em
/dev/mapper/testVG-testLV  279M   6.1M    259M    3% /mnt/testlvm


REFERENCES:





https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/4/html/Cluster_Logical_Volume_Manager/LVM_examples.html#vol_create_ex

sábado, 4 de junho de 2016

Backup Harddisk to image and access files inside the image in Linux

How to Backup Harddisk to image file 

and mount Backup images in Linux


Introduction: 

Linux deals a harddisk almost like a regular file. So the /dev/sda is like a big file that contains the harddisk data, so any command you use in a regular file works on /dev/sda file, so you always should take care with these files. 

The easiest way to backup the hole harddisk with all the partitions is copying the harddisk file like:

# cp /dev/sda backup_sda

or

# dd if=/dev/sda of=backup_sda bs=100M

Depending on the size of the harddisk the copy may take several hours because the "dd" or "cp"  command copies everything, including the empty space, and the created backup file have the harddisk size. The advantage of this method is that you can mount and access the files of your backup file. If you don't want to access the files later, only want to backup and restore later in another harddisk, I would recommend using other tools like partimage, that is faster and don't copy the empty space.

MOUNTING THE PARTITION FROM THE BACKUP


1) Look the partitions in your backup file:

# fdisk -l backup_sda

Disk backup_sda1: 209 MB, 209715200 bytes
255 heads, 63 sectors/track, 25 cylinders, total 409600 sectors
Units = setores of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00068d2c

Dispositivo Boot      Start         End      Blocks   Id  System
backup_sda1            2048      204799      101376   83  Linux
backup_sda2          204800      409599      102400    7  NTFS


2) Note the "Start" number of the partition you want to mount. 
Note that the first partition (backup_sda1), the start number is 2048 and the second partition (backup_sda2) the start number is 204800.

3) Create directory, and mount the first partition: 

# mkdir /mnt/part1
# mount backup_sda /mnt/part1 -o offset=$((2048*512))

Note: Use the noted number from the start of first partition.

The partition is mounted, now you can list and copy files from your backup inside  /mnt/part1


4) Create directory and mount the second partition with NTFS filesystem (Windows)

Mounting NTFS in read-only
# mkdir /mnt/part2
# mount -t ntfs backup_sda /mnt/part2 -o offset=$((204800*512)) 

Mounting NTFS in read-write
# mkdir /mnt/partition2
# mount -t ntfs-3g backup_sda /mnt/part2 -o offset=$((204800*512))

Note: Use the noted number from the start of second partition.

The partition is mounted, now you can list and copy files from your backup inside  /mnt/part2.



Creating a filesystem inside a regular file in Linux


CREATING THE FILESYSTEM INSIDE A REGULAR FILE



Follow the next steps to create a regular file that contains a filesystem (also know as RAW DISC, IMG DISC or IMAGE DISC). 
This kind of filesystem inside a file is used in Virtualization like XEN or when you copy a harddisk from /dev/hdxx or /dev/sdxx to a regular file.

1) Create an empty file (filled with zeros) with 100Mbytes size: 


# dd if=/dev/zero of=fakedisk bs=1M count=100
Change the "count" value to the desired space.


2) Create the partitions with fdisk:

# fdisk fakedisk
Command (m for help): n     (create a new partition)
Command (m for help): p     (create a primary partition)
Command (m for help): 1     (create the first partition of the disk)
Command (m for help): enter (use default values)
Command (m for help): enter (use default values)
Command (m for help): t -> 83 (use 83 for linux filesystems like ext2,ext3,ext4)
Command (m for help): w     (write changes to disk)

3) Using fdisk, note the "Start" number of the partition, in this case is 2048.

# fdisk -l fakedisk

Disk fakedisk: 104 MB, 104857600 bytes
191 heads, 50 sectors/track, 21 cylinders, total 204800 sectors
Units = setores of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xb5cd89fb

Dispositivo Boot   Start     End      Blocks   Id  System
fakedisk1          2048      204799   101376   83  Linux


4) Mount the partition, using the noted number and multiply by 512.

# losetup /dev/loop0 fakedisk -o $((2048 * 512))

5) Format the partition with the filesystem

# mkfs.ext3 /dev/loop0

6) Verify if the partition is recognized as ext3:

# file -s /dev/loop0

/dev/loop0: sticky Linux rev 1.0 ext3 filesystem data, UUID=c52cc60c-9419-4906-95a7-c742937b7769

7) Mount the partition

# mkdir /mnt/partition1

# mount /dev/loop0 /mnt/partition1

Case loop0 is already used, you can use loop1, loop2, ...

Now you can use the /mnt/partition directory like any other mounted point.


RESIZING YOUR PARTITION INSIDE REGULAR FILE


Now if you want to increase your partition/filesystem inside a regular file, follow the steps:

1) Unmount the partition

# umount /mnt/partition1

2) Increase the regular file that contains the filesystem with zeroes at the end. 
In this example we'll increase the file that holds the filesystem to more 100MB.

# dd if=/dev/zero bs=1M count=100 >> fakedisk

3) Reload the loop device mapping to recognize the new free space: 

# losetup -d /dev/loop0
# losetup /dev/loop0 fakedisk -o $((2048 * 512))

4) Check the Filesystem Integrity

# fsck -f /dev/loop0

fsck de util-linux 2.20.1
e2fsck 1.41.12 (17-May-2010)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/loop0: 11/25376 files (0.0% non-contiguous), 8874/101376 blocks

5) Resize the filesystem

# resize2fs /dev/loop0
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/loop0 to 203776 (1k) blocks.
The filesystem on /dev/loop0 is now 203776 blocks long.

7) Mount the loop0 device:

# mount /dev/loop0 /mnt/partition1

8) Verify the partition with the new size:

# df -h
Sist. Arq.                Tam  Usad Dispon. Uso% Montado em
rootfs                     19G   13G    4.9G  73% /
/dev/loop0                193M  5.6M    178M   4% /mnt/partition1

Done.
Now you have the partition with more space.

GOLPE LIGAÇÃO DE FALSO FUNCIONÁRIO DA CAIXA SOBRE DÍVIDA OU COMPRA NO MERCADO LIVRE

💥💥💥 ALERTA DE GOLPE DO FALSO FUNCIONÁRIO DE BANCO 💥💥💥 Se você receber uma ligação dizendo ser de algum banco como  CAIXA ECONÔMICA FED...