自制linux
今天说一说怎么自制一个linux,也就是用一块新硬盘来自己搞一个linux。硬盘拆掉撞到其他机器上可以照常使用,需要什么东西都是自己添加的哦。
那我就在虚拟机里进行了嘿嘿嘿。。。
1.在我的centos里加了一块新的硬盘并顺便分了个区,并且格式化了一下。
[[email protected] ~]# fdisk /dev/sdb WARNING: DOS-compatible mode is deprecated. It‘s strongly recom mended to switch off the mode (command ‘c‘) and change display u nits to sectors (command ‘u‘). Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-2610, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610 ): +10G Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 2 First cylinder (1307-2610, default 1307): Using default value 1307 Last cylinder, +cylinders or +size{K,M,G} (1307-2610, default 2 610): Using default value 2610 Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. [[email protected] ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sr0 11:0 1 3.7G 0 rom /media/CentOS_6.9_Final sda 8:0 0 200G 0 disk ├─sda1 8:1 0 1G 0 part /boot ├─sda2 8:2 0 50G 0 part / ├─sda3 8:3 0 50G 0 part /app ├─sda4 8:4 0 1K 0 part └─sda5 8:5 0 2G 0 part [SWAP] sdb 8:16 0 20G 0 disk ├─sdb1 8:17 0 10G 0 part └─sdb2 8:18 0 10G 0 part =》sdb就是我新硬盘,分了两个10G的分区。 [[email protected] ~]#
接下来就是两个分区的文件系统(另一个分区一样)
[[email protected] ~]# mkfs.ext4 /dev/sdb1 mke2fs 1.41.12 (17-May-2010) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 655776 inodes, 2622603 blocks 131130 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=2688548864 81 block groups 32768 blocks per group, 32768 fragments per group 8096 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632 Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 20 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. [[email protected] ~]#
创建两个分区一个是为根目录挂载,另一个是为了boot挂载。
2.在自己的linux下创建/media/boot(该boot是制作的boot),之后把/dev/sda1挂载到/media/boot,这个就不做演示了。
3.之后安装grub:grub-install --root-directory=/media /dev/sdb(不能是sdb{1,2},grub安装是安装前446字节还不到分区)
[[email protected] ~]# grub-install --root-directory=/media/ /dev/sdb Probing devices to guess BIOS drives. This may take a long time. Installation finished. No error reported. This is the contents of the device map /media//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 [[email protected] ~]# [[email protected] ~]# [[email protected] grub]# pwd /media/boot/grub [[email protected] grub]# ls device.map jfs_stage1_5 ufs2_stage1_5 e2fs_stage1_5 minix_stage1_5 vstafs_stage1_5 fat_stage1_5 reiserfs_stage1_5 xfs_stage1_5 ffs_stage1_5 stage1 iso9660_stage1_5 stage2 [[email protected] grub]# ===》现在grub下已经有基本的文件了
4.拷贝一些重要文件。在本人机器上vm,init文件名称是我自己定义的,和其他版本可能不一样。
[[email protected] ~]# cp /boot/vmlinuz /media/boot/ [[email protected] ~]# cp /boot/initramfs.img /media/boot/ [[email protected] ~]#
5.创建编辑grub.conf文件
[[email protected] ~]# vim /media/boot/grub/grub.conf default=0 timeout=5 title cnetos 8.8 kernel /vmlinuz root=/dev/sda2 init=/bin/bash initrd /initramfs.img ~
kernel和initrd添加的是我第四部拷贝的两个文件,所以文件名是一样的。
6.本人是提前做了个脚本,脚本功能是输入命令,把命令和关联的库都cp到某一个目录下(也就是现在新创建的boot下),用这个脚本来实现自己小Linux里面的命令功能。
[[email protected] ~]# copycmd31.sh please input command(q或quit退出) : ls 请输入一个目标路径(绝对路径): /media /bin/ls ======> /media/bin /lib64/libselinux.so.1 ======> /media/lib64/libselinux.so.1 /lib64/librt.so.1 ======> /media/lib64/librt.so.1 /lib64/libcap.so.2 ======> /media/lib64/libcap.so.2 /lib64/libacl.so.1 ======> /media/lib64/libacl.so.1 /lib64/libc.so.6 ======> /media/lib64/libc.so.6 /lib64/libdl.so.2 ======> /media/lib64/libdl.so.2 /lib64/ld-linux-x86-64.so.2 ======> /media/lib64/ld-linux-x86-64.so.2 /lib64/libpthread.so.0 ======> /media/lib64/libpthread.so.0 /lib64/libattr.so.1 ======> /media/lib64/libattr.so.1
功能如上,拷贝完需要的重要命令之后在把该创建的目录都创建一遍这个简单的linux就差不多完成了。
[[email protected] media]# mkdir {etc,root,sys,usr,var,proc} [[email protected] media]# ls bin dev home lib64 proc sbin tmp var boot etc lib lost+found root sys usr 现在就差不多像个简单的linux了。
现在可以把这个自己制作的硬盘安装到其他机器上看看效果了
时间: 2024-12-21 15:59:46