How to Set Up a USB Key to Install CentOS

<!> Some of the Alternatives at the bottom are preferred by other users, and are demonstrated to work with CentOS-6.

The procedures below have been tested by various users, but may not cover all eventualities. There is not a formal CentOS project recommended approach, as the supported installer is anaconda, or in some cases by a Live CD. Let the installer beware, or at least enjoy experimentation.

Motivation

Many recent systems, particularly netbooks and small notebooks, may not have a CD or DVD drive and a network install may be difficult, impractical, or impossible, depending on network connectivity and installer support for the available network hardware. This procedure allows a CentOS install without network connectivity and with no media other than a bootable USB device and the target system disk.

CentOS 6.5

Starting with CentOS 6.5, one can install from USB keys by simply transferring the desired ISO using dd.

For example:

dd if=CentOS-6.5-x86_64-bin-DVD1.iso of=/dev/sdb

When asked for the media to install from, select "hard disk" and then the device corresponding to the USB key.

Previous versions of CentOS 6

An end user recommends the following approach for CentOS-6, using livecd-iso-to-disk from livecd-tools with DVD1. This has been tested with livecd-tools-13.4-1.el6 from EPEL. Thanks to forum user AndrewSerk for the recommendation in a forum post. See also the notation of a need for installation of qemu in this mailing list post.

Older Method

<!> This method has been reported as still viable for CentOS 6.4

Building a distribution on a USB key as of CentOS 6.2 - thanks to Mark Roth for the CentOS-6 procedure, and to Yves Bellefeuille on the CentOS-Docs list for several additions and corrections.

Recommended: An 8GB or larger USB key.

  1. Partition the USB key into two partitions: the first, FAT32, and about 250M; the second, the rest of the drive, and ext3. (An ext3 partition is required because the ISO file, that will later be copied to it, is larger than the maximum file size for a FAT32 or VFAT partition.)
  2. Mount DVD1, if it isn't automounted (on /media, or /mnt). The following assumes /mnt/USB and /mnt/DVD.
  3. Mount the USB key on /mnt/USB/.

  4. Copy the directory and contents of /mnt/DVD/isolinux to /mnt/USB/.

  5. Rename /mnt/USB/isolinux to /mnt/USB/syslinux

  6. Rename /mnt/USB/syslinux/isolinux.cfg to /mnt/USB/syslinux/syslinux.cfg

  7. Copy the directory and contents of /mnt/DVD/images to /mnt/USB/. (A bug has been filed [ToDo - add link] because as of 11 Jan 2012, the install requires the .iso (see below), but linux rescue requires the contents of images to load and run.)

  8. Assuming that the USB key is /dev/sdb
    syslinux /dev/sdb1 
  9. Unmount the first USB partition, and mount the second
    umount /mnt/USB
    mount /dev/sdb2 /mnt/USB 
  10. Copy the .iso file to /mnt/USB. Do not use the LiveCD or LiveDVD. Use DVD1, the minimal CD ISO, or a combined DVD1/DVD2 created following CD to DVD Media. Do not copy the contents - the install now wants the .iso file itself, which it mounts during installation, and follow the upstream Installation_Guide to create an images directory on the same partition. Optionally, add DVD2 if you used DVD1, and need it for the install.

  11. Optional: On /mnt/USB, create a grub.conf. This may be required if your system wants the USB key as the first drive, so that you can later copy it to the hard disk. See HowTos/GrubInstallation section 2 for guidance.

  12. Umount the USB key, and it's ready to use.
  13. Boot from the USB media by setting it as the first BIOS boot device, or on some BIOSs by hitting a key such as F12 to select a boot device after POST.
  14. During the installation process, the user is asked "What type of media contains the installation image?" Select the first partition on the USB key, which should appear in the menu under "Hard drive", then /dev/sda1 or whatever device corresponds to the first USB partition.

  15. After partitioning, the user is asked whether to install the Grub boot loader and where to install it. After booting from the USB key, the BIOS may think that the USB key is the first drive. If the USB is seen as the first device, then to install the Grub boot loader on the hard drive MBR, which is the usual case, the user must change the order of the hard drives using the Grub advanced installation options.
  16. After the Grub installation options, the following error message may appear: "Missing ISO 9660 image: The installer has tried to mount image #1, but cannot find it on the hard drive". The installation program is looking for the ISO file on the first partition of the USB key, but it's on the second partition. Go to a terminal with a shell with Ctrl-Alt-F2, unmount the first partition of the USB

    umount /mnt/isodir
    mount the second partition on the USB device. This will be the same device used in #14 above - for example
    mount -t ext3 /dev/sda2 /mnt/isodir
    Return to the installation program (Ctrl-Alt-F6) and choose "Retry".
  17. Finish the installation and reboot without the USB device connected. If there are problems booting then it is likely the boot record was written to the wrong device. See How to re-install bootstrap code (GRUB), and if having difficulties determining the correct device, Troubleshooting GRUB Issues.

