Sunday, October 25, 2026

VGA Palette #2


Hi there. I've dedicated this post of the series to the mode 13h palette. In my opinion, the mode deserves to be explored in its own. Or maybe, I'm just being nostalgic and therefore biased when it comes to mode 13h. I'll handle the topics in this post by viewing some files using the BMP Viewer, I mentioned in my previous post. I'll additionally highlight some more details about the palette here.


First of all, almost everything I explained so far about the palette of other display modes in the first article also applies to mode 13h. To summarize: The VGA DAC can display only 16 of 262 144 colors in text mode as well as mode 12h, and 256 of them in mode 13h simultaneously. Each palette color has an index and an 18-bit RGB value consisting of 6-bit per channel. After the palette index is written to the port 3C8h, the color values of each channel are written to the port 3C9h sequentially with byte-length. Here is the loop of the BMP Viewer, which loads the palette.

  for(i = 0; i < ColorCount; i++)    {
    outportb(0x3C8, i);    // Palette index
    outportb(0x3C9, VGApalette.color[i].B >> 2);  // Blue
    outportb(0x3C9, VGApalette.color[i].G >> 2);  // Green
    outportb(0x3C9, VGApalette.color[i].R >> 2);  // Red
  }


The palette of mode 13h is simpler than other modes because, even though it is still possible to assign values to attribute register in this mode, there is no point in assigning a color to another index which can already be displayed on the screen, since all colors in the DAC (256 colors) can be displayed anyway without any trick. Therefore the attribute register has no use in this mode.

As a base example, I'll use the three parrots image from the VGA article on Wikipedia. As this image contains all 256 colors, it's quite useful for demonstrating VGA's capabilities.

Source: Wikipedia [2]

I first resized this image to 320x200. The original image is 1280x960, therefore the aspect ratio changed, but apart from that, I did not change anything on the existing colors or the palette. Here is a screenshot of the image from DOSBox.


There is no noticeable difference when comparing the two images visually. To show the effect of the palette better, let's comment out the setPalette function on line 208, and see how it would look with the default mode 13h palette:

As you can clearly see, the absence of the palette makes a significant difference.

Back in the 90s, when RAM in computers was measured in MBs and therefore software optimization was the primary concern, I wondered, "(How) Can I rearrange the colors in the image palette to fit the default VGA palette?". That way, I could get rid of both setPalette(...) function, which is around 400 bytes, and the palette, which is 1 KB per image. I'll briefly explain the theory behind this, using the image below, which I cropped and scaled a bit down from the original [1], which has 97 328 colors.


The problem of creating a palette by decreasing the number of colors had been studied long before the question above even came to my mind. First check point is the median cut algorithm from 1982. In this algorithm, the median of all colors in the color space is calculated, and it is assumed that this color could represent all others with the least average error. Depending on the target color depth,  the number of colors is decreased to a 2n (usually 16 or 256) colors by recursively calculating the medians of the color space. However, since this algorithm ignores small color deviations in images with a wide range of colors, it causes unnatural color islands. To show this effect, I decreased the number of colors to 16 using median cut (below). The color islands in the background and on the birds belly are clearly noticeable.


For this reason, techniques such as dithering and/or error diffusion are applied to insert pixels with similar colors from the palette, thus adding a small amount of noise to large color islands. This results in an image that is less unpleasant to the human eye.


Good old days 🥲

Although median cut is only used for decreasing the number of colors, error diffusion is also used in palette transformations. I won't go into the details of the algorithms in this post. I'll save them for the next post, if there is enough space. Back in the days, I tried and learned these conversions using Paint Shop Pro (PSP) and got quite nice results. Below, I put the image, whose palette I converted, at the top and the original image at the bottom for comparison. Although there are some shiny, flashy colors on the green bird and the road in the background due to error diffusion, the result is still not too bad.


When I display the converted image in DOSBox without setPalette(...):

I also displayed this image without modifying the viewer, and when I overlapped both in GIMP and subtracted the layers, I was left with a black image as expected.

To convert the palette, as I explained in my previous post, first I sent the palette index via port 0x3C7, read the color values from port 0x3C9, then multiplied them by four, and wrote them to a file in JASC (PSP) format. This format is a simple text file consisting of a three line header followed by R, G, B values separated by spaces. And the good thing is, GIMP can also read this format.

OPEN "13.PAL" FOR OUTPUT AS #1
PRINT #1, "JASC-PAL"
PRINT #1, "0100"
PRINT #1, "256"

SCREEN 13
FOR I% = 0 TO 255
    OUT &H3C7, I%
    R% = INP(&H3C9)
    G% = INP(&H3C9)
    B% = INP(&H3C9)
    PRINT #1, LTRIM$(STR$(R% * 4));
    PRINT #1, RTRIM$(STR$(G% * 4));
    PRINT #1, RTRIM$(STR$(B% * 4))
NEXT I%

CLOSE #1

I downloaded Paint Shop Pro from oldversion.com and could run it with Wine under Linux. I opened the file I wanted to get its palette converted and went to Colors -> Load Palette. Here, I loaded the 13.PAL, I had created in the previous step and selected "Error diffusion dithering" as the conversion algorithm. You can try out different algorithms and compare their results here.

In GIMP, you first need to open Palettes window (Windows -> Dockable Dialogs -> Palettes), then right click on an empty area and import 13.PAL. Then I opened the image, I wanted to convert. In the window, that appeared when I selected Image -> Mode -> Indexed, I checked "Use custom palette" and selected 13.PAL from the list below. No matter which dithering method I chose, GIMP unfortunately couldn't do this conversion any better than PSP.




