October 29, 2008

Using Iconv To Convert Character Sets On Linux And Unix

Today's topic was obliquely referenced in yesterday's post on using the online multilingual dictionary from the Bash CLI. Yes, after I woke up around 1 am and thought about it, the results did seem odd. It turns out, when I went to the page, that the multilingual dictionary on reference.com is actually house within a frameset under the online Thesaurus. My belated apologies for getting the name incorrect, although, if you think about it, it's not even a multilingual dictionary (not really). If you enter a word in English, get the definition in English and then get the word spelled for you in 30 different languages, that's more like an online dictionary with a keyword translator. If it were truly a multilingual dictionary, I would expect to get definitions in 30 different languages. Perhaps I want for too much at some times and not enough at others. Probably a good mix of the two ;)

Today, we're going to look at character conversion (Since, if you looked at the picture in yesterday's bash script post, you probably noticed that a lot of the foreign characters came up looking like garbage. Ok; maybe not garbage, but certainly not in the way they were intended to be shown. All manner of inflective symbols and more picturesque languages (Cyrillic) etc, just don't show up well on a standard U.S. computer.

There are, as I see it, two basic ways to get around this.

1. Install every font set and allow for every single type of encoding available. If your OS supports it, you should know be able to read Japanese Kanji and Russian, German, French, etc with the proper characters displayed. Still, I'm not sure there's any guarantee that this would always work, or that the benefits of keeping a huge cache of fonts and displays wouldn't end up under-weighing the burden.

2. Use a program called "iconv" (present on most Linux and Unix boxes nowadays), and convert when necessary.

I'll assume we want to proceed with option 2 and check out a simple example. But, before we do that, I'd like to point out that, from distro to distro, the implementations of the iconv program can vary greatly. For a striking difference, consider Sun's native version vs. Gnu's. In the Sun's version from 2.6 (Perhaps not a fair comparison, but their native client hasn't come up all that much either), you only have two flags to choose from (actually, you have to use them both): -f (The "convert from" character set) and -t (the "convert to" character set). Don't know what character sets you have installed? Your issue ;)

In the Gnu version, you've got those options (plus the long versions of both -- guess what those are ;) a few other options to suppress errors and gloss over characters that can't be decoded and, best of all, a "--list" option that will list all the character sets installed on your system. This list can be very useful if you want to convert to or from a character set but aren't sure if you can. With Gnu's iconv, you'll know in a few seconds without having to look anywhere else (For Sun 2.6, and later, you can find the definitions under /usr/lib/locale. If you do a "man" on "charmap" it's referring to the charmap source files in there - There may or may not be a standard directory like /usr/share/ii8n/charmaps on your Sun system). For useful information you can actually work with (on a less-than-abstract level) do a "man" on "localedef" and/or "locale." Didn't want to leave you with nothing on that side of the coin ;)

Here's a simple, but dramatically visual enough way, to show you what iconv can do. This example isn't really "practical," except insofar as it shows you proof from a few different angles that "iconv" actually can do what it says.

First, we'll check out our test file and make sure it's good to go:

host # ls
regular
host # file regular.ascii
regular: ASCII text
host # cat regular.ascii
This is a file created using our standard charset
host # cat -v regular.ascii
This is a file created using our standard charset


Then, we'll check the version of iconv (We want Gnu) to make sure we can work as efficiently as possible with as little charmap/locale/charset knowledge as possible:

host # iconv --version
iconv (GNU libc) 2.3.2...
<-- Good :)
host # iconv --list
The following list contain all the coded character sets known. This does
not necessarily mean that all combinations of these names can be used for
the FROM and TO command line parameters. One coded character set can be
listed with several different names (aliases).

437, 500, 500V1, 850, 851, 852, 855, 856, 857, 860, 861, 862, 863, 864, 865,
866, 866NAV, 869, 874, 904, 1026, 1046, 1047, 8859_1, 8859_2, 8859_3, 8859_4,
8859_5, 8859_6, 8859_7, 8859_8, 8859_9, 10646-1:1993, 10646-1:1993/UCS4,
ANSI_X3.4-1968, ANSI_X3.4-1986, ANSI_X3.4, ANSI_X3.110-1983, ANSI_X3.110,
ARABIC, ARABIC7, ARMSCII-8, ASCII, ASMO-708, ASMO_449, BALTIC, BIG-5...


There are literally 50, or so, more lines of available character sets we can convert from and to. Just run that command anytime if you need to check out the entire list. This is great because now we don't have to do any extra work to figure out that part.