CentOS-5

Note: The livecd-iso-to-disk from livecd-tools method as recommended above for CentOS-6 may be preferable, but has not been tested by the author.

Prerequisites

  1. A target system for the installation that supports booting from USB media. This may need to be configured in the BIOS setup.
  2. A USB key with sufficient capacity to hold the installation media - also known as: memory stick, flash drive, thumb drive, etc. CentOS 5.4 i386 should fit on a 4GB drive, x86_64 requires more than 4GB, a minimal install from CD#1 should be possible with a 1GB drive. The same technique should be usable with a USB hard drive. It is assumed that this procedure is being performed in a GUI (GNOME/KDE/etc.) environment so automounting of the USB media will be performed.
  3. A working Linux system (probably another computer with Linux installed, running from a live CD, or in a Virtual Machine) with syslinux ("yum install syslinux" or install using the native package manager for the distribution, if not already installed). The author encountered problems using the CentOS-5.3/5.4 syslinux package. The package supplied by the CentOS LiveCD Tools repo worked.

  4. A set of CentOS installation CD ISO images, or DVD ISO image, for the desired architecture.

It may be necessary to change device names, architecture, and/or syslinux paths to match your installation distribution, situation, and preferences.

Use caution when copying/pasting commands below to adjust for your situation!

Procedure

Assume the USB key shows up as /dev/sdg (adjust for your situation - will likely be different) and CentOS 5.4 i386 is being installed.

  1. Become root. Create a small VFAT partition (20 MB should be sufficient) and a big Linux one on the USB media. Make the VFAT partition active.

    fdisk /dev/sdg 
    In fdisk use "m" to see the the menu options. The sequence of commands goes something like the following (without "#" comments):
    d # delete existing partition - repeat as necessary
    n # new partition
    p # primary
    1 # partition number
    <enter> # take the default
    +20m # add 20 MB
    t # change the type
    1 # partition number
    b # VFAT
    n # new partition
    p # primary
    2 # partition number
    <enter> # take the default
    <enter> # take the default
    a # toggle bootable flag
    1 # partition number
    w # write to disk 
    Should look something like this when done:
    Disk /dev/sdg: 7948 MB, 7948206080 bytes
    81 heads, 10 sectors/track, 19165 cylinders
    Units = cylinders of 810 * 512 = 414720 bytes
    Disk identifier: 0x00000000
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sdg1   *           1          65       26320    b  W95 FAT32
    /dev/sdg2              66       19165     7735500   83  Linux 
  2. Make the filesystems:
    mkfs.vfat -n BOOT /dev/sdg1
    mkfs.ext2 -m 0 -b 4096 -L DATA /dev/sdg2 
    Unplug/plug the USB media. The partitions should mount under /media/BOOT and /media/DATA.
  3. Copy the CentOS ISO[s] to the ext2 partition. For CD ISOs:
    cd /dir/of/iso/images
    mkdir /media/DATA/centos
    mkdir /tmp/cdimage
    mount -ro loop CentOS-5.4-i386-bin-1of6.iso /tmp/cdimage
    cp *sum* /media/DATA/centos
    for i in 1 2 3 4 5 6; do cp -v CentOS-5.4-i386-bin-${i}of6.iso /media/DATA/centos/; sync; done
    cd /media/DATA/centos
    grep of6 sha1sum.txt | sha1sum -c - CentOS-5.4-i386-bin-?of6.iso 
    For DVD media replace the "for" loop above and the next two lines with
    cp -v CentOS-5.4-i386-bin-DVD.iso /media/DATA/centos/; sync
    cd /media/DATA/centos
    grep DVD sha1sum.txt | sha1sum -c - CentOS-5.4-i386-bin-DVD.iso 
  4. Install and configure syslinux on the VFAT partition and MBR:
    syslinux -s /dev/sdg1
    dd if=/usr/share/syslinux/mbr.bin of=/dev/sdg
    cd /media/BOOT
    cp -rv /tmp/cdimage/isolinux syslinux
    mv syslinux/isolinux.cfg syslinux/syslinux.cfg
    rm -f syslinux/isolinux.bin
    umount /tmp/cdimage
    <your-favorite-editor> syslinux/syslinux.cfg 
    Add to the "append" statements in the various sections:
      method=hd:sda2:/centos 
    To use kickstart:
      ks=hd:sda2:/ks.cfg method=hd:sda2:/centos 
  5. Use the GUI to "Safely Remove" the USB media. Move to the target system, boot and install.

