Tuesday, 25 June 2013

Centos 6/RHEL writing image file to USB drive

You can write a Centos 6 install image file on to a USB stick in order to boot from it. This is often useful for installing on a device which has no DVD drive but can be set to boot from a USB stick, such as netbooks etc.

Write image file to USB

To do this first download the required image from one of the mirrors The netinstall.iso is the one used on this blog for building up the system. Keep it in the home directory when done.

Now insert your USB drive and find out the device path it has been assigned, such as /dev/sdc, you can use mount or fdisk for this depending whether your system mounts it automatically.

$ sudo fdisk -l

 Disk /dev/sdc: 4022 MB, 4022337024 bytes
124 heads, 62 sectors/track, 1021 cylinders
Units = cylinders of 7688 * 512 = 3936256 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x6f20736b


You can also use

$ mount

To see if the device is mounted, if it has been, then use umount to unmount it. For example

$ sudo umount /dev/sdc

Now you have found the device name you can write the downloaded image file to it using the dd command. As root issue

# dd if=/home/anton/CentOS-6.4-i386-netinstall.iso of=/dev/sdc

Replacing the user and file/path names with yours, this will write the image you have in your home directory to the USB drive.

So you can now use this to boot from and install Centos 6 on a device which doesn't have a DVD drive but can be configured to boot from a USB stick.

Read image from a DVD drive

If you have the installation media on a DVD disc and wish to turn it into an image file to use as above or in another fashion then just place it in the drive and find the device name with ls

$ ls -l /sys/block/*/device



lrwxrwxrwx. 1 root root 0 Jun 29 18:16 /sys/block/sda/device -> ../../../1:0:1:0
lrwxrwxrwx. 1 root root 0 Jun 29 18:16 /sys/block/sdb/device -> ../../../4:0:1:0
lrwxrwxrwx. 1 root root 0 Jun 29 18:16 /sys/block/sr0/device -> ../../../4:0:0:0


The third entry down is not a hard drive or partition and is likely to be a cd drive, confirm it with

$ ls -l /dev/sr0

brw-rw----+ 1 root cdrom 11, 0 Jun 29 18:16 /dev/sr0



and you can use the dd command with the device path





# dd if=/dev/sr0 of=/path/and/filename.iso

to make an .iso image copy of the DVD disc, where /dev/sr0 is the disc to copy and path and filename is the location to copy to.

Mount an image file 

You may also want to mount the image you just created or any other image, do this with the mount -o loop. after creating a directory in which to mount it, for example

# mkdir /mnt/myimage

# mount -o loop nameofimage.iso /mnt/myimage

(Assumes you are in the same directory as the iso)

Having done this and running the mount command to check, shows my Centos image mounted in my /mnt/myimage directory.

$ mount
/dev/mapper/vg_centos-lv_root on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
/dev/sda2 on /boot type ext4 (rw)
/dev/mapper/vg_centos-lv_home on /home type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
/dev/sdb1 on /media/storage type vfat (rw)
/dev/sdc on /tmp/usb type vfat (rw)
/home/anton/CentOS-6.4-i386-netinstall.iso on /mnt/myimage type iso9660 (rw,loop=/dev/loop0)