Ensuring that /var is correctly mounted on HDD

From Alpine Linux
Jump to: navigation, search
Underconstruction clock icon gray.svg
This material is work-in-progress ...

Do not follow instructions here until this notice is removed.
(Last edited by Mhavela on 18 Sep 2013.)

Description

Before the days when 'setup-disk' wizard existed, someone might have manually mounted '/var' to HDD. This document will try to guide you to change those mounts so they are configured the same way as it would be when suing the 'setup-disk' wizard.

Old way of mounting

Lets assume that the user mounted using 'bind' mounts. When running the 'mount' command, you would see output similar to this:

/media/md0/var/log /var/log auto bind 0 0
/media/md0/var/lib/freeswitch /var/lib/freeswitch auto bind 0 0
/media/md0/var/www/localhost/provisioning /var/www/localhost/provisioning auto bind 0 0

As you can see in the above output, there are multiple mountpoints to '/media/md0/...'.

New way of mounting

The new way of mounting (when using the 'setup-disk' or 'setup-alpine' command) mounts the whole '/var' onto HDD. When running the 'mount' command, you would see output similar to this:

/dev/md2 on /var type ext4 (rw,relatime,data=ordered)

As you can see in the above output, whole '/var' is mounted on '/dev/md2' ('/dev/md2' could also be some other device that represents your HDD).

What could possibly go wrong?

Q: What could go wrong when using the 'old' way of mounting?
A: Let's say that you want to install a new package (e.g. postgresql. You configure it to do what you want, and you are happy that the system seems to work as it should. During installation postgresql will create it's database at '/var/lib/postgresql/...'. But because you have the 'old' type of mounts, this path is not stored on HDD, but instead in RAM (tmpfs). Most likely things will work as long as your system is powered on. But next time the box is rebooted, all your databases will get lost (they where stored in RAM and not on HDD).
A solution to this is either to make sure that whole '/var' is mounted on HDD (the 'new' type of mount as described in this article) or you will need to edit /etc/fstab telling the system that '/var/lib/postgresql' needs to be mounted on HDD (this article does not cover how you do that).

Am I effeted?

Run the 'mount' command:

mount

Analyze the output to figure out if you have 'old' or 'new' type of mount (see the above section 'Description' for help)

Tip: You can also run the following command that might help you figure out what type of 'mounts' you are using

if [ $( grep /var /etc/fstab | wc -l ) -gt 1 ] || grep -q 'var.*bind' /etc/fstab || grep -q md0 /etc/fstab; then echo "Huston... we got a problem!"; fi

If you see a output that saids: 'Huston... we got a problem!' then you know that you have the 'old' type of mounts and would need to do the below mentioned actions.


Changing to new way of 'mount'

Stop running processes

Various systems reads/writes to /var. These processes needs to be stopped to prevent you from losing any useful data.

Start by figuring out which processes might need to be stopped:

rc-status

Now stop the running processes
(Below list only gives a example. You might have more or less processes to stop.)

Note: If you are connected to this host using 'ssh', you might not want to stop 'sshd' or you will lose connection to the host.

/etc/init.d/syslog stop /etc/init.d/dhcpd stop /etc/init.d/postgresql stop /etc/init.d/postgresql2 stop /etc/init.d/freeswitch stop /etc/init.d/kamailio stop /etc/init.d/mini_httpd stop /etc/init.d/cron stop

Move files

Copy files from 'tmpfs' onto the the harddrive

find /var -xdev | cpio -vmpd /media/md0/

Remove files from 'tmpfs' (freeing space)

find /var/ -xdev -type f -delete

Unmount disk

Run this command just to verify which mounts will be about to be unmounted (next command)

awk '/\/var.*bind/ { print $2 }' /etc/fstab

You should see a output that looks something like:

/var/log
/var/lib/freeswitch
/var/www/localhost/provisioning
/var/lib/postgresql

Unmount current bind-mounts

awk '/\/var.*bind/ { print $2 }' /etc/fstab | xargs umount

Unmount 'md0'.

umount /media/md0

Rename the raid device

Rename 'md0`' to 'md2'

mdadm --stop /dev/md0 sed -i -e 's:/dev/md0:/dev/md2:' /etc/mdadm.conf

Verify that the /etc/mdadm.conf looks like the following (note that it saids 'md2' instead of 'md0')

cat /etc/mdadm.conf

Contents of /etc/mdadm.conf

ARRAY /dev/md2 metadata=1.2 name=sipaccessory:0 UUID=bca180e8:868dbad4:ee90c86a:2bcbc2a1

Start up raid

mdadm --assemble /dev/md2

Convert ext3 file system to ext4 file system

Do a file system check (this command will take approx 5-10min to execute - please be patient)

fsck.ext3 -pfv /dev/md2

Convert filesystem to ext4

tune2fs -O extents,uninit_bg,dir_index /dev/md2

Do a new file system check (this command will take approx 5-10min to execute - please be patient)
Note: The command will show some errors. These errors can be ignored.

fsck.ext4 -yfDv /dev/md2

Update fstab

Remove all references to old media in /etc/fstab

sed -i -e '/\/media\/md0/d' /etc/fstab

Create a reference to the new media in /etc/fstab

echo '/dev/md2 /var ext4 defaults 1 2' >> /etc/fstab

Mount the new media

mount /var

Reorganize content on the harddrive

Reorganize the content of the harddrive

mv /var/var/* /var

Make sure no 'var/' is saved onto the apk overlay

lbu inc -l

Permanently save changes

lbu ci

Start up services

rc default

Create raid device for swap

Stop current swap

swapoff -a

Create md1 partition on raid (confirm action by clicking 'y' if prompted)

mdadm --create /dev/md1 --level=1 --raid-devices=2 /dev/sdb1 missing

Update /etc/fstab to point to new swap device

sed -i -e 's:sdb1:md1:g' /etc/fstab

Format swap device
Tango-dialog-warning.png
Warning: Make sure not enter wrong device in this command or you will loose all data on /var!

mkswap /dev/md1


Start the new swap device

swapon -a

Permanently save changes

lbu ci

Reboot host

reboot

Summary

When you done all the above procedures you should now have a system that has '/var' mounted the same way as if you would run 'setup-disk' wizard and selecting 'data', on a newer Alpine Linux host.