Notes

<!> During installation, make sure that the Anaconda installer writes the boot record to the correct location and not to the USB key. It will probably be necessary to use "Advanced bootloader options" for GRUB and change the device order so the target boot device shows up at the top of the list to assure proper GRUB installation. If this is not done the GRUB bootloader will be written to the USB key, making it unusable without restoring the syslinux MBR, and the system unbootable. If you do find yourself in this predicament, see the TipsAndTricks/ReinstallGRUB article, and/or have a look at SuperGRUB.



 bootloader --driveorder=cciss/c0d0,sda --location=mbr

For the kickstart, I use the following:

  append initrd=initrd.img ks=hd:sda2:/ks.cfg method=hd:sda2:/centos

  harddrive --partition=sda2 --dir=/centos
  ignoredisk --drives=sda
  bootloader --driveorder=cciss/c0d0,sda --location=mbr

And, to be honest, I should not really get credit for this; I found most of the info on the web... _

And I also reported earlier that:

  1. My server HP does not seem to want to boot on a write protected USB key.
  2. Anaconda tries to fetch the ks.cfg too early... If, once the detection is over (just 2-3 seconds later), I retry, it works.
  3. There is an error message "cannot mount read/write, will mount read-only" (I like to write protect)

Alternatives

UNetbootin is probably the most popular "outside" method, has both Windows and Linux executables, and allows you to create bootable Live USB drives for a variety of Linux distributions, including CentOS. There was an anaconda bug that could render the media unusable - now closed for Fedora 13 and presumably thus RHEL/CentOS-6. It has been reported on the fora to work with at least the netinstall ISO for CentOS-6. When using the netinstall ISO the full installation media ISO can be copied to the root directory of the USB media and a "Hard Drive" install performed.


Much better is livecd-iso-to-disk which can be obtained from git:

http://git.fedorahosted.org/git/?p=livecd;a=blob_plain;f=tools/livecd-iso-to-disk.sh;hb=HEAD

Just download it, chmod +x and run:

./livecd-iso-to-disk.sh /path/to/iso /dev/partition_of_your_usb_stick

You could also add an option like:

--overlay-size-mb 1024

to add some persistent space to the key.

The resulting key could directly be used for installations, without the need of placing the iso manually on the key.






Using Windows

Starting with CentOS 6.5, one can create a bootable USB key simply by installing the ISO file on the key using a program such as Win32 Disk Imager. This will delete all information already on the key.

Note that simply copying the ISO file on the USB key won't work.


UNetbootin, mentioned above, has a Windows version and allows you to create bootable Live USB drives for a variety of Linux distributions, including CentOS.




HowTos/InstallFromUSBkey (last edited 2013-12-18 02:22:48 by YvesBellefeuille)