LinuxParty

NUESTRO SITIO necesita la publicidad para costear hosting y el dominio. Por favor considera deshabilitar tu AdBlock en nuestro sitio. También puedes hacernos una donación entrando en linuxparty.es, en la columna de la derecha.
Inicio desactivadoInicio desactivadoInicio desactivadoInicio desactivadoInicio desactivado
 

A veces se encuentra en una situación donde usted necesita montar un disco duro, una partición, o un directorio desde el host OpenVZ dentro de un contenedor de OpenVZ - por ejemplo, agregar un rápido SSD para el anfitrión y poner en él las bases de datos de su servidor de MySQL para hacer MySQL más rápido. En este tutorial se explica cómo se puede montar los dispositivos de acogida y las particiones y directorios en un contenedor de OpenVZ uniéndolos con los montajes.

 

1 Notas preliminares

En este tutorial tengo un contenedor OpenVZ con el ID 101 el cual tiene un MySQL. He añadido un segundo disco duro al host y quiero poner en el contenedor las Bases de datos que están en el directorio /var/lib/mysql

2 Preparando el host.

Host:

Si tienes que añadir un nuevo disco duro al host deberías comprobar lo siguiente

fdisk -l

... Y observarás que no tiene formato (a no ser que previamente lo hubieras formateado) - en este ejemplo la unidad de disco es /dev/sdb:

root@server1:~# 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: 0x00029d5c

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1        3793    30461952   83  Linux
/dev/sda2            3793        3917      992257    5  Extended
/dev/sda5            3793        3917      992256   82  Linux swap / Solaris

Disk /dev/sdb: 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: 0x00000000

Disk /dev/sdb doesn't contain a valid partition table
root@server1:~#

Crearemos una una partición.

fdisk /dev/sdb

root@server1:~# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x31e0dc4b.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

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): <-- 1
First cylinder (1-3916, default 1): <-- ENTER
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-3916, default 3916):
 <-- ENTER
Using default value 3916

Command (m for help):
 <-- t
Selected partition 1
Hex code (type L to list codes):
 <-- L

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris
 1  FAT12           39  Plan 9          82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      3c  PartitionMagic  83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       40  Venix 80286     84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      41  PPC PReP Boot   85  Linux extended  c7  Syrinx
 5  Extended        42  SFS             86  NTFS volume set da  Non-FS data
 6  FAT16           4d  QNX4.x          87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS       4e  QNX4.x 2nd part 88  Linux plaintext de  Dell Utility
 8  AIX             4f  QNX4.x 3rd part 8e  Linux LVM       df  BootIt
 9  AIX bootable    50  OnTrack DM      93  Amoeba          e1  DOS access
 a  OS/2 Boot Manag 51  OnTrack DM6 Aux 94  Amoeba BBT      e3  DOS R/O
 b  W95 FAT32       52  CP/M            9f  BSD/OS          e4  SpeedStor
 c  W95 FAT32 (LBA) 53  OnTrack DM6 Aux a0  IBM Thinkpad hi eb  BeOS fs
 e  W95 FAT16 (LBA) 54  OnTrackDM6      a5  FreeBSD         ee  GPT
 f  W95 Ext'd (LBA) 55  EZ-Drive        a6  OpenBSD         ef  EFI (FAT-12/16/
10  OPUS            56  Golden Bow      a7  NeXTSTEP        f0  Linux/PA-RISC b
11  Hidden FAT12    5c  Priam Edisk     a8  Darwin UFS      f1  SpeedStor
12  Compaq diagnost 61  SpeedStor       a9  NetBSD          f4  SpeedStor
14  Hidden FAT16 <3 63  GNU HURD or Sys ab  Darwin boot     f2  DOS secondary
16  Hidden FAT16    64  Novell Netware  af  HFS / HFS+      fb  VMware VMFS
17  Hidden HPFS/NTF 65  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE
18  AST SmartSleep  70  DiskSecure Mult b8  BSDI swap       fd  Linux raid auto
1b  Hidden W95 FAT3 75  PC/IX           bb  Boot Wizard hid fe  LANstep
1c  Hidden W95 FAT3 80  Old Minix       be  Solaris boot    ff  BBT
1e  Hidden W95 FAT1
Hex code (type L to list codes):
 <-- 83

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