[1]: https://commons.wikimedia.org/wiki/File:Perched_blue_and_orange_bird_%28Unsplash%29.jpg
[2]: https://en.wikipedia.org/wiki/Video_Graphics_Array#/media/File:Birds_VGA256.png

Tuesday, September 29, 2026

Bitmap (BMP) Files


Hi there. I initially had started this article as the second part in the VGA Palette series, but in this article, I've decided to focus on the bitmap (.bmp) file viewer, which I originally developed for the second part and consequently on bitmap files. That way, I can get back to VGA palette operations in mode 13h in the next article, without losing focus with file format specifications.


BMP File Format

In my previous post, I mentioned that I was developing a tiny bmp file viewer in C for mode 13h. Bitmap (bmp) files have a quite simple structure, so that I can cover the necessary aspects of this topic in this article.

BMP files consist of a simple two-part file header and pixel data. The first header contains the file size and a pointer to the image data. The second header contains information such as width, height, color depth and compression information. On the right figure, the first header is the area marked in red, while the second header is the area marked in yellow. The gray area shows the palette, which I will discuss later in this post. This figure shows a hex dump of an image with 320x200 pixels. I've marked fields such as width and height with boxes.

Technically speaking, BMP files can be compressed with RLE (run length encoding), but it's very unusual to find a compressed one. Even though the format was developed by Microsoft and IBM, not even MS Paint(brush) was able to save compressed BMP files, if I recall correctly. For this reason, I won't discuss RLE at all.

Although I don't want to go into the details of the code in this chapter, it will be still easier for me to explain the file format structures using the data structures in the code's header file ( VGA13.H ). At the very beginning of a BMP file is 'BM' file signature. The only important field in the bitmapfileheader structure is the OffsPixelArray pointer, which points to the pixel array, the area shown in the figure above with the values 36h, 04h. This is followed by bitmapimageheader, which holds the image-related info. I've also specified the file offsets of these fields in the VGA13.H file.

According to the BMP file format article, there are seven different versions of bitmapimageheader structure. The 40-byte version called BITMAPINFOHEADER, which I also used as a basis of my code, is the most common version. I tested my code with 23 files, some saved with various image processing software including GIMP, and some downloaded from internet. I cannot include one of these files, but I uploaded my test set of 22 files. Btw, even though GIMP has developed the latest version of bitmap header, BITMAPV5HEADER, it does not use it by default when saving bmp files.

Bitmap files support four different color depths. These are stored in BitsPerPixel field. If BitsPerPixel is;

  • 1, then the image has two colors, black and white and each byte contains 8 pixels.
  • 4, then the image is 16-color, and it contains a palette for these colors. Each byte contains two pixels.
  • 8, then the image has 256 colors and a palette for these colors. Each byte represents a single pixel.
  • 24, then each pixel of the image consists of R, G, B triplets each with 8 bits. And the image doesn't contain any palette.

I focused only on 8 bit per pixel images. Because, first, I can't display more than that with mode 13h, and second, the palette is only available for 4 and 8-bit color depths. I created some of the images in my test set for this article, by resizing the image of three parrots siting on a wire mesh fence from VGA article in Wikipedia to 320x200.

Direct Link

I need to make a note about 8-bit images here. Logically, grayscale palette consists of (i, i, i) RGB triplets, where i is their index. Therefore, there is no need to save the palette along with the file. I knew, that if a bitmap file has 8-bit color depth but no palette, it's treated as grayscale image, and some programs still used to save the palette in the file for compatibility anyway. A grayscale image, I saved with GIMP has been saved with a palette, and even after manually removing the palette from the file, and trying to open it with four different programs, it did not succeed. Moreover, Wikipedia mentions that a palette is mandatory for files with a color depth of 8 or lower. On the other hand, the bmp_08.bmp file in the test set is an example for such an 8-bit file without palette. In short, I think, this is a matter on which there is still no consensus. From coding perspective, this worked out quite well for me, as I did not have to do anything extra for this case. My code opens this file, but since it doesn't know that it needs to load a grayscale palette, it opens it with the default mode 13h palette.

Palette follows the bitmapimageheader structure. The size of bitmapfileheader is fixed 14 bytes. The size of bitmapimageheader is given in HeaderSize field. Therefore, the offset of the palette can be found by adding 14 to this value. If this sum equals to bitmapfileheader.OffsPixelArray value, it indicates that there is no palette (e.g. for 24-byte color depth). Each palette entry consists of 4 bytes representing R,G,B and A respectively. However the alpha channel and transparency implementation are not standard in bmp files. Therefore A is nearly always zero. And in total, the number of palette entries is 2BitsPerPixel.

Palette must end at bitmapfileheader.OffsPixelArray offset, and is followed by pixel array. Here, the pixels are stored from left to right and from bottom to top. The first data corresponds to the bottom-left pixel. If the image's width is not a multiple of four, the rows are padded with zeros to make them a multiple of four. For example, if an image is 133 pixels wide, three zeros are added at the end of each line while saving, to pad the data to 136.

Screenshot from DOSBox

BMP Viewer

Above is an example, demonstrating the programs output.

I will review my code starting with the main(...) function on line 174. Here, the memory is allocated statically for both file headers first (lines 177-178). If a BMP file has not been passed to the program as a command line parameter, the user is asked for the file name (line 188), and the file is then opened for reading (line 193).

The readFirstHeader(...) function reads from the file directly into the data structure, and if the file signature does not equal to 'BM' (line 43), it exists with a non-zero error and this also terminates the program (line 199).

