btrfs

$ btrfs filesystem defrag -v -r dir/

Recursively defragment files under dir/, print files as they are processed. The file names will be printed in batches, similarly the amount of data triggered by defragmentation will be proportional to last N printed files. The system dirty memory throttling will slow down the defragmentation but there can still be a lot of IO load and the system may stall for a moment.

$ btrfs filesystem defrag -v -r -f dir/

Recursively defragment files under dir/, be verbose and wait until all blocks are flushed before processing next file. You can note slower progress of the output and lower IO load (proportional to currently defragmented file).

$ btrfs filesystem defrag -v -r -f -clzo dir/

Recursively defragment files under dir/, be verbose, wait until all blocks are flushed and force file compression.

$ btrfs filesystem defrag -v -r -t 64M dir/

Recursively defragment files under dir/, be verbose and try to merge extents to be about 64MiB. As stated above, the success rate depends on actual free space fragmentation and the final result is not guaranteed to meet the target even if run repeatedly.

$ btrfs filesystem resize -1G /path

$ btrfs filesystem resize 1:-1G /path

Shrink size of the filesystem’s device id 1 by 1GiB. The first syntax expects a device with id 1 to exist, otherwise fails. The second is equivalent and more explicit. For a single-device filesystem it’s typically not necessary to specify the devid though.

$ btrfs filesystem resize max /path

$ btrfs filesystem resize 1:max /path

Let’s assume that devid 1 exists, the filesystem does not occupy the whole block device, eg. it has been enlarged and we wan the grow the filesystem. Simply using max as size we will achieve that.

root@test:/home/test/VMs# gdisk -l /dev/sda
GPT fdisk (gdisk) version 0.8.10

Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present

Found valid GPT with protective MBR; using GPT.
Disk /dev/sda: 3907029168 sectors, 1.8 TiB
Logical sector size: 512 bytes
Disk identifier (GUID): 7DFD9799-AE91-4827-AC15-E6E15863A453
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 3907029134
Partitions will be aligned on 2048-sector boundaries
Total free space is 31459294 sectors (15.0 GiB)

Number Start (sector) End (sector) Size Code Name
1 2048 411647 200.0 MiB EF02 Linux filesystem
2 411648 12994559 6.0 GiB 8200 Swap
3 12994560 65423359 25.0 GiB 8300 System
4 65423360 2162575359 1000.0 GiB 8300 Linux filesystem
5 2319861760 3907029134 756.8 GiB 8300 Linux filesystem
8 2194032640 2319861759 60.0 GiB 8300 Linux filesystem

root@test:/home/test/VMs# df -h
S.ficheros Tamaño Usados Disp Uso% Montado en
/dev/sda3 25G 19G 4,7G 81% /
udev 10M 0 10M 0% /dev
tmpfs 3,2G 9,2M 3,2G 1% /run
tmpfs 7,9G 460K 7,9G 1% /dev/shm
tmpfs 5,0M 4,0K 5,0M 1% /run/lock
tmpfs 7,9G 0 7,9G 0% /sys/fs/cgroup
/dev/sda8 60G 58G 2,4G 97% /xfs
/dev/sda4 200G 185G 14G 93% /home
tmpfs 1,6G 4,0K 1,6G 1% /run/user/118
tmpfs 1,6G 12K 1,6G 1% /run/user/1000

root@test:/home/test/VMs# btrfs filesystem df /home/
Data, single: total=188.01GiB, used=185.94GiB
System, DUP: total=8.00MiB, used=48.00KiB
System, single: total=4.00MiB, used=0.00B
Metadata, DUP: total=1.50GiB, used=680.66MiB
Metadata, single: total=8.00MiB, used=0.00B
GlobalReserve, single: total=240.00MiB, used=0.00B

root@test:/home/test/VMs# btrfs filesystem show /dev/sda4
Label: none uuid: 3ac934be-2651-48d2-8850-3d3bbc8a98e0
Total devices 1 FS bytes used 186.60GiB
devid 1 size 200.00GiB used 191.04GiB path /dev/sda4
Btrfs v3.17