Calling ioctl() to re-read partition table.
Syncing disks.
root@server1:~#

Tras la creación, comprobaremos la partición /dev/sdb1:

fdisk -l

root@server1:~# 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: 0x00029d5c

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1        3793    30461952   83  Linux
/dev/sda2            3793        3917      992257    5  Extended
/dev/sda5            3793        3917      992256   82  Linux swap / Solaris

Disk /dev/sdb: 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: 0x31e0dc4b

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        3916    31455238+  83  Linux
root@server1:~#

Lo formatearemos con ext4...

mkfs.ext4 /dev/sdb1

... Y lo montaremos en /mnt/sdb1:

mkdir /mnt/sdb1
mount /dev/sdb1 /mnt/sdb1

Comprobaremos que la partición /dev/sdb1 está montada (podrías usar mount o df -h)

mount

... now:

root@server1:~# mount
/dev/sda1 on / type ext3 (rw,errors=remount-ro)
tmpfs on /lib/init/rw type tmpfs (rw,nosuid,mode=0755)
proc on /proc type proc (rw,noexec,nosuid,nodev)
sysfs on /sys type sysfs (rw,noexec,nosuid,nodev)
udev on /dev type tmpfs (rw,mode=0755)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev)
devpts on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=620)
/dev/sdb1 on /mnt/sdb1 type ext4 (rw)
root@server1:~#

Haremos que /dev/sdb1 se monte automáticamente en el incio añadiéndolo al final del fichero /etc/fstab lo siguiente:

vi /etc/fstab

[...]
/dev/sdb1 /mnt/sdb1               ext4    errors=remount-ro 0       1

 

3 Preparando el contenedor

Container:

Como queremos mover /var/lib/mysql a la nueva partición, necesitamos detener MySQL primero, asegurate que no se inicia automáticamente mientras hacemos el cambio, haz también un backup de /var/lib/mysql and un nuevo, directorio vacio /var/lib/mysql:

/etc/init.d/mysql stop


update-rc.d -f mysql remove

mv /var/lib/mysql/ /var/lib/mysql_bak
mkdir /var/lib/mysql
chown mysql:mysql /var/lib/mysql
chmod 700 /var/lib/mysql

 

4 Creando un script de montaje en el contenedor.

Host:

En el host ahora crearemos el script de montaje /etc/vz/conf/101.mount (Asegurate de remplazar el ID 101 por por su correcto ID de su contenedor) como sigue

vi /etc/vz/conf/101.mount

#!/bin/bash
. /etc/vz/vz.conf
. ${VE_CONFFILE}
SRC=/mnt/sdb1
DST=/var/lib/mysql
if [ ! -e ${VE_ROOT}${DST} ]; then mkdir -p ${VE_ROOT}${DST}; fi
mount -n -t simfs ${SRC} ${VE_ROOT}${DST} -o ${SRC}

Debes ajutar las variables SRC y DST a tus necesidades. SRC es el directorio "fuente" (origen de los datos) el cual se quiere montar en el contenedor, en nuestro caso en particular el ya existente  (/mnt/sdb1 previamente creado), y DST es el punto de montaje del contenedor

Si quieres añadir opciones al punto de montaje como "noatime" puedes añadir parámetros -o en la línea de montaje como sigue:

#!/bin/bash
. /etc/vz/vz.conf
. ${VE_CONFFILE}
SRC=/mnt/sdb1
DST=/var/lib/mysql
if [ ! -e ${VE_ROOT}${DST} ]; then mkdir -p ${VE_ROOT}${DST}; fi
mount -o noatime -n -t simfs ${SRC} ${VE_ROOT}${DST} -o ${SRC}

Haremos el script ejecutable...

chmod +x /etc/vz/conf/101.mount