The readSecondHeader(...) function on line 199, similarly reads the second header from the file into the data structure. To determine file properties easily, I added a small showBMPinfo function (line 22) to print them. This function call can be commented out or combined with a debugging parameter. If the file does not meet the specifications we can display, i.e. if the color depth is different than eight or if the file is compressed, the function exits with an error value and it terminates the program. On line 69, a warning is printed to the screen if the size of the image is bigger than 320x200, but the file is opened anyway and viewed partially.

I simply implemented mode13() and textmode() functions with assembly (line 78 and 86).

As I mentioned above, there are multiple versions of bitmapimageheader. Common fields of these header versions are width, height and color depth. If the header of the input file happens to be something different than BITMAPINFOHEADER (maybe a header of a different length), the readSecondHeader(...) function will not read the header completely. For this reason, on line 207, any unread bytes are skipped. The file pointer is moved to the start offset of the palette, and processing of the palette begins with setPalette(...). In this function, the palette is read ColorCount times 4-byte chunks. On line 105, the color index is written to port 0x3C8, followed by R, G, B values written to port 0x3C9, as I explained in the previous post. Since VGA colors are 18-bit, the 8-bit values are shifted right two bits to convert them to 6-bit.

The fseek(...) on line 210 is there to read the correct offset, if the file's header version is not BITMAPINFOHEADER. Normally, the file pointer should already point to the firstHeader.OffsPixelArray value at that moment. setImage(...) reads the pixel array and renders the image to the screen. I wrote this function twice. The first version on line 134 is inefficient, because it reads the byte by byte. With the second version, images are read line by line. For this, I round the width up to the nearest multiple of 4 using ceilTo4 and allocate a memory line of that size (line 160). Finally, I read the bytes from the file with image width sized blocks (line 167) and write them to the screen with a for loop. Instead of PutPixel function, something like this could have been done here:

lds si, linebuffer
push A000
pop es
mov ax, i
mov di,ax
shr ax,8
shr di,6
add di,ax
mov cx, c4
rep movsb

This snippet is of course just a sketch. The linebuffer pointer is loaded into DS:SI. i is multiplied by 320 using shr and loaded to DI. Image width is loaded to CX, and the linebuffer could be copied block by block to VGA screen memory using rep movsb. In this case, the MIN macro should also need to be translated to assembly. In this way, it should run even faster than my recent approach, yet I did not implement it, because I did not want to deal with assembly and far pointers in C, which could have increased debugging time significantly.

Tuesday, August 25, 2026

VGA Palette #1


Hi there. In this new series of articles, I'll be writing about color palettes of VGA. Since this is a relatively deep topic, I planned to cover it over several posts. In this post, I'll first focus on VGA 640 x 480 (aka screen 12) and text mode to explain what the color palette is, how to access and change it. I'll mention mode 13h in a separate post later and discuss some visual effects, based on VGA palette.


VGA introduced two important graphics modes. First one is mode 12h with 640 x 480 resolution and 16 colors, and the other one is well-known mode 13h, with 320 x 200 resolution and 256 colors. Other modes were retained for backwards compatibility with older graphics hardware. The VGA card's digital-analog converter (DAC) can display colors from 18-bit RGB color gamut (218 = 256 K = 262 144 colors) on the screen [1], but under mode 13h, only 256 of these at the same time, and under mode 12h only 16. This subset of colors from this color gamut is called a palette.

Color information consists of a palette index and an 18-bit RGB color value (6+6+6). For example, if the index is 1 and the RGB code is 0,0,63, the first color is blue; if the index is 2 and the RGB code is 24,24,24, the second color is gray, and so on. The index value starts at zero, and the zeroth color is the background color, which is therefore usually black (RGB 0,0,0). If the indices from 0 to 255 are filled with linearly increasing t values in RGB t,t,t form, a grayscale palette is obtained. Or if they are filled in the form RGB 0,r,0, a purely green toned palette is obtained.

Colors are processed through the DAC of graphics card. Therefore, palette operations are carried out using three* DAC registers of VGA card [3]:

DAC Address Read Mode Register (Write at 3C7h)
76543210
DAC Read Address

Actually, 3C7h has two functions. If this register is read, the two least significant bits indicate, whether the DAC is in read or write mode. However, if I've already executed an out instruction, I've written, or if I've executed an in instruction, I've read. This is why, this function of the register isn't used any often. To read the palette, the index value is written to this register. Then, the DAC data register on port 3C9h is read three times each in byte-length:


DAC Data Register (Read/Write at 3C9h)
76543210


DAC Data

When writing to the palette, the color index is written to port 3C8h, and three byte-length values are sent to the DAC data register.


DAC Address Write Mode Register (Read/Write at 3C8h)
76543210
DAC Write Address

*There are actually four DAC registers. The DAC Mask register, which I did not mention above, is accessed via port 3C6h and always contains 0FFh value. Writing any other value to this register disables access to the DAC [2].

The aforementioned byte-length read and write operations refer to in al,dx and out dx,al instructions. However, only the lowest 6 bits of the read and written values are significant. Remember that the color gamut is 6+6+6=18 bits.


Let's focus on the text mode and mode 12h, first. In these two modes, a maximum of 16 colors can be displayed on the screen. In this respect, they are similar. One detail, which goes often unnoticed is that the text mode palette can also be modified. In my first VGA post [5], I explained, how to change text and background colors of a text directly. Now, let's take a look at the text mode palette using a simple BASIC code:

FOR I% = 0 TO 63
    OUT &H3C7, I%
    PRINT "("; I%; "="; INP(&H3C9); INP(&H3C9); INP(&H3C9); ")";
NEXT I%

DEF SEG = &HB800

FOR J% = 1 TO 15
    FOR I% = 1 TO 159 STEP 2
        POKE (I% + J% * 160), J%
    NEXT I%
NEXT J%

