OpenVZ Anleitung · 8 min read · Oct 11, 2025

OpenVZ: Hostgeräte/-partitionen/-verzeichnisse in einem Container mit Bind-Mounts (Debian/Ubuntu) einbinden

OpenVZ: Hostgeräte/-partitionen/-verzeichnisse in einem Container mit Bind-Mounts (Debian/Ubuntu) einbinden

Version 1.0
Autor: Falko Timme
Folge mir auf Twitter

Manchmal befindet man sich in einer Situation, in der man eine Festplatte, Partition oder ein Verzeichnis vom OpenVZ-Host innerhalb eines OpenVZ-Containers einbinden muss - zum Beispiel, wenn man eine schnelle SSD zum Host hinzufügt und die MySQL-Datenbanken des Containers darauf speichern möchte, um MySQL schneller zu machen. Dieses Tutorial erklärt, wie man Hostgeräte/-partitionen/-verzeichnisse in einem OpenVZ-Container mit Bind-Mounts einbindet.

Ich gebe keine Garantie, dass dies bei Ihnen funktioniert!

1 Vorbemerkung

In diesem Tutorial habe ich einen OpenVZ-Container mit der Container-ID 101, der MySQL ausführt. Ich füge dem Host eine zweite Festplatte hinzu und möchte das MySQL-Verzeichnis des Containers /var/lib/mysql auf die zweite Festplatte legen.

2 Vorbereitung des Hosts

Host:

Wenn Sie eine neue Festplatte zum Host hinzugefügt haben, sollten Sie sie in der Ausgabe von…

fdisk -l

… sehen und feststellen, dass sie nicht formatiert ist (es sei denn, Sie haben zuvor Partitionen darauf erstellt) - in diesem Beispiel ist die Festplatte /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:~#

Lassen Sie uns eine einzige Partition darauf erstellen:

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:~#

Danach sollte die Partition /dev/sdb1 vorhanden sein:

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:~#

Lassen Sie uns es mit ext4 formatieren…

mkfs.ext4 /dev/sdb1

… und es nach /mnt/sdb1 einbinden:

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

Sie sollten /dev/sdb1 in der Ausgabe von…

mount

… jetzt sehen:

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:~#

Um /dev/sdb1 beim Booten automatisch einzubinden, fügen Sie es zu /etc/fstab hinzu:

vi /etc/fstab

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

3 Vorbereitung des Containers

Container:

Da wir /var/lib/mysql auf die neue Partition verschieben möchten, müssen wir zuerst MySQL stoppen, sicherstellen, dass es beim Start des Containers nicht automatisch gestartet wird, ein Backup von /var/lib/mysql erstellen und ein neues, leeres Verzeichnis /var/lib/mysql anlegen:

/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 Erstellen eines Bind-Mount-Skripts für den Container auf dem Host

Host:

Auf dem Host erstellen wir jetzt das Bind-Mount-Skript /etc/vz/conf/101.mount (stellen Sie sicher, dass Sie 101 durch die korrekte Container-ID ersetzen!) wie folgt:

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} |

Sie müssen die Variablen SRC und DST an Ihre Bedürfnisse anpassen. SRC ist das Verzeichnis, das wir im Container einbinden möchten (/mnt/sdb1 in diesem Beispiel), und DST ist der Einhängepunkt im Container.

Wenn Sie Einhängeoptionen wie noatime hinzufügen möchten, können Sie die -o-Option zur Mount-Zeile hinzufügen, z. B. wie folgt:

| #!/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} |

Machen Sie das Skript ausführbar…

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

… und starten Sie den Container neu (deshalb haben wir die Systemstartlinks von MySQL für den Container im dritten Kapitel deaktiviert - /var/lib/mysql ist jetzt leer, was zu MySQL-Fehlern führen wird):

vzctl restart 101

5 Verwendung des Bind-Mounts im Container

Container:

Nachdem der Neustart des Containers abgeschlossen ist, können Sie sich in den Container einloggen und überprüfen, ob der neue Mount vorhanden ist:

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:~#

Gehen Sie zum Verzeichnis /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#

Wie Sie sehen, hat das neue Verzeichnis /var/lib/mysql falsche Berechtigungen/Eigentum - lassen Sie uns das korrigieren (die neuen Berechtigungen/Eigentum sollten auch nach einem Neustart des Containers beibehalten werden):

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

Lassen Sie uns nun die Datenbanken aus unserem Backup zurück nach /var/lib/mysql verschieben:

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

Dann starten Sie MySQL:

/etc/init.d/mysql start

Das war’s! Eine letzte Sache, die Sie tun sollten, ist, die Systemstartlinks von MySQL neu zu erstellen, damit es automatisch gestartet wird, wenn der Container gestartet wird:

update-rc.d mysql defaults

6 Links

Share: X/Twitter LinkedIn

Erhalte neue Beiträge in deinem Posteingang.

Kein Spam. Jederzeit abmelden.