Klaus Knopper answers your Linux questions

Ask Klaus

Article from Issue 190/2016
Author(s):

New fdisk behavior, booting Knoppix from USB, and using PHP mail() in Apache.

New Linux fdisk Partitioning Tool

I've read your article about repartitioning the hard disk using the famous fdisk command-line tool. I tend to partition a disk in a way that starts and ends partitions at cylinders, in order to adapt to the mechanical specs of the disk. However, the newer versions of fdisk that come with Knoppix 7.7 no longer show the disk geometry in cylinders and heads; all settings are based on sectors of 512 or 4096 bytes, depending on the disk's physical sector size.

Do I have to install an old version of fdisk in order to restore the old, cylinder-based behavior?

To illustrate what happens, start an fdisk session. Unless you quit fdisk with the w command for "write," nothing will be changed or saved on disk, so make sure you quit with q when leaving the program (unless you really want to change the disk's partitioning).

$ sudo fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only
   until you decide to write them.
Be careful before using the write
   command.
Command (m for help):

Next, list the current partitions (Listing  1). As you can see, there is one "dos" (or UEFI) FAT32 partition, two Linux data partitions, and one swap partition. All Starts and Ends are given in 512-byte "sectors," whereas the data range is stated in blocks of 1KB. (This may be confusing at first glance, but the partition size is actually easier to think about in decimals than fractions of a kilobyte.)

Listing 1

Listing Partitions in fdisk

 

So, how to get to the physical disk layout from there? The m command of fdisk reveals the option (Listing 2).

Listing 2

fdisk Commands

 

As you can guess from the g option, this version of fdisk also handles the GPT/GUID partitioning scheme, which is often used for disks larger than 2TB. Option u will switch display and input of partition limits to the traditional heads/cylinders/sectors scheme (Listing  3).

Listing 3

Changing fdisk Display Units to Cylinders

 

Now the start/end values are given in cylinders, while partition size is still displayed in kilobyte blocks. Note the "deprecated" warning message: Modern disks don't necessarily report physical "cylinders."

Instead of changing units inside the program, fdisk can be started with "cylinder" units as the default unit, restoring the old behavior, by adding -u=cylinders after the fdisk command.

Listing 4 shows a quick look into the "expert mode" partition display, splitting the partitions addresses again into cylinders, heads, and sectors:

Listing 4

fdisk Expert Mode

 

NAND flash-based solid-state disks (SSDs) in particular do not even have physically magnetic media organized in cylinder units; however, the operating system might require this information report in cylinders, heads, and sector size of tracks to calculate the correct size of the disk the traditional way.

Therefore, cylinder-size-based limits might not align to the physical medium or memory chips as desired when partitioning along "cylinder" limits; instead, the preferred (and probably more honest) method is now working with sector units, which also allows a more fine-grained space adjustment of partitions. You can still use the "cylinder" unit setting for partitioning, but it most likely won't give you any advantage over partitioning in sector units.

Don't forget to leave fdisk with q, to leave with no changes written to disk.

Booting Knoppix from USB Flash Disk

I installed Knoppix on a small computer with a tiny internal hard disk by flash-knoppix, so it boots with the same method it does from DVD or USB flash disk, saving space by the base system staying compressed. The overlay data partition is encrypted. Now I would like to extend partition sizes to fill up the entire disk and readjust the filesystem by booting from another Knoppix-installed USB flash disk so that none of the hard disk partitions are mounted. But, apparently, booting from the USB flash disk still results in both hard disk partitions being mounted, and I'm stuck at the boot screen asking me for my encryption password.

How can I boot from USB when Knoppix is already installed on the internal hard disk; is it possible?

The reason for Knoppix seeming to ignore the USB drive is that the operating system can't know from which media its kernel was loaded and started. After the CPU switches to "protected mode," the computer "forgets" which medium was selected in the BIOS boot menu. Knoppix uses a standard search order that tries DVD drives first, then hard disks, and flash drives in the order detected by the kernel and initial ramdisk scripts. Because of this, it might find your KNOPPIX directory on the hard disk first, before the DVD or USB flash disk are even ready to use.

You can tell Knoppix to search for its files and setup for overlay images by a boot option, thereby overriding the standard search path:

knoppix fromhd=/dev/sdb1

Assuming that your flash drive is /dev/sdb, the first partition containing the KNOPPIX data would be /dev/sdb1. The fromhd option just means to search for a partition on a specific medium giving its Linux device name; it is not specific to hard disks.

Furthermore, you can skip mounting the encrypted image or partition by adding boot option noimage. In the case of an encrypted overlay partition, you can also just enter an arbitrarily wrong encryption password three times (two retries), so mounting the encrypted partition will fail, and the system will continue to boot without the encrypted overlay.

Using the PHP mail() Function in Apache

I'm learning to write dynamic web pages using Apache with PHP on Linux. I stumbled across the mail() function of PHP, which is supposed to send the text enclosed in the function parameters, so I can send data input from the web page by email to my mail account instead of writing to a database or file. But, it does not seem to work, Mail is never sent out. What am I missing?

In PHP, mail() requires a working setup of a mail delivery agent on the computer running Apache/PHP; that is, it calls sendmail to send mail to an email address.

If sendmail is not installed, or if it isn't configured in a way that allows sending mail to the desired addresses, the mail() function of PHP will fail silently.

To check whether sendmail is configured correctly, you can invoke the command manually and try to send email. As <mailaddress>, use <your.username>@<your.providers.mail.domain> for sending email over the Internet, or <username>@localhost for sending mail to a local user present on the computer running Apache/PHP.

$ sendmail <mailaddress>
From: Klaus <mailaddress>
To: Klaus <mailaddress>
Subject: Test
Hello, World!
.

The single dot in the last input line tells sendmail to finish the message text body and send it off.

Now, if you get an error message (or Delivery Status Report – DSN) telling you the mail could not be send, or the sendmail command is not even installed, you need to install and configure local email. Most distros use Postfix, which is a very complete mail server and transport agent that fits the majority of mail server purposes, including the possibility to add spam filters. It also has a choice of a few straightforward and easy basic settings when installed first (Figure 1).

Figure 1: Initial Postfix configuration screen.

On Debian-based systems, such as Knoppix and Ubuntu, installation can be done with

sudo apt-get install postfix

which will run an interactive basic setup during installation. If Postfix is already installed, you get back to the initial setup (Figure 2) with the command:

sudo dpkg-reconfigure postfix
Figure 2: Postfix setup.

Local only is the safest option, but it only allows sending mail to users on the local system running Apache+PHP. Internet Site will attempt to send mail directly to the mail server of the designated recipient address, and allows receipt of mail via the SMTP protocol on the machine running Postfix. Thus, it's a potential target for spammers.

With Postfix installed and configured, the sendmail command should now be available for the PHP mail() function to work correctly.

The following lines,

<?php
    $message = "Hello, World";
    if( mail('user@mailserver', 'My subject line', $message) )
     echo "Mail sent out successfully.";
    else
     echo "Mail could not be sent.";

are an excerpt of an HTML page containing PHP code for sending mail.

Klaus Knopper

Klaus Knopper is an engineer, creator of Knoppix, and co-founder of LinuxTag expo. He works as a regular professor at the University of Applied Sciences, Kaiserslautern, Germany. If you have a configuration problem, or if you just want to learn more about how Linux works, send your questions to: klaus@linux-magazine.com

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Top 10 Knoppix Rescue Tricks

    The Knoppix Live Linux distro is packed with powerful tools for fixing broken systems. We ask Knoppix creator Klaus Knopper for his favorite Knoppix rescue tricks.

  • Professor Knopper's Lab – Hybrid Image Mode

    As DVD drives disappear from modern notebook computers, Knoppix offers a hybrid image mode that works for both DVDs and flash drives.

  • ASK KLAUS!

    Klaus Knopper is the creator of Knoppix and co-founder of the LinuxTag expo. He currently works as a teacher, programmer, and consultant. If you have a configuration problem, or if you just want to learn more about how Linux works, send your questions to: klaus@linux-magazine. com

  • Ask Klaus!

    Klaus Knopper is the creator of Knoppix and co-founder of the LinuxTag expo. He currently works as a teacher, programmer, and consultant. If you have a configuration problem, or if you just want to learn more about how Linux works, send your questions to: klaus@linux-magazine.com

  • Ask Klaus!

    Klaus Knopper is the creator of Knoppix and co-founder of the LinuxTag expo. He currently works as a teacher, programmer, and consultant. If you have a configuration problem, or if you just want to learn more about how Linux works, send your questions to: klaus@linux-magazine.com

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News