Then we'll move on to converting the regular ASCII file to a UTF-16 file. We could convert it to UTF-8, but the "file" command doesn't consider the two charsets "different" enough (and neither do your eyes) and we want this to stand out. Also, UTF-16 has some fun features like not having complete EOL chars, etc, and is unsearchable by standard "grep." Notice the output from the "file," "grep," "cat" and "cat -v" commands once we've finished with the converse and do our comparisons:

host # iconv --from-code ASCII --to-code UTF-16 --output regular.utf16 regular.ascii
host # ls
regular.ascii regular.utf16
host # file regular.ascii regular.utf16
regular.ascii: ASCII text
regular.utf16: Little-endian UTF-16 Unicode character data, with no line terminators
host # grep file regular.ascii regular.utf16
regular.ascii:This is a file created using our standard charset
host # cat regular.ascii
This is a file created using our standard charset
host # cat regular.utf16
ÿþThis is a file created using our standard charset
host # cat -v regular.ascii
This is a file created using our standard charset
host # cat -v regular.utf16
M-^?M-~T^@h^@i^@s^@ ^@i^@s^@ ^@a^@ ^@f^@i^@l^@e^@ ^@c^@r^@e^@a^@t^@e^@d^@ ^@u^@s^@i^@n^@g^@ ^@o^@u^@r^@ ^@s^@t^@a^@n^@d^@a^@r^@d^@ ^@c^@h^@a^@r^@s^@e^@t^@
^@


Man, did we make a mess of things ;) Now, we'll put everything back the way it was (hopefully) and run the same tests:

host # iconv --from-code UTF-16 --to-code ASCII --output regular.new regular.utf16
host # ls
regular.ascii regular.new regular.utf16
host # file regular.ascii regular.new
regular.ascii: ASCII text
regular.new: ASCII text
host # grep file regular.ascii regular.new
regular.ascii:This is a file created using our standard charset
regular.new:This is a file created using our standard charset
host # cat regular.ascii
This is a file created using our standard charset
host # cat regular.new
This is a file created using our standard charset
host # cat -v regular.ascii
This is a file created using our standard charset
host # cat -v regular.new
This is a file created using our standard charset


And, voila, we've got everything back. "file" returns the same information, both files can be catted again and "grep" works on the file that we converted back from UTF-16 to ASCII.

That, in a nutshell is what "iconv" can do for you. The possibilities are endless, as (of course) are the amount of problems you can create for yourself ;)

Cheers,

musa

October 27, 2008

Building a Windows XP Image in BDD 2007 Part 1

Building a Windows XP Image in BDD 2007 Part 1




Many people are interested in the utilities and features offered in Windows
Vista deployment tools. They are also very interested in the new BDD framework.
What many people don't realize is that you can use many of the Vista deployment
tools to manage and configure Windows XP images.

The following guide will get you on your way to automated greatness of BDD.

The first thing you need to do is copy the OS files into the BDD infrastructure.

To do this open the BDD workbench MMC.

Next click on "Operating Systems" and click "New" in the Actions Pane.






In the wizard choose "Full set of source Files" and Click next.

Insert the Windows XP SP2 CD or mount an ISO image on your BDD server. Browse to
the appropriate drive and select it. Click Next





Type in the name you wish to use for the directory and click copy.

The operation to copy all the files will take a few minutes you'll see a
progress bar similar to the following:





This is actually just copying the source files into a directory in your
distribution share. You can see the folder if you open your distribution share
and browse to the Operating System folder. Note: The distribution share would
have been created when you installed BDD. By default it will be on
c:\distribution.




