"Linux Basics" 20, Custom Linux Systems

Source: Internet
Author: User

One, host-based (host) to make a simple bootable Linux

1. Partition the target disk

Two:

On Host:/DEV/SDB1,/DEV/SDB2

/DEV/SDB1 mount to/mnt/boot,/dev/sdb2 mount to/mnt/sysroot


2. Install GRUB to target disk

# Grub-install--root-directory=/mnt/dev/sdb


3. Copy kernel and INITRD files

# Cp/boot/vmlinz-version/mnt/boot/vmlinuz

# cp/boot/initramfs-version.img/mnt/boot/initramfs.img

Note: Copy the files to the target machine for convenience here is not written version, suggest write version also to write on


4. Create the root file system of the target host

# Mkdir-pv/mnt/sysroot/{etc/rc.d,usr,var,proc,sys,dev,lib,lib64,bin,sbin,boot,srv,mnt,media,home,root}

Then porting common commands such as bash,ls,cat to the root file system of the target host


5. Provide configuration files for grub

# vim/mnt/boot/grub/grub.conf

Default=0

Timeout=5

Title magedu Little Linux

Root (hd0,0)

Kernel/vmlinuz ro root=/dev/sda2 Quiet selinux=0 init=/bin/bash

Initrd/initramfs.img

It's done. Make a simple bootable Linux based on host, remove the hard drive to the other machine (because there is no driver, only run on the virtual machine), test boot