In first part, I send the color index value from 3C7h, then read the color codes from 3C9h, and printed them to the screen. In second part, I accessed the text mode video memory and colored the first line with the first palette color, the second line with the second palette color, and so on:

I mentioned that 16 colors can be used in text mode, but I printed 64 color codes to the screen, and interestingly, it appears that non-zero codes have been assigned to the indices between [16, 63]. The codes for indices between [64, 255] are zero, therefore not printed, but colors can be assigned to them as well if needed. So, what's the meaning of this? Normally, a character on the screen consists of 2 bytes: one byte is its ASCII code, and the next one holds its color information. The lower 4 bits of the color byte represent the character's color, while the upper 4 bits represent the character's background color. Since just 4 bits are allocated for colors, assigning a color code to the indices 16 and above might seem pointless at first glance, but there is a trick: The VGA Attribute Register (3C0h) [4] can be used to change a color's palette index. Without getting into too much detail, here is a simple code snippet:

A% = INP(&H3DA)
OUT &H3C0, 5
OUT &H3C0, 60
OUT &H3C0, &H20

where I assigned the 60th color to the fifth one, by writing the value 60, into the fifth attribute register.

Getting back to the screenshot, the eighth palette entry (0, 0, 21) should be navy blue or dark blueish, and the ninth palette entry (0, 0, 63) should be pure blue. However, assuming that we're counting from zero, the eighth row is actually dark grey instead of navy blue, and the ninth row, which supposed to be vivid blue, is just a pale blue (neon blue). If these color codes printed on the screen were represented the actual colors of the rows, the screen would actually look like the right side of the image below. The left side shows, what actually visible is. For an easy comparison, I've put two images side by side:


The conclusion is, that the text mode is actually using attribute registers for the colors [8, 15]. To render the left side of the above image, I manually assigned the first fifteen colors to the first fifteen palette indices manually.

Everything about text mode palette also applies to mode 12h palette. Even though 16 colors can be shown at any time, the default palette contains 64 colors. Hint: If you switch from mode 13h to mode 12h or to text mode, mode 13h palette will also stay in DAC for other modes. In other words, when the computer (or DosBox) starts up, all colors in the range [64, 255] are set to (0, 0, 0). If you just enter mode 13h and switch back to text mode (or mode 12h), these indices won't be containing zeros anymore. 

Similar to text mode, the colors on the screen are selected from the palette, but the attribute register provides a second conversion layer. Here is another BASIC code snippet to demonstrate all these:

SCREEN 12
CONST K = 20

FOR I% = 0 TO 63
    OUT &H3C7, I%
    PRINT "("; I%; "="; INP(&H3C9); INP(&H3C9); INP(&H3C9); ")";
NEXT I%

SLEEP

FOR I% = 0 TO 15
    ' PRINTING RECTANGLES
    LINE (0, I% * K)-(640, (I% + 1) * K), I%, BF
NEXT I%

FOR I% = 0 TO 15
    ' SETTING PALETTE
    OUT &H3C8, I%
    OUT &H3C9, I% * 4
    OUT &H3C9, I% * 0
    OUT &H3C9, 63 - I% * 4
NEXT I%

SLEEP

' Change fifth color
A% = INP(&H3DA)
OUT &H3C0, 5
OUT &H3C0, 60
OUT &H3C0, &H20
 

In the first part, color codes of indices [0, 63] are printed to the screen. In the next part, 16 rectangles, whose size set by the const K, are drawn on the screen using first 16 colors and then 16 colors ranging from blue to red are assigned to the palette. As demonstrated, colors are written to the palette by writing their palette index to the port 3C8h and sending their color codes via port 3C9h afterwards.

Since blue is assigned to the zeroth color here, the background becomes blue. In the final part, the 60th palette entry is assigned to the fifth one, clearly highlighting the color difference.



[1]: https://en.wikipedia.org/wiki/Video_Graphics_Array
[2]: https://wiki.osdev.org/VGA_Hardware#Port_0x3C6
[3]: http://www.osdever.net/FreeVGA/vga/colorreg.htm
[4]: http://www.osdever.net/FreeVGA/vga/attrreg.htm
[5]: https://trapgate.blogspot.com/2025/11/programming-vga-smooth-scrolling-in.html

Sunday, March 22, 2026

Setting Up a 4-Node GlusterFS Cluster on CentOS 9: Is it Still Worth it?


Hi there. In this blog post of mine, I'll be setting up a storage cluster on Linux machines using a hyperconverged architecture, but first let me clarify what these mambo jambo terms mean. By hyperconverged, I mean, I don't have any disk enclosure or any hardware dedicated to store data. Instead, I have multiple identical machines and each of them have bunch of unused disks. I'll unify this disk space in a redundant and highly available configuration and serve it to the other machines, i.e. clients. It's important to have identical machines, because files will be split into chunks and these chunks will be distributed across all nodes for redundancy and performance. Therefore, if the smallest disk in the cluster fills up, new file chunks cannot be written to all disks. Additionally, the CPU and memory must be also equal, so that when one machine finishes writing, it doesn't have to wait for the other nodes.

I'll be using GlusterFS as distributed file system, primarily because its initial setup is relatively simple, whereas advanced configuration can get quite complicated. I won't be going into those details in this article. As an alternative, I could have used Ceph or Quobyte, but Ceph, in my opinion, is much more complicated than GlusterFS. I worked with Quobyte on a project, and unlike GlusterFS and Ceph, its setup and management is incredibly easy. However, GlusterFS is (was) directly supported by RHEL. But I'll get to that.