Now that we have the source files we want to create a build. The build is the
part where we specify custom options we want to automate.




  • Click on the "Builds" branch in the BDD MMC. In the Actions pane click "New"

  • Type in the Build ID, Build Name, and if needed Build Comments. click "Next"








  • Select the Windows XP source files you wish to use to build your install, click
    "Next":








            • Type in your VLK license key from your Microsoft Volume License Agreement. click
              "Next".

            • In the next screen you need to provide a Full Name, Organization, and an IE home
              page. Then click Next. (don't forget to use "http://" when entering in the web
              URL)

            • Specify a local Administrator Password and click Create.




            Now you should see your build in the Builds details pane (middle pane). If you
            double click on the build you will see some options that can be adjusted.




            In the General Tab you can adjust the operating system Source, Build names and
            Version number. You can also adjust comments.




            The Setting Tab allows you to fix any mistakes or allows for adjustments in the
            information you entered during the wizard. It also allows you to access the
            Sysprep.inf and Unattend.txt files.






















            We are still dealing with Windows XP so we still have to sometimes manipulate
            these files manually. A good example is the case where we need to add SATA
            drivers to a build. The majority of SATA drivers were released after Windows XP.
            So even in the SP2 release many new hard disk controllers are not in the Windows
            XP source files by default. (We deal with this manually by pressing F6 during
            the text mode setup)




            To lay down some context here is the usual startup process for Windows XP
            install:




            Boot Computer -> Start Windows XP install from CD or network -> Start Text Based
            install -> Load Mass Storage Drivers -> Detect Hard Drives ->Select install
            Options-> Reboot in the GUI Install Mode.




            If there is a problem with the Mass storage Driver you will often get a blue
            screen when loading the XP GUI installer.




            We will cover integrating mass storage drivers into the Sysprep.inf files and
            Unattend.txt files in a future article.




            Finally there is the Task Sequence Tab. The tasks that you find on this tab are
            all the steps BDD takes to build the image. This is an extremely flexible and
            easy way to customize your build.







            This brings us to another issue with Windows XP and BDD 2007. Due to the way BDD
            2007 partitions drives it causes that partition to not be compatible with
            Windows XP. This has been called the BDD Uber bug in some circles. Thanks to the
            fellows at http://www.deployvista.com/
            there is a workaround to patch this bug.




            First you need to create a text file with a .reg file extension. next past the
            following line of text (without the dashed line):




            -------------------------------------------------------------------------------------------------------------------




            Windows Registry Editor Version 5.00

            [HKEY_LOCAL_MACHINE\SYSTEM\CURRENTCONTROLSET\SERVICES\VDS\ALIGNMENT]




            "LessThan4GB"=dword:00000000

            "Between4_8GB"=dword:00000000

            "Between8_32GB"=dword:00000000

            "GreaterThan32GB"=dword:00000000




            --------------------------------------------------------------------------------------------------------------------




            Save the file to the Distributions\Scripts folder.




            Next in the Task Sequence dialog box seen above Click "Preinstall" Then click
            Add.




            You will see a "New task" appear in the list. Use the "UP" Arrow button to move
            the custom task to the beginning of the list.




            Name the task: Fix WinPE 2.0 Bug.




            Then enter the following in the command line: reg import %SCRIPTROOT%\YOUR_REG_FILE.reg




            This fix will make sure that Windows PE uses the correct settings when setting
            up the Windows XP partition.




            Finally you're ready to build your deployment point, and kick off your XP
            install. In the Deploy->Deployment Points branch in the BDD workbench click
            "New" Choos a lab deployment point. Accept the default options in the Deployment
            Point wizard. At the the end click "Create". Once your Lab deployment point has
            been created, click on it in the Information pane, and then in the actions pane
            click Update. This will build a new deployment point and create the necessary
            files you need to boot up Windows PE.




            Using the WDS server or the LiteTouchPE_x86.iso file found in
            [Drive]\Distribution\Boot. start WinPE 2.0 (See how to configure WDS For
            information on booting up over a network)





















            Once PE is booted up and initialized you will be presented with the Windows
            Deployment Wizard:








                                                        • Choose the correct Keyboard layout for your needs, and click "Next"

                                                        • You will be presented with a screen asking for an account with permissions to
                                                          access the Distribution share you set up on your BDD server.

                                                        • Next you will be asked for a computer name.

                                                        • Next you will be asked for a domain name. If you want to capture this install to
                                                          a base Wim Image you must leave the setting to the default "Join A workgroup"
                                                          Click "Next".

                                                        • The following screen prompts you to specify where your user settings are being
                                                          stored. As are preparing a base image, leave the default setting as is and click
                                                          "Next".

                                                        • Choose the Windows XP SP2 Build Files, Click "Next".

                                                        • Choose your Time Zone Click "Next".

                                                        • Choose any applications you added. (For base images, and first time installs
                                                          it's fine if you haven't added any applications yet) click "Next".

                                                        • accept the default setting for capturing the image. (Rename the image file
                                                          should you want another name) Click "Next".

                                                        • In the final screen you may review details about the install. When you are
                                                          satisfied with the results click "Begin".




                                                        This Window will pop up and detail the installation progress. If all went well
                                                        WindowsXP should be installed in a fully automated fashion. The end product will
                                                        be a Windows XP Wim image in your Distributions\Capture directory.




                                                        This concludes part one. In Part 2 we will pickup where we left off here,
                                                        and will explain how to use the Wim image to quickly deploy Windows XP desktops.