[[email protected] ~]# fdisk -l /dev/sdbdisk /dev/sdb: 21.5 gb,  21474836480 bytes255 heads, 63 sectors/track, 2610 cylindersunits =  cylinders of 16065 * 512 = 8225280 bytessector size  (logical/ Physical): 512 bytes / 512 bytesi/o size  (minimum/optimal): 512  bytes / 512 bytesdisk identifier: 0xf3b882a4   device boot       Start         End       Blocks   Id  System/dev/sdb1                1          500     4016218+  83  linux/dev/sdb2              501         632      1060290   83  linux[[email protected] ~]# mkdir /mnt/{boot, sysroot}[[email protected] ~]# mount /dev/sdb1 /mnt/boot[[email protected]  ~]# mount /dev/sdb2 /mnt/sysroot[[email protected] ~]# df -thfilesystem      type   size  used avail use% mounted  on/dev/sda2      ext4    20G  4.9G    14g  27% /tmpfs          tmpfs   981m     0  981m   0% /dev/shm/dev/sda1       ext4   508M   34M  448M    7% /boot/dev/sdc1      ext4   3.0T  211M  2.9T    1% /mnt/dev/sdb1      ext4   4.1g   76m   3.8G   2% /mnt/boot/dev/sdb2      ext4    1.1G   35M  980M   4% /mnt/sysroot[[email  protected] ~]# cp /boot/vmlinuz-2.6.32-431.el6.x86_64 /mnt/boot/vmlinuz[[email  Protected] ~]# cp /boot/initramfs-2.6.32-431.el6.x86_64.img /mnt/boot/initramfs[[email  protected] ~]# grub-install --root-directory=/mnt /dev/sdbProbing devices  To guess bios drives. this may take a long time. Installation finished. no error reported. This is the contents of the device map /mnt/boot/grub/device. map. Check if this is correct or not. if any of the lines  is incorrect,fix it and re-run the script  ' Grub-install '. (fd0)/dev/fd0 (hd0)/DEV/SDA (HD1)/dev/sdb (HD2)/dev/sdc[[email protected] ~]# ls /mnt/ bootgrub  initramfs  lost+found  vmlinuz[[email protected] ~]#  vim /mnt/boot/grub/grub.conf[[email protected] ~]# cat /mnt/boot/grub/grub.conf          #boot分区就完成了default =0 timeout=10titile anyfish linux     root  (hd0,0)     kernel /vmlinuz ro root=/dev/ sda2 quiet selinux=0 init=/bin/bash      initrd /initramfs# Init=bash specify kernel no longer go to query init, directly start bash, do not set kernel can not find the Init will error # start preparing the root file system [[email protected] ~]#  Mkdir -pv /mnt/sysroot/{etc/rc.d,usr,var,proc,sys,dev,lib,lib64,bin,sbin,boot,home,mnt}mkdir:  created directory   "/MNT/SYSROOT/ETC" mkdir:   Created directory  /MNT/SYSROOT/ETC/RC.D mkdir:  created directory  /MNT/SYSROOT/USR mkdir:  created directory  /mnt/ Sysroot/var "mkdir:  created directory  "/mnt/sysroot/proc "mkdir:  created directory  "/mnt/sysroot/sys "mkdir:  Directory created  /mnt/sysroot/dev mkdir:  created directory  /mnt/sysroot/lib mkdir:  created directory  /mnt/sysroot/ Lib64 "mkdir:  created directory  "/mnt/sysroot/bin "mkdir:  created directory  "/mnt/sysroot/sbin "mkdir:  created directory   "/mnt/sysroot/boot" mkdir:  created directory   "/mnt/sysroot/home" mkdir:  created directory   "/mnt/sysroot/mnt" [[ email protected] ~]# cp /bin/bash /mnt/sysroot/bin[[email protected] ~]#  chroot /mnt/sysrootchroot: failed to run command  '/bin/bash ':  no  such file or directory[[email protected] ~]# ls /mnt/sysroot/bin/bash  /mnt/sysroot/bin/bash[[email protected] ~]# ldd /bin/bash            linux-vdso.so.1 =>   (0x00007fff43b8e000) libtinfo.so.5 => /lib64/ libtinfo.so.5  (0x00007f08cb011000) libdl.so.2 => /lib64/libdl.so.2  (0x00007f08cae0d000) libc.so.6 => /lib64/libc.so.6  (0x00007f08caa78000)/lib64/ld-linux-x86-64.so.2  ( 0x00007f08cb242000) [[email protected] ~]# ldd /bin/bash|grep -o  '/[[:alnum:]/.-] \+ '/lib64/libtinfo.so.5/lib64/libdl.so.2/lib64/libc.so.6/lib64/ld-linux-x86-64.so.2[[email protected]  ~]# for i in  ' ldd /bin/bash|grep -o  '/[[:alnum:]/.-]\+ ' |cut -d/  -f3 ';d o cp /lib64/$i  /mnt/sysroot/lib64/;d one[[email protected] ~]# ls  /mnt/sysroot/lib64/ld-linux-x86-64.so.2  libc.so.6  libdl.so.2   libtinfo.so.5[[email protected] ~]# chroot /mnt/sysrootbash-4.1# lsbash: ls: command not foundbash-4.1#  bash-4.1# quitbash: quit: command not foundbash-4.1# exitexit# here to note: chroot switch root directory must have bin/bash in order to switch successfully, the command runs successfully need to rely on the library file [[email protected] ~]# cp /bin/ls / Bin/cat /mnt/sysroot/bin[[email protected] ~]# ls /mnt/sysroot/binbash  cat   ls[[email protected] ~]# ldd /bin/cat             #cat依赖的库文件和bash一样, linux-vdso.so.1 =>   (0x00007fff219ff000) libc.so.6 => /lib64/libc.so.6  (0x00007fcfd1c9b000)/lib64/ld-linux-x86-64.so.2  ( 0x00007fcfd203f000) [[email protected] ~]# cp /lib64/libc.so.6 ^c[[email  protected] ~]# ldd /bin/lslinux-vdso.so.1 =>   (0x00007fffa578d000) Libselinux.so.1 => /lib64/libselinux.so.1  (0x00007fbac4e29000) librt.so.1 => /lib64/librt.so.1  (0x00007fbac4c21000) libcap.so.2  => /lib64/libcap.so.2  (0x00007fbac4a1c000) libacl.so.1 => /lib64/libacl.so.1   (0x00007fbac4814000) libc.so.6 => /lib64/libc.so.6  (0x00007fbac4480000) libdl.so.2  => /lib64/libdl.so.2  (0x00007fbac427b000)/lib64/ld-linux-x86-64.so.2  (0x00007fbac5058000) libpthread.so.0 => /lib64/libpthread.so.0  (0x00007fbac405e000) libattr.so.1 => / lib64/libattr.so.1  (0x00007fbac3e59000) [[email protected] ~]# for i in  ' LDD  /bin/ls|grep -o  '/[[:alnum:]/.-]\+ ' |cut -d/ -f3 ';d o cp /lib64/$i  /mnt /sysroot/lib64/;d ONECP: Do you want to overwrite "/mnt/sysroot/lib64/libc.so.6"?  YCP: Do you want to overwrite "/mnt/sysroot/lib64/libdl.so.2"?  YCP: Do you want to overwrite "/mnt/sysroot/lib64/ld-linux-x86-64.so.2"?  y[[email protected] ~]# ls /mnt/sysroot/lib64ld-linux-x86-64.so.2  libcap.so.2  libpthread.so.0  libtinfo.so.5libacl.so.1            libc.so.6    librt.so.1libattr.so.1           libdl.so.2   libselinux.so.1[[email  protected] ~]# chroot /mnt/sysrootbash-4.1# lsbin   dev   Home  lib64     mnt   sbin  usrboot  etc   lib lost+found  proc  sys var

Boot + rootfs is complete, then create a new virtual machine and use the hard disk as the Startup disk to start:

"Linux Basics" 20, Custom Linux Systems

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.