root@test:/home/test/VMs# btrfs filesystem resize +50G /home
Resize ‘/home’ of ‘+50G’

root@test:/home/test/VMs# btrfs filesystem df /home/
Data, single: total=188.01GiB, used=185.94GiB
System, DUP: total=8.00MiB, used=48.00KiB
System, single: total=4.00MiB, used=0.00B
Metadata, DUP: total=1.50GiB, used=680.89MiB
Metadata, single: total=8.00MiB, used=0.00B
GlobalReserve, single: total=240.00MiB, used=0.00B

root@test:/home/test/VMs# df -h
S.ficheros Tamaño Usados Disp Uso% Montado en
/dev/sda3 25G 19G 4,7G 81% /
udev 10M 0 10M 0% /dev
tmpfs 3,2G 9,2M 3,2G 1% /run
tmpfs 7,9G 460K 7,9G 1% /dev/shm
tmpfs 5,0M 4,0K 5,0M 1% /run/lock
tmpfs 7,9G 0 7,9G 0% /sys/fs/cgroup
/dev/sda8 60G 58G 2,4G 97% /xfs
/dev/sda4 250G 188G 62G 76% /home
tmpfs 1,6G 4,0K 1,6G 1% /run/user/118
tmpfs 1,6G 12K 1,6G 1% /run/user/1000

root@test:/home/test/VMs# btrfs filesystem show /dev/sda4
Label: none uuid: 3ac934be-2651-48d2-8850-3d3bbc8a98e0
Total devices 1 FS bytes used 179.06GiB
devid 1 size 250.00GiB used 191.04GiB path /dev/sda4

Btrfs v3.17

Reparar imagen qcow2 corrupta, ampliar y reducir

Hoy he estado modificando tamaños de imágenes qcow2 y he dejado un imagen corrupta, pues bien hay solución.

Al querer abrir la imagen me daba este error indicando que esta corrupta.

qemu-img: Could not open 'test-i386.qcow2': qcow2: Image is corrupt; cannot be opened read/write

Para solucionarlo primero analizaremos la situación

file test-i386.qcow2
test-i386.qcow2: QEMU QCOW Image (v3), 12884901888 bytes
qemu-img info test-i386.qcow2
 image: test-i386.qcow2
 file format: qcow2
 virtual size: 12G (12884901888 bytes)
 disk size: 11G
 cluster_size: 65536
 Format specific information:
     compat: 1.1
     lazy refcounts: true

Hacemos un check

qemu-img check test-i386.qcow2
No errors were found on the image.
179022/196608 = 91.06% allocated, 3.54% fragmented, 0.00% compressed clusters

Si viésemos que no funciona, en mi caso no funcionaba ejecutamos el siguiente comando.

qemu-img check -r all test-i386.qcow2
No errors were found on the image.
179022/196608 = 91.06% allocated, 3.54% fragmented, 0.00% compressed clusters
Image end offset: 11734548480

Ahora  continuo haciendo lo que quería hacer reducir pero antes ampliare la imagen (4 veces).

qemu-img resize test-i386.qcow2 +2G
Image resized.
qemu-img resize test-i386.qcow2 +2G
Image resized.
qemu-img resize test-i386.qcow2 +2G
Image resized.
qemu-img resize test-i386.qcow2 +2G
Image resized.
qemu-img info test-i386.qcow2
image: test-i386.qcow2
file format: qcow2
virtual size: 20G (21474836480 bytes)
disk size: 11G
cluster_size: 65536
Format specific information:
 compat: 1.1
 lazy refcounts: true

Ahora pasamos de qcow2 a raw para luego poder reducir

qemu-img convert -O raw test-i386.qcow2 VM1.raw

Una vez pasado a raw

-rw-r--r-- 1 root root 11734548992 jun 25 10:23 test-i386.qcow2
-rw-r--r-- 1 root root 21474836480 jun 25 10:27 VM1.raw

Reduzco

qemu-img resize VM1.raw -8G
Image resized.

así queda pues

-rw-r--r-- 1 root root 11734548992 jun 25 10:23 test-i386.qcow2
-rw-r--r-- 1 root root 12884901888 jun 25 10:51 VM1.raw 