Normally, servers with terabyte-sized storage are used for such kind of project. To demonstrate a proof of concept, I'll use four machines with 20 GB disks and share a total of 60 GB of disk space with 75% efficiency, calculated as 20 GB * 75% * 4 = 60 GB. The remaining %25 of the space will be used for high availability and error correction. This capacity is directly proportional to the capacity of the underlying disks. On the other hand, the number of machine plays a more important role here. If I were to setup this cluster with three machines, the recommended configuration is a RAID1 like setup with 33% efficiency. This means, you can get a 20 GB shared disk from three machines, each with a 20 GB disk. Of course, the cluster could also be set up using RAID0 like logic, but in this case, even if just a single machine in the cluster gets rebooted, it will yield data corruption and loss. A four-machine cluster offers a quite high efficiency in terms of usable space with the smallest number of nodes and without compromising on high availability. In this configuration, the disks work under a RAID5 like logic, and the system doesn't get affected even if one machine crashes or gets rebooted. On the other hand, I mentioned that the configuration could get complicated. For example, four machines could also be configured with a RAID10 like logic, where half of the file chunks are stored on one node, and the other half on another, with the remaining two machines mirroring the data of the first two. In this case, the system can withstand the failure of two machines, as long as these two machines are not in same mirror group, but the storage efficiency drops to 50%.

I will use the samba service to share the disk over the network. If there are solely Linux clients on the network, NFS is also an option. Another reason to choose samba is that it can provide high availability with ctdb service. Ctdb is a part of samba and ctdb also supports NFS. On the other hand, if there are Windows clients on the network, too, samba is the only viable option. And if disk or directory authorization is going to be managed via Active Directory (AD), it is easier to handle this with samba.

As distro, I will be using CentOS9, but I also tested the GlusterFS commands and samba configuration on Ubuntu. In that regard, the configuration steps will be distro-agnostic as much as possible, except dnf. I will clone the machines from Cloud Image.

First of all, since I'll be installing on multiple machines, I opened four different panes in tmux and after connecting to each node on each pane, I ran set syn command of tmux. In this way, the commands will be run on all machines simultaneously.

An update on four panes in tmux

After establishing ssh session to each machine, I installed the necessary packages. The first line is for the tools for troubleshooting. The second line installs the repository containing GlusterFS packages, and the third line installs the glusterfs-server package. In the following lines, I install samba related packages, update the system and finally reboot the system if necessary using needs-restart .

dnf -y install tcpdump telnet wget epel-release
dnf -y install centos-release-gluster9
dnf -y install glusterfs-server

dnf -y install samba cifs-utils samba samba-common-tools samba-winbind ctdb --enablerepo=resilientstorage

dnf update
needs-restarting -r || reboot

GlusterFS requires, that the machines are able to reach each other using their hostnames. This should be normally done using DNS records. Since I'm working in a tiny test environment, I manually add the machines' IP addresses to their hosts file. I name the machines server01 thru server04 as shown below. I then assign the hostnames to the machines using the second command. Of course, this command must be entered individually, and if you want, bash to display the new hostname, you must log out and log back in (for Ubuntu use hostnamectl set-hostname server01).

cat >> /etc/hosts << EOF
172.18.186.101   server01
172.18.186.102   server02
172.18.186.103   server03
172.18.186.104   server04
EOF

nmcli gen hostname server01
chronyc sources

GlusterFS is a very time-sensitive service, so there should be theoretically no time difference between machines. Therefore, I check whether chrony is running using the last command above. By the way, Ubuntu has its own NTP client instead of chronyd but you can install chronyd explicitly and the system's own NTP client will be uninstalled automatically.

output of chronyc sources

Now, setting up the disks. First, I check the disks using lsblk . My disk is /dev/vdb. To make it more flexible, I create a disk partition, and set up a logical volume using LVM inside this partition and then use it.

fdisk /dev/vdb
# I won't go into the partitioning details here. You can find how to do it in previous posts.

pvcreate /dev/vdb1
vgcreate vg_gluster /dev/vdb1
lvcreate -l 100%FREE -n lv_brick vg_gluster
mkfs.xfs -f -i size=512 /dev/vg_gluster/lv_brick
mkdir -p /data/glusterfs/brick1
echo "/dev/vg_gluster/lv_brick  /data/glusterfs/brick1  xfs  defaults  1  2" >> /etc/fstab
systemctl daemon-reload
mount -a

If everything has gone smoothly so far, the final command should not produce any output, and the newly mounted partition should appear in df -hP output. At this point, the machines are ready for GlusterFS installation.

I create a GlusterFS volume called shared_storage using the commands below. The systemctl command will be run on all machines. The remaining gluster commands will be run on the first machine, only.

systemctl enable --now glusterd

gluster peer probe server02
gluster peer probe server03
gluster peer probe server04
gluster volume create shared_storage disperse 4 redundancy 1 \
server01:/data/glusterfs/brick1/brick \
server02:/data/glusterfs/brick1/brick \
server03:/data/glusterfs/brick1/brick \
server04:/data/glusterfs/brick1/brick

gluster volume start shared_storage

gluster volume status shared_storage

gluster volume status shared_storage

In the image above, when I entered the command on all machines at the same moment, the first two machines are waiting for the command to finish on the other machines. This is the normal case. The command likely ran on server03 and server04 with a few milliseconds difference. Normally, it should have produced output on just a single machine.

By the way, I do not make any configuration on the host firewall, as firewalld isn't running on my machines.

At this point, I have to use a little workaround for samba. Normally, samba had GlusterFS integration, and it used to work quite well [1]. However, RedHat decided to remove GlusterFS support as of the end of 2024 and shift the resources to Ceph [2][3]. For this reason, this integration has been removed from the distros [5]. In other words, samba-vhs-glusterfs package is no longer available for RedHat and its compatibles. This issue can be worked around as follows (to be run on all machines). Fedora still supports this [4], but I do not know for how long.

