1diff --git a/content/posts/2023-04-30-using-data-mode-alpine.md b/content/posts/2023-04-30-using-data-mode-alpine.md
2new file mode 100644
3index 0000000000000000000000000000000000000000..d0e76236b7037a897961407ed94e83577b759dfb
4--- /dev/null
5+++ b/content/posts/2023-04-30-using-data-mode-alpine.md
6@@ -0,0 +1,156 @@
7+---
8+title: "Running alpine in memory with data mode"
9+date: 2023-04-30
10+tags: ['alpine', 'linux']
11+---
12+
13+# Local testing
14+
15+To facilitate iteration and testing of this setup, we can use `qemu` and for the
16+whole setup we will be using two disks: one for mounting the /var folder and
17+another for storing lbu files (more on that later). To create the two images,
18+follow these steps:
19+
20+```bash
21+qemu-img create -f qcow2 var.cow2 10G
22+qemu-img create -f qcow2 media.cow2 5G
23+```
24+
25+Those images have arbitrary sizes and probably won't even required that much for
26+this setup so feel free to choose another size.
27+
28+Once you have created the images go to alpine download page[^1] and grab the
29+latest virtual image.
30+
31+We all of that we can now start the virtual machine
32+
33+```bash
34+qemu-system-x86_64 \
35+ -machine accel=kvm \
36+ -display gtk \
37+ -m 2048 \
38+ -boot d \
39+ -cdrom alpine-virt-3.17.3-x86_64.iso \
40+ -drive file=var.cow2,if=virtio \
41+ -drive file=media.cow2,if=virtio
42+```
43+
44+For more info about what are those parameters head to `qemu` documentation[^2].
45+Just pay attention to the `-boot d` option which will force cdrom to boot first
46+(more on that later as well).
47+
48+# Setting up environment
49+
50+Before we can run `setup-alpine` we need to mount a persistent media folder so
51+it can be picked up by the script and used to store the `lbu` files. To do so we
52+need to install some extra package that are not available in the live ISO.
53+
54+Run `setup-interfaces` to configure interfaces. The default values will do.
55+After that start the networking service `rc-service networking start`. Now we
56+have internet we can setup a repository. You could edit `/etc/apk/repositories`
57+directly but there is handy command for that already `setup-apkrepos`. Run it
58+and pick any option you see fit. I'd go for `f` but `1` also works.
59+
60+Now we can install some packages required for the remaining of the setup:
61+
62+```bash
63+apk add lsbkl e2fsprogs
64+```
65+
66+`lsblk` is useful to identify devices and `e2fsprogs` will provide `ext4`
67+support.
68+
69+Run `lsblk` and will display the device we attached, e.g.:
70+
71+```bash
72+vda 253:0 0 10G 0 disk
73+vdb 253:0 0 5G 0 disk
74+```
75+
76+Now let's format and mount `vdb` on `/media`.
77+
78+```bash
79+# formatting using ext4
80+mkfs.ext4 /dev/vdb
81+
82+# creating target folder for mouting
83+# the name is arbitrary, feel free to choose another one
84+mkdir /media/vdb
85+
86+# mouting
87+mount -t ext4 /dev/vdb /media/vdb
88+```
89+
90+To confirm if device is mounted you can run `df -h /media/vdb`, it shows the
91+size and which device is mounted on that folder.
92+
93+# Setting up alpine on data mode
94+
95+Now we can run `setup-alpine`. Choose whatever options fits your need up to the
96+point where it asks to choose the device. It may repeat some of the step we
97+already did but it is be fine.
98+
99+When it asks to choose a disks to use enter the name of disk, which, in this
100+particularly setup, is `vda`. Then it will ask to choose how you would like to
101+run alpine[^3], pick `data`.
102+
103+Now it will prompt to choose which media device we want to use for storing the
104+`lbu` files. By default it should the media folder we mounted in the previous
105+step, if not just enter `vdb`. Select place for cache. Default is fine.
106+
107+The cache folder is used to store the apk files we come to add. Since it does
108+not have internet access when booting it needs to store those extra package in
109+folder so later it can be restored.
110+
111+**Warning**, do not reboot now. We need to use `lbu` to make a backup of all
112+changes we did, otherwise everything will be lost. Take a careful read of the
113+`lbu` documentation[^4], it will provide the necessary information to understand
114+how `lbu` works.
115+
116+Run `lbu commit` to backup it. You can check the `apkvol` file stored in the
117+`/media/vdb/`. Now you changes as saved and you are good to reboot.
118+
119+The live ISO will look for `apkvol` files and try to restore it. That is why it
120+is required the ISO to be the first to boot. There is no boot info store
121+anywhere else since the one device is used to store `lbu` and the other one is
122+mount on `/var` so we use live ISO to boot and restore the state.
123+
124+You can check here[^5] how that is possible and here [^6] how we can expand that
125+idea and netboot using the apkvol to boot any machine to specific state.
126+
127+# Making changes
128+
129+After rebooting your system, you can now log into your fresh installation. You
130+can then install a new package, such as vim, using the command `apk add vim`.
131+However, if you reboot the system again, the vim package will be lost and you
132+will need to reinstall it.
133+
134+If you run `lbu status` will show what was changed and in this case
135+`/etc/apk/world`. The world file store all the package you have installed and
136+since you have added a new packaged it has been modified. `lbu
137+commit` to persist it.
138+
139+You can check the `/media/vdb/cache` folder to see that it has stored the vim
140+package and its dependencies.
141+
142+# Why /var?
143+
144+The /var folder is a directory in Linux that is used to store variable data
145+files as the contents of this folder can change by the OS. This folder contains
146+files that are not critical to the basic operation of the system, but are
147+instead used for tasks such as logging, spooling, and caching. For example
148+postgresql store all its data on the var folder allowing us to use a database on
149+data mode and still have its data persistent between boots.
150+
151+# In conclusion
152+
153+We can take advantage of speed boost provided by `tmpfs`, and we can still
154+restore the system state even if the computer is rebooted. The only thing to
155+keep in mind is to commit any changes made before rebooting ;).
156+
157+[^1]: https://alpinelinux.org/downloads/
158+[^2]: https://www.qemu.org/docs/master/system/invocation.html
159+[^3]: https://wiki.alpinelinux.org/wiki/Installation
160+[^4]: https://wiki.alpinelinux.org/wiki/Alpine_local_backup
161+[^5]: https://bitfehler.srht.site/posts/2022-11-28_messing-with-your-initramfs---alpine-edition.html
162+[^6]: https://www.apalrd.net/posts/2022/alpine_pxe/