Lo volvemos a convertir a qcow2

 qemu-img convert -c -O qcow2 VM1.raw test-i386.qcow2

Se a reducido mucho el tamaño de la imagen

-rw-r--r-- 1 root root 5369626624 jun 25 10:59 test-i386.qcow2
-rw-r--r-- 1 root root 12884901888 jun 25 10:51 VM1.raw

Comprobamos que todo sea correcto

qemu-img info test-i386.qcow2
image: test-i386.qcow2
file format: qcow2
virtual size: 12G (12884901888 bytes)
disk size: 5.0G
cluster_size: 65536
Format specific information:
 compat: 1.1
 lazy refcounts: false

Por si quedan dudas la imagen se va incrementando tal i como podéis observar

-rw-r--r-- 1 libvirt-qemu libvirt-qemu 5394399232 jun 25 11:23 test-i386.qcow2
-rw-r--r-- 1 root root 12884901888 jun 25 10:51 VM1.raw

Captura de pantalla de 2016-06-25 11:17:03

 

chroot

Cuando no podemos o queremos acceder a un sistema por diversas causas o queremos simplemente hacer pruebas con un sistema operativo GNU-Linux sin instalar, podemos hacer un chroot para así realizar las modificaciones que creamos oportunas o simplemente testear el sistema seguiremos estos pasos.

Montamos la partición, en este caso utilizamos una partición pero podíamos haber montado una iso, tener  todos los ficheros en una carpeta, y simplemente es lo que hacemos lo montamos en una carpeta para poder ver los ficheros y así acceder a ellos.

mount /dev/sda1 /mnt

Una vez montado tenemos que montar los recursos del sistema para que una vez cambiemos la raíz podamos acceder a ellos.

mount --bind /dev /mnt/dev

Hemos montado dev, ahora montamos proc

mount --bind /proc /mnt/proc

Y nos queda sys

mount --bind /sys /mnt/sys

Bien con esto ya podemos cambiar la raíz

chroot /mnt

y realizar las tareas necesarias.

rsync

Hoy me he visto con la imperiosa necesidad de copiar unos directorios locales a un equipo remoto, pues bien para hacerlo lo hacemos con rsync y de la siguiente manera.

Antes de empezar ha de quedar bien claro lo que se quiere copiar ya que

/localdirectory/

no es lo mismo que

/localdirectory

Uno lleva slash al final y el otro no, por tanto uno copia el directorio completo y el otro lo que hay dentro del directorio

rsync -av /localdirectory/ user@server.com:/remote_directory/

Si quisiéramos copiar los ficheros del equipo remoto a aquí, se hace a la inversa

rsync -av user@server.com:/remote_directory/ /local_directory/

Captura de pantalla de 2016-06-22 20:44:56

scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root@server.com:/fichero /

rsync -va root@server.com:/tmp/fichero /ruta/fichero

 

Conectar por vnc

Hoy tenia la necesidad de conectar por vnc a mi equipo, para ello he ejecutado los siguientes comandos

export DISPLAY=:0
x11vnc -display :0 -nopw -listen [localhost o ip equipo] -xkb -ncache 10 -ncache_cr -forever 

Ahora desde otro equipo ya nos podemos conectar por vnc y con ello ya tenemos acceso a nuestro escritorio.

Podemos crear un script para que se ejecute al iniciar sesión, aunque yo prefiero dar acceso cuando sea necesario, no obstante el script lo haríamos así :

#!/bin/bash

export DISPLAY=:0
sleep 5
x11vnc -display :0 -nopw -listen [localhost o ip equipo] -xkb -ncache 10 -ncache_cr -forever 

Ahora bien, si queremos abrir otra sesión diferente, en este caso yo utilizo como wm Awesome

#!/bin/bash

export DISPLAY=:1
Xvfb :1 -screen 0 1280x1024x24 &
sleep 5 
awesome & 
x11vnc -display :1 -nopw -listen [localhost o ip equipo] -xkb -ncache 10 -ncache_cr -forever 

Eso es todo.