mkdir -p /mnt/gluster_shared
echo "localhost:shared_storage  /mnt/gluster_shared  glusterfs  defaults,_netdev  0  0" >> /etc/fstab
systemctl daemon-reload
mount -a

In this way, all machines mount GlusterFS shared disk on themselves. Since I cannot get samba to communicate directly with GlusterFS, I mount and serve this resource via samba as if it is a normal directory. Its drawback is slightly less performance, due to the file system operations having to go through the kernel twice instead of once [1].

Now I can configure ctdb and share the disk over the network, but before I do that, I need to set selinux to Permissive mode. A few sources mention that a ctdb cluster can also be set up without disabling selinux by adjusting only couple booleans, but it didn't work for me.

sed -i -e "s/SELINUX=enforcing/SELINUX=permissive/" /etc/selinux/config
setenforce 0

Only the IPs of the nodes should be in /etc/ctdb/nodes file. In /etc/ctdb/public_addresses file, you must enter the floating IP of the cluster and the network interface to which this IP will be assigned.

cat > /etc/ctdb/nodes << EOF
172.18.186.101
172.18.186.102
172.18.186.103
172.18.186.104
EOF

cat > /etc/ctdb/public_addresses << EOF
172.18.186.200/24    eth0
EOF

The contents of smb.conf file should be like this:

[global]
        netbios name = GLUSTER_CLUSTER
        workgroup = SAMBA
        #security = user
        clustering = yes

        passdb backend = tdbsam
        idmap config * : backend = tdb
        idmap config * : range = 1000000-1999999

        #printing = cups
        #printcap name = cups
        #load printers = yes
        #cups options = raw

[shared_storage]
    comment = GlusterFS
    path = /mnt/gluster_shared
    valid users = sambauser
    read only = no
    guest ok = yes
    create mask = 0664
    directory mask = 0775

And I configure ctdb then as follows:

systemctl stop smb nmb
systemctl disable smb nmb
ctdb event script enable legacy 00.ctdb
ctdb event script enable legacy 10.interface
ctdb event script enable legacy 50.samba
systemctl enable --now ctdb

The enabled scripts are for ctdb to manage the floating IP and clustered samba. The status of the cluster can be checked using ctdb status command. If everything is configured correctly, all nodes should be "OK" in the output, a few seconds after running the last command. By the way, it is also possible to assign multiple floating IPs to the cluster, I only assigned one. The status of the IP(s) can be checked using ctdb ip all command.

ctdb status

Now final step is to create a user on all samba nodes:

groupadd -g 2000 sambagroup
useradd -u 2000 -g sambagroup -s /sbin/nologin sambauser
smbpasswd -a sambauser

The system password of sambauser isn't needed as samba keeps its own database.

Thus, the storage cluster is up and running. Now I need a client to mount and test it. Setting this up is much easier. I run following commands on a fifth machine on the same subnet:

dnf -y install centos-release-gluster9
dnf -y install glusterfs-fuse samba-client cifs-utils
mkdir -p /mnt/my_shared_storage

cat >> /etc/hosts << EOF
172.18.186.101   server01
172.18.186.102   server02
172.18.186.103   server03
172.18.186.104   server04
EOF

mount -t glusterfs 172.18.186.200:/shared_storage /mnt/my_shared_storage/
df -hP

With the first command, I install the glusterfs repository. Then I install the package, needed to mount glusterfs and also samba client, with the second command. On the client, I also create the hosts file containing the list of all servers. Even though the client mounts the storage via a floating IP, it has to be able to resolve IP addresses of cluster nodes for intracluster communication. This step is of course not necessary in an environment with DNS server. In the final step, I mount shared_storage as glusterfs, and when I check with df a 60 GB disk was mounted. But I have not mounted it as samba, yet.

Before mounting as samba, I can first view shared resources using the first command below, and then connect to this share using the second command:

smbclient -L //172.18.186.200 -U sambauser
smbclient //172.18.186.200/shared_storage -U sambauser

Before mounting this share, I unmount the GlusterFS, mounted in previous step, and then mount the resource to the same mount point as a samba share:

umount /mnt/my_shared_storage
mount -t cifs //172.18.186.200/shared_storage /mnt/my_shared_storage -o username=sambauser

And add the following line to /etc/fstab to make it permanent:

//10.0.100.231/shared_storage  /mnt/my_shared_storage  cifs  credentials=/etc/samba/user.cred,iocharset=utf8,_netdev 0 0

/etc/samba/user.cred has the login credentials.

cat >> /etc/samba/user.cred << EOF
username=sambauser
password=secret
domain=SAMBA
EOF

chmod go= /etc/samba/user.cred
chown root:root /etc/samba/user.cred
systemctl daemon-reload
mount -a
df -hP

I conclude this post here for now, as it has got long enough. I plan to explain how to integrate this with AD or LDAP in a future post.


Sources:

[1]: https://lalatendu.org/2014/04/20/glusterfs-vfs-plugin-for-samba/
[2]: https://en.wikipedia.org/wiki/Gluster#cite_ref-10
[3]: https://www.reddit.com/r/kubernetes/comments/zojdl7/whats_the_story_behind_the_abandonment_with/
[4]: https://pkgs.org/search/?q=samba-vfs-glusterfs
[5]: https://www.samba.org/samba/docs/4.7/man-html/vfs_glusterfs.8.html

Friday, February 20, 2026

Using CUPS on Raspberry 4 to Connect Canon MF4450 to Wi-Fi