... Y reiniciaremos el el contenedor ... and restart the container (that's why we disabled MySQL's system startup links for the container in chapter three - /var/lib/mysql is empty right now which will result in MySQL errors):


... y reinicie el contenedor (ahora debería desactivar los enlaces de MySQL de inicio del sistema para el contenedor - /var/lib/mysql estrá vacío y podría dar lugar a errores de MySQL):

vzctl restart 101

 

5 Utilizando el enlace el mount del contenedor

Container:

Cuando el reinicio del contenedor haya finalizado, podrás comprobar en los log las entradas si el nuevo contenedor existe.

mount

root@test:~# mount
/dev/simfs on / type simfs (rw,relatime)
/dev/simfs on /var/lib/mysql type simfs (rw,relatime)
proc on /proc type proc (rw,relatime)
sysfs on /sys type sysfs (rw,relatime)
tmpfs on /lib/init/rw type tmpfs (rw,nosuid,relatime,mode=755)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev,relatime)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620)
root@test:~#

df -h

root@test:~# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/simfs            1.0G  381M  644M  38% /
/dev/simfs             30G  172M   28G   1% /var/lib/mysql
tmpfs                 128M     0  128M   0% /lib/init/rw
tmpfs                 128M     0  128M   0% /dev/shm
root@test:~#

Ve al directorio /var/lib

cd /var/lib
ls -la

root@test:/var/lib# ls -la
total 72
drwxr-xr-x 18 root    root    4096 Jul 17 10:25 .
drwxr-xr-x 13 root    root    4096 Feb 13  2011 ..
drwxr-xr-x  5 root    root    4096 Jul 17 10:16 apt
drwxr-xr-x  2 root    root    4096 Oct 16  2010 aptitude
drwxr-xr-x  2 root    root    4096 Feb 13  2011 dhcp
drwxr-xr-x  7 root    root    4096 Jul 17 10:16 dpkg
drwxr-xr-x  2 root    root    4096 Jan  1  2011 initscripts
drwxr-xr-x  2 root    root    4096 Jan  1  2011 insserv
drwxrwsr-x  2 libuuid libuuid 4096 Feb 13  2011 libuuid
drwxr-xr-x  2 root    root    4096 Apr 17  2010 logrotate
drwxr-xr-x  2 root    root    4096 Dec 14  2010 misc
drwxr-xr-x  4 root    root    4096 Jul 17 10:53 mysql
drwx------  3 mysql   mysql   4096 Jul 17 10:24 mysql_bak
drwxr-xr-x  2 root    root    4096 Feb 13  2011 pam
drwxr-xr-x  2 root    root    4096 Jul 17 10:23 quota
drwxr-xr-x  2 root    root    4096 Jul 17 10:52 update-rc.d
drwxr-xr-x  2 root    root    4096 Jul 17 10:53 urandom
drwxr-xr-x  3 root    root    4096 Feb 13  2011 vim
root@test:/var/lib#

Como podrás observar el nuevo directorio /var/lib/mysql tiene mal los permisos/deuño - corrigelo con los permisos y dueños apropiados y entonces reinicia el contenedor nuevamente)

chown mysql:mysql /var/lib/mysql
chmod 700 /var/lib/mysql

Ahora mueve las databases de nuestro backup a /var/lib/mysql:

cp -pfr /var/lib/mysql_bak/* /var/lib/mysql

Entonces inicia MySQL:

/etc/init.d/mysql start

Ya está

¡Eso es! Una última cosa que debe hacer es volver a crear vínculos de inicio del sistema de MySQL, de modo que se inicie automáticamente cuando el contenedor se inicia:

update-rc.d mysql defaults

 

6 Links

Pin It

Escribir un comentario


Código de seguridad
Refescar



Redes:



 

Suscribete / Newsletter

Suscribete a nuestras Newsletter y periódicamente recibirás un resumen de las noticias publicadas.

Donar a LinuxParty

Probablemente te niegues, pero.. ¿Podrías ayudarnos con una donación?


Tutorial de Linux

Filtro por Categorías