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

Command (m for help): p
Disk /dev/sda: 30.0 GB, 30016659456 bytes, 58626288 sectorsUnits = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000c12bc
   Device Boot    Start         End      Blocks   Id  System
/dev/sda1   *      2048      411647      204800    c  W95 FAT32 (LBA)
/dev/sda2        411648    25577471    12582912   83  Linux
/dev/sda3      25577472    50743295    12582912   83  Linux
/dev/sda4      50743296    58626287     3941496   82  Linux swap / Solaris

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

Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system idu   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

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

Command (m for help): u
Changing display/entry units to cylinders (DEPRECATED!).
Command (m for help): p
Disk /dev/sda: 30.0 GB, 30016659456 bytes, 58626288 sectorsUnits = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000c12bc
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800    c  W95 FAT32 (LBA)
/dev/sda2              26        1593    12582912   83  Linux
/dev/sda3            1593        3159    12582912   83  Linux
/dev/sda4            3159        3650     3941496   82  Linux swap / Solaris

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

Command (m for help): x
Expert command (m for help): p
Disk /dev/sda: 255 heads, 63 sectors, 3649 cylinders
Nr AF  Hd Sec  Cyl  Hd Sec  Cyl   Start      Size     ID
 1 80  32  33    0 159   6   25       2048     409600 0c
 2 00 159   7   25  31  39  568     411648   25165824 83
 3 00  31  40  568 159   9   86   25577472   25165824 83
 4 00 159  10   86  80  63  577   50743296    7882992 82

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

Direct Download

Read full article as PDF:

Price $2.95

News

njobs Europe
What:
Where:
Country:
Njobs Netherlands Njobs Deutschland Njobs United Kingdom Njobs Italia Njobs France Njobs Espana Njobs Poland
Njobs Austria Njobs Denmark Njobs Belgium Njobs Czech Republic Njobs Mexico Njobs India Njobs Colombia