Hi there. As the title suggests, I'll explain how I converted my old Canon MF4450 printer, which doesn't have a network port, into a shared network printer in my home network using a Raspberry Pi4. This is actually quite easy to do. I actually wanted to explain the steps I took and the difficulties I encountered while doing this. Therefore, I first want to discuss the design before the real configuration.

Note: In the rest of this article, I will use the abbreviation RPi instead of Raspberry Pi.


The first thing is selecting the platform. As I mentioned in the title, I worked with RPi4, or I had to work with it, to be more precise. I also have a RPi 1B at home, and it's actually more than sufficient just for this task in terms of hardware. If this machine will be a dedicated print server, an 8 GB SD card (half of it just for operating system) is just fine as storage. And since RPi 1B has no onboard WiFi adapter, a WiFi dongle is also needed. I'm using a 128 GB SD card on RPi 4, because I run other services on it, but it is also mostly empty. RPi 4 has an onboard WiFi, so no additional hardware is required for wireless networking.

Using an RPi 4 solely for this task is clearly wasting resources, but here's what happened to me with RPi 1:


Selecting OS Distro

For print server, I first planned to use RPi 1B. This has a 32-bit CPU. 32-bit version of Raspberry Pi OS (formerly Raspbian) can be downloaded from the official website. This is a Debian-based distro. I opted for the lite version of it, even though RPi 1 is able to launch GUI, it is beyond usable slow. Apart from that, I also evaluated Alpine RPi, DietPi and piCore, but only found DietPi somewhat useful. It is also Debian-based and gave me the impression of being a customized version of RPi OS. However, DietPi configuration script requires a working internet connection during the very first boot. I'll address this issue later.

RPi4 is 64-bit. For this, I downloaded the 64-bit version of RPi OS from its official website. I also downloaded Manjaro ARM, Ubuntu and Fedora IOT, but I didn't have much time to use the first two. Fedora IOT's approach is quite different from both classic Fedora and other distributions. It uses a script called ignition for the initial configuration, which is similar to cloud-init, but based on JSON. RPi OS, on the other hand, is configured using a classic startup script.


WiFi Config on Raspberry Pi 1

I had a TP-Link AC600 WiFi dongle to use with my RPi 1, and I realized that this stick is not recognized automatically, because the driver for this chipset (Realtek rtl8821au) is missing in RPi OS as well as Dietpi. There is a driver for this chipset, openly developed on github, but you still need internet to download it. Therefore, I had to connect the RPi initially via cable. As I mentioned earlier, DietPi cannot be initially configured without internet. This creates a chicken-egg problem. Alternative: Download the github repo directly to the SD card on the computer.

In the meantime, I ordered TP-Link's TL-WM725N model (rtl8188eu chipset). Its driver is shipped with RPi OS and the card is recognized automatically. While my order was on its way, I compiled the driver I downloaded from github. gcc, make and bc is prerequisites and it gets compiled with a simple install-driver.sh script and installed via DKMS, but due to the RPi 1's CPU speed, compilation takes about 4-5 hours. I also didn't like having to install extra packages, that I'll be only using once or twice, especially on a distribution like DietPi, which is supposed to be optimized for being small.


Printer Config on Raspberry Pi 1

The second problem I had with the RPi1 (after WiFi), was that there is no official 32-bit ARM driver for Canon MF4450. If you have a different model printer, that works perfectly on RPi 1, feel free to skip this section.

I downloaded the printer driver from Canon's official website. According to their website, Linux ARM is supported. The fire (linux-UFRII-drv-v620-m17n-20.tar.gz) contains .deb and .rpm files for ARM64, x86 and x84 platforms but no ARM32. Therefore, I decided to compile the source code in the Sources directory. But this means installing a bunch of packages again, which will be only used for compilation. Moreover, spoiler alert: I couldn't succeed after compiling the driver (if you're not curious about the process, you can skip this part, too). To compile the driver, you'd need to install a total of 11 prerequisite packages.

apt install autoconf automake libcups2-dev libglib2.0-dev libltdl-dev libtool-bin libglade2-0 libglade2-dev libgtk-3-dev libxml2 libxml2-dev

After installing these, I ran allgen.sh script in the cnrdrvcups-common-6.20 and cnrdrvcups-lb-6.20 directories in sequence, to compile the code and copied the files with make install. When compiling cnrdrvcups-lb-6.20 on DietPi, a pointer conversion error occurs. Therefore, CFLAGS variable needs to include -fpermissive flag. To do this, I added the following case to the case structure in allgen.sh :

    "armv6l")
        _machine_type="MACHINETYPE=armv6"
        _cflags="CFLAGS=-fpermissive"
    ;;

After setting all these up, I did not get any errors from compilation and neither did I from CUPS while sending print jobs, but even the local print jobs did not reach the printer. I haven't solved the problem yet, but I still have some cards up my sleeve. If I can solve this issue later, it could be a future blog post. TL;DR: Due to both the WiFi issue -even if it's resolved now- and the printer driver, I decided not to waste time with RPi 1B and to focus on RPi 4 instead.


Configuration Steps

In this section, I based my work on the "Raspberry Pi Print Server" article. I won't go into details of RPi 4 setup. After writing the image file to the SD card and booting it up, a wizard asks for details such as username, WiFi settings and prepares the system accordingly. If SSH isn't enabled yet, enable it with following command:

sudo systemctl enable --now ssh

Then I installed the packages, mentioned in the article:

sudo apt install cups printer-driver-gutenprint hplip samba cups-bsd

Frankly, hplip and printer-driver-gutenprint packages didn't work for me. According to the article, they contain drivers for many printers, including Canon, but apparently the MF4450 model isn't there. cups-bsd is just a dependency of Canon driver.

CUPS is configured through a web UI, and the user must be in the lpadmin group or be root, to configure it. Following commands add the current user to this group and grant external access to CUPS:

sudo usermod -a -G lpadmin $USER
cupsctl --remote-any
sudo systemctl restart cups

After this step, you should be able to access CUPS webUI via http://<machineIP>:631/ from a computer on your network. If not, the host firewall may be running. In this case, you can disable it by running sudo ufw disable or allow the port to the network. If it's still not working, check whether there is a process listening on that port and whether CUPS is running.

If everything is fine so far, I need to install the printer driver.


Printer Config on Raspberry Pi 4

To install the printer on RPi 4, I used the same driver package, I used for RPi 1. After connecting the printer, I installed the .deb file in ARM64/Debian directory, using following command.

sudo dpkg -i cnrdrvcups-ufr2-uk_6.20-1.20_arm64.deb

There is one big detail here: If a window manager is running, a window will pop up (see the image right) on the desktop during the installation. If you're installing it via SSH, it may seem like the installation is frozen, yet the installer is waiting for input via GUI. Therefore, a monitor should be connected during the installation. The installer finds the connected printer automatically. In next step, you select the printer model and the installation is complete.



After spending hours compiling in various ways on RPi 1, I couldn't reach this point, where I got to in about ten minutes with RPi 4. Now, I went back to service configuration.


samba, which I installed above, is necessary to share the printer with Windows clients. I didn't need to make any change in cups.conf. Since I won't be creating any Windows share, I commented out the [homes] section in smb.conf. In the [printers] and [print$] sections, I set guest ok = yes. I checked for errors using testparm -s command and loaded the new settings with systemctl reload smbd command.

These steps are slightly different in DietPi. First, it's recommended to install CUPS and samba using dietpi-software command. Second, DietPi has a customized smb.conf. You should copy the default [printers] and [print$] sections from smb.conf.example file.

CUPS and the driver are independent of eachother. Even if the driver isn't installed, CUPS can find the printer, display it in the web interface and add it without any issue. To do this, I first clicked 'Administration' from the upper bar. A SSL certificate warning should appear in the browser, because Administration page is served with HTTPS and you're getting redirected to HTTPS now. I skipped it and logged in with the user I added to lpadmin. When I clicked "Find New Printers", it automatically found the printer. After I clicked "Add This Printer", a page appeared, asking me what alias to give it to. The only thing important is to select "Share This Printer" here. If the driver is already installed, Canon's model list appears on the next page. You can still go further without installing the driver, by providing the PPD file for the printer. The correct file for Canon MF4450 printer is CNRCUPSMF4400ZK.ppd. And I can extract this file from any .deb or .rpm file in the driver package. In Fedora, this can be achieved with

rpm2cpio cnrdrvcups-ufr2-uk-6.20-1.20.aarch64.rpm | cpio -idmv

command. PPD files are definition files, that contain the specs of printers. They're platform and distribution independent. The driver selection page looks very similar to the one in the image below, but since I had already added the printer, I have included an image of "Modify Printer" page.


After adding the printer, it can be now listed under Printers page, on the rightmost side of upper bar. (At this point, I had to delete the printer and add it again with a different name, this is why the printer name is different in the following screenshots).


When I click on this printer, "Printer and Queue Settings" page opens (on the left). Here, I'll check whether I have loaded the correct PPD file. Default paper size is different (A4 vs Letter) in European and American versions of the same PPD file. If I accidentally load the US version, the print job is delivered to the printer with Letter sized paper, and an annoying warning appears during printing. As it can be seen from the screenshot, the settings are correct here. If Letter appears in these settings, you should select 'Set Default Options' from 'Administration' menu

... and change the paper size to A4 on the page that appears next.


Finally, I took a test printout on this interface via Maintenance -> Print Test Page.


If this gets printed correctly and without problems, the printer is installed. When I tried this for the first time, just input the PPD file without installing the driver, there was no printer activity, and when I clicked the 'Show All Jobs' button, it was showing an error with rastertoufr2. This mean, the driver has/could not been installed correctly. This file should be normally located at /usr/lib/cups/filter/rastertoufr2. At this point, there is no way around installing the driver. However, it is the PPD file, specifies that this file must be called. Perhaps something can be still done by changing corresponding line in PPD file.

And finally, one last magic touch: Debian shuts down the wireless network card, when it hasn't detected any activity for a long time. To prevent this, you need to run

nmcli connection mod <SSID> 802-11-wireless.powersave 2

command. So, the printer is now online on the local network.


Installing the Printer to Windows and Linux Clients

Installing a network printer to clients is quite easy. Moreover, I must admit, that it's easier in Windows than Fedora. This procedure is probably similar in other Linux distros, but I have Fedora and I'll focus on that.


To add a printer in Fedora, open System Settings -> Printers, click on '+ Add' button above. The network printer is found quickly. When I clicked on 'Select Recommended Driver' here (below), IPP Everywhere gets selected.


I continued by selecting this, and the form on the right image appeared. I continued without changing anything here. Queue name must be a unique name. Description and Location can be changed as desired, if the defaults are not descriptive enough.

Since there is no other printer installed in my system, 'Default printer' option isn't important. If everything is OK, click 'Add' to add the printer, and it should appear in the printers list.


A test job can be sent, by clicking on this printer and then 'Print Test Page' button in the window.

In Windows (v10 ve v11), when you select printers from settings or type "Printers" on the Start Menu, a form appears with an 'Add a printer or scanner' entry and 'Add device' button next to it. The printer gets listed automatically, when you click on this button. There is another 'Add device' button, next to the newly found printer. When you click it, the printer is installed automatically, but this step may take 1-2 minutes. The drivers are probably being downloaded from the internet during this time.